V4L/DVB (4236): Rearrange things in pvrusb2 driver in preparation for using cx2341x module

Signed-off-by: Mike Isely <isely@pobox.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/video/pvrusb2/pvrusb2-encoder.c b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
index f3d2c2b..47c8e02 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-encoder.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-encoder.c
@@ -22,8 +22,6 @@
 
 #include <linux/device.h>   // for linux/firmware.h
 #include <linux/firmware.h>
-#include <linux/videodev2.h>
-#include <media/cx2341x.h>
 #include "pvrusb2-util.h"
 #include "pvrusb2-encoder.h"
 #include "pvrusb2-hdw-internal.h"
@@ -166,6 +164,7 @@
 	u32 rdData[16];
 	struct pvr2_hdw *hdw = (struct pvr2_hdw *)ctxt;
 
+
 	/*
 
 	The encoder seems to speak entirely using blocks 32 bit words.
@@ -339,7 +338,7 @@
 		is_30fps=0;
 	}
 
-	pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure");
+	pvr2_trace(PVR2_TRACE_ENCODER,"pvr2_encoder_configure (native)");
 
 	/* set stream output port.  Some notes here: The ivtv-derived
 	   encoder documentation says that this command only gets a
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
index 217bbe5..4fd5ca2 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw-internal.h
@@ -39,6 +39,7 @@
 #include <linux/mutex.h>
 #include "pvrusb2-hdw.h"
 #include "pvrusb2-io.h"
+#include <media/cx2341x.h>
 
 /* Legal values for the SRATE state variable */
 #define PVR2_CVAL_SRATE_48 0
@@ -144,6 +145,13 @@
 };
 
 
+/* Same as pvr2_ctl_info, but includes storage for the control description */
+#define PVR2_CTLD_INFO_DESC_SIZE 32
+struct pvr2_ctld_info {
+	struct pvr2_ctl_info info;
+	char desc[PVR2_CTLD_INFO_DESC_SIZE];
+};
+
 struct pvr2_ctrl {
 	const struct pvr2_ctl_info *info;
 	struct pvr2_hdw *hdw;
@@ -312,6 +320,7 @@
 	int flag_bilingual;
 	struct pvr2_audio_stat *audio_stat;
 
+
 	/* Control state */
 #define VCREATE_DATA(lab) int lab##_val; int lab##_dirty
 	VCREATE_DATA(brightness);
@@ -323,6 +332,10 @@
 	VCREATE_DATA(bass);
 	VCREATE_DATA(treble);
 	VCREATE_DATA(mute);
+	VCREATE_DATA(input);
+	VCREATE_DATA(audiomode);
+	VCREATE_DATA(res_hor);
+	VCREATE_DATA(res_ver);
 	VCREATE_DATA(srate);
 	VCREATE_DATA(audiobitrate);
 	VCREATE_DATA(audiocrc);
@@ -330,15 +343,13 @@
 	VCREATE_DATA(vbr);
 	VCREATE_DATA(videobitrate);
 	VCREATE_DATA(videopeak);
-	VCREATE_DATA(input);
-	VCREATE_DATA(audiomode);
-	VCREATE_DATA(res_hor);
-	VCREATE_DATA(res_ver);
 	VCREATE_DATA(interlace);
 	VCREATE_DATA(audiolayer);
 #undef VCREATE_DATA
 
+
 	struct pvr2_ctrl *controls;
+	unsigned int control_cnt;
 };
 
 int pvr2_hdw_commit_ctl_internal(struct pvr2_hdw *hdw);
diff --git a/drivers/media/video/pvrusb2/pvrusb2-hdw.c b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
index c1a2447..e8ea472 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-hdw.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-hdw.c
@@ -25,7 +25,6 @@
 #include <linux/firmware.h>
 #include <linux/videodev2.h>
 #include <asm/semaphore.h>
-#include <media/cx2341x.h>
 #include "pvrusb2.h"
 #include "pvrusb2-std.h"
 #include "pvrusb2-util.h"
@@ -131,6 +130,7 @@
 /* size of a firmware chunk */
 #define FIRMWARE_CHUNK_SIZE 0x2000
 
+
 static const char *control_values_srate[] = {
 	[PVR2_CVAL_SRATE_48]   = "48KHz",
 	[PVR2_CVAL_SRATE_44_1] = "44.1KHz",
@@ -467,6 +467,10 @@
 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)
 VCREATE_FUNCS(audiobitrate)
 VCREATE_FUNCS(audiocrc)
@@ -474,10 +478,6 @@
 VCREATE_FUNCS(vbr)
 VCREATE_FUNCS(videobitrate)
 VCREATE_FUNCS(videopeak)
-VCREATE_FUNCS(input)
-VCREATE_FUNCS(audiomode)
-VCREATE_FUNCS(res_hor)
-VCREATE_FUNCS(res_ver)
 VCREATE_FUNCS(interlace)
 VCREATE_FUNCS(audiolayer)
 
@@ -550,6 +550,34 @@
 		DEFREF(mute),
 		DEFINT(0,1),
 	},{
+		.desc = "Video Source",
+		.name = "input",
+		.internal_id = PVR2_CID_INPUT,
+		.default_value = PVR2_CVAL_INPUT_TV,
+		DEFREF(input),
+		DEFENUM(control_values_input),
+	},{
+		.desc = "Audio Mode",
+		.name = "audio_mode",
+		.internal_id = PVR2_CID_AUDIOMODE,
+		.default_value = V4L2_TUNER_MODE_STEREO,
+		DEFREF(audiomode),
+		DEFENUM(control_values_audiomode),
+	},{
+		.desc = "Horizontal capture resolution",
+		.name = "resolution_hor",
+		.internal_id = PVR2_CID_HRES,
+		.default_value = 720,
+		DEFREF(res_hor),
+		DEFINT(320,720),
+	},{
+		.desc = "Vertical capture resolution",
+		.name = "resolution_ver",
+		.internal_id = PVR2_CID_VRES,
+		.default_value = 480,
+		DEFREF(res_ver),
+		DEFINT(200,625),
+	},{
 		.v4l_id = V4L2_CID_PVR_SRATE,
 		.desc = "Sample rate",
 		.name = "srate",
@@ -571,12 +599,6 @@
 		DEFREF(audiocrc),
 		DEFINT(0,1),
 	},{
-		.desc = "Audio Layer",
-		.name = "audio_layer",
-		.default_value = 2,
-		DEFREF(audiolayer),
-		DEFINT(0,3),
-	},{
 		.v4l_id = V4L2_CID_PVR_AUDIOEMPHASIS,
 		.desc = "Audio Emphasis",
 		.name = "audio_emphasis",
@@ -584,13 +606,6 @@
 		DEFREF(audioemphasis),
 		DEFENUM(control_values_audioemphasis),
 	},{
-		.desc = "Interlace mode",
-		.name = "interlace",
-		.internal_id = PVR2_CID_INTERLACE,
-		.default_value = 0,
-		DEFREF(interlace),
-		DEFINT(0,1),
-	},{
 		.v4l_id = V4L2_CID_PVR_VBR,
 		.desc = "Variable video bitrate",
 		.name = "vbr",
@@ -612,19 +627,18 @@
 		DEFREF(videopeak),
 		DEFINT(500000,20000000),
 	},{
-		.desc = "Video Source",
-		.name = "input",
-		.internal_id = PVR2_CID_INPUT,
-		.default_value = PVR2_CVAL_INPUT_TV,
-		DEFREF(input),
-		DEFENUM(control_values_input),
+		.desc = "Interlace mode",
+		.name = "interlace",
+		.internal_id = PVR2_CID_INTERLACE,
+		.default_value = 0,
+		DEFREF(interlace),
+		DEFINT(0,1),
 	},{
-		.desc = "Audio Mode",
-		.name = "audio_mode",
-		.internal_id = PVR2_CID_AUDIOMODE,
-		.default_value = V4L2_TUNER_MODE_STEREO,
-		DEFREF(audiomode),
-		DEFENUM(control_values_audiomode),
+		.desc = "Audio Layer",
+		.name = "audio_layer",
+		.default_value = 2,
+		DEFREF(audiolayer),
+		DEFINT(0,3),
 	},{
 		.desc = "Tuner Frequency (Hz)",
 		.name = "frequency",
@@ -654,20 +668,6 @@
 		.get_value = ctrl_channelprog_get,
 		DEFINT(0,FREQTABLE_SIZE),
 	},{
-		.desc = "Horizontal capture resolution",
-		.name = "resolution_hor",
-		.internal_id = PVR2_CID_HRES,
-		.default_value = 720,
-		DEFREF(res_hor),
-		DEFINT(320,720),
-	},{
-		.desc = "Vertical capture resolution",
-		.name = "resolution_ver",
-		.internal_id = PVR2_CID_VRES,
-		.default_value = 480,
-		DEFREF(res_ver),
-		DEFINT(200,625),
-	},{
 		.desc = "Streaming Enabled",
 		.name = "streaming_enabled",
 		.get_value = ctrl_streamingenabled_get,
@@ -731,7 +731,7 @@
 	}
 };
 
-#define CTRL_COUNT (sizeof(control_defs)/sizeof(control_defs[0]))
+#define CTRLDEF_COUNT (sizeof(control_defs)/sizeof(control_defs[0]))
 
 
 const char *pvr2_config_get_name(enum pvr2_config cfg)
@@ -1508,7 +1508,7 @@
 	pvr2_i2c_core_init(hdw);
 	if (!pvr2_hdw_dev_ok(hdw)) return;
 
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
 		cptr = hdw->controls + idx;
 		if (cptr->info->skip_init) continue;
 		if (!cptr->info->set_value) continue;
@@ -1665,19 +1665,21 @@
 	if (!hdw) goto fail;
 	memset(hdw,0,sizeof(*hdw));
 
-	hdw->controls = kmalloc(sizeof(struct pvr2_ctrl) * CTRL_COUNT,
+	hdw->control_cnt = CTRLDEF_COUNT;
+	hdw->controls = kmalloc(sizeof(struct pvr2_ctrl) * hdw->control_cnt,
 				GFP_KERNEL);
 	if (!hdw->controls) goto fail;
-	memset(hdw->controls,0,sizeof(struct pvr2_ctrl) * CTRL_COUNT);
+	memset(hdw->controls,0,sizeof(struct pvr2_ctrl) * hdw->control_cnt);
 	hdw->hdw_type = hdw_type;
-
+	for (idx = 0; idx < hdw->control_cnt; idx++) {
+		cptr = hdw->controls + idx;
+		cptr->hdw = hdw;
+	}
 	for (idx = 0; idx < 32; idx++) {
 		hdw->std_mask_ptrs[idx] = hdw->std_mask_names[idx];
 	}
-
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < CTRLDEF_COUNT; idx++) {
 		cptr = hdw->controls + idx;
-		cptr->hdw = hdw;
 		cptr->info = control_defs+idx;
 	}
 
@@ -1846,7 +1848,7 @@
 			unit_pointers[hdw->unit_number] = 0;
 		}
 	} while (0); up(&pvr2_unit_sem);
-	kfree(hdw->controls);
+	if (hdw->controls) kfree(hdw->controls);
 	if (hdw->std_defs) kfree(hdw->std_defs);
 	if (hdw->std_enum_names) kfree(hdw->std_enum_names);
 	kfree(hdw);
@@ -1952,7 +1954,7 @@
 /* Get the number of defined controls */
 unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *hdw)
 {
-	return CTRL_COUNT;
+	return hdw->control_cnt;
 }
 
 
@@ -1960,7 +1962,7 @@
 struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *hdw,
 					     unsigned int idx)
 {
-	if (idx >= CTRL_COUNT) return 0;
+	if (idx >= hdw->control_cnt) return 0;
 	return hdw->controls + idx;
 }
 
@@ -1974,7 +1976,7 @@
 	int i;
 
 	/* This could be made a lot more efficient, but for now... */
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < hdw->control_cnt; idx++) {
 		cptr = hdw->controls + idx;
 		i = cptr->info->internal_id;
 		if (i && (i == ctl_id)) return cptr;
@@ -1991,7 +1993,7 @@
 	int i;
 
 	/* This could be made a lot more efficient, but for now... */
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < hdw->control_cnt; idx++) {
 		cptr = hdw->controls + idx;
 		i = cptr->info->v4l_id;
 		if (i && (i == ctl_id)) return cptr;
@@ -2029,7 +2031,7 @@
 	char buf[100];
 	unsigned int bcnt,ccnt;
 
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < hdw->control_cnt; idx++) {
 		cptr = hdw->controls + idx;
 		if (cptr->info->is_dirty == 0) continue;
 		if (!cptr->info->is_dirty(cptr)) continue;
@@ -2080,30 +2082,31 @@
 	}
 
 	if (hdw->std_dirty ||
-	    hdw->res_ver_dirty ||
-	    hdw->res_hor_dirty ||
 	    hdw->interlace_dirty ||
 	    hdw->vbr_dirty ||
 	    hdw->videobitrate_dirty ||
 	    hdw->videopeak_dirty ||
 	    hdw->audiobitrate_dirty ||
-	    hdw->srate_dirty ||
 	    hdw->audiolayer_dirty ||
 	    hdw->audiocrc_dirty ||
-	    hdw->audioemphasis_dirty) {
+	    hdw->audioemphasis_dirty ||
+	    hdw->srate_dirty ||
+	    hdw->res_ver_dirty ||
+	    hdw->res_hor_dirty) {
 		/* If any of this changes, then the encoder needs to be
 		   reconfigured, and we need to reset the stream. */
 		stale_subsys_mask |= (1<<PVR2_SUBSYS_B_ENC_CFG);
 		stale_subsys_mask |= hdw->subsys_stream_mask;
 	}
 
+
 	/* Scan i2c core at this point - before we clear all the dirty
 	   bits.  Various parts of the i2c core will notice dirty bits as
 	   appropriate and arrange to broadcast or directly send updates to
 	   the client drivers in order to keep everything in sync */
 	pvr2_i2c_core_check_stale(hdw);
 
-	for (idx = 0; idx < CTRL_COUNT; idx++) {
+	for (idx = 0; idx < hdw->control_cnt; idx++) {
 		cptr = hdw->controls + idx;
 		if (!cptr->info->clear_dirty) continue;
 		cptr->info->clear_dirty(cptr);