Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013,2016 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  * Product web site:
0033  * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
0034  *
0035  * Following LWN articles were very useful in construction of this driver:
0036  * Video4Linux2 API series: http://lwn.net/Articles/203924/
0037  * videobuf2 API explanation: http://lwn.net/Articles/447435/
0038  * Thanks go to Jonathan Corbet for providing this quality documentation.
0039  * He is awesome.
0040  *
0041  * No physical hardware was harmed running Windows during the
0042  * reverse-engineering activity
0043  */
0044 
0045 #include <media/v4l2-ioctl.h>
0046 #include <media/videobuf2-v4l2.h>
0047 
0048 #include "usbtv.h"
0049 
0050 static const struct usbtv_norm_params norm_params[] = {
0051     {
0052         .norm = V4L2_STD_525_60,
0053         .cap_width = 720,
0054         .cap_height = 480,
0055     },
0056     {
0057         .norm = V4L2_STD_625_50,
0058         .cap_width = 720,
0059         .cap_height = 576,
0060     }
0061 };
0062 
0063 static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
0064 {
0065     int i, ret = 0;
0066     const struct usbtv_norm_params *params = NULL;
0067 
0068     for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
0069         if (norm_params[i].norm & norm) {
0070             params = &norm_params[i];
0071             break;
0072         }
0073     }
0074 
0075     if (params) {
0076         usbtv->width = params->cap_width;
0077         usbtv->height = params->cap_height;
0078         usbtv->n_chunks = usbtv->width * usbtv->height
0079                         / 4 / USBTV_CHUNK;
0080         usbtv->norm = norm;
0081     } else
0082         ret = -EINVAL;
0083 
0084     return ret;
0085 }
0086 
0087 static int usbtv_select_input(struct usbtv *usbtv, int input)
0088 {
0089     int ret;
0090 
0091     static const u16 composite[][2] = {
0092         { USBTV_BASE + 0x0105, 0x0060 },
0093         { USBTV_BASE + 0x011f, 0x00f2 },
0094         { USBTV_BASE + 0x0127, 0x0060 },
0095         { USBTV_BASE + 0x00ae, 0x0010 },
0096         { USBTV_BASE + 0x0239, 0x0060 },
0097     };
0098 
0099     static const u16 svideo[][2] = {
0100         { USBTV_BASE + 0x0105, 0x0010 },
0101         { USBTV_BASE + 0x011f, 0x00ff },
0102         { USBTV_BASE + 0x0127, 0x0060 },
0103         { USBTV_BASE + 0x00ae, 0x0030 },
0104         { USBTV_BASE + 0x0239, 0x0060 },
0105     };
0106 
0107     switch (input) {
0108     case USBTV_COMPOSITE_INPUT:
0109         ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
0110         break;
0111     case USBTV_SVIDEO_INPUT:
0112         ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
0113         break;
0114     default:
0115         ret = -EINVAL;
0116     }
0117 
0118     if (!ret)
0119         usbtv->input = input;
0120 
0121     return ret;
0122 }
0123 
0124 static uint16_t usbtv_norm_to_16f_reg(v4l2_std_id norm)
0125 {
0126     /* NTSC M/M-JP/M-KR */
0127     if (norm & V4L2_STD_NTSC)
0128         return 0x00b8;
0129     /* PAL BG/DK/H/I */
0130     if (norm & V4L2_STD_PAL)
0131         return 0x00ee;
0132     /* SECAM B/D/G/H/K/K1/L/Lc */
0133     if (norm & V4L2_STD_SECAM)
0134         return 0x00ff;
0135     if (norm & V4L2_STD_NTSC_443)
0136         return 0x00a8;
0137     if (norm & (V4L2_STD_PAL_M | V4L2_STD_PAL_60))
0138         return 0x00bc;
0139     if (norm & V4L2_STD_PAL_Nc)
0140         return 0x00fe;
0141     /* Fallback to automatic detection for other standards */
0142     return 0x0000;
0143 }
0144 
0145 static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
0146 {
0147     int ret;
0148     /* These are the series of register values used to configure the
0149      * decoder for a specific standard.
0150      * The first 21 register writes are copied from the
0151      * Settings\DecoderDefaults registry keys present in the Windows driver
0152      * .INF file, and control various image tuning parameters (color
0153      * correction, sharpness, ...).
0154      */
0155     static const u16 pal[][2] = {
0156         /* "AVPAL" tuning sequence from .INF file */
0157         { USBTV_BASE + 0x0003, 0x0004 },
0158         { USBTV_BASE + 0x001a, 0x0068 },
0159         { USBTV_BASE + 0x0100, 0x00d3 },
0160         { USBTV_BASE + 0x010e, 0x0072 },
0161         { USBTV_BASE + 0x010f, 0x00a2 },
0162         { USBTV_BASE + 0x0112, 0x00b0 },
0163         { USBTV_BASE + 0x0115, 0x0015 },
0164         { USBTV_BASE + 0x0117, 0x0001 },
0165         { USBTV_BASE + 0x0118, 0x002c },
0166         { USBTV_BASE + 0x012d, 0x0010 },
0167         { USBTV_BASE + 0x012f, 0x0020 },
0168         { USBTV_BASE + 0x0220, 0x002e },
0169         { USBTV_BASE + 0x0225, 0x0008 },
0170         { USBTV_BASE + 0x024e, 0x0002 },
0171         { USBTV_BASE + 0x024f, 0x0002 },
0172         { USBTV_BASE + 0x0254, 0x0059 },
0173         { USBTV_BASE + 0x025a, 0x0016 },
0174         { USBTV_BASE + 0x025b, 0x0035 },
0175         { USBTV_BASE + 0x0263, 0x0017 },
0176         { USBTV_BASE + 0x0266, 0x0016 },
0177         { USBTV_BASE + 0x0267, 0x0036 },
0178         /* End image tuning */
0179         { USBTV_BASE + 0x024e, 0x0002 },
0180         { USBTV_BASE + 0x024f, 0x0002 },
0181     };
0182 
0183     static const u16 ntsc[][2] = {
0184         /* "AVNTSC" tuning sequence from .INF file */
0185         { USBTV_BASE + 0x0003, 0x0004 },
0186         { USBTV_BASE + 0x001a, 0x0079 },
0187         { USBTV_BASE + 0x0100, 0x00d3 },
0188         { USBTV_BASE + 0x010e, 0x0068 },
0189         { USBTV_BASE + 0x010f, 0x009c },
0190         { USBTV_BASE + 0x0112, 0x00f0 },
0191         { USBTV_BASE + 0x0115, 0x0015 },
0192         { USBTV_BASE + 0x0117, 0x0000 },
0193         { USBTV_BASE + 0x0118, 0x00fc },
0194         { USBTV_BASE + 0x012d, 0x0004 },
0195         { USBTV_BASE + 0x012f, 0x0008 },
0196         { USBTV_BASE + 0x0220, 0x002e },
0197         { USBTV_BASE + 0x0225, 0x0008 },
0198         { USBTV_BASE + 0x024e, 0x0002 },
0199         { USBTV_BASE + 0x024f, 0x0001 },
0200         { USBTV_BASE + 0x0254, 0x005f },
0201         { USBTV_BASE + 0x025a, 0x0012 },
0202         { USBTV_BASE + 0x025b, 0x0001 },
0203         { USBTV_BASE + 0x0263, 0x001c },
0204         { USBTV_BASE + 0x0266, 0x0011 },
0205         { USBTV_BASE + 0x0267, 0x0005 },
0206         /* End image tuning */
0207         { USBTV_BASE + 0x024e, 0x0002 },
0208         { USBTV_BASE + 0x024f, 0x0002 },
0209     };
0210 
0211     static const u16 secam[][2] = {
0212         /* "AVSECAM" tuning sequence from .INF file */
0213         { USBTV_BASE + 0x0003, 0x0004 },
0214         { USBTV_BASE + 0x001a, 0x0073 },
0215         { USBTV_BASE + 0x0100, 0x00dc },
0216         { USBTV_BASE + 0x010e, 0x0072 },
0217         { USBTV_BASE + 0x010f, 0x00a2 },
0218         { USBTV_BASE + 0x0112, 0x0090 },
0219         { USBTV_BASE + 0x0115, 0x0035 },
0220         { USBTV_BASE + 0x0117, 0x0001 },
0221         { USBTV_BASE + 0x0118, 0x0030 },
0222         { USBTV_BASE + 0x012d, 0x0004 },
0223         { USBTV_BASE + 0x012f, 0x0008 },
0224         { USBTV_BASE + 0x0220, 0x002d },
0225         { USBTV_BASE + 0x0225, 0x0028 },
0226         { USBTV_BASE + 0x024e, 0x0008 },
0227         { USBTV_BASE + 0x024f, 0x0002 },
0228         { USBTV_BASE + 0x0254, 0x0069 },
0229         { USBTV_BASE + 0x025a, 0x0016 },
0230         { USBTV_BASE + 0x025b, 0x0035 },
0231         { USBTV_BASE + 0x0263, 0x0021 },
0232         { USBTV_BASE + 0x0266, 0x0016 },
0233         { USBTV_BASE + 0x0267, 0x0036 },
0234         /* End image tuning */
0235         { USBTV_BASE + 0x024e, 0x0002 },
0236         { USBTV_BASE + 0x024f, 0x0002 },
0237     };
0238 
0239     ret = usbtv_configure_for_norm(usbtv, norm);
0240 
0241     if (!ret) {
0242         /* Masks for norms using a NTSC or PAL color encoding. */
0243         static const v4l2_std_id ntsc_mask =
0244             V4L2_STD_NTSC | V4L2_STD_NTSC_443;
0245         static const v4l2_std_id pal_mask =
0246             V4L2_STD_PAL | V4L2_STD_PAL_60 | V4L2_STD_PAL_M |
0247             V4L2_STD_PAL_Nc;
0248 
0249         if (norm & ntsc_mask)
0250             ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
0251         else if (norm & pal_mask)
0252             ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
0253         else if (norm & V4L2_STD_SECAM)
0254             ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam));
0255         else
0256             ret = -EINVAL;
0257     }
0258 
0259     if (!ret) {
0260         /* Configure the decoder for the color standard */
0261         const u16 cfg[][2] = {
0262             { USBTV_BASE + 0x016f, usbtv_norm_to_16f_reg(norm) }
0263         };
0264         ret = usbtv_set_regs(usbtv, cfg, ARRAY_SIZE(cfg));
0265     }
0266 
0267     return ret;
0268 }
0269 
0270 static int usbtv_setup_capture(struct usbtv *usbtv)
0271 {
0272     int ret;
0273     static const u16 setup[][2] = {
0274         /* These seem to enable the device. */
0275         { USBTV_BASE + 0x0008, 0x0001 },
0276         { USBTV_BASE + 0x01d0, 0x00ff },
0277         { USBTV_BASE + 0x01d9, 0x0002 },
0278 
0279         /* These seem to influence color parameters, such as
0280          * brightness, etc. */
0281         { USBTV_BASE + 0x0239, 0x0040 },
0282         { USBTV_BASE + 0x0240, 0x0000 },
0283         { USBTV_BASE + 0x0241, 0x0000 },
0284         { USBTV_BASE + 0x0242, 0x0002 },
0285         { USBTV_BASE + 0x0243, 0x0080 },
0286         { USBTV_BASE + 0x0244, 0x0012 },
0287         { USBTV_BASE + 0x0245, 0x0090 },
0288         { USBTV_BASE + 0x0246, 0x0000 },
0289 
0290         { USBTV_BASE + 0x0278, 0x002d },
0291         { USBTV_BASE + 0x0279, 0x000a },
0292         { USBTV_BASE + 0x027a, 0x0032 },
0293         { 0xf890, 0x000c },
0294         { 0xf894, 0x0086 },
0295 
0296         { USBTV_BASE + 0x00ac, 0x00c0 },
0297         { USBTV_BASE + 0x00ad, 0x0000 },
0298         { USBTV_BASE + 0x00a2, 0x0012 },
0299         { USBTV_BASE + 0x00a3, 0x00e0 },
0300         { USBTV_BASE + 0x00a4, 0x0028 },
0301         { USBTV_BASE + 0x00a5, 0x0082 },
0302         { USBTV_BASE + 0x00a7, 0x0080 },
0303         { USBTV_BASE + 0x0000, 0x0014 },
0304         { USBTV_BASE + 0x0006, 0x0003 },
0305         { USBTV_BASE + 0x0090, 0x0099 },
0306         { USBTV_BASE + 0x0091, 0x0090 },
0307         { USBTV_BASE + 0x0094, 0x0068 },
0308         { USBTV_BASE + 0x0095, 0x0070 },
0309         { USBTV_BASE + 0x009c, 0x0030 },
0310         { USBTV_BASE + 0x009d, 0x00c0 },
0311         { USBTV_BASE + 0x009e, 0x00e0 },
0312         { USBTV_BASE + 0x0019, 0x0006 },
0313         { USBTV_BASE + 0x008c, 0x00ba },
0314         { USBTV_BASE + 0x0101, 0x00ff },
0315         { USBTV_BASE + 0x010c, 0x00b3 },
0316         { USBTV_BASE + 0x01b2, 0x0080 },
0317         { USBTV_BASE + 0x01b4, 0x00a0 },
0318         { USBTV_BASE + 0x014c, 0x00ff },
0319         { USBTV_BASE + 0x014d, 0x00ca },
0320         { USBTV_BASE + 0x0113, 0x0053 },
0321         { USBTV_BASE + 0x0119, 0x008a },
0322         { USBTV_BASE + 0x013c, 0x0003 },
0323         { USBTV_BASE + 0x0150, 0x009c },
0324         { USBTV_BASE + 0x0151, 0x0071 },
0325         { USBTV_BASE + 0x0152, 0x00c6 },
0326         { USBTV_BASE + 0x0153, 0x0084 },
0327         { USBTV_BASE + 0x0154, 0x00bc },
0328         { USBTV_BASE + 0x0155, 0x00a0 },
0329         { USBTV_BASE + 0x0156, 0x00a0 },
0330         { USBTV_BASE + 0x0157, 0x009c },
0331         { USBTV_BASE + 0x0158, 0x001f },
0332         { USBTV_BASE + 0x0159, 0x0006 },
0333         { USBTV_BASE + 0x015d, 0x0000 },
0334     };
0335 
0336     ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
0337     if (ret)
0338         return ret;
0339 
0340     ret = usbtv_select_norm(usbtv, usbtv->norm);
0341     if (ret)
0342         return ret;
0343 
0344     ret = usbtv_select_input(usbtv, usbtv->input);
0345     if (ret)
0346         return ret;
0347 
0348     ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
0349     if (ret)
0350         return ret;
0351 
0352     return 0;
0353 }
0354 
0355 /* Copy data from chunk into a frame buffer, deinterlacing the data
0356  * into every second line. Unfortunately, they don't align nicely into
0357  * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
0358  * Therefore, we break down the chunk into two halves before copying,
0359  * so that we can interleave a line if needed.
0360  *
0361  * Each "chunk" is 240 words; a word in this context equals 4 bytes.
0362  * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
0363  * pixels, the Cr and Cb shared between the two pixels, but each having
0364  * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
0365  * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
0366  * The image is interlaced, so there is a "scan" of odd lines, followed
0367  * by "scan" of even numbered lines.
0368  *
0369  * Following code is writing the chunks in correct sequence, skipping
0370  * the rows based on "odd" value.
0371  * line 1: chunk[0][  0..479] chunk[0][480..959] chunk[1][  0..479]
0372  * line 3: chunk[1][480..959] chunk[2][  0..479] chunk[2][480..959]
0373  * ...etc.
0374  */
0375 static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
0376 {
0377     int half;
0378 
0379     for (half = 0; half < 2; half++) {
0380         int part_no = chunk_no * 2 + half;
0381         int line = part_no / 3;
0382         int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
0383 
0384         u32 *dst = &frame[part_index * USBTV_CHUNK/2];
0385 
0386         memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
0387         src += USBTV_CHUNK/2;
0388     }
0389 }
0390 
0391 /* Called for each 256-byte image chunk.
0392  * First word identifies the chunk, followed by 240 words of image
0393  * data and padding. */
0394 static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
0395 {
0396     int frame_id, odd, chunk_no;
0397     u32 *frame;
0398     struct usbtv_buf *buf;
0399     unsigned long flags;
0400 
0401     /* Ignore corrupted lines. */
0402     if (!USBTV_MAGIC_OK(chunk))
0403         return;
0404     frame_id = USBTV_FRAME_ID(chunk);
0405     odd = USBTV_ODD(chunk);
0406     chunk_no = USBTV_CHUNK_NO(chunk);
0407     if (chunk_no >= usbtv->n_chunks)
0408         return;
0409 
0410     /* Beginning of a frame. */
0411     if (chunk_no == 0) {
0412         usbtv->frame_id = frame_id;
0413         usbtv->chunks_done = 0;
0414     }
0415 
0416     if (usbtv->frame_id != frame_id)
0417         return;
0418 
0419     spin_lock_irqsave(&usbtv->buflock, flags);
0420     if (list_empty(&usbtv->bufs)) {
0421         /* No free buffers. Userspace likely too slow. */
0422         spin_unlock_irqrestore(&usbtv->buflock, flags);
0423         return;
0424     }
0425 
0426     /* First available buffer. */
0427     buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
0428     frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
0429 
0430     /* Copy the chunk data. */
0431     usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
0432     usbtv->chunks_done++;
0433 
0434     /* Last chunk in a field */
0435     if (chunk_no == usbtv->n_chunks-1) {
0436         /* Last chunk in a frame, signalling an end */
0437         if (odd && !usbtv->last_odd) {
0438             int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
0439             enum vb2_buffer_state state = usbtv->chunks_done ==
0440                 usbtv->n_chunks ?
0441                 VB2_BUF_STATE_DONE :
0442                 VB2_BUF_STATE_ERROR;
0443 
0444             buf->vb.field = V4L2_FIELD_INTERLACED;
0445             buf->vb.sequence = usbtv->sequence++;
0446             buf->vb.vb2_buf.timestamp = ktime_get_ns();
0447             vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
0448             vb2_buffer_done(&buf->vb.vb2_buf, state);
0449             list_del(&buf->list);
0450         }
0451         usbtv->last_odd = odd;
0452     }
0453 
0454     spin_unlock_irqrestore(&usbtv->buflock, flags);
0455 }
0456 
0457 /* Got image data. Each packet contains a number of 256-word chunks we
0458  * compose the image from. */
0459 static void usbtv_iso_cb(struct urb *ip)
0460 {
0461     int ret;
0462     int i;
0463     struct usbtv *usbtv = (struct usbtv *)ip->context;
0464 
0465     switch (ip->status) {
0466     /* All fine. */
0467     case 0:
0468         break;
0469     /* Device disconnected or capture stopped? */
0470     case -ENODEV:
0471     case -ENOENT:
0472     case -ECONNRESET:
0473     case -ESHUTDOWN:
0474         return;
0475     /* Unknown error. Retry. */
0476     default:
0477         dev_warn(usbtv->dev, "Bad response for ISO request.\n");
0478         goto resubmit;
0479     }
0480 
0481     for (i = 0; i < ip->number_of_packets; i++) {
0482         int size = ip->iso_frame_desc[i].actual_length;
0483         unsigned char *data = ip->transfer_buffer +
0484                 ip->iso_frame_desc[i].offset;
0485         int offset;
0486 
0487         for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
0488             usbtv_image_chunk(usbtv,
0489                 (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
0490     }
0491 
0492 resubmit:
0493     ret = usb_submit_urb(ip, GFP_ATOMIC);
0494     if (ret < 0)
0495         dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
0496 }
0497 
0498 static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
0499 {
0500     struct urb *ip;
0501     int size = usbtv->iso_size;
0502     int i;
0503 
0504     ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
0505     if (ip == NULL)
0506         return NULL;
0507 
0508     ip->dev = usbtv->udev;
0509     ip->context = usbtv;
0510     ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
0511     ip->interval = 1;
0512     ip->transfer_flags = URB_ISO_ASAP;
0513     ip->transfer_buffer = kcalloc(USBTV_ISOC_PACKETS, size,
0514                         GFP_KERNEL);
0515     if (!ip->transfer_buffer) {
0516         usb_free_urb(ip);
0517         return NULL;
0518     }
0519     ip->complete = usbtv_iso_cb;
0520     ip->number_of_packets = USBTV_ISOC_PACKETS;
0521     ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
0522     for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
0523         ip->iso_frame_desc[i].offset = size * i;
0524         ip->iso_frame_desc[i].length = size;
0525     }
0526 
0527     return ip;
0528 }
0529 
0530 static void usbtv_stop(struct usbtv *usbtv)
0531 {
0532     int i;
0533     unsigned long flags;
0534 
0535     /* Cancel running transfers. */
0536     for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
0537         struct urb *ip = usbtv->isoc_urbs[i];
0538 
0539         if (ip == NULL)
0540             continue;
0541         usb_kill_urb(ip);
0542         kfree(ip->transfer_buffer);
0543         usb_free_urb(ip);
0544         usbtv->isoc_urbs[i] = NULL;
0545     }
0546 
0547     /* Return buffers to userspace. */
0548     spin_lock_irqsave(&usbtv->buflock, flags);
0549     while (!list_empty(&usbtv->bufs)) {
0550         struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
0551                         struct usbtv_buf, list);
0552         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0553         list_del(&buf->list);
0554     }
0555     spin_unlock_irqrestore(&usbtv->buflock, flags);
0556 }
0557 
0558 static int usbtv_start(struct usbtv *usbtv)
0559 {
0560     int i;
0561     int ret;
0562 
0563     usbtv_audio_suspend(usbtv);
0564 
0565     ret = usb_set_interface(usbtv->udev, 0, 0);
0566     if (ret < 0)
0567         return ret;
0568 
0569     ret = usbtv_setup_capture(usbtv);
0570     if (ret < 0)
0571         return ret;
0572 
0573     ret = usb_set_interface(usbtv->udev, 0, 1);
0574     if (ret < 0)
0575         return ret;
0576 
0577     usbtv_audio_resume(usbtv);
0578 
0579     for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
0580         struct urb *ip;
0581 
0582         ip = usbtv_setup_iso_transfer(usbtv);
0583         if (ip == NULL) {
0584             ret = -ENOMEM;
0585             goto start_fail;
0586         }
0587         usbtv->isoc_urbs[i] = ip;
0588 
0589         ret = usb_submit_urb(ip, GFP_KERNEL);
0590         if (ret < 0)
0591             goto start_fail;
0592     }
0593 
0594     return 0;
0595 
0596 start_fail:
0597     usbtv_stop(usbtv);
0598     return ret;
0599 }
0600 
0601 static int usbtv_querycap(struct file *file, void *priv,
0602                 struct v4l2_capability *cap)
0603 {
0604     struct usbtv *dev = video_drvdata(file);
0605 
0606     strscpy(cap->driver, "usbtv", sizeof(cap->driver));
0607     strscpy(cap->card, "usbtv", sizeof(cap->card));
0608     usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0609     return 0;
0610 }
0611 
0612 static int usbtv_enum_input(struct file *file, void *priv,
0613                     struct v4l2_input *i)
0614 {
0615     struct usbtv *dev = video_drvdata(file);
0616 
0617     switch (i->index) {
0618     case USBTV_COMPOSITE_INPUT:
0619         strscpy(i->name, "Composite", sizeof(i->name));
0620         break;
0621     case USBTV_SVIDEO_INPUT:
0622         strscpy(i->name, "S-Video", sizeof(i->name));
0623         break;
0624     default:
0625         return -EINVAL;
0626     }
0627 
0628     i->type = V4L2_INPUT_TYPE_CAMERA;
0629     i->std = dev->vdev.tvnorms;
0630     return 0;
0631 }
0632 
0633 static int usbtv_enum_fmt_vid_cap(struct file *file, void  *priv,
0634                     struct v4l2_fmtdesc *f)
0635 {
0636     if (f->index > 0)
0637         return -EINVAL;
0638 
0639     f->pixelformat = V4L2_PIX_FMT_YUYV;
0640     return 0;
0641 }
0642 
0643 static int usbtv_fmt_vid_cap(struct file *file, void *priv,
0644                     struct v4l2_format *f)
0645 {
0646     struct usbtv *usbtv = video_drvdata(file);
0647 
0648     f->fmt.pix.width = usbtv->width;
0649     f->fmt.pix.height = usbtv->height;
0650     f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
0651     f->fmt.pix.field = V4L2_FIELD_INTERLACED;
0652     f->fmt.pix.bytesperline = usbtv->width * 2;
0653     f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
0654     f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
0655 
0656     return 0;
0657 }
0658 
0659 static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
0660 {
0661     struct usbtv *usbtv = video_drvdata(file);
0662     *norm = usbtv->norm;
0663     return 0;
0664 }
0665 
0666 static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
0667 {
0668     int ret = -EINVAL;
0669     struct usbtv *usbtv = video_drvdata(file);
0670 
0671     if (norm & USBTV_TV_STD)
0672         ret = usbtv_select_norm(usbtv, norm);
0673 
0674     return ret;
0675 }
0676 
0677 static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
0678 {
0679     struct usbtv *usbtv = video_drvdata(file);
0680     *i = usbtv->input;
0681     return 0;
0682 }
0683 
0684 static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
0685 {
0686     struct usbtv *usbtv = video_drvdata(file);
0687 
0688     return usbtv_select_input(usbtv, i);
0689 }
0690 
0691 static const struct v4l2_ioctl_ops usbtv_ioctl_ops = {
0692     .vidioc_querycap = usbtv_querycap,
0693     .vidioc_enum_input = usbtv_enum_input,
0694     .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
0695     .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
0696     .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
0697     .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
0698     .vidioc_g_std = usbtv_g_std,
0699     .vidioc_s_std = usbtv_s_std,
0700     .vidioc_g_input = usbtv_g_input,
0701     .vidioc_s_input = usbtv_s_input,
0702 
0703     .vidioc_reqbufs = vb2_ioctl_reqbufs,
0704     .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
0705     .vidioc_querybuf = vb2_ioctl_querybuf,
0706     .vidioc_create_bufs = vb2_ioctl_create_bufs,
0707     .vidioc_qbuf = vb2_ioctl_qbuf,
0708     .vidioc_dqbuf = vb2_ioctl_dqbuf,
0709     .vidioc_streamon = vb2_ioctl_streamon,
0710     .vidioc_streamoff = vb2_ioctl_streamoff,
0711 };
0712 
0713 static const struct v4l2_file_operations usbtv_fops = {
0714     .owner = THIS_MODULE,
0715     .unlocked_ioctl = video_ioctl2,
0716     .mmap = vb2_fop_mmap,
0717     .open = v4l2_fh_open,
0718     .release = vb2_fop_release,
0719     .read = vb2_fop_read,
0720     .poll = vb2_fop_poll,
0721 };
0722 
0723 static int usbtv_queue_setup(struct vb2_queue *vq,
0724     unsigned int *nbuffers,
0725     unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
0726 {
0727     struct usbtv *usbtv = vb2_get_drv_priv(vq);
0728     unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
0729 
0730     if (vq->num_buffers + *nbuffers < 2)
0731         *nbuffers = 2 - vq->num_buffers;
0732     if (*nplanes)
0733         return sizes[0] < size ? -EINVAL : 0;
0734     *nplanes = 1;
0735     sizes[0] = size;
0736 
0737     return 0;
0738 }
0739 
0740 static void usbtv_buf_queue(struct vb2_buffer *vb)
0741 {
0742     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0743     struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
0744     struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
0745     unsigned long flags;
0746 
0747     if (usbtv->udev == NULL) {
0748         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
0749         return;
0750     }
0751 
0752     spin_lock_irqsave(&usbtv->buflock, flags);
0753     list_add_tail(&buf->list, &usbtv->bufs);
0754     spin_unlock_irqrestore(&usbtv->buflock, flags);
0755 }
0756 
0757 static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
0758 {
0759     struct usbtv *usbtv = vb2_get_drv_priv(vq);
0760 
0761     if (usbtv->udev == NULL)
0762         return -ENODEV;
0763 
0764     usbtv->last_odd = 1;
0765     usbtv->sequence = 0;
0766     return usbtv_start(usbtv);
0767 }
0768 
0769 static void usbtv_stop_streaming(struct vb2_queue *vq)
0770 {
0771     struct usbtv *usbtv = vb2_get_drv_priv(vq);
0772 
0773     if (usbtv->udev)
0774         usbtv_stop(usbtv);
0775 }
0776 
0777 static const struct vb2_ops usbtv_vb2_ops = {
0778     .queue_setup = usbtv_queue_setup,
0779     .buf_queue = usbtv_buf_queue,
0780     .start_streaming = usbtv_start_streaming,
0781     .stop_streaming = usbtv_stop_streaming,
0782     .wait_prepare = vb2_ops_wait_prepare,
0783     .wait_finish = vb2_ops_wait_finish,
0784 };
0785 
0786 static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
0787 {
0788     struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
0789                                 ctrl);
0790     u8 *data;
0791     u16 index, size;
0792     int ret;
0793 
0794     data = kmalloc(3, GFP_KERNEL);
0795     if (!data)
0796         return -ENOMEM;
0797 
0798     /*
0799      * Read in the current brightness/contrast registers. We need them
0800      * both, because the values are for some reason interleaved.
0801      */
0802     if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
0803         ret = usb_control_msg(usbtv->udev,
0804             usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
0805             USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0806             0, USBTV_BASE + 0x0244, (void *)data, 3,
0807             USB_CTRL_GET_TIMEOUT);
0808         if (ret < 0)
0809             goto error;
0810     }
0811 
0812     switch (ctrl->id) {
0813     case V4L2_CID_BRIGHTNESS:
0814         index = USBTV_BASE + 0x0244;
0815         size = 3;
0816         data[0] &= 0xf0;
0817         data[0] |= (ctrl->val >> 8) & 0xf;
0818         data[2] = ctrl->val & 0xff;
0819         break;
0820     case V4L2_CID_CONTRAST:
0821         index = USBTV_BASE + 0x0244;
0822         size = 3;
0823         data[0] &= 0x0f;
0824         data[0] |= (ctrl->val >> 4) & 0xf0;
0825         data[1] = ctrl->val & 0xff;
0826         break;
0827     case V4L2_CID_SATURATION:
0828         index = USBTV_BASE + 0x0242;
0829         data[0] = ctrl->val >> 8;
0830         data[1] = ctrl->val & 0xff;
0831         size = 2;
0832         break;
0833     case V4L2_CID_HUE:
0834         index = USBTV_BASE + 0x0240;
0835         size = 2;
0836         if (ctrl->val > 0) {
0837             data[0] = 0x92 + (ctrl->val >> 8);
0838             data[1] = ctrl->val & 0xff;
0839         } else {
0840             data[0] = 0x82 + (-ctrl->val >> 8);
0841             data[1] = -ctrl->val & 0xff;
0842         }
0843         break;
0844     case V4L2_CID_SHARPNESS:
0845         index = USBTV_BASE + 0x0239;
0846         data[0] = 0;
0847         data[1] = ctrl->val;
0848         size = 2;
0849         break;
0850     default:
0851         kfree(data);
0852         return -EINVAL;
0853     }
0854 
0855     ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
0856             USBTV_CONTROL_REG,
0857             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0858             0, index, (void *)data, size, USB_CTRL_SET_TIMEOUT);
0859 
0860 error:
0861     if (ret < 0)
0862         dev_warn(usbtv->dev, "Failed to submit a control request.\n");
0863 
0864     kfree(data);
0865     return ret;
0866 }
0867 
0868 static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
0869     .s_ctrl = usbtv_s_ctrl,
0870 };
0871 
0872 static void usbtv_release(struct v4l2_device *v4l2_dev)
0873 {
0874     struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
0875 
0876     v4l2_device_unregister(&usbtv->v4l2_dev);
0877     v4l2_ctrl_handler_free(&usbtv->ctrl);
0878     kfree(usbtv);
0879 }
0880 
0881 int usbtv_video_init(struct usbtv *usbtv)
0882 {
0883     int ret;
0884 
0885     (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
0886 
0887     spin_lock_init(&usbtv->buflock);
0888     mutex_init(&usbtv->v4l2_lock);
0889     mutex_init(&usbtv->vb2q_lock);
0890     INIT_LIST_HEAD(&usbtv->bufs);
0891 
0892     /* videobuf2 structure */
0893     usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
0894     usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
0895     usbtv->vb2q.drv_priv = usbtv;
0896     usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
0897     usbtv->vb2q.ops = &usbtv_vb2_ops;
0898     usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
0899     usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
0900     usbtv->vb2q.lock = &usbtv->vb2q_lock;
0901     ret = vb2_queue_init(&usbtv->vb2q);
0902     if (ret < 0) {
0903         dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
0904         return ret;
0905     }
0906 
0907     /* controls */
0908     v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
0909     v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
0910             V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
0911     v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
0912             V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
0913     v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
0914             V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
0915     v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
0916             V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
0917     v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
0918             V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
0919     ret = usbtv->ctrl.error;
0920     if (ret < 0) {
0921         dev_warn(usbtv->dev, "Could not initialize controls\n");
0922         goto ctrl_fail;
0923     }
0924 
0925     /* v4l2 structure */
0926     usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
0927     usbtv->v4l2_dev.release = usbtv_release;
0928     ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
0929     if (ret < 0) {
0930         dev_warn(usbtv->dev, "Could not register v4l2 device\n");
0931         goto v4l2_fail;
0932     }
0933 
0934     /* Video structure */
0935     strscpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
0936     usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
0937     usbtv->vdev.release = video_device_release_empty;
0938     usbtv->vdev.fops = &usbtv_fops;
0939     usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
0940     usbtv->vdev.tvnorms = USBTV_TV_STD;
0941     usbtv->vdev.queue = &usbtv->vb2q;
0942     usbtv->vdev.lock = &usbtv->v4l2_lock;
0943     usbtv->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
0944                   V4L2_CAP_STREAMING;
0945     video_set_drvdata(&usbtv->vdev, usbtv);
0946     ret = video_register_device(&usbtv->vdev, VFL_TYPE_VIDEO, -1);
0947     if (ret < 0) {
0948         dev_warn(usbtv->dev, "Could not register video device\n");
0949         goto vdev_fail;
0950     }
0951 
0952     return 0;
0953 
0954 vdev_fail:
0955     v4l2_device_unregister(&usbtv->v4l2_dev);
0956 v4l2_fail:
0957 ctrl_fail:
0958     v4l2_ctrl_handler_free(&usbtv->ctrl);
0959 
0960     return ret;
0961 }
0962 
0963 void usbtv_video_free(struct usbtv *usbtv)
0964 {
0965     mutex_lock(&usbtv->vb2q_lock);
0966     mutex_lock(&usbtv->v4l2_lock);
0967 
0968     usbtv_stop(usbtv);
0969     vb2_video_unregister_device(&usbtv->vdev);
0970     v4l2_device_disconnect(&usbtv->v4l2_dev);
0971 
0972     mutex_unlock(&usbtv->v4l2_lock);
0973     mutex_unlock(&usbtv->vb2q_lock);
0974 
0975     v4l2_device_put(&usbtv->v4l2_dev);
0976 }