Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013 Lubomir Rintel
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions, and the following disclaimer,
0010  *    without modification.
0011  * 2. The name of the author may not be used to endorse or promote products
0012  *    derived from this software without specific prior written permission.
0013  *
0014  * Alternatively, this software may be distributed under the terms of the
0015  * GNU General Public License ("GPL").
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0020  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0021  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0022  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0023  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0024  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0025  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0027  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028  */
0029 /*
0030  * Fushicai USBTV007 Audio-Video Grabber Driver
0031  *
0032  * No physical hardware was harmed running Windows during the
0033  * reverse-engineering activity
0034  */
0035 
0036 #include <linux/module.h>
0037 #include <linux/slab.h>
0038 #include <linux/usb.h>
0039 
0040 #include <media/v4l2-device.h>
0041 #include <media/v4l2-ctrls.h>
0042 #include <media/videobuf2-v4l2.h>
0043 #include <media/videobuf2-vmalloc.h>
0044 
0045 /* Hardware. */
0046 #define USBTV_VIDEO_ENDP    0x81
0047 #define USBTV_AUDIO_ENDP    0x83
0048 #define USBTV_BASE      0xc000
0049 #define USBTV_CONTROL_REG   11
0050 #define USBTV_REQUEST_REG   12
0051 
0052 /* Number of concurrent isochronous urbs submitted.
0053  * Higher numbers was seen to overly saturate the USB bus. */
0054 #define USBTV_ISOC_TRANSFERS    16
0055 #define USBTV_ISOC_PACKETS  8
0056 
0057 #define USBTV_CHUNK_SIZE    256
0058 #define USBTV_CHUNK     240
0059 
0060 #define USBTV_AUDIO_URBSIZE 20480
0061 #define USBTV_AUDIO_HDRSIZE 4
0062 #define USBTV_AUDIO_BUFFER  65536
0063 
0064 /* Chunk header. */
0065 #define USBTV_MAGIC_OK(chunk)   ((be32_to_cpu(chunk[0]) & 0xff000000) \
0066                             == 0x88000000)
0067 #define USBTV_FRAME_ID(chunk)   ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16)
0068 #define USBTV_ODD(chunk)    ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
0069 #define USBTV_CHUNK_NO(chunk)   (be32_to_cpu(chunk[0]) & 0x00000fff)
0070 
0071 #define USBTV_TV_STD        (V4L2_STD_525_60 | V4L2_STD_PAL | \
0072                  V4L2_STD_PAL_Nc | V4L2_STD_SECAM)
0073 
0074 /* parameters for supported TV norms */
0075 struct usbtv_norm_params {
0076     v4l2_std_id norm;
0077     int cap_width, cap_height;
0078 };
0079 
0080 /* A single videobuf2 frame buffer. */
0081 struct usbtv_buf {
0082     struct vb2_v4l2_buffer vb;
0083     struct list_head list;
0084 };
0085 
0086 /* Per-device structure. */
0087 struct usbtv {
0088     struct device *dev;
0089     struct usb_device *udev;
0090 
0091     /* video */
0092     struct v4l2_device v4l2_dev;
0093     struct v4l2_ctrl_handler ctrl;
0094     struct video_device vdev;
0095     struct vb2_queue vb2q;
0096     struct mutex v4l2_lock;
0097     struct mutex vb2q_lock;
0098 
0099     /* List of videobuf2 buffers protected by a lock. */
0100     spinlock_t buflock;
0101     struct list_head bufs;
0102 
0103     /* Number of currently processed frame, useful find
0104      * out when a new one begins. */
0105     u32 frame_id;
0106     int chunks_done;
0107 
0108     enum {
0109         USBTV_COMPOSITE_INPUT,
0110         USBTV_SVIDEO_INPUT,
0111     } input;
0112     v4l2_std_id norm;
0113     int width, height;
0114     int n_chunks;
0115     int iso_size;
0116     int last_odd;
0117     unsigned int sequence;
0118     struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS];
0119 
0120     /* audio */
0121     struct snd_card *snd;
0122     struct snd_pcm_substream *snd_substream;
0123     atomic_t snd_stream;
0124     struct work_struct snd_trigger;
0125     struct urb *snd_bulk_urb;
0126     size_t snd_buffer_pos;
0127     size_t snd_period_pos;
0128 };
0129 
0130 int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size);
0131 
0132 int usbtv_video_init(struct usbtv *usbtv);
0133 void usbtv_video_free(struct usbtv *usbtv);
0134 
0135 int usbtv_audio_init(struct usbtv *usbtv);
0136 void usbtv_audio_free(struct usbtv *usbtv);
0137 void usbtv_audio_suspend(struct usbtv *usbtv);
0138 void usbtv_audio_resume(struct usbtv *usbtv);