]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/media/video/pvrusb2/pvrusb2-hdw.c
V4L/DVB (5090): Pvrusb2: A patch to use ARRAY_SIZE macro when appropriate
[linux-2.6-omap-h63xx.git] / drivers / media / video / pvrusb2 / pvrusb2-hdw.c
index 88604365777c32c936bec578935ce3337ceb2dd5..1c79ce1e16c42638962cfe765664de23ab450758 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/slab.h>
 #include <linux/firmware.h>
 #include <linux/videodev2.h>
+#include <media/v4l2-common.h>
 #include <asm/semaphore.h>
 #include "pvrusb2.h"
 #include "pvrusb2-std.h"
@@ -36,6 +37,9 @@
 #include "pvrusb2-encoder.h"
 #include "pvrusb2-debug.h"
 
+#define TV_MIN_FREQ     55250000L
+#define TV_MAX_FREQ    850000000L
+
 struct usb_device_id pvr2_device_table[] = {
        [PVR2_HDW_TYPE_29XXX] = { USB_DEVICE(0x2040, 0x2900) },
        [PVR2_HDW_TYPE_24XXX] = { USB_DEVICE(0x2040, 0x2400) },
@@ -70,12 +74,10 @@ static const char *pvr2_client_29xxx[] = {
 
 static struct pvr2_string_table pvr2_client_lists[] = {
        [PVR2_HDW_TYPE_29XXX] = {
-               pvr2_client_29xxx,
-               sizeof(pvr2_client_29xxx)/sizeof(pvr2_client_29xxx[0]),
+               pvr2_client_29xxx, ARRAY_SIZE(pvr2_client_29xxx)
        },
        [PVR2_HDW_TYPE_24XXX] = {
-               pvr2_client_24xxx,
-               sizeof(pvr2_client_24xxx)/sizeof(pvr2_client_24xxx[0]),
+               pvr2_client_24xxx, ARRAY_SIZE(pvr2_client_24xxx)
        },
 };
 
@@ -158,9 +160,6 @@ static const struct pvr2_mpeg_ids mpeg_ids[] = {
        },{
                .strid = "video_gop_closure",
                .id = V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
-       },{
-               .strid = "video_pulldown",
-               .id = V4L2_CID_MPEG_VIDEO_PULLDOWN,
        },{
                .strid = "video_bitrate_mode",
                .id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
@@ -211,7 +210,7 @@ static const struct pvr2_mpeg_ids mpeg_ids[] = {
                .id = V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
        }
 };
-#define MPEGDEF_COUNT (sizeof(mpeg_ids)/sizeof(mpeg_ids[0]))
+#define MPEGDEF_COUNT ARRAY_SIZE(mpeg_ids)
 
 
 static const char *control_values_srate[] = {
@@ -254,10 +253,10 @@ static const char *control_values_subsystem[] = {
        [PVR2_SUBSYS_B_ENC_RUN] = "enc_run",
 };
 
+static void pvr2_hdw_set_cur_freq(struct pvr2_hdw *,unsigned long);
 static int pvr2_hdw_cmd_usbstream(struct pvr2_hdw *hdw,int runFl);
 static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw);
 static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw);
-static unsigned int pvr2_hdw_get_signal_status_internal(struct pvr2_hdw *hdw);
 static void pvr2_hdw_internal_find_stdenum(struct pvr2_hdw *hdw);
 static void pvr2_hdw_internal_set_std_avail(struct pvr2_hdw *hdw);
 static void pvr2_hdw_render_useless_unlocked(struct pvr2_hdw *hdw);
@@ -288,8 +287,21 @@ static int ctrl_channelfreq_get(struct pvr2_ctrl *cptr,int *vp)
 static int ctrl_channelfreq_set(struct pvr2_ctrl *cptr,int m,int v)
 {
        struct pvr2_hdw *hdw = cptr->hdw;
-       if ((hdw->freqProgSlot > 0) && (hdw->freqProgSlot <= FREQTABLE_SIZE)) {
-               hdw->freqTable[hdw->freqProgSlot-1] = v;
+       unsigned int slotId = hdw->freqProgSlot;
+       if ((slotId > 0) && (slotId <= FREQTABLE_SIZE)) {
+               hdw->freqTable[slotId-1] = v;
+               /* Handle side effects correctly - if we're tuned to this
+                  slot, then forgot the slot id relation since the stored
+                  frequency has been changed. */
+               if (hdw->freqSelector) {
+                       if (hdw->freqSlotRadio == slotId) {
+                               hdw->freqSlotRadio = 0;
+                       }
+               } else {
+                       if (hdw->freqSlotTelevision == slotId) {
+                               hdw->freqSlotTelevision = 0;
+                       }
+               }
        }
        return 0;
 }
@@ -311,28 +323,32 @@ static int ctrl_channelprog_set(struct pvr2_ctrl *cptr,int m,int v)
 
 static int ctrl_channel_get(struct pvr2_ctrl *cptr,int *vp)
 {
-       *vp = cptr->hdw->freqSlot;
+       struct pvr2_hdw *hdw = cptr->hdw;
+       *vp = hdw->freqSelector ? hdw->freqSlotRadio : hdw->freqSlotTelevision;
        return 0;
 }
 
-static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int v)
+static int ctrl_channel_set(struct pvr2_ctrl *cptr,int m,int slotId)
 {
        unsigned freq = 0;
        struct pvr2_hdw *hdw = cptr->hdw;
-       hdw->freqSlot = v;
-       if ((hdw->freqSlot > 0) && (hdw->freqSlot <= FREQTABLE_SIZE)) {
-               freq = hdw->freqTable[hdw->freqSlot-1];
-       }
-       if (freq && (freq != hdw->freqVal)) {
-               hdw->freqVal = freq;
-               hdw->freqDirty = !0;
+       if ((slotId < 0) || (slotId > FREQTABLE_SIZE)) return 0;
+       if (slotId > 0) {
+               freq = hdw->freqTable[slotId-1];
+               if (!freq) return 0;
+               pvr2_hdw_set_cur_freq(hdw,freq);
+       }
+       if (hdw->freqSelector) {
+               hdw->freqSlotRadio = slotId;
+       } else {
+               hdw->freqSlotTelevision = slotId;
        }
        return 0;
 }
 
 static int ctrl_freq_get(struct pvr2_ctrl *cptr,int *vp)
 {
-       *vp = cptr->hdw->freqVal;
+       *vp = pvr2_hdw_get_cur_freq(cptr->hdw);
        return 0;
 }
 
@@ -348,32 +364,7 @@ static void ctrl_freq_clear_dirty(struct pvr2_ctrl *cptr)
 
 static int ctrl_freq_set(struct pvr2_ctrl *cptr,int m,int v)
 {
-       struct pvr2_hdw *hdw = cptr->hdw;
-       hdw->freqVal = v;
-       hdw->freqDirty = !0;
-       hdw->freqSlot = 0;
-       return 0;
-}
-
-static int ctrl_hres_max_get(struct pvr2_ctrl *cptr,int *vp)
-{
-       /* If we're dealing with a 24xxx device, force the horizontal
-          maximum to be 720 no matter what, since we can't get the device
-          to work properly with any other value.  Otherwise just return
-          the normal value. */
-       *vp = cptr->info->def.type_int.max_value;
-       if (cptr->hdw->hdw_type == PVR2_HDW_TYPE_24XXX) *vp = 720;
-       return 0;
-}
-
-static int ctrl_hres_min_get(struct pvr2_ctrl *cptr,int *vp)
-{
-       /* If we're dealing with a 24xxx device, force the horizontal
-          minimum to be 720 no matter what, since we can't get the device
-          to work properly with any other value.  Otherwise just return
-          the normal value. */
-       *vp = cptr->info->def.type_int.min_value;
-       if (cptr->hdw->hdw_type == PVR2_HDW_TYPE_24XXX) *vp = 720;
+       pvr2_hdw_set_cur_freq(cptr->hdw,v);
        return 0;
 }
 
@@ -399,6 +390,89 @@ static int ctrl_vres_min_get(struct pvr2_ctrl *cptr,int *vp)
        return 0;
 }
 
+static int ctrl_get_input(struct pvr2_ctrl *cptr,int *vp)
+{
+       *vp = cptr->hdw->input_val;
+       return 0;
+}
+
+static int ctrl_set_input(struct pvr2_ctrl *cptr,int m,int v)
+{
+       struct pvr2_hdw *hdw = cptr->hdw;
+
+       if (hdw->input_val != v) {
+               hdw->input_val = v;
+               hdw->input_dirty = !0;
+       }
+
+       /* Handle side effects - if we switch to a mode that needs the RF
+          tuner, then select the right frequency choice as well and mark
+          it dirty. */
+       if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
+               hdw->freqSelector = 0;
+               hdw->freqDirty = !0;
+       } else if (hdw->input_val == PVR2_CVAL_INPUT_TV) {
+               hdw->freqSelector = 1;
+               hdw->freqDirty = !0;
+       }
+       return 0;
+}
+
+static int ctrl_isdirty_input(struct pvr2_ctrl *cptr)
+{
+       return cptr->hdw->input_dirty != 0;
+}
+
+static void ctrl_cleardirty_input(struct pvr2_ctrl *cptr)
+{
+       cptr->hdw->input_dirty = 0;
+}
+
+
+static int ctrl_freq_max_get(struct pvr2_ctrl *cptr, int *vp)
+{
+       unsigned long fv;
+       struct pvr2_hdw *hdw = cptr->hdw;
+       if (hdw->tuner_signal_stale) {
+               pvr2_i2c_core_status_poll(hdw);
+       }
+       fv = hdw->tuner_signal_info.rangehigh;
+       if (!fv) {
+               /* Safety fallback */
+               *vp = TV_MAX_FREQ;
+               return 0;
+       }
+       if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
+               fv = (fv * 125) / 2;
+       } else {
+               fv = fv * 62500;
+       }
+       *vp = fv;
+       return 0;
+}
+
+static int ctrl_freq_min_get(struct pvr2_ctrl *cptr, int *vp)
+{
+       unsigned long fv;
+       struct pvr2_hdw *hdw = cptr->hdw;
+       if (hdw->tuner_signal_stale) {
+               pvr2_i2c_core_status_poll(hdw);
+       }
+       fv = hdw->tuner_signal_info.rangelow;
+       if (!fv) {
+               /* Safety fallback */
+               *vp = TV_MIN_FREQ;
+               return 0;
+       }
+       if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
+               fv = (fv * 125) / 2;
+       } else {
+               fv = fv * 62500;
+       }
+       *vp = fv;
+       return 0;
+}
+
 static int ctrl_cx2341x_is_dirty(struct pvr2_ctrl *cptr)
 {
        return cptr->hdw->enc_stale != 0;
@@ -555,8 +629,32 @@ static void ctrl_stdcur_clear_dirty(struct pvr2_ctrl *cptr)
 
 static int ctrl_signal_get(struct pvr2_ctrl *cptr,int *vp)
 {
-       *vp = ((pvr2_hdw_get_signal_status_internal(cptr->hdw) &
-               PVR2_SIGNAL_OK) ? 1 : 0);
+       struct pvr2_hdw *hdw = cptr->hdw;
+       pvr2_i2c_core_status_poll(hdw);
+       *vp = hdw->tuner_signal_info.signal;
+       return 0;
+}
+
+static int ctrl_audio_modes_present_get(struct pvr2_ctrl *cptr,int *vp)
+{
+       int val = 0;
+       unsigned int subchan;
+       struct pvr2_hdw *hdw = cptr->hdw;
+       pvr2_i2c_core_status_poll(hdw);
+       subchan = hdw->tuner_signal_info.rxsubchans;
+       if (subchan & V4L2_TUNER_SUB_MONO) {
+               val |= (1 << V4L2_TUNER_MODE_MONO);
+       }
+       if (subchan & V4L2_TUNER_SUB_STEREO) {
+               val |= (1 << V4L2_TUNER_MODE_STEREO);
+       }
+       if (subchan & V4L2_TUNER_SUB_LANG1) {
+               val |= (1 << V4L2_TUNER_MODE_LANG1);
+       }
+       if (subchan & V4L2_TUNER_SUB_LANG2) {
+               val |= (1 << V4L2_TUNER_MODE_LANG2);
+       }
+       *vp = val;
        return 0;
 }
 
@@ -662,15 +760,11 @@ VCREATE_FUNCS(balance)
 VCREATE_FUNCS(bass)
 VCREATE_FUNCS(treble)
 VCREATE_FUNCS(mute)
-VCREATE_FUNCS(input)
 VCREATE_FUNCS(audiomode)
 VCREATE_FUNCS(res_hor)
 VCREATE_FUNCS(res_ver)
 VCREATE_FUNCS(srate)
 
-#define MIN_FREQ 55250000L
-#define MAX_FREQ 850000000L
-
 /* Table definition of all controls which can be manipulated */
 static const struct pvr2_ctl_info control_defs[] = {
        {
@@ -705,7 +799,7 @@ static const struct pvr2_ctl_info control_defs[] = {
                .v4l_id = V4L2_CID_AUDIO_VOLUME,
                .desc = "Volume",
                .name = "volume",
-               .default_value = 65535,
+               .default_value = 62000,
                DEFREF(volume),
                DEFINT(0,65535),
        },{
@@ -757,10 +851,6 @@ static const struct pvr2_ctl_info control_defs[] = {
                .default_value = 720,
                DEFREF(res_hor),
                DEFINT(19,720),
-               /* Hook in check for clamp on horizontal resolution in
-                  order to avoid unsolved problem involving cx25840. */
-               .get_max_value = ctrl_hres_max_get,
-               .get_min_value = ctrl_hres_min_get,
        },{
                .desc = "Vertical capture resolution",
                .name = "resolution_ver",
@@ -783,12 +873,16 @@ static const struct pvr2_ctl_info control_defs[] = {
                .desc = "Tuner Frequency (Hz)",
                .name = "frequency",
                .internal_id = PVR2_CID_FREQUENCY,
-               .default_value = 175250000L,
+               .default_value = 0,
                .set_value = ctrl_freq_set,
                .get_value = ctrl_freq_get,
                .is_dirty = ctrl_freq_is_dirty,
                .clear_dirty = ctrl_freq_clear_dirty,
-               DEFINT(MIN_FREQ,MAX_FREQ),
+               DEFINT(0,0),
+               /* Hook in check for input value (tv/radio) and adjust
+                  max/min values accordingly */
+               .get_max_value = ctrl_freq_max_get,
+               .get_min_value = ctrl_freq_min_get,
        },{
                .desc = "Channel",
                .name = "channel",
@@ -800,7 +894,11 @@ static const struct pvr2_ctl_info control_defs[] = {
                .name = "freq_table_value",
                .set_value = ctrl_channelfreq_set,
                .get_value = ctrl_channelfreq_get,
-               DEFINT(MIN_FREQ,MAX_FREQ),
+               DEFINT(0,0),
+               /* Hook in check for input value (tv/radio) and adjust
+                  max/min values accordingly */
+               .get_max_value = ctrl_freq_max_get,
+               .get_min_value = ctrl_freq_min_get,
        },{
                .desc = "Channel Program ID",
                .name = "freq_table_channel",
@@ -821,7 +919,20 @@ static const struct pvr2_ctl_info control_defs[] = {
                .desc = "Signal Present",
                .name = "signal_present",
                .get_value = ctrl_signal_get,
-               DEFBOOL,
+               DEFINT(0,65535),
+       },{
+               .desc = "Audio Modes Present",
+               .name = "audio_modes_present",
+               .get_value = ctrl_audio_modes_present_get,
+               /* For this type we "borrow" the V4L2_TUNER_MODE enum from
+                  v4l.  Nothing outside of this module cares about this,
+                  but I reuse it in order to also reuse the
+                  control_values_audiomode string table. */
+               DEFMASK(((1 << V4L2_TUNER_MODE_MONO)|
+                        (1 << V4L2_TUNER_MODE_STEREO)|
+                        (1 << V4L2_TUNER_MODE_LANG1)|
+                        (1 << V4L2_TUNER_MODE_LANG2)),
+                       control_values_audiomode),
        },{
                .desc = "Video Standards Available Mask",
                .name = "video_standard_mask_available",
@@ -871,7 +982,7 @@ static const struct pvr2_ctl_info control_defs[] = {
        }
 };
 
-#define CTRLDEF_COUNT (sizeof(control_defs)/sizeof(control_defs[0]))
+#define CTRLDEF_COUNT ARRAY_SIZE(control_defs)
 
 
 const char *pvr2_config_get_name(enum pvr2_config cfg)
@@ -880,7 +991,8 @@ const char *pvr2_config_get_name(enum pvr2_config cfg)
        case pvr2_config_empty: return "empty";
        case pvr2_config_mpeg: return "mpeg";
        case pvr2_config_vbi: return "vbi";
-       case pvr2_config_radio: return "radio";
+       case pvr2_config_pcm: return "pcm";
+       case pvr2_config_rawvideo: return "raw video";
        }
        return "<unknown>";
 }
@@ -897,6 +1009,40 @@ unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *hdw)
        return hdw->serial_number;
 }
 
+unsigned long pvr2_hdw_get_cur_freq(struct pvr2_hdw *hdw)
+{
+       return hdw->freqSelector ? hdw->freqValTelevision : hdw->freqValRadio;
+}
+
+/* Set the currently tuned frequency and account for all possible
+   driver-core side effects of this action. */
+void pvr2_hdw_set_cur_freq(struct pvr2_hdw *hdw,unsigned long val)
+{
+       if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
+               if (hdw->freqSelector) {
+                       /* Swing over to radio frequency selection */
+                       hdw->freqSelector = 0;
+                       hdw->freqDirty = !0;
+               }
+               if (hdw->freqValRadio != val) {
+                       hdw->freqValRadio = val;
+                       hdw->freqSlotRadio = 0;
+                       hdw->freqDirty = !0;
+               }
+       } else {
+               if (!(hdw->freqSelector)) {
+                       /* Swing over to television frequency selection */
+                       hdw->freqSelector = 1;
+                       hdw->freqDirty = !0;
+               }
+               if (hdw->freqValTelevision != val) {
+                       hdw->freqValTelevision = val;
+                       hdw->freqSlotTelevision = 0;
+                       hdw->freqDirty = !0;
+               }
+       }
+}
+
 int pvr2_hdw_get_unit_number(struct pvr2_hdw *hdw)
 {
        return hdw->unit_number;
@@ -985,12 +1131,10 @@ static int pvr2_upload_firmware1(struct pvr2_hdw *hdw)
        };
        static const struct pvr2_string_table fw_file_defs[] = {
                [PVR2_HDW_TYPE_29XXX] = {
-                       fw_files_29xxx,
-                       sizeof(fw_files_29xxx)/sizeof(fw_files_29xxx[0]),
+                       fw_files_29xxx, ARRAY_SIZE(fw_files_29xxx)
                },
                [PVR2_HDW_TYPE_24XXX] = {
-                       fw_files_24xxx,
-                       sizeof(fw_files_24xxx)/sizeof(fw_files_24xxx[0]),
+                       fw_files_24xxx, ARRAY_SIZE(fw_files_24xxx)
                },
        };
        hdw->fw1_state = FW1_STATE_FAILED; // default result
@@ -1077,8 +1221,7 @@ int pvr2_upload_firmware2(struct pvr2_hdw *hdw)
        trace_firmware("pvr2_upload_firmware2");
 
        ret = pvr2_locate_firmware(hdw,&fw_entry,"encoder",
-                                  sizeof(fw_files)/sizeof(fw_files[0]),
-                                  fw_files);
+                                  ARRAY_SIZE(fw_files), fw_files);
        if (ret < 0) return ret;
        fwidx = ret;
        ret = 0;
@@ -1636,6 +1779,16 @@ static void pvr2_hdw_setup_low(struct pvr2_hdw *hdw)
                cptr->info->set_value(cptr,~0,cptr->info->default_value);
        }
 
+       /* Set up special default values for the television and radio
+          frequencies here.  It's not really important what these defaults
+          are, but I set them to something usable in the Chicago area just
+          to make driver testing a little easier. */
+
+       /* US Broadcast channel 7 (175.25 MHz) */
+       hdw->freqValTelevision = 175250000L;
+       /* 104.3 MHz, a usable FM station for my area */
+       hdw->freqValRadio = 104300000L;
+
        // Do not use pvr2_reset_ctl_endpoints() here.  It is not
        // thread-safe against the normal pvr2_send_request() mechanism.
        // (We should make it thread safe).
@@ -1775,8 +1928,7 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
        struct pvr2_ctl_info *ciptr;
 
        hdw_type = devid - pvr2_device_table;
-       if (hdw_type >=
-           sizeof(pvr2_device_names)/sizeof(pvr2_device_names[0])) {
+       if (hdw_type >= ARRAY_SIZE(pvr2_device_names)) {
                pvr2_trace(PVR2_TRACE_ERROR_LEGS,
                           "Bogus device type of %u reported",hdw_type);
                return NULL;
@@ -1787,6 +1939,7 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
                   hdw,pvr2_device_names[hdw_type]);
        if (!hdw) goto fail;
        memset(hdw,0,sizeof(*hdw));
+       hdw->tuner_signal_stale = !0;
        cx2341x_fill_defaults(&hdw->enc_ctl_state);
 
        hdw->control_cnt = CTRLDEF_COUNT;
@@ -1897,7 +2050,9 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
 
        hdw->eeprom_addr = -1;
        hdw->unit_number = -1;
-       hdw->v4l_minor_number = -1;
+       hdw->v4l_minor_number_video = -1;
+       hdw->v4l_minor_number_vbi = -1;
+       hdw->v4l_minor_number_radio = -1;
        hdw->ctl_write_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
        if (!hdw->ctl_write_buffer) goto fail;
        hdw->ctl_read_buffer = kmalloc(PVR2_CTL_BUFFSIZE,GFP_KERNEL);
@@ -1952,12 +2107,12 @@ struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
        return hdw;
  fail:
        if (hdw) {
-               if (hdw->ctl_read_urb) usb_free_urb(hdw->ctl_read_urb);
-               if (hdw->ctl_write_urb) usb_free_urb(hdw->ctl_write_urb);
-               if (hdw->ctl_read_buffer) kfree(hdw->ctl_read_buffer);
-               if (hdw->ctl_write_buffer) kfree(hdw->ctl_write_buffer);
-               if (hdw->controls) kfree(hdw->controls);
-               if (hdw->mpeg_ctrl_info) kfree(hdw->mpeg_ctrl_info);
+               usb_free_urb(hdw->ctl_read_urb);
+               usb_free_urb(hdw->ctl_write_urb);
+               kfree(hdw->ctl_read_buffer);
+               kfree(hdw->ctl_write_buffer);
+               kfree(hdw->controls);
+               kfree(hdw->mpeg_ctrl_info);
                kfree(hdw);
        }
        return NULL;
@@ -2007,9 +2162,6 @@ void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
                pvr2_stream_destroy(hdw->vid_stream);
                hdw->vid_stream = NULL;
        }
-       if (hdw->audio_stat) {
-               hdw->audio_stat->detach(hdw->audio_stat->ctxt);
-       }
        if (hdw->decoder_ctrl) {
                hdw->decoder_ctrl->detach(hdw->decoder_ctrl->ctxt);
        }
@@ -2022,10 +2174,10 @@ void pvr2_hdw_destroy(struct pvr2_hdw *hdw)
                        unit_pointers[hdw->unit_number] = NULL;
                }
        } while (0); up(&pvr2_unit_sem);
-       if (hdw->controls) kfree(hdw->controls);
-       if (hdw->mpeg_ctrl_info) kfree(hdw->mpeg_ctrl_info);
-       if (hdw->std_defs) kfree(hdw->std_defs);
-       if (hdw->std_enum_names) kfree(hdw->std_enum_names);
+       kfree(hdw->controls);
+       kfree(hdw->mpeg_ctrl_info);
+       kfree(hdw->std_defs);
+       kfree(hdw->std_enum_names);
        kfree(hdw);
 }
 
@@ -2235,10 +2387,9 @@ static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
                cptr = hdw->controls + idx;
                if (cptr->info->is_dirty == 0) continue;
                if (!cptr->info->is_dirty(cptr)) continue;
-               if (!commit_flag) {
-                       commit_flag = !0;
-               }
+               commit_flag = !0;
 
+               if (!(pvrusb2_debug & PVR2_TRACE_CTL)) continue;
                bcnt = scnprintf(buf,sizeof(buf),"\"%s\" <-- ",
                                 cptr->info->name);
                value = 0;
@@ -2288,6 +2439,13 @@ static int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw)
                stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
        }
 
+       if (hdw->input_dirty) {
+               /* pk: If input changes to or from radio, then the encoder
+                  needs to be restarted (for ENC_MUTE_VIDEO to work) */
+               stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_RUN);
+       }
+
+
        if (hdw->srate_dirty) {
                /* Write new sample rate into control structure since
                 * the master copy is stale.  We must track srate
@@ -2368,34 +2526,6 @@ const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *hdw)
 }
 
 
-/* Return bit mask indicating signal status */
-static unsigned int pvr2_hdw_get_signal_status_internal(struct pvr2_hdw *hdw)
-{
-       unsigned int msk = 0;
-       switch (hdw->input_val) {
-       case PVR2_CVAL_INPUT_TV:
-       case PVR2_CVAL_INPUT_RADIO:
-               if (hdw->decoder_ctrl &&
-                   hdw->decoder_ctrl->tuned(hdw->decoder_ctrl->ctxt)) {
-                       msk |= PVR2_SIGNAL_OK;
-                       if (hdw->audio_stat &&
-                           hdw->audio_stat->status(hdw->audio_stat->ctxt)) {
-                               if (hdw->flag_stereo) {
-                                       msk |= PVR2_SIGNAL_STEREO;
-                               }
-                               if (hdw->flag_bilingual) {
-                                       msk |= PVR2_SIGNAL_SAP;
-                               }
-                       }
-               }
-               break;
-       default:
-               msk |= PVR2_SIGNAL_OK | PVR2_SIGNAL_STEREO;
-       }
-       return msk;
-}
-
-
 int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
 {
        int result;
@@ -2411,14 +2541,25 @@ int pvr2_hdw_is_hsm(struct pvr2_hdw *hdw)
 }
 
 
-/* Return bit mask indicating signal status */
-unsigned int pvr2_hdw_get_signal_status(struct pvr2_hdw *hdw)
+/* Execute poll of tuner status */
+void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *hdw)
 {
-       unsigned int msk = 0;
        LOCK_TAKE(hdw->big_lock); do {
-               msk = pvr2_hdw_get_signal_status_internal(hdw);
+               pvr2_i2c_core_status_poll(hdw);
        } while (0); LOCK_GIVE(hdw->big_lock);
-       return msk;
+}
+
+
+/* Return information about the tuner */
+int pvr2_hdw_get_tuner_status(struct pvr2_hdw *hdw,struct v4l2_tuner *vtp)
+{
+       LOCK_TAKE(hdw->big_lock); do {
+               if (hdw->tuner_signal_stale) {
+                       pvr2_i2c_core_status_poll(hdw);
+               }
+               memcpy(vtp,&hdw->tuner_signal_info,sizeof(struct v4l2_tuner));
+       } while (0); LOCK_GIVE(hdw->big_lock);
+       return 0;
 }
 
 
@@ -2538,20 +2679,32 @@ int pvr2_hdw_cpufw_get(struct pvr2_hdw *hdw,unsigned int offs,
 }
 
 
-int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw)
+int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *hdw,
+                                 enum pvr2_v4l_type index)
 {
-       return hdw->v4l_minor_number;
+       switch (index) {
+       case pvr2_v4l_type_video: return hdw->v4l_minor_number_video;
+       case pvr2_v4l_type_vbi: return hdw->v4l_minor_number_vbi;
+       case pvr2_v4l_type_radio: return hdw->v4l_minor_number_radio;
+       default: return -1;
+       }
 }
 
 
-/* Store the v4l minor device number */
-void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,int v)
+/* Store a v4l minor device number */
+void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *hdw,
+                                    enum pvr2_v4l_type index,int v)
 {
-       hdw->v4l_minor_number = v;
+       switch (index) {
+       case pvr2_v4l_type_video: hdw->v4l_minor_number_video = v;
+       case pvr2_v4l_type_vbi: hdw->v4l_minor_number_vbi = v;
+       case pvr2_v4l_type_radio: hdw->v4l_minor_number_radio = v;
+       default: break;
+       }
 }
 
 
-static void pvr2_ctl_write_complete(struct urb *urb, struct pt_regs *regs)
+static void pvr2_ctl_write_complete(struct urb *urb)
 {
        struct pvr2_hdw *hdw = urb->context;
        hdw->ctl_write_pend_flag = 0;
@@ -2560,7 +2713,7 @@ static void pvr2_ctl_write_complete(struct urb *urb, struct pt_regs *regs)
 }
 
 
-static void pvr2_ctl_read_complete(struct urb *urb, struct pt_regs *regs)
+static void pvr2_ctl_read_complete(struct urb *urb)
 {
        struct pvr2_hdw *hdw = urb->context;
        hdw->ctl_read_pend_flag = 0;
@@ -2574,12 +2727,10 @@ static void pvr2_ctl_timeout(unsigned long data)
        struct pvr2_hdw *hdw = (struct pvr2_hdw *)data;
        if (hdw->ctl_write_pend_flag || hdw->ctl_read_pend_flag) {
                hdw->ctl_timeout_flag = !0;
-               if (hdw->ctl_write_pend_flag && hdw->ctl_write_urb) {
+               if (hdw->ctl_write_pend_flag)
                        usb_unlink_urb(hdw->ctl_write_urb);
-               }
-               if (hdw->ctl_read_pend_flag && hdw->ctl_read_urb) {
+               if (hdw->ctl_read_pend_flag)
                        usb_unlink_urb(hdw->ctl_read_urb);
-               }
        }
 }
 
@@ -3131,6 +3282,42 @@ static int pvr2_hdw_get_eeprom_addr(struct pvr2_hdw *hdw)
 }
 
 
+int pvr2_hdw_register_access(struct pvr2_hdw *hdw,
+                            u32 chip_id,unsigned long reg_id,
+                            int setFl,u32 *val_ptr)
+{
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+       struct list_head *item;
+       struct pvr2_i2c_client *cp;
+       struct v4l2_register req;
+       int stat = 0;
+       int okFl = 0;
+
+       req.i2c_id = chip_id;
+       req.reg = reg_id;
+       if (setFl) req.val = *val_ptr;
+       mutex_lock(&hdw->i2c_list_lock); do {
+               list_for_each(item,&hdw->i2c_clients) {
+                       cp = list_entry(item,struct pvr2_i2c_client,list);
+                       if (cp->client->driver->id != chip_id) continue;
+                       stat = pvr2_i2c_client_cmd(
+                               cp,(setFl ? VIDIOC_INT_S_REGISTER :
+                                   VIDIOC_INT_G_REGISTER),&req);
+                       if (!setFl) *val_ptr = req.val;
+                       okFl = !0;
+                       break;
+               }
+       } while (0); mutex_unlock(&hdw->i2c_list_lock);
+       if (okFl) {
+               return stat;
+       }
+       return -EINVAL;
+#else
+       return -ENOSYS;
+#endif
+}
+
+
 /*
   Stuff for Emacs to see, in order to encourage consistent editing style:
   *** Local Variables: ***