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 /*---------------------------------------------------------------------------*/
42 #if (!defined(EASYCAP_H))
45 #if defined(EASYCAP_DEBUG)
46 #if (9 < EASYCAP_DEBUG)
47 #error Debug levels 0 to 9 are okay.\
48 To achieve higher levels, remove this trap manually from easycap.h
50 #endif /*EASYCAP_DEBUG*/
51 /*---------------------------------------------------------------------------*/
53 * THESE ARE FOR MAINTENANCE ONLY - NORMALLY UNDEFINED:
55 /*---------------------------------------------------------------------------*/
57 #undef EASYCAP_TESTCARD
58 #undef EASYCAP_TESTTONE
62 /*---------------------------------------------------------------------------*/
65 * DEFINE BRIDGER TO ACTIVATE THE ROUTINE FOR BRIDGING VIDEOTAPE DROPOUTS.
67 * *** UNDER DEVELOPMENT/TESTING - NOT READY YET!***
70 /*---------------------------------------------------------------------------*/
72 /*---------------------------------------------------------------------------*/
74 #include <linux/kernel.h>
75 #include <linux/errno.h>
76 #include <linux/init.h>
77 #include <linux/slab.h>
78 #include <linux/module.h>
79 #include <linux/kref.h>
80 #include <linux/smp_lock.h>
81 #include <linux/usb.h>
82 #include <linux/uaccess.h>
84 #include <linux/i2c.h>
85 #include <linux/version.h>
86 #include <linux/workqueue.h>
87 #include <linux/poll.h>
90 #include <linux/delay.h>
91 #include <linux/types.h>
93 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
94 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
95 #if (!defined(__OLD_VIDIOC_))
97 #endif /* !defined(__OLD_VIDIOC_) */
99 #include <media/v4l2-dev.h>
101 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
102 #include <media/v4l2-device.h>
103 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
104 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
105 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
107 #if (!defined(__OLD_VIDIOC_))
108 #define __OLD_VIDIOC_
109 #endif /* !defined(__OLD_VIDIOC_) */
110 #include <linux/videodev2.h>
112 #include <linux/soundcard.h>
114 #if defined(EASYCAP_NEEDS_USBVIDEO_H)
115 #include <config/video/usbvideo.h>
116 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
118 #if (!defined(PAGE_SIZE))
119 #error "PAGE_SIZE not defined"
122 #define STRINGIZE_AGAIN(x) #x
123 #define STRINGIZE(x) STRINGIZE_AGAIN(x)
125 /*---------------------------------------------------------------------------*/
126 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
128 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
129 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
131 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
132 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
134 /*---------------------------------------------------------------------------*/
135 #define USB_EASYCAP_VENDOR_ID 0x05e1
136 #define USB_EASYCAP_PRODUCT_ID 0x0408
138 #define EASYCAP_DRIVER_VERSION "0.8.21"
139 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
141 #define USB_SKEL_MINOR_BASE 192
142 #define VIDEO_DEVICE_MANY 8
144 /*---------------------------------------------------------------------------*/
146 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
148 /*---------------------------------------------------------------------------*/
149 #define SAA_0A_DEFAULT 0x7F
150 #define SAA_0B_DEFAULT 0x3F
151 #define SAA_0C_DEFAULT 0x2F
152 #define SAA_0D_DEFAULT 0x00
153 /*---------------------------------------------------------------------------*/
155 * VIDEO STREAMING PARAMETERS:
156 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
157 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
159 /*---------------------------------------------------------------------------*/
160 #define VIDEO_ISOC_BUFFER_MANY 16
161 #define VIDEO_ISOC_ORDER 3
162 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
163 #define USB_2_0_MAXPACKETSIZE 3072
164 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
165 #error video_isoc_buffer[.] will not be big enough
167 /*---------------------------------------------------------------------------*/
171 /*---------------------------------------------------------------------------*/
172 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
173 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
174 #define FIELD_BUFFER_MANY 4
175 #define FRAME_BUFFER_MANY 6
176 /*---------------------------------------------------------------------------*/
178 * AUDIO STREAMING PARAMETERS
180 /*---------------------------------------------------------------------------*/
181 #define AUDIO_ISOC_BUFFER_MANY 16
182 #define AUDIO_ISOC_ORDER 3
183 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
184 /*---------------------------------------------------------------------------*/
188 /*---------------------------------------------------------------------------*/
189 #define AUDIO_FRAGMENT_MANY 32
190 /*---------------------------------------------------------------------------*/
192 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
193 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
194 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
195 * ONLY MUST THE PARAMETER
197 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
198 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
199 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
200 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
202 /*---------------------------------------------------------------------------*/
213 #define STANDARD_MANY 10
214 /*---------------------------------------------------------------------------*/
218 /*---------------------------------------------------------------------------*/
244 #define SETTINGS_MANY (STANDARD_MANY * \
249 /*---------------------------------------------------------------------------*/
251 * STRUCTURE DEFINITIONS
253 /*---------------------------------------------------------------------------*/
255 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;
277 /*---------------------------------------------------------------------------*/
279 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
280 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
281 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
283 /*---------------------------------------------------------------------------*/
285 unsigned int audio_pages_per_fragment;
286 unsigned int audio_bytes_per_fragment;
287 unsigned int audio_buffer_page_many;
290 #if defined(UPSAMPLE)
294 struct easycap_format easycap_format[1 + SETTINGS_MANY];
299 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
300 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
301 struct video_device *pvideo_device;
302 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
303 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
305 struct usb_device *pusb_device;
306 struct usb_interface *pusb_interface;
310 struct mutex mutex_mmap_video[FRAME_BUFFER_MANY];
311 struct mutex mutex_timeval0;
312 struct mutex mutex_timeval1;
314 int queued[FRAME_BUFFER_MANY];
315 int done[FRAME_BUFFER_MANY];
317 wait_queue_head_t wq_video;
318 wait_queue_head_t wq_audio;
330 struct timeval timeval0;
331 struct timeval timeval1;
332 struct timeval timeval2;
333 struct timeval timeval7;
334 long long int dnbydt;
337 int video_altsetting_on;
338 int video_altsetting_off;
339 int video_endpointnumber;
340 int video_isoc_maxframesize;
341 int video_isoc_buffer_size;
342 int video_isoc_framesperdesc;
344 int video_isoc_streaming;
345 int video_isoc_sequence;
352 struct data_buffer video_isoc_buffer[VIDEO_ISOC_BUFFER_MANY];
354 field_buffer[FIELD_BUFFER_MANY][(FIELD_BUFFER_SIZE/PAGE_SIZE)];
356 frame_buffer[FRAME_BUFFER_MANY][(FRAME_BUFFER_SIZE/PAGE_SIZE)];
358 struct list_head urb_video_head;
359 struct list_head *purb_video_head;
363 /*---------------------------------------------------------------------------*/
367 /*---------------------------------------------------------------------------*/
368 int field_fill; /* Field buffer being filled by easycap_complete(). */
369 /* Bumped only by easycap_complete(). */
370 int field_page; /* Page of field buffer page being filled by */
371 /* easycap_complete(). */
372 int field_read; /* Field buffer to be read by field2frame(). */
373 /* Bumped only by easycap_complete(). */
374 int frame_fill; /* Frame buffer being filled by field2frame(). */
375 /* Bumped only by easycap_dqbuf() when */
376 /* field2frame() has created a complete frame. */
377 int frame_read; /* Frame buffer offered to user by DQBUF. */
378 /* Set only by easycap_dqbuf() to trail frame_fill.*/
379 int frame_lock; /* Flag set to 1 by DQBUF and cleared by QBUF */
380 /*---------------------------------------------------------------------------*/
384 /*---------------------------------------------------------------------------*/
393 int frame_buffer_used;
394 int frame_buffer_many;
395 int videofieldamount;
402 int allocation_video_urb;
403 int allocation_video_page;
404 int allocation_video_struct;
405 int registered_video;
406 /*---------------------------------------------------------------------------*/
410 /*---------------------------------------------------------------------------*/
412 int audio_altsetting_on;
413 int audio_altsetting_off;
414 int audio_endpointnumber;
415 int audio_isoc_maxframesize;
416 int audio_isoc_buffer_size;
417 int audio_isoc_framesperdesc;
419 int audio_isoc_streaming;
425 struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
427 struct list_head urb_audio_head;
428 struct list_head *purb_audio_head;
429 /*---------------------------------------------------------------------------*/
433 /*---------------------------------------------------------------------------*/
434 int audio_fill; /* Audio buffer being filled by easysnd_complete(). */
435 /* Bumped only by easysnd_complete(). */
436 int audio_read; /* Audio buffer page being read by easysnd_read(). */
437 /* Set by easysnd_read() to trail audio_fill by */
439 /*---------------------------------------------------------------------------*/
443 /*---------------------------------------------------------------------------*/
445 int audio_buffer_many;
447 int allocation_audio_urb;
448 int allocation_audio_page;
449 int allocation_audio_struct;
450 int registered_audio;
452 long long int audio_sample;
453 long long int audio_niveau;
454 long long int audio_square;
456 struct data_buffer audio_buffer[];
458 /*---------------------------------------------------------------------------*/
460 * VIDEO FUNCTION PROTOTYPES
462 /*---------------------------------------------------------------------------*/
463 void easycap_complete(struct urb *);
464 int easycap_open(struct inode *, struct file *);
465 int easycap_release(struct inode *, struct file *);
466 long easycap_ioctl(struct file *, unsigned int, unsigned long);
468 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
469 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
470 int easycap_open_noinode(struct file *);
471 int easycap_release_noinode(struct file *);
472 int videodev_release(struct video_device *);
473 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
474 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
476 unsigned int easycap_poll(struct file *, poll_table *);
477 int easycap_mmap(struct file *, struct vm_area_struct *);
478 int easycap_usb_probe(struct usb_interface *, \
479 const struct usb_device_id *);
480 void easycap_usb_disconnect(struct usb_interface *);
481 void easycap_delete(struct kref *);
483 void easycap_vma_open(struct vm_area_struct *);
484 void easycap_vma_close(struct vm_area_struct *);
485 int easycap_vma_fault(struct vm_area_struct *, struct vm_fault *);
486 int easycap_dqbuf(struct easycap *, int);
487 int submit_video_urbs(struct easycap *);
488 int kill_video_urbs(struct easycap *);
489 int field2frame(struct easycap *);
490 int redaub(struct easycap *, void *, void *, \
491 int, int, __u8, __u8, bool);
492 void debrief(struct easycap *);
493 void sayreadonly(struct easycap *);
494 void easycap_testcard(struct easycap *, int);
495 int explain_ioctl(__u32);
496 int explain_cid(__u32);
497 int fillin_formats(void);
498 int adjust_standard(struct easycap *, v4l2_std_id);
499 int adjust_format(struct easycap *, __u32, __u32, __u32, \
501 int adjust_brightness(struct easycap *, int);
502 int adjust_contrast(struct easycap *, int);
503 int adjust_saturation(struct easycap *, int);
504 int adjust_hue(struct easycap *, int);
505 int adjust_volume(struct easycap *, int);
506 /*---------------------------------------------------------------------------*/
508 * AUDIO FUNCTION PROTOTYPES
510 /*---------------------------------------------------------------------------*/
511 void easysnd_complete(struct urb *);
512 ssize_t easysnd_read(struct file *, char __user *, size_t, loff_t *);
513 int easysnd_open(struct inode *, struct file *);
514 int easysnd_release(struct inode *, struct file *);
515 long easysnd_ioctl(struct file *, unsigned int, unsigned long);
516 unsigned int easysnd_poll(struct file *, poll_table *);
517 void easysnd_delete(struct kref *);
518 int submit_audio_urbs(struct easycap *);
519 int kill_audio_urbs(struct easycap *);
520 void easysnd_testtone(struct easycap *, int);
521 int audio_setup(struct easycap *);
522 /*---------------------------------------------------------------------------*/
524 * LOW-LEVEL FUNCTION PROTOTYPES
526 /*---------------------------------------------------------------------------*/
527 int audio_gainget(struct usb_device *);
528 int audio_gainset(struct usb_device *, __s8);
530 int set_interface(struct usb_device *, __u16);
531 int wakeup_device(struct usb_device *);
532 int confirm_resolution(struct usb_device *);
533 int confirm_stream(struct usb_device *);
535 int setup_stk(struct usb_device *);
536 int setup_saa(struct usb_device *);
537 int setup_vt(struct usb_device *);
538 int check_stk(struct usb_device *);
539 int check_saa(struct usb_device *);
540 int ready_saa(struct usb_device *);
541 int merit_saa(struct usb_device *);
542 int check_vt(struct usb_device *);
543 int select_input(struct usb_device *, int, int);
544 int set_resolution(struct usb_device *, \
545 __u16, __u16, __u16, __u16);
547 int read_saa(struct usb_device *, __u16);
548 int read_stk(struct usb_device *, __u32);
549 int write_saa(struct usb_device *, __u16, __u16);
550 int wait_i2c(struct usb_device *);
551 int write_000(struct usb_device *, __u16, __u16);
552 int start_100(struct usb_device *);
553 int stop_100(struct usb_device *);
554 int write_300(struct usb_device *);
555 int read_vt(struct usb_device *, __u16);
556 int write_vt(struct usb_device *, __u16, __u16);
558 int set2to78(struct usb_device *);
559 int set2to93(struct usb_device *);
561 int regset(struct usb_device *, __u16, __u16);
562 int regget(struct usb_device *, __u16, void *);
563 /*---------------------------------------------------------------------------*/
564 struct signed_div_result {
565 long long int quotient;
566 unsigned long long int remainder;
567 } signed_div(long long int, long long int);
568 /*---------------------------------------------------------------------------*/
572 /*---------------------------------------------------------------------------*/
573 #define GET(X, Y, Z) do { \
576 rc = regget(X, Y, Z); \
578 JOT(8, ":-(%i\n", __LINE__); return(rc); \
582 #define SET(X, Y, Z) do { \
584 rc = regset(X, Y, Z); \
586 JOT(8, ":-(%i\n", __LINE__); return(rc); \
589 /*---------------------------------------------------------------------------*/
591 #define SAY(format, args...) do { \
592 printk(KERN_DEBUG "easycap: %s: " format, __func__, ##args); \
596 #if defined(EASYCAP_DEBUG)
597 #define JOT(n, format, args...) do { \
598 if (n <= easycap_debug) { \
599 printk(KERN_DEBUG "easycap: %s: " format, __func__, ##args); \
603 #define JOT(n, format, args...) do {} while (0)
604 #endif /*EASYCAP_DEBUG*/
606 #define POUT JOT(8, ":-(in file %s line %4i\n", __FILE__, __LINE__)
608 #define MICROSECONDS(X, Y) \
609 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
610 (long long int)(X.tv_usec - Y.tv_usec))
612 /*---------------------------------------------------------------------------*/
614 * (unsigned char *)P pointer to next byte pair
615 * (long int *)X pointer to accumulating count
616 * (long int *)Y pointer to accumulating sum
617 * (long long int *)Z pointer to accumulating sum of squares
619 /*---------------------------------------------------------------------------*/
620 #define SUMMER(P, X, Y, Z) do { \
622 unsigned int u0, u1, u2; \
624 p = (unsigned char *)(P); \
625 u0 = (unsigned int) (*p); \
626 u1 = (unsigned int) (*(p + 1)); \
627 u2 = (unsigned int) ((u1 << 8) | u0); \
629 s = -(long int)(0x7FFF & (~u2)); \
631 s = (long int)(0x7FFF & u2); \
632 *((X)) += (long int) 1; \
633 *((Y)) += (long int) s; \
634 *((Z)) += ((long long int)(s) * (long long int)(s)); \
636 /*---------------------------------------------------------------------------*/