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/usb.h>
81 #include <linux/uaccess.h>
83 #include <linux/i2c.h>
84 #include <linux/version.h>
85 #include <linux/workqueue.h>
86 #include <linux/poll.h>
89 #include <linux/delay.h>
90 #include <linux/types.h>
92 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
93 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
94 #if (!defined(__OLD_VIDIOC_))
96 #endif /* !defined(__OLD_VIDIOC_) */
98 #include <media/v4l2-dev.h>
100 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
101 #include <media/v4l2-device.h>
102 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
103 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
104 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
106 #if (!defined(__OLD_VIDIOC_))
107 #define __OLD_VIDIOC_
108 #endif /* !defined(__OLD_VIDIOC_) */
109 #include <linux/videodev2.h>
111 #include <linux/soundcard.h>
113 #if defined(EASYCAP_NEEDS_USBVIDEO_H)
114 #include <config/video/usbvideo.h>
115 #endif /*EASYCAP_NEEDS_USBVIDEO_H*/
117 #if (!defined(PAGE_SIZE))
118 #error "PAGE_SIZE not defined"
121 #define STRINGIZE_AGAIN(x) #x
122 #define STRINGIZE(x) STRINGIZE_AGAIN(x)
124 /*---------------------------------------------------------------------------*/
125 /* VENDOR, PRODUCT: Syntek Semiconductor Co., Ltd
127 * EITHER EasyCAP USB 2.0 Video Adapter with Audio, Model No. DC60
128 * with input cabling: AUDIO(L), AUDIO(R), CVBS, S-VIDEO.
130 * OR EasyCAP 4CHANNEL USB 2.0 DVR, Model No. EasyCAP002
131 * with input cabling: MICROPHONE, CVBS1, CVBS2, CVBS3, CVBS4.
133 /*---------------------------------------------------------------------------*/
134 #define USB_EASYCAP_VENDOR_ID 0x05e1
135 #define USB_EASYCAP_PRODUCT_ID 0x0408
137 #define EASYCAP_DRIVER_VERSION "0.8.21"
138 #define EASYCAP_DRIVER_DESCRIPTION "easycapdc60"
140 #define USB_SKEL_MINOR_BASE 192
141 #define VIDEO_DEVICE_MANY 8
143 /*---------------------------------------------------------------------------*/
145 * DEFAULT LUMINANCE, CONTRAST, SATURATION AND HUE
147 /*---------------------------------------------------------------------------*/
148 #define SAA_0A_DEFAULT 0x7F
149 #define SAA_0B_DEFAULT 0x3F
150 #define SAA_0C_DEFAULT 0x2F
151 #define SAA_0D_DEFAULT 0x00
152 /*---------------------------------------------------------------------------*/
154 * VIDEO STREAMING PARAMETERS:
155 * USB 2.0 PROVIDES FOR HIGH-BANDWIDTH ENDPOINTS WITH AN UPPER LIMIT
156 * OF 3072 BYTES PER MICROFRAME for wMaxPacketSize.
158 /*---------------------------------------------------------------------------*/
159 #define VIDEO_ISOC_BUFFER_MANY 16
160 #define VIDEO_ISOC_ORDER 3
161 #define VIDEO_ISOC_FRAMESPERDESC ((unsigned int) 1 << VIDEO_ISOC_ORDER)
162 #define USB_2_0_MAXPACKETSIZE 3072
163 #if (USB_2_0_MAXPACKETSIZE > PAGE_SIZE)
164 #error video_isoc_buffer[.] will not be big enough
166 /*---------------------------------------------------------------------------*/
170 /*---------------------------------------------------------------------------*/
171 #define FIELD_BUFFER_SIZE (203 * PAGE_SIZE)
172 #define FRAME_BUFFER_SIZE (405 * PAGE_SIZE)
173 #define FIELD_BUFFER_MANY 4
174 #define FRAME_BUFFER_MANY 6
175 /*---------------------------------------------------------------------------*/
177 * AUDIO STREAMING PARAMETERS
179 /*---------------------------------------------------------------------------*/
180 #define AUDIO_ISOC_BUFFER_MANY 16
181 #define AUDIO_ISOC_ORDER 3
182 #define AUDIO_ISOC_BUFFER_SIZE (PAGE_SIZE << AUDIO_ISOC_ORDER)
183 /*---------------------------------------------------------------------------*/
187 /*---------------------------------------------------------------------------*/
188 #define AUDIO_FRAGMENT_MANY 32
189 /*---------------------------------------------------------------------------*/
191 * IT IS ESSENTIAL THAT EVEN-NUMBERED STANDARDS ARE 25 FRAMES PER SECOND,
192 * ODD-NUMBERED STANDARDS ARE 30 FRAMES PER SECOND.
193 * THE NUMBERING OF STANDARDS MUST NOT BE CHANGED WITHOUT DUE CARE. NOT
194 * ONLY MUST THE PARAMETER
196 * BE CHANGED TO CORRESPOND TO THE NEW NUMBER OF STANDARDS, BUT ALSO THE
197 * NUMBERING MUST REMAIN AN UNBROKEN ASCENDING SEQUENCE: DUMMY STANDARDS
198 * MAY NEED TO BE ADDED. APPROPRIATE CHANGES WILL ALWAYS BE REQUIRED IN
199 * ROUTINE fillin_formats() AND POSSIBLY ELSEWHERE. BEWARE.
201 /*---------------------------------------------------------------------------*/
212 #define STANDARD_MANY 10
213 /*---------------------------------------------------------------------------*/
217 /*---------------------------------------------------------------------------*/
243 #define SETTINGS_MANY (STANDARD_MANY * \
248 /*---------------------------------------------------------------------------*/
250 * STRUCTURE DEFINITIONS
252 /*---------------------------------------------------------------------------*/
254 struct list_head list_head;
259 /*---------------------------------------------------------------------------*/
261 struct list_head list_head;
266 /*---------------------------------------------------------------------------*/
267 struct easycap_standard {
269 struct v4l2_standard v4l2_standard;
271 struct easycap_format {
274 struct v4l2_format v4l2_format;
276 /*---------------------------------------------------------------------------*/
278 * easycap.ilk == 0 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=256
279 * easycap.ilk == 2 => CVBS+S-VIDEO HARDWARE, AUDIO wMaxPacketSize=9
280 * easycap.ilk == 3 => FOUR-CVBS HARDWARE, AUDIO wMaxPacketSize=9
282 /*---------------------------------------------------------------------------*/
284 unsigned int audio_pages_per_fragment;
285 unsigned int audio_bytes_per_fragment;
286 unsigned int audio_buffer_page_many;
289 #if defined(UPSAMPLE)
293 struct easycap_format easycap_format[1 + SETTINGS_MANY];
298 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
299 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
300 struct video_device *pvideo_device;
301 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
302 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
304 struct usb_device *pusb_device;
305 struct usb_interface *pusb_interface;
309 struct mutex mutex_mmap_video[FRAME_BUFFER_MANY];
310 struct mutex mutex_timeval0;
311 struct mutex mutex_timeval1;
313 int queued[FRAME_BUFFER_MANY];
314 int done[FRAME_BUFFER_MANY];
316 wait_queue_head_t wq_video;
317 wait_queue_head_t wq_audio;
329 struct timeval timeval0;
330 struct timeval timeval1;
331 struct timeval timeval2;
332 struct timeval timeval7;
333 long long int dnbydt;
336 int video_altsetting_on;
337 int video_altsetting_off;
338 int video_endpointnumber;
339 int video_isoc_maxframesize;
340 int video_isoc_buffer_size;
341 int video_isoc_framesperdesc;
343 int video_isoc_streaming;
344 int video_isoc_sequence;
351 struct data_buffer video_isoc_buffer[VIDEO_ISOC_BUFFER_MANY];
353 field_buffer[FIELD_BUFFER_MANY][(FIELD_BUFFER_SIZE/PAGE_SIZE)];
355 frame_buffer[FRAME_BUFFER_MANY][(FRAME_BUFFER_SIZE/PAGE_SIZE)];
357 struct list_head urb_video_head;
358 struct list_head *purb_video_head;
362 /*---------------------------------------------------------------------------*/
366 /*---------------------------------------------------------------------------*/
367 int field_fill; /* Field buffer being filled by easycap_complete(). */
368 /* Bumped only by easycap_complete(). */
369 int field_page; /* Page of field buffer page being filled by */
370 /* easycap_complete(). */
371 int field_read; /* Field buffer to be read by field2frame(). */
372 /* Bumped only by easycap_complete(). */
373 int frame_fill; /* Frame buffer being filled by field2frame(). */
374 /* Bumped only by easycap_dqbuf() when */
375 /* field2frame() has created a complete frame. */
376 int frame_read; /* Frame buffer offered to user by DQBUF. */
377 /* Set only by easycap_dqbuf() to trail frame_fill.*/
378 int frame_lock; /* Flag set to 1 by DQBUF and cleared by QBUF */
379 /*---------------------------------------------------------------------------*/
383 /*---------------------------------------------------------------------------*/
392 int frame_buffer_used;
393 int frame_buffer_many;
394 int videofieldamount;
401 int allocation_video_urb;
402 int allocation_video_page;
403 int allocation_video_struct;
404 int registered_video;
405 /*---------------------------------------------------------------------------*/
409 /*---------------------------------------------------------------------------*/
411 int audio_altsetting_on;
412 int audio_altsetting_off;
413 int audio_endpointnumber;
414 int audio_isoc_maxframesize;
415 int audio_isoc_buffer_size;
416 int audio_isoc_framesperdesc;
418 int audio_isoc_streaming;
424 struct data_buffer audio_isoc_buffer[AUDIO_ISOC_BUFFER_MANY];
426 struct list_head urb_audio_head;
427 struct list_head *purb_audio_head;
428 /*---------------------------------------------------------------------------*/
432 /*---------------------------------------------------------------------------*/
433 int audio_fill; /* Audio buffer being filled by easysnd_complete(). */
434 /* Bumped only by easysnd_complete(). */
435 int audio_read; /* Audio buffer page being read by easysnd_read(). */
436 /* Set by easysnd_read() to trail audio_fill by */
438 /*---------------------------------------------------------------------------*/
442 /*---------------------------------------------------------------------------*/
444 int audio_buffer_many;
446 int allocation_audio_urb;
447 int allocation_audio_page;
448 int allocation_audio_struct;
449 int registered_audio;
451 long long int audio_sample;
452 long long int audio_niveau;
453 long long int audio_square;
455 struct data_buffer audio_buffer[];
457 /*---------------------------------------------------------------------------*/
459 * VIDEO FUNCTION PROTOTYPES
461 /*---------------------------------------------------------------------------*/
462 void easycap_complete(struct urb *);
463 int easycap_open(struct inode *, struct file *);
464 int easycap_release(struct inode *, struct file *);
465 long easycap_ioctl(struct file *, unsigned int, unsigned long);
467 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
468 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
469 int easycap_open_noinode(struct file *);
470 int easycap_release_noinode(struct file *);
471 int videodev_release(struct video_device *);
472 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
473 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
475 unsigned int easycap_poll(struct file *, poll_table *);
476 int easycap_mmap(struct file *, struct vm_area_struct *);
477 int easycap_usb_probe(struct usb_interface *, \
478 const struct usb_device_id *);
479 void easycap_usb_disconnect(struct usb_interface *);
480 void easycap_delete(struct kref *);
482 void easycap_vma_open(struct vm_area_struct *);
483 void easycap_vma_close(struct vm_area_struct *);
484 int easycap_vma_fault(struct vm_area_struct *, struct vm_fault *);
485 int easycap_dqbuf(struct easycap *, int);
486 int submit_video_urbs(struct easycap *);
487 int kill_video_urbs(struct easycap *);
488 int field2frame(struct easycap *);
489 int redaub(struct easycap *, void *, void *, \
490 int, int, __u8, __u8, bool);
491 void debrief(struct easycap *);
492 void sayreadonly(struct easycap *);
493 void easycap_testcard(struct easycap *, int);
494 int explain_ioctl(__u32);
495 int explain_cid(__u32);
496 int fillin_formats(void);
497 int adjust_standard(struct easycap *, v4l2_std_id);
498 int adjust_format(struct easycap *, __u32, __u32, __u32, \
500 int adjust_brightness(struct easycap *, int);
501 int adjust_contrast(struct easycap *, int);
502 int adjust_saturation(struct easycap *, int);
503 int adjust_hue(struct easycap *, int);
504 int adjust_volume(struct easycap *, int);
505 /*---------------------------------------------------------------------------*/
507 * AUDIO FUNCTION PROTOTYPES
509 /*---------------------------------------------------------------------------*/
510 void easysnd_complete(struct urb *);
511 ssize_t easysnd_read(struct file *, char __user *, size_t, loff_t *);
512 int easysnd_open(struct inode *, struct file *);
513 int easysnd_release(struct inode *, struct file *);
514 long easysnd_ioctl(struct file *, unsigned int, unsigned long);
515 unsigned int easysnd_poll(struct file *, poll_table *);
516 void easysnd_delete(struct kref *);
517 int submit_audio_urbs(struct easycap *);
518 int kill_audio_urbs(struct easycap *);
519 void easysnd_testtone(struct easycap *, int);
520 int audio_setup(struct easycap *);
521 /*---------------------------------------------------------------------------*/
523 * LOW-LEVEL FUNCTION PROTOTYPES
525 /*---------------------------------------------------------------------------*/
526 int audio_gainget(struct usb_device *);
527 int audio_gainset(struct usb_device *, __s8);
529 int set_interface(struct usb_device *, __u16);
530 int wakeup_device(struct usb_device *);
531 int confirm_resolution(struct usb_device *);
532 int confirm_stream(struct usb_device *);
534 int setup_stk(struct usb_device *);
535 int setup_saa(struct usb_device *);
536 int setup_vt(struct usb_device *);
537 int check_stk(struct usb_device *);
538 int check_saa(struct usb_device *);
539 int ready_saa(struct usb_device *);
540 int merit_saa(struct usb_device *);
541 int check_vt(struct usb_device *);
542 int select_input(struct usb_device *, int, int);
543 int set_resolution(struct usb_device *, \
544 __u16, __u16, __u16, __u16);
546 int read_saa(struct usb_device *, __u16);
547 int read_stk(struct usb_device *, __u32);
548 int write_saa(struct usb_device *, __u16, __u16);
549 int wait_i2c(struct usb_device *);
550 int write_000(struct usb_device *, __u16, __u16);
551 int start_100(struct usb_device *);
552 int stop_100(struct usb_device *);
553 int write_300(struct usb_device *);
554 int read_vt(struct usb_device *, __u16);
555 int write_vt(struct usb_device *, __u16, __u16);
557 int set2to78(struct usb_device *);
558 int set2to93(struct usb_device *);
560 int regset(struct usb_device *, __u16, __u16);
561 int regget(struct usb_device *, __u16, void *);
562 /*---------------------------------------------------------------------------*/
563 struct signed_div_result {
564 long long int quotient;
565 unsigned long long int remainder;
566 } signed_div(long long int, long long int);
567 /*---------------------------------------------------------------------------*/
571 /*---------------------------------------------------------------------------*/
572 #define GET(X, Y, Z) do { \
575 rc = regget(X, Y, Z); \
577 JOT(8, ":-(%i\n", __LINE__); return(rc); \
581 #define SET(X, Y, Z) do { \
583 rc = regset(X, Y, Z); \
585 JOT(8, ":-(%i\n", __LINE__); return(rc); \
588 /*---------------------------------------------------------------------------*/
590 #define SAY(format, args...) do { \
591 printk(KERN_DEBUG "easycap: %s: " format, __func__, ##args); \
595 #if defined(EASYCAP_DEBUG)
596 #define JOT(n, format, args...) do { \
597 if (n <= easycap_debug) { \
598 printk(KERN_DEBUG "easycap: %s: " format, __func__, ##args); \
602 #define JOT(n, format, args...) do {} while (0)
603 #endif /*EASYCAP_DEBUG*/
605 #define POUT JOT(8, ":-(in file %s line %4i\n", __FILE__, __LINE__)
607 #define MICROSECONDS(X, Y) \
608 ((1000000*((long long int)(X.tv_sec - Y.tv_sec))) + \
609 (long long int)(X.tv_usec - Y.tv_usec))
611 /*---------------------------------------------------------------------------*/
613 * (unsigned char *)P pointer to next byte pair
614 * (long int *)X pointer to accumulating count
615 * (long int *)Y pointer to accumulating sum
616 * (long long int *)Z pointer to accumulating sum of squares
618 /*---------------------------------------------------------------------------*/
619 #define SUMMER(P, X, Y, Z) do { \
621 unsigned int u0, u1, u2; \
623 p = (unsigned char *)(P); \
624 u0 = (unsigned int) (*p); \
625 u1 = (unsigned int) (*(p + 1)); \
626 u2 = (unsigned int) ((u1 << 8) | u0); \
628 s = -(long int)(0x7FFF & (~u2)); \
630 s = (long int)(0x7FFF & u2); \
631 *((X)) += (long int) 1; \
632 *((Y)) += (long int) s; \
633 *((Z)) += ((long long int)(s) * (long long int)(s)); \
635 /*---------------------------------------------------------------------------*/