Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // DVB USB compliant linux driver for Conexant USB reference design -
0004 // (analog part).
0005 //
0006 // Copyright (C) 2011, 2017, 2018
0007 //  Maciej S. Szmigiero (mail@maciej.szmigiero.name)
0008 //
0009 // In case there are new analog / DVB-T hybrid devices released in the market
0010 // using the same general design as Medion MD95700: a CX25840 video decoder
0011 // outputting a BT.656 stream to a USB bridge chip which then forwards it to
0012 // the host in isochronous USB packets this code should be made generic, with
0013 // board specific bits implemented via separate card structures.
0014 //
0015 // This is, however, unlikely as the Medion model was released
0016 // years ago (in 2005).
0017 //
0018 // TODO:
0019 //  * audio support,
0020 //  * finish radio support (requires audio of course),
0021 //  * VBI support,
0022 //  * controls support
0023 
0024 #include <linux/bitops.h>
0025 #include <linux/device.h>
0026 #include <linux/slab.h>
0027 #include <linux/string.h>
0028 #include <linux/ktime.h>
0029 #include <linux/vmalloc.h>
0030 #include <media/drv-intf/cx25840.h>
0031 #include <media/tuner.h>
0032 #include <media/v4l2-fh.h>
0033 #include <media/v4l2-ioctl.h>
0034 #include <media/v4l2-subdev.h>
0035 #include <media/videobuf2-vmalloc.h>
0036 
0037 #include "cxusb.h"
0038 
0039 static int cxusb_medion_v_queue_setup(struct vb2_queue *q,
0040                       unsigned int *num_buffers,
0041                       unsigned int *num_planes,
0042                       unsigned int sizes[],
0043                       struct device *alloc_devs[])
0044 {
0045     struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
0046     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0047     unsigned int size = cxdev->width * cxdev->height * 2;
0048 
0049     if (*num_planes > 0) {
0050         if (*num_planes != 1)
0051             return -EINVAL;
0052 
0053         if (sizes[0] < size)
0054             return -EINVAL;
0055     } else {
0056         *num_planes = 1;
0057         sizes[0] = size;
0058     }
0059 
0060     return 0;
0061 }
0062 
0063 static int cxusb_medion_v_buf_init(struct vb2_buffer *vb)
0064 {
0065     struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
0066     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0067 
0068     cxusb_vprintk(dvbdev, OPS, "buffer init\n");
0069 
0070     if (vb2_plane_size(vb, 0) < cxdev->width * cxdev->height * 2)
0071         return -ENOMEM;
0072 
0073     cxusb_vprintk(dvbdev, OPS, "buffer OK\n");
0074 
0075     return 0;
0076 }
0077 
0078 static void cxusb_auxbuf_init(struct dvb_usb_device *dvbdev,
0079                   struct cxusb_medion_auxbuf *auxbuf,
0080                   u8 *buf, unsigned int len)
0081 {
0082     cxusb_vprintk(dvbdev, AUXB, "initializing auxbuf of len %u\n", len);
0083 
0084     auxbuf->buf = buf;
0085     auxbuf->len = len;
0086     auxbuf->paylen = 0;
0087 }
0088 
0089 static void cxusb_auxbuf_head_trim(struct dvb_usb_device *dvbdev,
0090                    struct cxusb_medion_auxbuf *auxbuf,
0091                    unsigned int pos)
0092 {
0093     if (pos == 0)
0094         return;
0095 
0096     if (WARN_ON(pos > auxbuf->paylen))
0097         return;
0098 
0099     cxusb_vprintk(dvbdev, AUXB,
0100               "trimming auxbuf len by %u to %u\n",
0101               pos, auxbuf->paylen - pos);
0102 
0103     memmove(auxbuf->buf, auxbuf->buf + pos, auxbuf->paylen - pos);
0104     auxbuf->paylen -= pos;
0105 }
0106 
0107 static unsigned int cxusb_auxbuf_paylen(struct cxusb_medion_auxbuf *auxbuf)
0108 {
0109     return auxbuf->paylen;
0110 }
0111 
0112 static bool cxusb_auxbuf_make_space(struct dvb_usb_device *dvbdev,
0113                     struct cxusb_medion_auxbuf *auxbuf,
0114                     unsigned int howmuch)
0115 {
0116     unsigned int freespace;
0117 
0118     if (WARN_ON(howmuch >= auxbuf->len))
0119         howmuch = auxbuf->len - 1;
0120 
0121     freespace = auxbuf->len - cxusb_auxbuf_paylen(auxbuf);
0122 
0123     cxusb_vprintk(dvbdev, AUXB, "freespace is %u\n", freespace);
0124 
0125     if (freespace >= howmuch)
0126         return true;
0127 
0128     howmuch -= freespace;
0129 
0130     cxusb_vprintk(dvbdev, AUXB, "will overwrite %u bytes of buffer\n",
0131               howmuch);
0132 
0133     cxusb_auxbuf_head_trim(dvbdev, auxbuf, howmuch);
0134 
0135     return false;
0136 }
0137 
0138 /* returns false if some data was overwritten */
0139 static bool cxusb_auxbuf_append_urb(struct dvb_usb_device *dvbdev,
0140                     struct cxusb_medion_auxbuf *auxbuf,
0141                     struct urb *urb)
0142 {
0143     unsigned long len;
0144     int i;
0145     bool ret;
0146 
0147     for (i = 0, len = 0; i < urb->number_of_packets; i++)
0148         len += urb->iso_frame_desc[i].actual_length;
0149 
0150     ret = cxusb_auxbuf_make_space(dvbdev, auxbuf, len);
0151 
0152     for (i = 0; i < urb->number_of_packets; i++) {
0153         unsigned int to_copy;
0154 
0155         to_copy = urb->iso_frame_desc[i].actual_length;
0156 
0157         memcpy(auxbuf->buf + auxbuf->paylen, urb->transfer_buffer +
0158                urb->iso_frame_desc[i].offset, to_copy);
0159 
0160         auxbuf->paylen += to_copy;
0161     }
0162 
0163     return ret;
0164 }
0165 
0166 static bool cxusb_auxbuf_copy(struct cxusb_medion_auxbuf *auxbuf,
0167                   unsigned int pos, unsigned char *dest,
0168                   unsigned int len)
0169 {
0170     if (pos + len > auxbuf->paylen)
0171         return false;
0172 
0173     memcpy(dest, auxbuf->buf + pos, len);
0174 
0175     return true;
0176 }
0177 
0178 static bool cxusb_medion_cf_refc_fld_chg(struct dvb_usb_device *dvbdev,
0179                      struct cxusb_bt656_params *bt656,
0180                      bool firstfield,
0181                      unsigned int maxlines,
0182                      unsigned int maxlinesamples,
0183                      unsigned char buf[4])
0184 {
0185     bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
0186         CXUSB_BT656_FIELD_1;
0187     unsigned int remlines;
0188 
0189     if (bt656->line == 0 || firstfield == firstfield_code)
0190         return false;
0191 
0192     if (bt656->fmode == LINE_SAMPLES) {
0193         unsigned int remsamples = maxlinesamples -
0194             bt656->linesamples;
0195 
0196         cxusb_vprintk(dvbdev, BT656,
0197                   "field %c after line %u field change\n",
0198                   firstfield ? '1' : '2', bt656->line);
0199 
0200         if (bt656->buf && remsamples > 0) {
0201             memset(bt656->buf, 0, remsamples);
0202             bt656->buf += remsamples;
0203 
0204             cxusb_vprintk(dvbdev, BT656,
0205                       "field %c line %u %u samples still remaining (of %u)\n",
0206                       firstfield ? '1' : '2',
0207                       bt656->line, remsamples,
0208                       maxlinesamples);
0209         }
0210 
0211         bt656->line++;
0212     }
0213 
0214     remlines = maxlines - bt656->line;
0215     if (bt656->buf && remlines > 0) {
0216         memset(bt656->buf, 0, remlines * maxlinesamples);
0217         bt656->buf += remlines * maxlinesamples;
0218 
0219         cxusb_vprintk(dvbdev, BT656,
0220                   "field %c %u lines still remaining (of %u)\n",
0221                   firstfield ? '1' : '2', remlines,
0222                   maxlines);
0223     }
0224 
0225     return true;
0226 }
0227 
0228 static void cxusb_medion_cf_refc_start_sch(struct dvb_usb_device *dvbdev,
0229                        struct cxusb_bt656_params *bt656,
0230                        bool firstfield,
0231                        unsigned char buf[4])
0232 {
0233     bool firstfield_code = (buf[3] & CXUSB_BT656_FIELD_MASK) ==
0234         CXUSB_BT656_FIELD_1;
0235     bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
0236         CXUSB_BT656_SEAV_SAV;
0237     bool vbi_code = (buf[3] & CXUSB_BT656_VBI_MASK) ==
0238         CXUSB_BT656_VBI_ON;
0239 
0240     if (!sav_code || firstfield != firstfield_code)
0241         return;
0242 
0243     if (!vbi_code) {
0244         cxusb_vprintk(dvbdev, BT656, "line start @ pos %u\n",
0245                   bt656->pos);
0246 
0247         bt656->linesamples = 0;
0248         bt656->fmode = LINE_SAMPLES;
0249     } else {
0250         cxusb_vprintk(dvbdev, BT656, "VBI start @ pos %u\n",
0251                   bt656->pos);
0252 
0253         bt656->fmode = VBI_SAMPLES;
0254     }
0255 }
0256 
0257 static void cxusb_medion_cf_refc_line_smpl(struct dvb_usb_device *dvbdev,
0258                        struct cxusb_bt656_params *bt656,
0259                        bool firstfield,
0260                        unsigned int maxlinesamples,
0261                        unsigned char buf[4])
0262 {
0263     bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
0264         CXUSB_BT656_SEAV_SAV;
0265     unsigned int remsamples;
0266 
0267     if (sav_code)
0268         cxusb_vprintk(dvbdev, BT656,
0269                   "SAV in line samples @ line %u, pos %u\n",
0270                   bt656->line, bt656->pos);
0271 
0272     remsamples = maxlinesamples - bt656->linesamples;
0273     if (bt656->buf && remsamples > 0) {
0274         memset(bt656->buf, 0, remsamples);
0275         bt656->buf += remsamples;
0276 
0277         cxusb_vprintk(dvbdev, BT656,
0278                   "field %c line %u %u samples still remaining (of %u)\n",
0279                   firstfield ? '1' : '2', bt656->line, remsamples,
0280                   maxlinesamples);
0281     }
0282 
0283     bt656->fmode = START_SEARCH;
0284     bt656->line++;
0285 }
0286 
0287 static void cxusb_medion_cf_refc_vbi_smpl(struct dvb_usb_device *dvbdev,
0288                       struct cxusb_bt656_params *bt656,
0289                       unsigned char buf[4])
0290 {
0291     bool sav_code = (buf[3] & CXUSB_BT656_SEAV_MASK) ==
0292         CXUSB_BT656_SEAV_SAV;
0293 
0294     if (sav_code)
0295         cxusb_vprintk(dvbdev, BT656, "SAV in VBI samples @ pos %u\n",
0296                   bt656->pos);
0297 
0298     bt656->fmode = START_SEARCH;
0299 }
0300 
0301 /* returns whether the whole 4-byte code should be skipped in the buffer */
0302 static bool cxusb_medion_cf_ref_code(struct dvb_usb_device *dvbdev,
0303                      struct cxusb_bt656_params *bt656,
0304                      bool firstfield,
0305                      unsigned int maxlines,
0306                      unsigned int maxlinesamples,
0307                      unsigned char buf[4])
0308 {
0309     if (bt656->fmode == START_SEARCH) {
0310         cxusb_medion_cf_refc_start_sch(dvbdev, bt656, firstfield, buf);
0311     } else if (bt656->fmode == LINE_SAMPLES) {
0312         cxusb_medion_cf_refc_line_smpl(dvbdev, bt656, firstfield,
0313                            maxlinesamples, buf);
0314         return false;
0315     } else if (bt656->fmode == VBI_SAMPLES) {
0316         cxusb_medion_cf_refc_vbi_smpl(dvbdev, bt656, buf);
0317         return false;
0318     }
0319 
0320     return true;
0321 }
0322 
0323 static bool cxusb_medion_cs_start_sch(struct dvb_usb_device *dvbdev,
0324                       struct cxusb_medion_auxbuf *auxbuf,
0325                       struct cxusb_bt656_params *bt656,
0326                       unsigned int maxlinesamples)
0327 {
0328     unsigned char buf[64];
0329     unsigned int idx;
0330     unsigned int tocheck = clamp_t(size_t, maxlinesamples / 4, 3,
0331                        sizeof(buf));
0332 
0333     if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1, buf, tocheck))
0334         return false;
0335 
0336     for (idx = 0; idx <= tocheck - 3; idx++)
0337         if (memcmp(buf + idx, CXUSB_BT656_PREAMBLE, 3) == 0) {
0338             bt656->pos += (1 + idx);
0339             return true;
0340         }
0341 
0342     cxusb_vprintk(dvbdev, BT656, "line %u early start, pos %u\n",
0343               bt656->line, bt656->pos);
0344 
0345     bt656->linesamples = 0;
0346     bt656->fmode = LINE_SAMPLES;
0347 
0348     return true;
0349 }
0350 
0351 static void cxusb_medion_cs_line_smpl(struct cxusb_bt656_params *bt656,
0352                       unsigned int maxlinesamples,
0353                       unsigned char val)
0354 {
0355     if (bt656->buf)
0356         *(bt656->buf++) = val;
0357 
0358     bt656->linesamples++;
0359     bt656->pos++;
0360 
0361     if (bt656->linesamples >= maxlinesamples) {
0362         bt656->fmode = START_SEARCH;
0363         bt656->line++;
0364     }
0365 }
0366 
0367 static bool cxusb_medion_copy_samples(struct dvb_usb_device *dvbdev,
0368                       struct cxusb_medion_auxbuf *auxbuf,
0369                       struct cxusb_bt656_params *bt656,
0370                       unsigned int maxlinesamples,
0371                       unsigned char val)
0372 {
0373     if (bt656->fmode == START_SEARCH && bt656->line > 0)
0374         return cxusb_medion_cs_start_sch(dvbdev, auxbuf, bt656,
0375                          maxlinesamples);
0376     else if (bt656->fmode == LINE_SAMPLES)
0377         cxusb_medion_cs_line_smpl(bt656, maxlinesamples, val);
0378     else /* TODO: copy VBI samples */
0379         bt656->pos++;
0380 
0381     return true;
0382 }
0383 
0384 static bool cxusb_medion_copy_field(struct dvb_usb_device *dvbdev,
0385                     struct cxusb_medion_auxbuf *auxbuf,
0386                     struct cxusb_bt656_params *bt656,
0387                     bool firstfield,
0388                     unsigned int maxlines,
0389                     unsigned int maxlinesmpls)
0390 {
0391     while (bt656->line < maxlines) {
0392         unsigned char val;
0393 
0394         if (!cxusb_auxbuf_copy(auxbuf, bt656->pos, &val, 1))
0395             break;
0396 
0397         if (val == CXUSB_BT656_PREAMBLE[0]) {
0398             unsigned char buf[4];
0399 
0400             buf[0] = val;
0401             if (!cxusb_auxbuf_copy(auxbuf, bt656->pos + 1,
0402                            buf + 1, 3))
0403                 break;
0404 
0405             if (buf[1] == CXUSB_BT656_PREAMBLE[1] &&
0406                 buf[2] == CXUSB_BT656_PREAMBLE[2]) {
0407                 /*
0408                  * is this a field change?
0409                  * if so, terminate copying the current field
0410                  */
0411                 if (cxusb_medion_cf_refc_fld_chg(dvbdev,
0412                                  bt656,
0413                                  firstfield,
0414                                  maxlines,
0415                                  maxlinesmpls,
0416                                  buf))
0417                     return true;
0418 
0419                 if (cxusb_medion_cf_ref_code(dvbdev, bt656,
0420                                  firstfield,
0421                                  maxlines,
0422                                  maxlinesmpls,
0423                                  buf))
0424                     bt656->pos += 4;
0425 
0426                 continue;
0427             }
0428         }
0429 
0430         if (!cxusb_medion_copy_samples(dvbdev, auxbuf, bt656,
0431                            maxlinesmpls, val))
0432             break;
0433     }
0434 
0435     if (bt656->line < maxlines) {
0436         cxusb_vprintk(dvbdev, BT656,
0437                   "end of buffer pos = %u, line = %u\n",
0438                   bt656->pos, bt656->line);
0439         return false;
0440     }
0441 
0442     return true;
0443 }
0444 
0445 static bool cxusb_medion_v_process_auxbuf(struct cxusb_medion_dev *cxdev,
0446                       bool reset)
0447 {
0448     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
0449     struct cxusb_bt656_params *bt656 = &cxdev->bt656;
0450 
0451     /*
0452      * if this is a new frame
0453      * fetch a buffer from list
0454      */
0455     if (bt656->mode == NEW_FRAME) {
0456         if (!list_empty(&cxdev->buflist)) {
0457             cxdev->vbuf =
0458                 list_first_entry(&cxdev->buflist,
0459                          struct cxusb_medion_vbuffer,
0460                          list);
0461             list_del(&cxdev->vbuf->list);
0462         } else {
0463             dev_warn(&dvbdev->udev->dev, "no free buffers\n");
0464         }
0465     }
0466 
0467     if (bt656->mode == NEW_FRAME || reset) {
0468         cxusb_vprintk(dvbdev, URB, "will copy field 1\n");
0469         bt656->pos = 0;
0470         bt656->mode = FIRST_FIELD;
0471         bt656->fmode = START_SEARCH;
0472         bt656->line = 0;
0473 
0474         if (cxdev->vbuf) {
0475             cxdev->vbuf->vb2.vb2_buf.timestamp = ktime_get_ns();
0476             bt656->buf = vb2_plane_vaddr(&cxdev->vbuf->vb2.vb2_buf,
0477                              0);
0478         }
0479     }
0480 
0481     if (bt656->mode == FIRST_FIELD) {
0482         if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
0483                          true, cxdev->height / 2,
0484                          cxdev->width * 2))
0485             return false;
0486 
0487         /*
0488          * do not trim buffer there in case
0489          * we need to reset the search later
0490          */
0491 
0492         cxusb_vprintk(dvbdev, URB, "will copy field 2\n");
0493         bt656->mode = SECOND_FIELD;
0494         bt656->fmode = START_SEARCH;
0495         bt656->line = 0;
0496     }
0497 
0498     if (bt656->mode == SECOND_FIELD) {
0499         if (!cxusb_medion_copy_field(dvbdev, &cxdev->auxbuf, bt656,
0500                          false, cxdev->height / 2,
0501                          cxdev->width * 2))
0502             return false;
0503 
0504         cxusb_auxbuf_head_trim(dvbdev, &cxdev->auxbuf, bt656->pos);
0505 
0506         bt656->mode = NEW_FRAME;
0507 
0508         if (cxdev->vbuf) {
0509             vb2_set_plane_payload(&cxdev->vbuf->vb2.vb2_buf, 0,
0510                           cxdev->width * cxdev->height * 2);
0511 
0512             cxdev->vbuf->vb2.field = cxdev->field_order;
0513             cxdev->vbuf->vb2.sequence = cxdev->vbuf_sequence++;
0514 
0515             vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
0516                     VB2_BUF_STATE_DONE);
0517 
0518             cxdev->vbuf = NULL;
0519             cxdev->bt656.buf = NULL;
0520 
0521             cxusb_vprintk(dvbdev, URB, "frame done\n");
0522         } else {
0523             cxusb_vprintk(dvbdev, URB, "frame skipped\n");
0524             cxdev->vbuf_sequence++;
0525         }
0526     }
0527 
0528     return true;
0529 }
0530 
0531 static bool cxusb_medion_v_complete_handle_urb(struct cxusb_medion_dev *cxdev,
0532                            bool *auxbuf_reset)
0533 {
0534     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
0535     unsigned int urbn;
0536     struct urb *urb;
0537     int ret;
0538 
0539     *auxbuf_reset = false;
0540 
0541     urbn = cxdev->nexturb;
0542     if (!test_bit(urbn, &cxdev->urbcomplete))
0543         return false;
0544 
0545     clear_bit(urbn, &cxdev->urbcomplete);
0546 
0547     do {
0548         cxdev->nexturb++;
0549         cxdev->nexturb %= CXUSB_VIDEO_URBS;
0550         urb = cxdev->streamurbs[cxdev->nexturb];
0551     } while (!urb);
0552 
0553     urb = cxdev->streamurbs[urbn];
0554     cxusb_vprintk(dvbdev, URB, "URB %u status = %d\n", urbn, urb->status);
0555 
0556     if (urb->status == 0 || urb->status == -EXDEV) {
0557         int i;
0558         unsigned long len;
0559 
0560         for (i = 0, len = 0; i < urb->number_of_packets; i++)
0561             len += urb->iso_frame_desc[i].actual_length;
0562 
0563         cxusb_vprintk(dvbdev, URB, "URB %u data len = %lu\n", urbn,
0564                   len);
0565 
0566         if (len > 0) {
0567             cxusb_vprintk(dvbdev, URB, "appending URB\n");
0568 
0569             /*
0570              * append new data to auxbuf while
0571              * overwriting old data if necessary
0572              *
0573              * if any overwrite happens then we can no
0574              * longer rely on consistency of the whole
0575              * data so let's start again the current
0576              * auxbuf frame assembling process from
0577              * the beginning
0578              */
0579             *auxbuf_reset =
0580                 !cxusb_auxbuf_append_urb(dvbdev,
0581                              &cxdev->auxbuf,
0582                              urb);
0583         }
0584     }
0585 
0586     cxusb_vprintk(dvbdev, URB, "URB %u resubmit\n", urbn);
0587 
0588     ret = usb_submit_urb(urb, GFP_KERNEL);
0589     if (ret != 0)
0590         dev_err(&dvbdev->udev->dev,
0591             "unable to resubmit URB %u (%d), you'll have to restart streaming\n",
0592             urbn, ret);
0593 
0594     /* next URB is complete already? reschedule us then to handle it */
0595     return test_bit(cxdev->nexturb, &cxdev->urbcomplete);
0596 }
0597 
0598 static void cxusb_medion_v_complete_work(struct work_struct *work)
0599 {
0600     struct cxusb_medion_dev *cxdev = container_of(work,
0601                               struct cxusb_medion_dev,
0602                               urbwork);
0603     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
0604     bool auxbuf_reset;
0605     bool reschedule;
0606 
0607     mutex_lock(cxdev->videodev->lock);
0608 
0609     cxusb_vprintk(dvbdev, URB, "worker called, stop_streaming = %d\n",
0610               (int)cxdev->stop_streaming);
0611 
0612     if (cxdev->stop_streaming)
0613         goto unlock;
0614 
0615     reschedule = cxusb_medion_v_complete_handle_urb(cxdev, &auxbuf_reset);
0616 
0617     if (cxusb_medion_v_process_auxbuf(cxdev, auxbuf_reset))
0618         /* reschedule us until auxbuf no longer can produce any frame */
0619         reschedule = true;
0620 
0621     if (reschedule) {
0622         cxusb_vprintk(dvbdev, URB, "rescheduling worker\n");
0623         schedule_work(&cxdev->urbwork);
0624     }
0625 
0626 unlock:
0627     mutex_unlock(cxdev->videodev->lock);
0628 }
0629 
0630 static void cxusb_medion_v_complete(struct urb *u)
0631 {
0632     struct dvb_usb_device *dvbdev = u->context;
0633     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0634     unsigned int i;
0635 
0636     for (i = 0; i < CXUSB_VIDEO_URBS; i++)
0637         if (cxdev->streamurbs[i] == u)
0638             break;
0639 
0640     if (i >= CXUSB_VIDEO_URBS) {
0641         dev_err(&dvbdev->udev->dev,
0642             "complete on unknown URB\n");
0643         return;
0644     }
0645 
0646     cxusb_vprintk(dvbdev, URB, "URB %u complete\n", i);
0647 
0648     set_bit(i, &cxdev->urbcomplete);
0649     schedule_work(&cxdev->urbwork);
0650 }
0651 
0652 static void cxusb_medion_urbs_free(struct cxusb_medion_dev *cxdev)
0653 {
0654     unsigned int i;
0655 
0656     for (i = 0; i < CXUSB_VIDEO_URBS; i++)
0657         if (cxdev->streamurbs[i]) {
0658             kfree(cxdev->streamurbs[i]->transfer_buffer);
0659             usb_free_urb(cxdev->streamurbs[i]);
0660             cxdev->streamurbs[i] = NULL;
0661         }
0662 }
0663 
0664 static void cxusb_medion_return_buffers(struct cxusb_medion_dev *cxdev,
0665                     bool requeue)
0666 {
0667     struct cxusb_medion_vbuffer *vbuf, *vbuf_tmp;
0668 
0669     list_for_each_entry_safe(vbuf, vbuf_tmp, &cxdev->buflist,
0670                  list) {
0671         list_del(&vbuf->list);
0672         vb2_buffer_done(&vbuf->vb2.vb2_buf,
0673                 requeue ? VB2_BUF_STATE_QUEUED :
0674                 VB2_BUF_STATE_ERROR);
0675     }
0676 
0677     if (cxdev->vbuf) {
0678         vb2_buffer_done(&cxdev->vbuf->vb2.vb2_buf,
0679                 requeue ? VB2_BUF_STATE_QUEUED :
0680                 VB2_BUF_STATE_ERROR);
0681 
0682         cxdev->vbuf = NULL;
0683         cxdev->bt656.buf = NULL;
0684     }
0685 }
0686 
0687 static int cxusb_medion_v_ss_auxbuf_alloc(struct cxusb_medion_dev *cxdev,
0688                       int *npackets)
0689 {
0690     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
0691     u8 *buf;
0692     unsigned int framelen, urblen, auxbuflen;
0693 
0694     framelen = (cxdev->width * 2 + 4 + 4) *
0695         (cxdev->height + 50 /* VBI lines */);
0696 
0697     /*
0698      * try to fit a whole frame into each URB, as long as doing so
0699      * does not require very high order memory allocations
0700      */
0701     BUILD_BUG_ON(CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE >
0702              CXUSB_VIDEO_MAX_FRAME_PKTS);
0703     *npackets = min_t(int, (framelen + CXUSB_VIDEO_PKT_SIZE - 1) /
0704               CXUSB_VIDEO_PKT_SIZE,
0705               CXUSB_VIDEO_URB_MAX_SIZE / CXUSB_VIDEO_PKT_SIZE);
0706     urblen = *npackets * CXUSB_VIDEO_PKT_SIZE;
0707 
0708     cxusb_vprintk(dvbdev, URB,
0709               "each URB will have %d packets for total of %u bytes (%u x %u @ %u)\n",
0710               *npackets, urblen, (unsigned int)cxdev->width,
0711               (unsigned int)cxdev->height, framelen);
0712 
0713     auxbuflen = framelen + urblen;
0714 
0715     buf = vmalloc(auxbuflen);
0716     if (!buf)
0717         return -ENOMEM;
0718 
0719     cxusb_auxbuf_init(dvbdev, &cxdev->auxbuf, buf, auxbuflen);
0720 
0721     return 0;
0722 }
0723 
0724 static u32 cxusb_medion_norm2field_order(v4l2_std_id norm)
0725 {
0726     bool is625 = norm & V4L2_STD_625_50;
0727     bool is525 = norm & V4L2_STD_525_60;
0728 
0729     if (!is625 && !is525)
0730         return V4L2_FIELD_NONE;
0731 
0732     if (is625 && is525)
0733         return V4L2_FIELD_NONE;
0734 
0735     if (is625)
0736         return V4L2_FIELD_SEQ_TB;
0737     else /* is525 */
0738         return V4L2_FIELD_SEQ_BT;
0739 }
0740 
0741 static u32 cxusb_medion_field_order(struct cxusb_medion_dev *cxdev)
0742 {
0743     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
0744     u32 field;
0745     int ret;
0746     v4l2_std_id norm;
0747 
0748     /* TV tuner is PAL-only so it is always TB */
0749     if (cxdev->input == 0)
0750         return V4L2_FIELD_SEQ_TB;
0751 
0752     field = cxusb_medion_norm2field_order(cxdev->norm);
0753     if (field != V4L2_FIELD_NONE)
0754         return field;
0755 
0756     ret = v4l2_subdev_call(cxdev->cx25840, video, g_std, &norm);
0757     if (ret != 0) {
0758         cxusb_vprintk(dvbdev, OPS,
0759                   "cannot get current standard for input %u\n",
0760                   (unsigned int)cxdev->input);
0761     } else {
0762         field = cxusb_medion_norm2field_order(norm);
0763         if (field != V4L2_FIELD_NONE)
0764             return field;
0765     }
0766 
0767     dev_warn(&dvbdev->udev->dev,
0768          "cannot determine field order for the current standard setup and received signal, using TB\n");
0769     return V4L2_FIELD_SEQ_TB;
0770 }
0771 
0772 static int cxusb_medion_v_start_streaming(struct vb2_queue *q,
0773                       unsigned int count)
0774 {
0775     struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
0776     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0777     u8 streamon_params[2] = { 0x03, 0x00 };
0778     int npackets, i;
0779     int ret;
0780 
0781     cxusb_vprintk(dvbdev, OPS, "should start streaming\n");
0782 
0783     if (cxdev->stop_streaming) {
0784         /* stream is being stopped */
0785         ret = -EBUSY;
0786         goto ret_retbufs;
0787     }
0788 
0789     cxdev->field_order = cxusb_medion_field_order(cxdev);
0790 
0791     ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 1);
0792     if (ret != 0) {
0793         dev_err(&dvbdev->udev->dev,
0794             "unable to start stream (%d)\n", ret);
0795         goto ret_retbufs;
0796     }
0797 
0798     ret = cxusb_ctrl_msg(dvbdev, CMD_STREAMING_ON, streamon_params, 2,
0799                  NULL, 0);
0800     if (ret != 0) {
0801         dev_err(&dvbdev->udev->dev,
0802             "unable to start streaming (%d)\n", ret);
0803         goto ret_unstream_cx;
0804     }
0805 
0806     ret = cxusb_medion_v_ss_auxbuf_alloc(cxdev, &npackets);
0807     if (ret != 0)
0808         goto ret_unstream_md;
0809 
0810     for (i = 0; i < CXUSB_VIDEO_URBS; i++) {
0811         int framen;
0812         u8 *streambuf;
0813         struct urb *surb;
0814 
0815         /*
0816          * TODO: change this to an array of single pages to avoid
0817          * doing a large continuous allocation when (if)
0818          * s-g isochronous USB transfers are supported
0819          */
0820         streambuf = kmalloc(npackets * CXUSB_VIDEO_PKT_SIZE,
0821                     GFP_KERNEL);
0822         if (!streambuf) {
0823             if (i < 2) {
0824                 ret = -ENOMEM;
0825                 goto ret_freeab;
0826             }
0827             break;
0828         }
0829 
0830         surb = usb_alloc_urb(npackets, GFP_KERNEL);
0831         if (!surb) {
0832             kfree(streambuf);
0833             ret = -ENOMEM;
0834             goto ret_freeu;
0835         }
0836 
0837         cxdev->streamurbs[i] = surb;
0838         surb->dev = dvbdev->udev;
0839         surb->context = dvbdev;
0840         surb->pipe = usb_rcvisocpipe(dvbdev->udev, 2);
0841 
0842         surb->interval = 1;
0843         surb->transfer_flags = URB_ISO_ASAP;
0844 
0845         surb->transfer_buffer = streambuf;
0846 
0847         surb->complete = cxusb_medion_v_complete;
0848         surb->number_of_packets = npackets;
0849         surb->transfer_buffer_length = npackets * CXUSB_VIDEO_PKT_SIZE;
0850 
0851         for (framen = 0; framen < npackets; framen++) {
0852             surb->iso_frame_desc[framen].offset =
0853                 CXUSB_VIDEO_PKT_SIZE * framen;
0854 
0855             surb->iso_frame_desc[framen].length =
0856                 CXUSB_VIDEO_PKT_SIZE;
0857         }
0858     }
0859 
0860     cxdev->urbcomplete = 0;
0861     cxdev->nexturb = 0;
0862     cxdev->vbuf_sequence = 0;
0863 
0864     cxdev->vbuf = NULL;
0865     cxdev->bt656.mode = NEW_FRAME;
0866     cxdev->bt656.buf = NULL;
0867 
0868     for (i = 0; i < CXUSB_VIDEO_URBS; i++)
0869         if (cxdev->streamurbs[i]) {
0870             ret = usb_submit_urb(cxdev->streamurbs[i],
0871                          GFP_KERNEL);
0872             if (ret != 0)
0873                 dev_err(&dvbdev->udev->dev,
0874                     "URB %d submission failed (%d)\n", i,
0875                     ret);
0876         }
0877 
0878     return 0;
0879 
0880 ret_freeu:
0881     cxusb_medion_urbs_free(cxdev);
0882 
0883 ret_freeab:
0884     vfree(cxdev->auxbuf.buf);
0885 
0886 ret_unstream_md:
0887     cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
0888 
0889 ret_unstream_cx:
0890     v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
0891 
0892 ret_retbufs:
0893     cxusb_medion_return_buffers(cxdev, true);
0894 
0895     return ret;
0896 }
0897 
0898 static void cxusb_medion_v_stop_streaming(struct vb2_queue *q)
0899 {
0900     struct dvb_usb_device *dvbdev = vb2_get_drv_priv(q);
0901     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0902     int ret;
0903     unsigned int i;
0904 
0905     cxusb_vprintk(dvbdev, OPS, "should stop streaming\n");
0906 
0907     if (WARN_ON(cxdev->stop_streaming))
0908         return;
0909 
0910     cxdev->stop_streaming = true;
0911 
0912     cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
0913 
0914     ret = v4l2_subdev_call(cxdev->cx25840, video, s_stream, 0);
0915     if (ret != 0)
0916         dev_err(&dvbdev->udev->dev, "unable to stop stream (%d)\n",
0917             ret);
0918 
0919     /* let URB completion run */
0920     mutex_unlock(cxdev->videodev->lock);
0921 
0922     for (i = 0; i < CXUSB_VIDEO_URBS; i++)
0923         if (cxdev->streamurbs[i])
0924             usb_kill_urb(cxdev->streamurbs[i]);
0925 
0926     flush_work(&cxdev->urbwork);
0927 
0928     mutex_lock(cxdev->videodev->lock);
0929 
0930     /* free transfer buffer and URB */
0931     vfree(cxdev->auxbuf.buf);
0932 
0933     cxusb_medion_urbs_free(cxdev);
0934 
0935     cxusb_medion_return_buffers(cxdev, false);
0936 
0937     cxdev->stop_streaming = false;
0938 }
0939 
0940 static void cxusub_medion_v_buf_queue(struct vb2_buffer *vb)
0941 {
0942     struct vb2_v4l2_buffer *v4l2buf = to_vb2_v4l2_buffer(vb);
0943     struct cxusb_medion_vbuffer *vbuf =
0944         container_of(v4l2buf, struct cxusb_medion_vbuffer, vb2);
0945     struct dvb_usb_device *dvbdev = vb2_get_drv_priv(vb->vb2_queue);
0946     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0947 
0948     /* cxusb_vprintk(dvbdev, OPS, "mmmm.. a fresh buffer...\n"); */
0949 
0950     list_add_tail(&vbuf->list, &cxdev->buflist);
0951 }
0952 
0953 static const struct vb2_ops cxdev_video_qops = {
0954     .queue_setup = cxusb_medion_v_queue_setup,
0955     .buf_init = cxusb_medion_v_buf_init,
0956     .start_streaming = cxusb_medion_v_start_streaming,
0957     .stop_streaming = cxusb_medion_v_stop_streaming,
0958     .buf_queue = cxusub_medion_v_buf_queue,
0959     .wait_prepare = vb2_ops_wait_prepare,
0960     .wait_finish = vb2_ops_wait_finish
0961 };
0962 
0963 static const __u32 videocaps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
0964     V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
0965 static const __u32 radiocaps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
0966 
0967 static int cxusb_medion_v_querycap(struct file *file, void *fh,
0968                    struct v4l2_capability *cap)
0969 {
0970     struct dvb_usb_device *dvbdev = video_drvdata(file);
0971 
0972     strscpy(cap->driver, dvbdev->udev->dev.driver->name,
0973         sizeof(cap->driver));
0974     strscpy(cap->card, "Medion 95700", sizeof(cap->card));
0975     usb_make_path(dvbdev->udev, cap->bus_info, sizeof(cap->bus_info));
0976 
0977     cap->capabilities = videocaps | radiocaps | V4L2_CAP_DEVICE_CAPS;
0978 
0979     return 0;
0980 }
0981 
0982 static int cxusb_medion_v_enum_fmt_vid_cap(struct file *file, void *fh,
0983                        struct v4l2_fmtdesc *f)
0984 {
0985     if (f->index != 0)
0986         return -EINVAL;
0987 
0988     f->pixelformat = V4L2_PIX_FMT_UYVY;
0989 
0990     return 0;
0991 }
0992 
0993 static int cxusb_medion_g_fmt_vid_cap(struct file *file, void *fh,
0994                       struct v4l2_format *f)
0995 {
0996     struct dvb_usb_device *dvbdev = video_drvdata(file);
0997     struct cxusb_medion_dev *cxdev = dvbdev->priv;
0998 
0999     f->fmt.pix.width = cxdev->width;
1000     f->fmt.pix.height = cxdev->height;
1001     f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1002     f->fmt.pix.field = vb2_start_streaming_called(&cxdev->videoqueue) ?
1003         cxdev->field_order : cxusb_medion_field_order(cxdev);
1004     f->fmt.pix.bytesperline = cxdev->width * 2;
1005     f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1006     f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
1007 
1008     return 0;
1009 }
1010 
1011 static int cxusb_medion_try_s_fmt_vid_cap(struct file *file,
1012                       struct v4l2_format *f,
1013                       bool isset)
1014 {
1015     struct dvb_usb_device *dvbdev = video_drvdata(file);
1016     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1017     struct v4l2_subdev_format subfmt;
1018     u32 field;
1019     int ret;
1020 
1021     if (isset && vb2_is_busy(&cxdev->videoqueue))
1022         return -EBUSY;
1023 
1024     field = vb2_start_streaming_called(&cxdev->videoqueue) ?
1025         cxdev->field_order : cxusb_medion_field_order(cxdev);
1026 
1027     memset(&subfmt, 0, sizeof(subfmt));
1028     subfmt.which = isset ? V4L2_SUBDEV_FORMAT_ACTIVE :
1029         V4L2_SUBDEV_FORMAT_TRY;
1030     subfmt.format.width = f->fmt.pix.width & ~1;
1031     subfmt.format.height = f->fmt.pix.height & ~1;
1032     subfmt.format.code = MEDIA_BUS_FMT_FIXED;
1033     subfmt.format.field = field;
1034     subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1035 
1036     ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
1037     if (ret != 0)
1038         return ret;
1039 
1040     f->fmt.pix.width = subfmt.format.width;
1041     f->fmt.pix.height = subfmt.format.height;
1042     f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1043     f->fmt.pix.field = field;
1044     f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
1045     f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
1046     f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1047 
1048     if (isset) {
1049         cxdev->width = f->fmt.pix.width;
1050         cxdev->height = f->fmt.pix.height;
1051     }
1052 
1053     return 0;
1054 }
1055 
1056 static int cxusb_medion_try_fmt_vid_cap(struct file *file, void *fh,
1057                     struct v4l2_format *f)
1058 {
1059     return cxusb_medion_try_s_fmt_vid_cap(file, f, false);
1060 }
1061 
1062 static int cxusb_medion_s_fmt_vid_cap(struct file *file, void *fh,
1063                       struct v4l2_format *f)
1064 {
1065     return cxusb_medion_try_s_fmt_vid_cap(file, f, true);
1066 }
1067 
1068 static const struct {
1069     struct v4l2_input input;
1070     u32 inputcfg;
1071 } cxusb_medion_inputs[] = {
1072     { .input = { .name = "TV tuner", .type = V4L2_INPUT_TYPE_TUNER,
1073              .tuner = 0, .std = V4L2_STD_PAL },
1074       .inputcfg = CX25840_COMPOSITE2, },
1075 
1076     {  .input = { .name = "Composite", .type = V4L2_INPUT_TYPE_CAMERA,
1077              .std = V4L2_STD_ALL },
1078        .inputcfg = CX25840_COMPOSITE1, },
1079 
1080     {  .input = { .name = "S-Video", .type = V4L2_INPUT_TYPE_CAMERA,
1081               .std = V4L2_STD_ALL },
1082        .inputcfg = CX25840_SVIDEO_LUMA3 | CX25840_SVIDEO_CHROMA4 }
1083 };
1084 
1085 #define CXUSB_INPUT_CNT ARRAY_SIZE(cxusb_medion_inputs)
1086 
1087 static int cxusb_medion_enum_input(struct file *file, void *fh,
1088                    struct v4l2_input *inp)
1089 {
1090     struct dvb_usb_device *dvbdev = video_drvdata(file);
1091     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1092     u32 index = inp->index;
1093 
1094     if (index >= CXUSB_INPUT_CNT)
1095         return -EINVAL;
1096 
1097     *inp = cxusb_medion_inputs[index].input;
1098     inp->index = index;
1099     inp->capabilities |= V4L2_IN_CAP_STD;
1100 
1101     if (index == cxdev->input) {
1102         int ret;
1103         u32 status = 0;
1104 
1105         ret = v4l2_subdev_call(cxdev->cx25840, video, g_input_status,
1106                        &status);
1107         if (ret != 0)
1108             dev_warn(&dvbdev->udev->dev,
1109                  "cx25840 input status query failed (%d)\n",
1110                  ret);
1111         else
1112             inp->status = status;
1113     }
1114 
1115     return 0;
1116 }
1117 
1118 static int cxusb_medion_g_input(struct file *file, void *fh,
1119                 unsigned int *i)
1120 {
1121     struct dvb_usb_device *dvbdev = video_drvdata(file);
1122     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1123 
1124     *i = cxdev->input;
1125 
1126     return 0;
1127 }
1128 
1129 static int cxusb_medion_set_norm(struct cxusb_medion_dev *cxdev,
1130                  v4l2_std_id norm)
1131 {
1132     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
1133     int ret;
1134 
1135     cxusb_vprintk(dvbdev, OPS,
1136               "trying to set standard for input %u to %lx\n",
1137               (unsigned int)cxdev->input,
1138               (unsigned long)norm);
1139 
1140     /* no autodetection support */
1141     if (norm == V4L2_STD_UNKNOWN)
1142         return -EINVAL;
1143 
1144     /* on composite or S-Video any std is acceptable */
1145     if (cxdev->input != 0) {
1146         ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
1147         if (ret)
1148             return ret;
1149 
1150         goto ret_savenorm;
1151     }
1152 
1153     /* TV tuner is only able to demodulate PAL */
1154     if ((norm & ~V4L2_STD_PAL) != 0)
1155         return -EINVAL;
1156 
1157     ret = v4l2_subdev_call(cxdev->tda9887, video, s_std, norm);
1158     if (ret != 0) {
1159         dev_err(&dvbdev->udev->dev,
1160             "tda9887 norm setup failed (%d)\n",
1161             ret);
1162         return ret;
1163     }
1164 
1165     ret = v4l2_subdev_call(cxdev->tuner, video, s_std, norm);
1166     if (ret != 0) {
1167         dev_err(&dvbdev->udev->dev,
1168             "tuner norm setup failed (%d)\n",
1169             ret);
1170         return ret;
1171     }
1172 
1173     ret = v4l2_subdev_call(cxdev->cx25840, video, s_std, norm);
1174     if (ret != 0) {
1175         dev_err(&dvbdev->udev->dev,
1176             "cx25840 norm setup failed (%d)\n",
1177             ret);
1178         return ret;
1179     }
1180 
1181 ret_savenorm:
1182     cxdev->norm = norm;
1183 
1184     return 0;
1185 }
1186 
1187 static int cxusb_medion_s_input(struct file *file, void *fh,
1188                 unsigned int i)
1189 {
1190     struct dvb_usb_device *dvbdev = video_drvdata(file);
1191     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1192     int ret;
1193     v4l2_std_id norm;
1194 
1195     if (i >= CXUSB_INPUT_CNT)
1196         return -EINVAL;
1197 
1198     ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
1199                    cxusb_medion_inputs[i].inputcfg, 0, 0);
1200     if (ret != 0)
1201         return ret;
1202 
1203     cxdev->input = i;
1204     cxdev->videodev->tvnorms = cxusb_medion_inputs[i].input.std;
1205 
1206     norm = cxdev->norm & cxusb_medion_inputs[i].input.std;
1207     if (norm == 0)
1208         norm = cxusb_medion_inputs[i].input.std;
1209 
1210     cxusb_medion_set_norm(cxdev, norm);
1211 
1212     return 0;
1213 }
1214 
1215 static int cxusb_medion_g_tuner(struct file *file, void *fh,
1216                 struct v4l2_tuner *tuner)
1217 {
1218     struct dvb_usb_device *dvbdev = video_drvdata(file);
1219     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1220     struct video_device *vdev = video_devdata(file);
1221     int ret;
1222 
1223     if (tuner->index != 0)
1224         return -EINVAL;
1225 
1226     if (vdev->vfl_type == VFL_TYPE_VIDEO)
1227         tuner->type = V4L2_TUNER_ANALOG_TV;
1228     else
1229         tuner->type = V4L2_TUNER_RADIO;
1230 
1231     tuner->capability = 0;
1232     tuner->afc = 0;
1233 
1234     /*
1235      * fills:
1236      * always: capability (static), rangelow (static), rangehigh (static)
1237      * radio mode: afc (may fail silently), rxsubchans (static), audmode
1238      */
1239     ret = v4l2_subdev_call(cxdev->tda9887, tuner, g_tuner, tuner);
1240     if (ret != 0)
1241         return ret;
1242 
1243     /*
1244      * fills:
1245      * always: capability (static), rangelow (static), rangehigh (static)
1246      * radio mode: rxsubchans (always stereo), audmode,
1247      * signal (might be wrong)
1248      */
1249     ret = v4l2_subdev_call(cxdev->tuner, tuner, g_tuner, tuner);
1250     if (ret != 0)
1251         return ret;
1252 
1253     tuner->signal = 0;
1254 
1255     /*
1256      * fills: TV mode: capability, rxsubchans, audmode, signal
1257      */
1258     ret = v4l2_subdev_call(cxdev->cx25840, tuner, g_tuner, tuner);
1259     if (ret != 0)
1260         return ret;
1261 
1262     if (vdev->vfl_type == VFL_TYPE_VIDEO)
1263         strscpy(tuner->name, "TV tuner", sizeof(tuner->name));
1264     else
1265         strscpy(tuner->name, "Radio tuner", sizeof(tuner->name));
1266 
1267     memset(tuner->reserved, 0, sizeof(tuner->reserved));
1268 
1269     return 0;
1270 }
1271 
1272 static int cxusb_medion_s_tuner(struct file *file, void *fh,
1273                 const struct v4l2_tuner *tuner)
1274 {
1275     struct dvb_usb_device *dvbdev = video_drvdata(file);
1276     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1277     struct video_device *vdev = video_devdata(file);
1278     int ret;
1279 
1280     if (tuner->index != 0)
1281         return -EINVAL;
1282 
1283     ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_tuner, tuner);
1284     if (ret != 0)
1285         return ret;
1286 
1287     ret = v4l2_subdev_call(cxdev->tuner, tuner, s_tuner, tuner);
1288     if (ret != 0)
1289         return ret;
1290 
1291     /*
1292      * make sure that cx25840 is in a correct TV / radio mode,
1293      * since calls above may have changed it for tuner / IF demod
1294      */
1295     if (vdev->vfl_type == VFL_TYPE_VIDEO)
1296         v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1297     else
1298         v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
1299 
1300     return v4l2_subdev_call(cxdev->cx25840, tuner, s_tuner, tuner);
1301 }
1302 
1303 static int cxusb_medion_g_frequency(struct file *file, void *fh,
1304                     struct v4l2_frequency *freq)
1305 {
1306     struct dvb_usb_device *dvbdev = video_drvdata(file);
1307     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1308 
1309     if (freq->tuner != 0)
1310         return -EINVAL;
1311 
1312     return v4l2_subdev_call(cxdev->tuner, tuner, g_frequency, freq);
1313 }
1314 
1315 static int cxusb_medion_s_frequency(struct file *file, void *fh,
1316                     const struct v4l2_frequency *freq)
1317 {
1318     struct dvb_usb_device *dvbdev = video_drvdata(file);
1319     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1320     struct video_device *vdev = video_devdata(file);
1321     int ret;
1322 
1323     if (freq->tuner != 0)
1324         return -EINVAL;
1325 
1326     ret = v4l2_subdev_call(cxdev->tda9887, tuner, s_frequency, freq);
1327     if (ret != 0)
1328         return ret;
1329 
1330     ret = v4l2_subdev_call(cxdev->tuner, tuner, s_frequency, freq);
1331     if (ret != 0)
1332         return ret;
1333 
1334     /*
1335      * make sure that cx25840 is in a correct TV / radio mode,
1336      * since calls above may have changed it for tuner / IF demod
1337      */
1338     if (vdev->vfl_type == VFL_TYPE_VIDEO)
1339         v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1340     else
1341         v4l2_subdev_call(cxdev->cx25840, tuner, s_radio);
1342 
1343     return v4l2_subdev_call(cxdev->cx25840, tuner, s_frequency, freq);
1344 }
1345 
1346 static int cxusb_medion_g_std(struct file *file, void *fh,
1347                   v4l2_std_id *norm)
1348 {
1349     struct dvb_usb_device *dvbdev = video_drvdata(file);
1350     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1351 
1352     *norm = cxdev->norm;
1353 
1354     if (*norm == V4L2_STD_UNKNOWN)
1355         return -ENODATA;
1356 
1357     return 0;
1358 }
1359 
1360 static int cxusb_medion_s_std(struct file *file, void *fh,
1361                   v4l2_std_id norm)
1362 {
1363     struct dvb_usb_device *dvbdev = video_drvdata(file);
1364     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1365 
1366     return cxusb_medion_set_norm(cxdev, norm);
1367 }
1368 
1369 static int cxusb_medion_querystd(struct file *file, void *fh,
1370                  v4l2_std_id *norm)
1371 {
1372     struct dvb_usb_device *dvbdev = video_drvdata(file);
1373     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1374     v4l2_std_id norm_mask;
1375     int ret;
1376 
1377     /*
1378      * make sure we don't have improper std bits set for the TV tuner
1379      * (could happen when no signal was present yet after reset)
1380      */
1381     if (cxdev->input == 0)
1382         norm_mask = V4L2_STD_PAL;
1383     else
1384         norm_mask = V4L2_STD_ALL;
1385 
1386     ret = v4l2_subdev_call(cxdev->cx25840, video, querystd, norm);
1387     if (ret != 0) {
1388         cxusb_vprintk(dvbdev, OPS,
1389                   "cannot get detected standard for input %u\n",
1390                   (unsigned int)cxdev->input);
1391         return ret;
1392     }
1393 
1394     cxusb_vprintk(dvbdev, OPS, "input %u detected standard is %lx\n",
1395               (unsigned int)cxdev->input, (unsigned long)*norm);
1396     *norm &= norm_mask;
1397 
1398     return 0;
1399 }
1400 
1401 static int cxusb_medion_log_status(struct file *file, void *fh)
1402 {
1403     struct dvb_usb_device *dvbdev = video_drvdata(file);
1404     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1405 
1406     v4l2_device_call_all(&cxdev->v4l2dev, 0, core, log_status);
1407 
1408     return 0;
1409 }
1410 
1411 static const struct v4l2_ioctl_ops cxusb_video_ioctl = {
1412     .vidioc_querycap = cxusb_medion_v_querycap,
1413     .vidioc_enum_fmt_vid_cap = cxusb_medion_v_enum_fmt_vid_cap,
1414     .vidioc_g_fmt_vid_cap = cxusb_medion_g_fmt_vid_cap,
1415     .vidioc_s_fmt_vid_cap = cxusb_medion_s_fmt_vid_cap,
1416     .vidioc_try_fmt_vid_cap = cxusb_medion_try_fmt_vid_cap,
1417     .vidioc_enum_input = cxusb_medion_enum_input,
1418     .vidioc_g_input = cxusb_medion_g_input,
1419     .vidioc_s_input = cxusb_medion_s_input,
1420     .vidioc_g_tuner = cxusb_medion_g_tuner,
1421     .vidioc_s_tuner = cxusb_medion_s_tuner,
1422     .vidioc_g_frequency = cxusb_medion_g_frequency,
1423     .vidioc_s_frequency = cxusb_medion_s_frequency,
1424     .vidioc_g_std = cxusb_medion_g_std,
1425     .vidioc_s_std = cxusb_medion_s_std,
1426     .vidioc_querystd = cxusb_medion_querystd,
1427     .vidioc_log_status = cxusb_medion_log_status,
1428     .vidioc_reqbufs = vb2_ioctl_reqbufs,
1429     .vidioc_querybuf = vb2_ioctl_querybuf,
1430     .vidioc_qbuf = vb2_ioctl_qbuf,
1431     .vidioc_dqbuf = vb2_ioctl_dqbuf,
1432     .vidioc_create_bufs = vb2_ioctl_create_bufs,
1433     .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1434     .vidioc_streamon = vb2_ioctl_streamon,
1435     .vidioc_streamoff = vb2_ioctl_streamoff
1436 };
1437 
1438 static const struct v4l2_ioctl_ops cxusb_radio_ioctl = {
1439     .vidioc_querycap = cxusb_medion_v_querycap,
1440     .vidioc_g_tuner = cxusb_medion_g_tuner,
1441     .vidioc_s_tuner = cxusb_medion_s_tuner,
1442     .vidioc_g_frequency = cxusb_medion_g_frequency,
1443     .vidioc_s_frequency = cxusb_medion_s_frequency,
1444     .vidioc_log_status = cxusb_medion_log_status
1445 };
1446 
1447 /*
1448  * in principle, this should be const, but s_io_pin_config is declared
1449  * to take non-const, and gcc complains
1450  */
1451 static struct v4l2_subdev_io_pin_config cxusub_medion_pin_config[] = {
1452     { .pin = CX25840_PIN_DVALID_PRGM0, .function = CX25840_PAD_DEFAULT,
1453       .strength = CX25840_PIN_DRIVE_MEDIUM },
1454     { .pin = CX25840_PIN_PLL_CLK_PRGM7, .function = CX25840_PAD_AUX_PLL },
1455     { .pin = CX25840_PIN_HRESET_PRGM2, .function = CX25840_PAD_ACTIVE,
1456       .strength = CX25840_PIN_DRIVE_MEDIUM }
1457 };
1458 
1459 int cxusb_medion_analog_init(struct dvb_usb_device *dvbdev)
1460 {
1461     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1462     u8 tuner_analog_msg_data[] = { 0x9c, 0x60, 0x85, 0x54 };
1463     struct i2c_msg tuner_analog_msg = { .addr = 0x61, .flags = 0,
1464                         .buf = tuner_analog_msg_data,
1465                         .len =
1466                         sizeof(tuner_analog_msg_data) };
1467     struct v4l2_subdev_format subfmt;
1468     int ret;
1469 
1470     /* switch tuner to analog mode so IF demod will become accessible */
1471     ret = i2c_transfer(&dvbdev->i2c_adap, &tuner_analog_msg, 1);
1472     if (ret != 1)
1473         dev_warn(&dvbdev->udev->dev,
1474              "tuner analog switch failed (%d)\n", ret);
1475 
1476     /*
1477      * cx25840 might have lost power during mode switching so we need
1478      * to set it again
1479      */
1480     ret = v4l2_subdev_call(cxdev->cx25840, core, reset, 0);
1481     if (ret != 0)
1482         dev_warn(&dvbdev->udev->dev,
1483              "cx25840 reset failed (%d)\n", ret);
1484 
1485     ret = v4l2_subdev_call(cxdev->cx25840, video, s_routing,
1486                    CX25840_COMPOSITE1, 0, 0);
1487     if (ret != 0)
1488         dev_warn(&dvbdev->udev->dev,
1489              "cx25840 initial input setting failed (%d)\n", ret);
1490 
1491     /* composite */
1492     cxdev->input = 1;
1493     cxdev->videodev->tvnorms = V4L2_STD_ALL;
1494     cxdev->norm = V4L2_STD_PAL;
1495 
1496     /* TODO: setup audio samples insertion */
1497 
1498     ret = v4l2_subdev_call(cxdev->cx25840, core, s_io_pin_config,
1499                    ARRAY_SIZE(cxusub_medion_pin_config),
1500                    cxusub_medion_pin_config);
1501     if (ret != 0)
1502         dev_warn(&dvbdev->udev->dev,
1503              "cx25840 pin config failed (%d)\n", ret);
1504 
1505     /* make sure that we aren't in radio mode */
1506     v4l2_subdev_call(cxdev->tda9887, video, s_std, cxdev->norm);
1507     v4l2_subdev_call(cxdev->tuner, video, s_std, cxdev->norm);
1508     v4l2_subdev_call(cxdev->cx25840, video, s_std, cxdev->norm);
1509 
1510     memset(&subfmt, 0, sizeof(subfmt));
1511     subfmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1512     subfmt.format.width = cxdev->width;
1513     subfmt.format.height = cxdev->height;
1514     subfmt.format.code = MEDIA_BUS_FMT_FIXED;
1515     subfmt.format.field = V4L2_FIELD_SEQ_TB;
1516     subfmt.format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1517 
1518     ret = v4l2_subdev_call(cxdev->cx25840, pad, set_fmt, NULL, &subfmt);
1519     if (ret != 0)
1520         dev_warn(&dvbdev->udev->dev,
1521              "cx25840 format set failed (%d)\n", ret);
1522 
1523     if (ret == 0) {
1524         cxdev->width = subfmt.format.width;
1525         cxdev->height = subfmt.format.height;
1526     }
1527 
1528     return 0;
1529 }
1530 
1531 static int cxusb_videoradio_open(struct file *f)
1532 {
1533     struct dvb_usb_device *dvbdev = video_drvdata(f);
1534     int ret;
1535 
1536     /*
1537      * no locking needed since this call only modifies analog
1538      * state if there are no other analog handles currenly
1539      * opened so ops done via them cannot create a conflict
1540      */
1541     ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_ANALOG);
1542     if (ret != 0)
1543         return ret;
1544 
1545     ret = v4l2_fh_open(f);
1546     if (ret != 0)
1547         goto ret_release;
1548 
1549     cxusb_vprintk(dvbdev, OPS, "got open\n");
1550 
1551     return 0;
1552 
1553 ret_release:
1554     cxusb_medion_put(dvbdev);
1555 
1556     return ret;
1557 }
1558 
1559 static int cxusb_videoradio_release(struct file *f)
1560 {
1561     struct video_device *vdev = video_devdata(f);
1562     struct dvb_usb_device *dvbdev = video_drvdata(f);
1563     int ret;
1564 
1565     cxusb_vprintk(dvbdev, OPS, "got release\n");
1566 
1567     if (vdev->vfl_type == VFL_TYPE_VIDEO)
1568         ret = vb2_fop_release(f);
1569     else
1570         ret = v4l2_fh_release(f);
1571 
1572     cxusb_medion_put(dvbdev);
1573 
1574     return ret;
1575 }
1576 
1577 static const struct v4l2_file_operations cxusb_video_fops = {
1578     .owner = THIS_MODULE,
1579     .read = vb2_fop_read,
1580     .poll = vb2_fop_poll,
1581     .unlocked_ioctl = video_ioctl2,
1582     .mmap = vb2_fop_mmap,
1583     .open = cxusb_videoradio_open,
1584     .release = cxusb_videoradio_release
1585 };
1586 
1587 static const struct v4l2_file_operations cxusb_radio_fops = {
1588     .owner = THIS_MODULE,
1589     .unlocked_ioctl = video_ioctl2,
1590     .open = cxusb_videoradio_open,
1591     .release = cxusb_videoradio_release
1592 };
1593 
1594 static void cxusb_medion_v4l2_release(struct v4l2_device *v4l2_dev)
1595 {
1596     struct cxusb_medion_dev *cxdev =
1597         container_of(v4l2_dev, struct cxusb_medion_dev, v4l2dev);
1598     struct dvb_usb_device *dvbdev = cxdev->dvbdev;
1599 
1600     cxusb_vprintk(dvbdev, OPS, "v4l2 device release\n");
1601 
1602     v4l2_device_unregister(&cxdev->v4l2dev);
1603 
1604     mutex_destroy(&cxdev->dev_lock);
1605 
1606     while (completion_done(&cxdev->v4l2_release))
1607         schedule();
1608 
1609     complete(&cxdev->v4l2_release);
1610 }
1611 
1612 static void cxusb_medion_videodev_release(struct video_device *vdev)
1613 {
1614     struct dvb_usb_device *dvbdev = video_get_drvdata(vdev);
1615 
1616     cxusb_vprintk(dvbdev, OPS, "video device release\n");
1617 
1618     video_device_release(vdev);
1619 }
1620 
1621 static int cxusb_medion_register_analog_video(struct dvb_usb_device *dvbdev)
1622 {
1623     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1624     int ret;
1625 
1626     cxdev->videoqueue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1627     cxdev->videoqueue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ |
1628         VB2_DMABUF;
1629     cxdev->videoqueue.ops = &cxdev_video_qops;
1630     cxdev->videoqueue.mem_ops = &vb2_vmalloc_memops;
1631     cxdev->videoqueue.drv_priv = dvbdev;
1632     cxdev->videoqueue.buf_struct_size =
1633         sizeof(struct cxusb_medion_vbuffer);
1634     cxdev->videoqueue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1635     cxdev->videoqueue.min_buffers_needed = 6;
1636     cxdev->videoqueue.lock = &cxdev->dev_lock;
1637 
1638     ret = vb2_queue_init(&cxdev->videoqueue);
1639     if (ret) {
1640         dev_err(&dvbdev->udev->dev,
1641             "video queue init failed, ret = %d\n", ret);
1642         return ret;
1643     }
1644 
1645     cxdev->videodev = video_device_alloc();
1646     if (!cxdev->videodev) {
1647         dev_err(&dvbdev->udev->dev, "video device alloc failed\n");
1648         return -ENOMEM;
1649     }
1650 
1651     cxdev->videodev->device_caps = videocaps;
1652     cxdev->videodev->fops = &cxusb_video_fops;
1653     cxdev->videodev->v4l2_dev = &cxdev->v4l2dev;
1654     cxdev->videodev->queue = &cxdev->videoqueue;
1655     strscpy(cxdev->videodev->name, "cxusb", sizeof(cxdev->videodev->name));
1656     cxdev->videodev->vfl_dir = VFL_DIR_RX;
1657     cxdev->videodev->ioctl_ops = &cxusb_video_ioctl;
1658     cxdev->videodev->tvnorms = V4L2_STD_ALL;
1659     cxdev->videodev->release = cxusb_medion_videodev_release;
1660     cxdev->videodev->lock = &cxdev->dev_lock;
1661     video_set_drvdata(cxdev->videodev, dvbdev);
1662 
1663     ret = video_register_device(cxdev->videodev, VFL_TYPE_VIDEO, -1);
1664     if (ret) {
1665         dev_err(&dvbdev->udev->dev,
1666             "video device register failed, ret = %d\n", ret);
1667         goto ret_vrelease;
1668     }
1669 
1670     return 0;
1671 
1672 ret_vrelease:
1673     video_device_release(cxdev->videodev);
1674     return ret;
1675 }
1676 
1677 static int cxusb_medion_register_analog_radio(struct dvb_usb_device *dvbdev)
1678 {
1679     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1680     int ret;
1681 
1682     cxdev->radiodev = video_device_alloc();
1683     if (!cxdev->radiodev) {
1684         dev_err(&dvbdev->udev->dev, "radio device alloc failed\n");
1685         return -ENOMEM;
1686     }
1687 
1688     cxdev->radiodev->device_caps = radiocaps;
1689     cxdev->radiodev->fops = &cxusb_radio_fops;
1690     cxdev->radiodev->v4l2_dev = &cxdev->v4l2dev;
1691     strscpy(cxdev->radiodev->name, "cxusb", sizeof(cxdev->radiodev->name));
1692     cxdev->radiodev->vfl_dir = VFL_DIR_RX;
1693     cxdev->radiodev->ioctl_ops = &cxusb_radio_ioctl;
1694     cxdev->radiodev->release = video_device_release;
1695     cxdev->radiodev->lock = &cxdev->dev_lock;
1696     video_set_drvdata(cxdev->radiodev, dvbdev);
1697 
1698     ret = video_register_device(cxdev->radiodev, VFL_TYPE_RADIO, -1);
1699     if (ret) {
1700         dev_err(&dvbdev->udev->dev,
1701             "radio device register failed, ret = %d\n", ret);
1702         video_device_release(cxdev->radiodev);
1703         return ret;
1704     }
1705 
1706     return 0;
1707 }
1708 
1709 static int cxusb_medion_register_analog_subdevs(struct dvb_usb_device *dvbdev)
1710 {
1711     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1712     int ret;
1713     struct tuner_setup tun_setup;
1714 
1715     /* attach cx25840 capture chip */
1716     cxdev->cx25840 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1717                          &dvbdev->i2c_adap,
1718                          "cx25840", 0x44, NULL);
1719     if (!cxdev->cx25840) {
1720         dev_err(&dvbdev->udev->dev, "cx25840 not found\n");
1721         return -ENODEV;
1722     }
1723 
1724     /*
1725      * Initialize cx25840 chip by calling its subdevice init core op.
1726      *
1727      * This switches it into the generic mode that disables some of
1728      * ivtv-related hacks in the cx25840 driver while allowing setting
1729      * of the chip video output configuration (passed in the call below
1730      * as the last argument).
1731      */
1732     ret = v4l2_subdev_call(cxdev->cx25840, core, init,
1733                    CX25840_VCONFIG_FMT_BT656 |
1734                    CX25840_VCONFIG_RES_8BIT |
1735                    CX25840_VCONFIG_VBIRAW_DISABLED |
1736                    CX25840_VCONFIG_ANCDATA_DISABLED |
1737                    CX25840_VCONFIG_ACTIVE_COMPOSITE |
1738                    CX25840_VCONFIG_VALID_ANDACTIVE |
1739                    CX25840_VCONFIG_HRESETW_NORMAL |
1740                    CX25840_VCONFIG_CLKGATE_NONE |
1741                    CX25840_VCONFIG_DCMODE_DWORDS);
1742     if (ret != 0) {
1743         dev_err(&dvbdev->udev->dev,
1744             "cx25840 init failed (%d)\n", ret);
1745         return ret;
1746     }
1747 
1748     /* attach analog tuner */
1749     cxdev->tuner = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1750                        &dvbdev->i2c_adap,
1751                        "tuner", 0x61, NULL);
1752     if (!cxdev->tuner) {
1753         dev_err(&dvbdev->udev->dev, "tuner not found\n");
1754         return -ENODEV;
1755     }
1756 
1757     /* configure it */
1758     memset(&tun_setup, 0, sizeof(tun_setup));
1759     tun_setup.addr = 0x61;
1760     tun_setup.type = TUNER_PHILIPS_FMD1216ME_MK3;
1761     tun_setup.mode_mask = T_RADIO | T_ANALOG_TV;
1762     v4l2_subdev_call(cxdev->tuner, tuner, s_type_addr, &tun_setup);
1763 
1764     /* attach IF demod */
1765     cxdev->tda9887 = v4l2_i2c_new_subdev(&cxdev->v4l2dev,
1766                          &dvbdev->i2c_adap,
1767                          "tuner", 0x43, NULL);
1768     if (!cxdev->tda9887) {
1769         dev_err(&dvbdev->udev->dev, "tda9887 not found\n");
1770         return -ENODEV;
1771     }
1772 
1773     return 0;
1774 }
1775 
1776 int cxusb_medion_register_analog(struct dvb_usb_device *dvbdev)
1777 {
1778     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1779     int ret;
1780 
1781     mutex_init(&cxdev->dev_lock);
1782 
1783     init_completion(&cxdev->v4l2_release);
1784 
1785     cxdev->v4l2dev.release = cxusb_medion_v4l2_release;
1786 
1787     ret = v4l2_device_register(&dvbdev->udev->dev, &cxdev->v4l2dev);
1788     if (ret != 0) {
1789         dev_err(&dvbdev->udev->dev,
1790             "V4L2 device registration failed, ret = %d\n", ret);
1791         mutex_destroy(&cxdev->dev_lock);
1792         return ret;
1793     }
1794 
1795     ret = cxusb_medion_register_analog_subdevs(dvbdev);
1796     if (ret)
1797         goto ret_unregister;
1798 
1799     INIT_WORK(&cxdev->urbwork, cxusb_medion_v_complete_work);
1800     INIT_LIST_HEAD(&cxdev->buflist);
1801 
1802     cxdev->width = 320;
1803     cxdev->height = 240;
1804 
1805     ret = cxusb_medion_register_analog_video(dvbdev);
1806     if (ret)
1807         goto ret_unregister;
1808 
1809     ret = cxusb_medion_register_analog_radio(dvbdev);
1810     if (ret)
1811         goto ret_vunreg;
1812 
1813     return 0;
1814 
1815 ret_vunreg:
1816     vb2_video_unregister_device(cxdev->videodev);
1817 
1818 ret_unregister:
1819     v4l2_device_put(&cxdev->v4l2dev);
1820     wait_for_completion(&cxdev->v4l2_release);
1821 
1822     return ret;
1823 }
1824 
1825 void cxusb_medion_unregister_analog(struct dvb_usb_device *dvbdev)
1826 {
1827     struct cxusb_medion_dev *cxdev = dvbdev->priv;
1828 
1829     cxusb_vprintk(dvbdev, OPS, "unregistering analog\n");
1830 
1831     video_unregister_device(cxdev->radiodev);
1832     vb2_video_unregister_device(cxdev->videodev);
1833 
1834     v4l2_device_put(&cxdev->v4l2dev);
1835     wait_for_completion(&cxdev->v4l2_release);
1836 
1837     cxusb_vprintk(dvbdev, OPS, "analog unregistered\n");
1838 }