include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-v4l2.c
index dda560c..bf1e0fe 100644 (file)
@@ -1,6 +1,5 @@
 /*
  *
- *  $Id$
  *
  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
@@ -21,6 +20,8 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/version.h>
 #include "pvrusb2-context.h"
 #include "pvrusb2-hdw.h"
 #include "pvrusb2.h"
 #include "pvrusb2-v4l2.h"
 #include "pvrusb2-ioread.h"
 #include <linux/videodev2.h>
+#include <media/v4l2-dev.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
 
 struct pvr2_v4l2_dev;
 struct pvr2_v4l2_fh;
 struct pvr2_v4l2;
 
-/* V4L no longer provide the ability to set / get a private context pointer
-   (i.e. video_get_drvdata / video_set_drvdata), which means we have to
-   concoct our own context locating mechanism.  Supposedly this is intended
-   to simplify driver implementation.  It's not clear to me how that can
-   possibly be true.  Our solution here is to maintain a lookup table of
-   our context instances, indexed by the minor device number of the V4L
-   device.  See pvr2_v4l2_open() for some implications of this approach. */
-static struct pvr2_v4l2_dev *devices[256];
-static DEFINE_MUTEX(device_lock);
-
 struct pvr2_v4l2_dev {
+       struct video_device devbase; /* MUST be first! */
        struct pvr2_v4l2 *v4lp;
-       struct video_device *vdev;
        struct pvr2_context_stream *stream;
-       int ctxt_idx;
-       enum pvr2_config config;
+       /* Information about this device: */
+       enum pvr2_config config; /* Expected stream format */
+       int v4l_type; /* V4L defined type for this device node */
+       enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
 };
 
 struct pvr2_v4l2_fh {
@@ -63,6 +58,9 @@ struct pvr2_v4l2_fh {
        struct pvr2_v4l2_fh *vprev;
        wait_queue_head_t wait_data;
        int fw_mode_flag;
+       /* Map contiguous ordinal value to input id */
+       unsigned char *input_map;
+       unsigned int input_cnt;
 };
 
 struct pvr2_v4l2 {
@@ -72,45 +70,35 @@ struct pvr2_v4l2 {
 
        struct v4l2_prio_state prio;
 
-       /* streams */
-       struct pvr2_v4l2_dev video_dev;
+       /* streams - Note that these must be separately, individually,
+        * allocated pointers.  This is because the v4l core is going to
+        * manage their deletion - separately, individually...  */
+       struct pvr2_v4l2_dev *dev_video;
+       struct pvr2_v4l2_dev *dev_radio;
 };
 
 static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
 module_param_array(video_nr, int, NULL, 0444);
-MODULE_PARM_DESC(video_nr, "Offset for device's minor");
-
-struct v4l2_capability pvr_capability ={
+MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
+static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
+module_param_array(radio_nr, int, NULL, 0444);
+MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
+static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
+module_param_array(vbi_nr, int, NULL, 0444);
+MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
+
+static struct v4l2_capability pvr_capability ={
        .driver         = "pvrusb2",
        .card           = "Hauppauge WinTV pvr-usb2",
        .bus_info       = "usb",
-       .version        = KERNEL_VERSION(0,8,0),
-       .capabilities   = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
-                          V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
+       .version        = KERNEL_VERSION(0, 9, 0),
+       .capabilities   = (V4L2_CAP_VIDEO_CAPTURE |
+                          V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
                           V4L2_CAP_READWRITE),
        .reserved       = {0,0,0,0}
 };
 
-static struct v4l2_tuner pvr_v4l2_tuners[]= {
-       {
-               .index      = 0,
-               .name       = "TV Tuner",
-               .type           = V4L2_TUNER_ANALOG_TV,
-               .capability     = (V4L2_TUNER_CAP_NORM |
-                                  V4L2_TUNER_CAP_STEREO |
-                                  V4L2_TUNER_CAP_LANG1 |
-                                  V4L2_TUNER_CAP_LANG2),
-               .rangelow   = 0,
-               .rangehigh  = 0,
-               .rxsubchans     = V4L2_TUNER_SUB_STEREO,
-               .audmode        = V4L2_TUNER_MODE_STEREO,
-               .signal         = 0,
-               .afc            = 0,
-               .reserved       = {0,0,0,0}
-       }
-};
-
-struct v4l2_fmtdesc pvr_fmtdesc [] = {
+static struct v4l2_fmtdesc pvr_fmtdesc [] = {
        {
                .index          = 0,
                .type           = V4L2_BUF_TYPE_VIDEO_CAPTURE,
@@ -126,7 +114,7 @@ struct v4l2_fmtdesc pvr_fmtdesc [] = {
 #define PVR_FORMAT_PIX  0
 #define PVR_FORMAT_VBI  1
 
-struct v4l2_format pvr_format [] = {
+static struct v4l2_format pvr_format [] = {
        [PVR_FORMAT_PIX] = {
                .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
                .fmt    = {
@@ -163,20 +151,20 @@ struct v4l2_format pvr_format [] = {
        }
 };
 
+
 /*
  * pvr_ioctl()
  *
  * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  *
  */
-static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
-                             unsigned int cmd, void *arg)
+static long pvr2_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
 {
        struct pvr2_v4l2_fh *fh = file->private_data;
        struct pvr2_v4l2 *vp = fh->vhead;
        struct pvr2_v4l2_dev *dev_info = fh->dev_info;
        struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
-       int ret = -EINVAL;
+       long ret = -EINVAL;
 
        if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
                v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
@@ -206,6 +194,9 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                struct v4l2_capability *cap = arg;
 
                memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
+               strlcpy(cap->bus_info,pvr2_hdw_get_bus_info(hdw),
+                       sizeof(cap->bus_info));
+               strlcpy(cap->card,pvr2_hdw_get_desc(hdw),sizeof(cap->card));
 
                ret = 0;
                break;
@@ -259,14 +250,21 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                struct v4l2_input *vi = (struct v4l2_input *)arg;
                struct v4l2_input tmp;
                unsigned int cnt;
+               int val;
 
                cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
 
                memset(&tmp,0,sizeof(tmp));
                tmp.index = vi->index;
                ret = 0;
-               switch (vi->index) {
+               if (vi->index >= fh->input_cnt) {
+                       ret = -EINVAL;
+                       break;
+               }
+               val = fh->input_map[vi->index];
+               switch (val) {
                case PVR2_CVAL_INPUT_TV:
+               case PVR2_CVAL_INPUT_DTV:
                case PVR2_CVAL_INPUT_RADIO:
                        tmp.type = V4L2_INPUT_TYPE_TUNER;
                        break;
@@ -281,7 +279,7 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                if (ret < 0) break;
 
                cnt = 0;
-               pvr2_ctrl_get_valname(cptr,vi->index,
+               pvr2_ctrl_get_valname(cptr,val,
                                      tmp.name,sizeof(tmp.name)-1,&cnt);
                tmp.name[cnt] = 0;
 
@@ -303,34 +301,71 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
 
        case VIDIOC_G_INPUT:
        {
+               unsigned int idx;
                struct pvr2_ctrl *cptr;
                struct v4l2_input *vi = (struct v4l2_input *)arg;
                int val;
                cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
                val = 0;
                ret = pvr2_ctrl_get_value(cptr,&val);
-               vi->index = val;
+               vi->index = 0;
+               for (idx = 0; idx < fh->input_cnt; idx++) {
+                       if (fh->input_map[idx] == val) {
+                               vi->index = idx;
+                               break;
+                       }
+               }
                break;
        }
 
        case VIDIOC_S_INPUT:
        {
                struct v4l2_input *vi = (struct v4l2_input *)arg;
+               if (vi->index >= fh->input_cnt) {
+                       ret = -ERANGE;
+                       break;
+               }
                ret = pvr2_ctrl_set_value(
                        pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
-                       vi->index);
+                       fh->input_map[vi->index]);
                break;
        }
 
        case VIDIOC_ENUMAUDIO:
        {
+               /* pkt: FIXME: We are returning one "fake" input here
+                  which could very well be called "whatever_we_like".
+                  This is for apps that want to see an audio input
+                  just to feel comfortable, as well as to test if
+                  it can do stereo or sth. There is actually no guarantee
+                  that the actual audio input cannot change behind the app's
+                  back, but most applications should not mind that either.
+
+                  Hopefully, mplayer people will work with us on this (this
+                  whole mess is to support mplayer pvr://), or Hans will come
+                  up with a more standard way to say "we have inputs but we
+                  don 't want you to change them independent of video" which
+                  will sort this mess.
+                */
+               struct v4l2_audio *vin = arg;
                ret = -EINVAL;
+               if (vin->index > 0) break;
+               strncpy(vin->name, "PVRUSB2 Audio",14);
+               vin->capability = V4L2_AUDCAP_STEREO;
+               ret = 0;
+               break;
                break;
        }
 
        case VIDIOC_G_AUDIO:
        {
-               ret = -EINVAL;
+               /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
+               struct v4l2_audio *vin = arg;
+               memset(vin,0,sizeof(*vin));
+               vin->index = 0;
+               strncpy(vin->name, "PVRUSB2 Audio",14);
+               vin->capability = V4L2_AUDCAP_STEREO;
+               ret = 0;
                break;
        }
 
@@ -342,34 +377,11 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
        case VIDIOC_G_TUNER:
        {
                struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
-               unsigned int status_mask;
-               int val;
-               if (vt->index !=0) break;
 
-               status_mask = pvr2_hdw_get_signal_status(hdw);
+               if (vt->index != 0) break; /* Only answer for the 1st tuner */
 
-               memcpy(vt, &pvr_v4l2_tuners[vt->index],
-                      sizeof(struct v4l2_tuner));
-
-               vt->signal = 0;
-               if (status_mask & PVR2_SIGNAL_OK) {
-                       if (status_mask & PVR2_SIGNAL_STEREO) {
-                               vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
-                       } else {
-                               vt->rxsubchans = V4L2_TUNER_SUB_MONO;
-                       }
-                       if (status_mask & PVR2_SIGNAL_SAP) {
-                               vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
-                                                  V4L2_TUNER_SUB_LANG2);
-                       }
-                       vt->signal = 65535;
-               }
-
-               val = 0;
-               ret = pvr2_ctrl_get_value(
-                       pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
-                       &val);
-               vt->audmode = val;
+               pvr2_hdw_execute_tuner_poll(hdw);
+               ret = pvr2_hdw_get_tuner_status(hdw,vt);
                break;
        }
 
@@ -383,14 +395,40 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                ret = pvr2_ctrl_set_value(
                        pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
                        vt->audmode);
+               break;
        }
 
        case VIDIOC_S_FREQUENCY:
        {
                const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
+               unsigned long fv;
+               struct v4l2_tuner vt;
+               int cur_input;
+               struct pvr2_ctrl *ctrlp;
+               ret = pvr2_hdw_get_tuner_status(hdw,&vt);
+               if (ret != 0) break;
+               ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
+               ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
+               if (ret != 0) break;
+               if (vf->type == V4L2_TUNER_RADIO) {
+                       if (cur_input != PVR2_CVAL_INPUT_RADIO) {
+                               pvr2_ctrl_set_value(ctrlp,
+                                                   PVR2_CVAL_INPUT_RADIO);
+                       }
+               } else {
+                       if (cur_input == PVR2_CVAL_INPUT_RADIO) {
+                               pvr2_ctrl_set_value(ctrlp,
+                                                   PVR2_CVAL_INPUT_TV);
+                       }
+               }
+               fv = vf->frequency;
+               if (vt.capability & V4L2_TUNER_CAP_LOW) {
+                       fv = (fv * 125) / 2;
+               } else {
+                       fv = fv * 62500;
+               }
                ret = pvr2_ctrl_set_value(
-                       pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
-                       vf->frequency * 62500);
+                       pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
                break;
        }
 
@@ -398,10 +436,27 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
        {
                struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
                int val = 0;
+               int cur_input;
+               struct v4l2_tuner vt;
+               ret = pvr2_hdw_get_tuner_status(hdw,&vt);
+               if (ret != 0) break;
                ret = pvr2_ctrl_get_value(
                        pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
                        &val);
-               val /= 62500;
+               if (ret != 0) break;
+               pvr2_ctrl_get_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
+                       &cur_input);
+               if (cur_input == PVR2_CVAL_INPUT_RADIO) {
+                       vf->type = V4L2_TUNER_RADIO;
+               } else {
+                       vf->type = V4L2_TUNER_ANALOG_TV;
+               }
+               if (vt.capability & V4L2_TUNER_CAP_LOW) {
+                       val = (val * 2) / 125;
+               } else {
+                       val /= 62500;
+               }
                vf->frequency = val;
                break;
        }
@@ -458,18 +513,32 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                ret = 0;
                switch(vf->type) {
                case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
+                       int lmin,lmax,ldef;
+                       struct pvr2_ctrl *hcp,*vcp;
                        int h = vf->fmt.pix.height;
                        int w = vf->fmt.pix.width;
-
-                       if (h < 200) {
-                               h = 200;
-                       } else if (h > 625) {
-                               h = 625;
+                       hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
+                       vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
+
+                       lmin = pvr2_ctrl_get_min(hcp);
+                       lmax = pvr2_ctrl_get_max(hcp);
+                       pvr2_ctrl_get_def(hcp, &ldef);
+                       if (w == -1) {
+                               w = ldef;
+                       } else if (w < lmin) {
+                               w = lmin;
+                       } else if (w > lmax) {
+                               w = lmax;
                        }
-                       if (w < 320) {
-                               w = 320;
-                       } else if (w > 720) {
-                               w = 720;
+                       lmin = pvr2_ctrl_get_min(vcp);
+                       lmax = pvr2_ctrl_get_max(vcp);
+                       pvr2_ctrl_get_def(vcp, &ldef);
+                       if (h == -1) {
+                               h = ldef;
+                       } else if (h < lmin) {
+                               h = lmin;
+                       } else if (h > lmax) {
+                               h = lmax;
                        }
 
                        memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
@@ -478,14 +547,8 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        vf->fmt.pix.height = h;
 
                        if (cmd == VIDIOC_S_FMT) {
-                               pvr2_ctrl_set_value(
-                                       pvr2_hdw_get_ctrl_by_id(hdw,
-                                                               PVR2_CID_HRES),
-                                       vf->fmt.pix.width);
-                               pvr2_ctrl_set_value(
-                                       pvr2_hdw_get_ctrl_by_id(hdw,
-                                                               PVR2_CID_VRES),
-                                       vf->fmt.pix.height);
+                               pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
+                               pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
                        }
                } break;
                case V4L2_BUF_TYPE_VBI_CAPTURE:
@@ -501,6 +564,13 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
 
        case VIDIOC_STREAMON:
        {
+               if (!fh->dev_info->stream) {
+                       /* No stream defined for this node.  This means
+                          that we're not currently allowed to stream from
+                          this node. */
+                       ret = -EPERM;
+                       break;
+               }
                ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
                if (ret < 0) return ret;
                ret = pvr2_hdw_set_streaming(hdw,!0);
@@ -509,6 +579,13 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
 
        case VIDIOC_STREAMOFF:
        {
+               if (!fh->dev_info->stream) {
+                       /* No stream defined for this node.  This means
+                          that we're not currently allowed to stream from
+                          this node. */
+                       ret = -EPERM;
+                       break;
+               }
                ret = pvr2_hdw_set_streaming(hdw,0);
                break;
        }
@@ -516,16 +593,32 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
        case VIDIOC_QUERYCTRL:
        {
                struct pvr2_ctrl *cptr;
+               int val;
                struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
                ret = 0;
-               cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
+               if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
+                       cptr = pvr2_hdw_get_ctrl_nextv4l(
+                               hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
+                       if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
+               } else {
+                       cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
+               }
                if (!cptr) {
+                       pvr2_trace(PVR2_TRACE_V4LIOCTL,
+                                  "QUERYCTRL id=0x%x not implemented here",
+                                  vc->id);
                        ret = -EINVAL;
                        break;
                }
 
-               strlcpy(vc->name,pvr2_ctrl_get_name(cptr),sizeof(vc->name));
-               vc->default_value = pvr2_ctrl_get_def(cptr);
+               pvr2_trace(PVR2_TRACE_V4LIOCTL,
+                          "QUERYCTRL id=0x%x mapping name=%s (%s)",
+                          vc->id,pvr2_ctrl_get_name(cptr),
+                          pvr2_ctrl_get_desc(cptr));
+               strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
+               vc->flags = pvr2_ctrl_get_v4lflags(cptr);
+               pvr2_ctrl_get_def(cptr, &val);
+               vc->default_value = val;
                switch (pvr2_ctrl_get_type(cptr)) {
                case pvr2_ctl_enum:
                        vc->type = V4L2_CTRL_TYPE_MENU;
@@ -533,6 +626,12 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
                        vc->step = 1;
                        break;
+               case pvr2_ctl_bool:
+                       vc->type = V4L2_CTRL_TYPE_BOOLEAN;
+                       vc->minimum = 0;
+                       vc->maximum = 1;
+                       vc->step = 1;
+                       break;
                case pvr2_ctl_int:
                        vc->type = V4L2_CTRL_TYPE_INTEGER;
                        vc->minimum = pvr2_ctrl_get_min(cptr);
@@ -540,6 +639,9 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        vc->step = 1;
                        break;
                default:
+                       pvr2_trace(PVR2_TRACE_V4LIOCTL,
+                                  "QUERYCTRL id=0x%x name=%s not mappable",
+                                  vc->id,pvr2_ctrl_get_name(cptr));
                        ret = -EINVAL;
                        break;
                }
@@ -576,61 +678,248 @@ static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
                break;
        }
 
+       case VIDIOC_G_EXT_CTRLS:
+       {
+               struct v4l2_ext_controls *ctls =
+                       (struct v4l2_ext_controls *)arg;
+               struct v4l2_ext_control *ctrl;
+               unsigned int idx;
+               int val;
+               ret = 0;
+               for (idx = 0; idx < ctls->count; idx++) {
+                       ctrl = ctls->controls + idx;
+                       ret = pvr2_ctrl_get_value(
+                               pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
+                       if (ret) {
+                               ctls->error_idx = idx;
+                               break;
+                       }
+                       /* Ensure that if read as a 64 bit value, the user
+                          will still get a hopefully sane value */
+                       ctrl->value64 = 0;
+                       ctrl->value = val;
+               }
+               break;
+       }
+
+       case VIDIOC_S_EXT_CTRLS:
+       {
+               struct v4l2_ext_controls *ctls =
+                       (struct v4l2_ext_controls *)arg;
+               struct v4l2_ext_control *ctrl;
+               unsigned int idx;
+               ret = 0;
+               for (idx = 0; idx < ctls->count; idx++) {
+                       ctrl = ctls->controls + idx;
+                       ret = pvr2_ctrl_set_value(
+                               pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
+                               ctrl->value);
+                       if (ret) {
+                               ctls->error_idx = idx;
+                               break;
+                       }
+               }
+               break;
+       }
+
+       case VIDIOC_TRY_EXT_CTRLS:
+       {
+               struct v4l2_ext_controls *ctls =
+                       (struct v4l2_ext_controls *)arg;
+               struct v4l2_ext_control *ctrl;
+               struct pvr2_ctrl *pctl;
+               unsigned int idx;
+               /* For the moment just validate that the requested control
+                  actually exists. */
+               ret = 0;
+               for (idx = 0; idx < ctls->count; idx++) {
+                       ctrl = ctls->controls + idx;
+                       pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
+                       if (!pctl) {
+                               ret = -EINVAL;
+                               ctls->error_idx = idx;
+                               break;
+                       }
+               }
+               break;
+       }
+
+       case VIDIOC_CROPCAP:
+       {
+               struct v4l2_cropcap *cap = (struct v4l2_cropcap *)arg;
+               if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+                       ret = -EINVAL;
+                       break;
+               }
+               ret = pvr2_hdw_get_cropcap(hdw, cap);
+               cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
+               break;
+       }
+       case VIDIOC_G_CROP:
+       {
+               struct v4l2_crop *crop = (struct v4l2_crop *)arg;
+               int val = 0;
+               if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+                       ret = -EINVAL;
+                       break;
+               }
+               ret = pvr2_ctrl_get_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               crop->c.left = val;
+               ret = pvr2_ctrl_get_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               crop->c.top = val;
+               ret = pvr2_ctrl_get_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               crop->c.width = val;
+               ret = pvr2_ctrl_get_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               crop->c.height = val;
+       }
+       case VIDIOC_S_CROP:
+       {
+               struct v4l2_crop *crop = (struct v4l2_crop *)arg;
+               struct v4l2_cropcap cap;
+               if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+                       ret = -EINVAL;
+                       break;
+               }
+               cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+               ret = pvr2_ctrl_set_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
+                       crop->c.left);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               ret = pvr2_ctrl_set_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
+                       crop->c.top);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               ret = pvr2_ctrl_set_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
+                       crop->c.width);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+               ret = pvr2_ctrl_set_value(
+                       pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
+                       crop->c.height);
+               if (ret != 0) {
+                       ret = -EINVAL;
+                       break;
+               }
+       }
        case VIDIOC_LOG_STATUS:
        {
                pvr2_hdw_trigger_module_log(hdw);
                ret = 0;
                break;
        }
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+       case VIDIOC_DBG_S_REGISTER:
+       case VIDIOC_DBG_G_REGISTER:
+       {
+               u64 val;
+               struct v4l2_dbg_register *req = (struct v4l2_dbg_register *)arg;
+               if (cmd == VIDIOC_DBG_S_REGISTER) val = req->val;
+               ret = pvr2_hdw_register_access(
+                       hdw, &req->match, req->reg,
+                       cmd == VIDIOC_DBG_S_REGISTER, &val);
+               if (cmd == VIDIOC_DBG_G_REGISTER) req->val = val;
+               break;
+       }
+#endif
 
        default :
-               ret = v4l_compat_translate_ioctl(inode,file,cmd,
-                                                arg,pvr2_v4l2_do_ioctl);
+               ret = v4l_compat_translate_ioctl(file, cmd,
+                                                arg, pvr2_v4l2_do_ioctl);
        }
 
        pvr2_hdw_commit_ctl(hdw);
 
        if (ret < 0) {
                if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
-                       pvr2_trace(PVR2_TRACE_ERROR_LEGS,
-                                  "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
+                       pvr2_trace(PVR2_TRACE_V4LIOCTL,
+                                  "pvr2_v4l2_do_ioctl failure, ret=%ld", ret);
                } else {
-                       if (pvrusb2_debug & PVR2_TRACE_ERROR_LEGS) {
-                               pvr2_trace(PVR2_TRACE_ERROR_LEGS,
-                                          "pvr2_v4l2_do_ioctl failure, ret=%d"
-                                          " command was:",ret);
+                       if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
+                               pvr2_trace(PVR2_TRACE_V4LIOCTL,
+                                          "pvr2_v4l2_do_ioctl failure, ret=%ld"
+                                          " command was:", ret);
                                v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
                                                cmd);
                        }
                }
        } else {
                pvr2_trace(PVR2_TRACE_V4LIOCTL,
-                          "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
-                          ret,ret);
+                          "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
+                          ret, ret);
        }
        return ret;
 }
 
-
 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
 {
-       pvr2_trace(PVR2_TRACE_INIT,
-                  "unregistering device video%d [%s]",
-                  dip->vdev->minor,pvr2_config_get_name(dip->config));
-       if (dip->ctxt_idx >= 0) {
-               mutex_lock(&device_lock);
-               devices[dip->ctxt_idx] = NULL;
-               dip->ctxt_idx = -1;
-               mutex_unlock(&device_lock);
-       }
-       video_unregister_device(dip->vdev);
+       struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
+       enum pvr2_config cfg = dip->config;
+
+       pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
+
+       /* Paranoia */
+       dip->v4lp = NULL;
+       dip->stream = NULL;
+
+       /* Actual deallocation happens later when all internal references
+          are gone. */
+       video_unregister_device(&dip->devbase);
+
+       printk(KERN_INFO "pvrusb2: unregistered device %s [%s]\n",
+              video_device_node_name(&dip->devbase),
+              pvr2_config_get_name(cfg));
+
+}
+
+
+static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
+{
+       if (!dip) return;
+       if (!dip->devbase.parent) return;
+       dip->devbase.parent = NULL;
+       device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
 }
 
 
 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
 {
-       pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,-1);
-       pvr2_v4l2_dev_destroy(&vp->video_dev);
+       if (vp->dev_video) {
+               pvr2_v4l2_dev_destroy(vp->dev_video);
+               vp->dev_video = NULL;
+       }
+       if (vp->dev_radio) {
+               pvr2_v4l2_dev_destroy(vp->dev_radio);
+               vp->dev_radio = NULL;
+       }
 
        pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
        pvr2_channel_done(&vp->channel);
@@ -638,111 +927,93 @@ static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
 }
 
 
-void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
+static void pvr2_video_device_release(struct video_device *vdev)
+{
+       struct pvr2_v4l2_dev *dev;
+       dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
+       kfree(dev);
+}
+
+
+static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
 {
        struct pvr2_v4l2 *vp;
        vp = container_of(chp,struct pvr2_v4l2,channel);
        if (!vp->channel.mc_head->disconnect_flag) return;
+       pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
+       pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
        if (vp->vfirst) return;
        pvr2_v4l2_destroy_no_lock(vp);
 }
 
 
-int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
-                   unsigned int cmd, unsigned long arg)
+static long pvr2_v4l2_ioctl(struct file *file,
+                          unsigned int cmd, unsigned long arg)
 {
 
-/* Temporary hack : use ivtv api until a v4l2 one is available. */
-#define IVTV_IOC_G_CODEC        0xFFEE7703
-#define IVTV_IOC_S_CODEC        0xFFEE7704
-       if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
-       return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
+       return video_usercopy(file, cmd, arg, pvr2_v4l2_do_ioctl);
 }
 
 
-int pvr2_v4l2_release(struct inode *inode, struct file *file)
+static int pvr2_v4l2_release(struct file *file)
 {
        struct pvr2_v4l2_fh *fhp = file->private_data;
        struct pvr2_v4l2 *vp = fhp->vhead;
-       struct pvr2_context *mp = fhp->vhead->channel.mc_head;
+       struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
 
        pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
 
        if (fhp->rhp) {
                struct pvr2_stream *sp;
-               struct pvr2_hdw *hdw;
-               hdw = fhp->channel.mc_head->hdw;
                pvr2_hdw_set_streaming(hdw,0);
                sp = pvr2_ioread_get_stream(fhp->rhp);
-               if (sp) pvr2_stream_set_callback(sp,0,0);
+               if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
                pvr2_ioread_destroy(fhp->rhp);
-               fhp->rhp = 0;
+               fhp->rhp = NULL;
        }
+
        v4l2_prio_close(&vp->prio, &fhp->prio);
        file->private_data = NULL;
 
-       pvr2_context_enter(mp); do {
-               if (fhp->vnext) {
-                       fhp->vnext->vprev = fhp->vprev;
-               } else {
-                       vp->vlast = fhp->vprev;
-               }
-               if (fhp->vprev) {
-                       fhp->vprev->vnext = fhp->vnext;
-               } else {
-                       vp->vfirst = fhp->vnext;
-               }
-               fhp->vnext = 0;
-               fhp->vprev = 0;
-               fhp->vhead = 0;
-               pvr2_channel_done(&fhp->channel);
-               pvr2_trace(PVR2_TRACE_STRUCT,
-                          "Destroying pvr_v4l2_fh id=%p",fhp);
-               kfree(fhp);
-               if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
-                       pvr2_v4l2_destroy_no_lock(vp);
-               }
-       } while (0); pvr2_context_exit(mp);
+       if (fhp->vnext) {
+               fhp->vnext->vprev = fhp->vprev;
+       } else {
+               vp->vlast = fhp->vprev;
+       }
+       if (fhp->vprev) {
+               fhp->vprev->vnext = fhp->vnext;
+       } else {
+               vp->vfirst = fhp->vnext;
+       }
+       fhp->vnext = NULL;
+       fhp->vprev = NULL;
+       fhp->vhead = NULL;
+       pvr2_channel_done(&fhp->channel);
+       pvr2_trace(PVR2_TRACE_STRUCT,
+                  "Destroying pvr_v4l2_fh id=%p",fhp);
+       if (fhp->input_map) {
+               kfree(fhp->input_map);
+               fhp->input_map = NULL;
+       }
+       kfree(fhp);
+       if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
+               pvr2_v4l2_destroy_no_lock(vp);
+       }
        return 0;
 }
 
 
-int pvr2_v4l2_open(struct inode *inode, struct file *file)
+static int pvr2_v4l2_open(struct file *file)
 {
-       struct pvr2_v4l2_dev *dip = 0; /* Our own context pointer */
+       struct pvr2_v4l2_dev *dip; /* Our own context pointer */
        struct pvr2_v4l2_fh *fhp;
        struct pvr2_v4l2 *vp;
        struct pvr2_hdw *hdw;
+       unsigned int input_mask = 0;
+       unsigned int input_cnt,idx;
+       int ret = 0;
 
-       mutex_lock(&device_lock);
-       /* MCI 7-Jun-2006 Even though we're just doing what amounts to an
-          atomic read of the device mapping array here, we still need the
-          mutex.  The problem is that there is a tiny race possible when
-          we register the device.  We can't update the device mapping
-          array until after the device has been registered, owing to the
-          fact that we can't know the minor device number until after the
-          registration succeeds.  And if another thread tries to open the
-          device in the window of time after registration but before the
-          map is updated, then it will get back an erroneous null pointer
-          and the open will result in a spurious failure.  The only way to
-          prevent that is to (a) be inside the mutex here before we access
-          the array, and (b) cover the entire registration process later
-          on with this same mutex.  Thus if we get inside the mutex here,
-          then we can be assured that the registration process actually
-          completed correctly.  This is an unhappy complication from the
-          use of global data in a driver that lives in a preemptible
-          environment.  It sure would be nice if the video device itself
-          had a means for storing and retrieving a local context pointer.
-          Oh wait.  It did.  But now it's gone.  Silly me. */
-       {
-               unsigned int midx = iminor(file->f_dentry->d_inode);
-               if (midx < sizeof(devices)/sizeof(devices[0])) {
-                       dip = devices[midx];
-               }
-       }
-       mutex_unlock(&device_lock);
-
-       if (!dip) return -ENODEV; /* Should be impossible but I'm paranoid */
+       dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
 
        vp = dip->v4lp;
        hdw = vp->channel.hdw;
@@ -755,28 +1026,70 @@ int pvr2_v4l2_open(struct inode *inode, struct file *file)
                return -EIO;
        }
 
-       fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
+       fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
        if (!fhp) {
                return -ENOMEM;
        }
-       memset(fhp,0,sizeof(*fhp));
 
        init_waitqueue_head(&fhp->wait_data);
        fhp->dev_info = dip;
 
-       pvr2_context_enter(vp->channel.mc_head); do {
-               pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
-               pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
-               fhp->vnext = 0;
-               fhp->vprev = vp->vlast;
-               if (vp->vlast) {
-                       vp->vlast->vnext = fhp;
-               } else {
-                       vp->vfirst = fhp;
-               }
-               vp->vlast = fhp;
-               fhp->vhead = vp;
-       } while (0); pvr2_context_exit(vp->channel.mc_head);
+       pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
+       pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
+
+       if (dip->v4l_type == VFL_TYPE_RADIO) {
+               /* Opening device as a radio, legal input selection subset
+                  is just the radio. */
+               input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
+       } else {
+               /* Opening the main V4L device, legal input selection
+                  subset includes all analog inputs. */
+               input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
+                             (1 << PVR2_CVAL_INPUT_TV) |
+                             (1 << PVR2_CVAL_INPUT_COMPOSITE) |
+                             (1 << PVR2_CVAL_INPUT_SVIDEO));
+       }
+       ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
+       if (ret) {
+               pvr2_channel_done(&fhp->channel);
+               pvr2_trace(PVR2_TRACE_STRUCT,
+                          "Destroying pvr_v4l2_fh id=%p (input mask error)",
+                          fhp);
+
+               kfree(fhp);
+               return ret;
+       }
+
+       input_mask &= pvr2_hdw_get_input_available(hdw);
+       input_cnt = 0;
+       for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
+               if (input_mask & (1 << idx)) input_cnt++;
+       }
+       fhp->input_cnt = input_cnt;
+       fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
+       if (!fhp->input_map) {
+               pvr2_channel_done(&fhp->channel);
+               pvr2_trace(PVR2_TRACE_STRUCT,
+                          "Destroying pvr_v4l2_fh id=%p (input map failure)",
+                          fhp);
+               kfree(fhp);
+               return -ENOMEM;
+       }
+       input_cnt = 0;
+       for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
+               if (!(input_mask & (1 << idx))) continue;
+               fhp->input_map[input_cnt++] = idx;
+       }
+
+       fhp->vnext = NULL;
+       fhp->vprev = vp->vlast;
+       if (vp->vlast) {
+               vp->vlast->vnext = fhp;
+       } else {
+               vp->vfirst = fhp;
+       }
+       vp->vlast = fhp;
+       fhp->vhead = vp;
 
        fhp->file = file;
        file->private_data = fhp;
@@ -800,6 +1113,12 @@ static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
        struct pvr2_hdw *hdw;
        if (fh->rhp) return 0;
 
+       if (!fh->dev_info->stream) {
+               /* No stream defined for this node.  This means that we're
+                  not currently allowed to stream from this node. */
+               return -EPERM;
+       }
+
        /* First read() attempt.  Try to claim the stream and start
           it... */
        if ((ret = pvr2_channel_claim_stream(&fh->channel,
@@ -810,7 +1129,7 @@ static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
 
        fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
        if (!fh->rhp) {
-               pvr2_channel_claim_stream(&fh->channel,0);
+               pvr2_channel_claim_stream(&fh->channel,NULL);
                return -ENOMEM;
        }
 
@@ -818,10 +1137,8 @@ static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
        sp = fh->dev_info->stream->stream;
        pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
        pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
-       pvr2_hdw_set_streaming(hdw,!0);
-       ret = pvr2_ioread_set_enabled(fh->rhp,!0);
-
-       return ret;
+       if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
+       return pvr2_ioread_set_enabled(fh->rhp,!0);
 }
 
 
@@ -913,98 +1230,87 @@ static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
 }
 
 
-static struct file_operations vdev_fops = {
+static const struct v4l2_file_operations vdev_fops = {
        .owner      = THIS_MODULE,
        .open       = pvr2_v4l2_open,
        .release    = pvr2_v4l2_release,
        .read       = pvr2_v4l2_read,
        .ioctl      = pvr2_v4l2_ioctl,
-       .llseek     = no_llseek,
        .poll       = pvr2_v4l2_poll,
 };
 
 
-#define VID_HARDWARE_PVRUSB2    38  /* FIXME : need a good value */
-
 static struct video_device vdev_template = {
-       .owner      = THIS_MODULE,
-       .type       = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
-       .type2      = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
-                      | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
-                      | V4L2_CAP_READWRITE),
-       .hardware   = VID_HARDWARE_PVRUSB2,
        .fops       = &vdev_fops,
 };
 
 
 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
                               struct pvr2_v4l2 *vp,
-                              enum pvr2_config cfg)
+                              int v4l_type)
 {
+       struct usb_device *usbdev;
        int mindevnum;
        int unit_number;
-       int v4l_type;
+       int *nr_ptr = NULL;
        dip->v4lp = vp;
-       dip->config = cfg;
-
 
-       switch (cfg) {
-       case pvr2_config_mpeg:
-               v4l_type = VFL_TYPE_GRABBER;
+       usbdev = pvr2_hdw_get_dev(vp->channel.mc_head->hdw);
+       dip->v4l_type = v4l_type;
+       switch (v4l_type) {
+       case VFL_TYPE_GRABBER:
                dip->stream = &vp->channel.mc_head->video_stream;
+               dip->config = pvr2_config_mpeg;
+               dip->minor_type = pvr2_v4l_type_video;
+               nr_ptr = video_nr;
+               if (!dip->stream) {
+                       pr_err(KBUILD_MODNAME
+                               ": Failed to set up pvrusb2 v4l video dev"
+                               " due to missing stream instance\n");
+                       return;
+               }
                break;
-       case pvr2_config_vbi:
-               v4l_type = VFL_TYPE_VBI;
+       case VFL_TYPE_VBI:
+               dip->config = pvr2_config_vbi;
+               dip->minor_type = pvr2_v4l_type_vbi;
+               nr_ptr = vbi_nr;
                break;
-       case pvr2_config_radio:
-               v4l_type = VFL_TYPE_RADIO;
+       case VFL_TYPE_RADIO:
+               dip->stream = &vp->channel.mc_head->video_stream;
+               dip->config = pvr2_config_mpeg;
+               dip->minor_type = pvr2_v4l_type_radio;
+               nr_ptr = radio_nr;
                break;
        default:
                /* Bail out (this should be impossible) */
-               err("Failed to set up pvrusb2 v4l dev"
-                   " due to unrecognized config");
+               pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
+                   " due to unrecognized config\n");
                return;
        }
 
-       if (!dip->stream) {
-               err("Failed to set up pvrusb2 v4l dev"
-                   " due to missing stream instance");
-               return;
-       }
-
-       dip->vdev = video_device_alloc();
-       if (!dip->vdev) {
-               err("Alloc of pvrusb2 v4l video device failed");
-               return;
-       }
-
-       memcpy(dip->vdev,&vdev_template,sizeof(vdev_template));
-       dip->vdev->release = video_device_release;
-       mutex_lock(&device_lock);
+       memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
+       dip->devbase.release = pvr2_video_device_release;
 
        mindevnum = -1;
        unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
-       if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
-               mindevnum = video_nr[unit_number];
+       if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
+               mindevnum = nr_ptr[unit_number];
        }
-       if ((video_register_device(dip->vdev, v4l_type, mindevnum) < 0) &&
-           (video_register_device(dip->vdev, v4l_type, -1) < 0)) {
-               err("Failed to register pvrusb2 v4l video device");
-       } else {
-               pvr2_trace(PVR2_TRACE_INIT,
-                          "registered device video%d [%s]",
-                          dip->vdev->minor,pvr2_config_get_name(dip->config));
+       dip->devbase.parent = &usbdev->dev;
+       if ((video_register_device(&dip->devbase,
+                                  dip->v4l_type, mindevnum) < 0) &&
+           (video_register_device(&dip->devbase,
+                                  dip->v4l_type, -1) < 0)) {
+               pr_err(KBUILD_MODNAME
+                       ": Failed to register pvrusb2 v4l device\n");
        }
 
-       if ((dip->vdev->minor < sizeof(devices)/sizeof(devices[0])) &&
-           (devices[dip->vdev->minor] == NULL)) {
-               dip->ctxt_idx = dip->vdev->minor;
-               devices[dip->ctxt_idx] = dip;
-       }
-       mutex_unlock(&device_lock);
+       printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
+              video_device_node_name(&dip->devbase),
+              pvr2_config_get_name(dip->config));
 
        pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
-                                       dip->vdev->minor);
+                                       dip->minor_type,dip->devbase.minor);
 }
 
 
@@ -1012,20 +1318,29 @@ struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
 {
        struct pvr2_v4l2 *vp;
 
-       vp = kmalloc(sizeof(*vp),GFP_KERNEL);
+       vp = kzalloc(sizeof(*vp),GFP_KERNEL);
        if (!vp) return vp;
-       memset(vp,0,sizeof(*vp));
-       vp->video_dev.ctxt_idx = -1;
        pvr2_channel_init(&vp->channel,mnp);
        pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
 
        vp->channel.check_func = pvr2_v4l2_internal_check;
 
        /* register streams */
-       pvr2_v4l2_dev_init(&vp->video_dev,vp,pvr2_config_mpeg);
-
+       vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
+       if (!vp->dev_video) goto fail;
+       pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
+       if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
+           (1 << PVR2_CVAL_INPUT_RADIO)) {
+               vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
+               if (!vp->dev_radio) goto fail;
+               pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
+       }
 
        return vp;
+ fail:
+       pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
+       pvr2_v4l2_destroy_no_lock(vp);
+       return NULL;
 }
 
 /*