V4L/DVB (10138): v4l2-ioctl: change to long return type to match unlocked_ioctl.
[safe/jmp/linux-2.6] / drivers / media / video / msp3400-driver.c
index 994c340..b8577ad 100644 (file)
@@ -42,7 +42,8 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
 
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/i2c.h>
-#include <linux/videodev.h>
-#include <linux/videodev2.h>
-#include <media/v4l2-common.h>
-#include <media/audiochip.h>
 #include <linux/kthread.h>
-#include <linux/suspend.h>
-#include "msp3400.h"
+#include <linux/freezer.h>
+#include <linux/videodev2.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+#include <media/v4l2-i2c-drv-legacy.h>
+#include <media/msp3400.h>
+#include <media/tvaudio.h>
+#include "msp3400-driver.h"
 
 /* ---------------------------------------------------------------------- */
 
@@ -70,7 +73,8 @@ int msp_debug;                 /* msp_debug output */
 int msp_once;           /* no continous stereo monitoring */
 int msp_amsound;        /* hard-wire AM sound at 6.5 Hz (france),
                            the autoscan seems work well only with FM... */
-int msp_standard = 1;    /* Override auto detect of audio msp_standard, if needed. */
+int msp_standard = 1;    /* Override auto detect of audio msp_standard,
+                           if needed. */
 int msp_dolby;
 
 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
@@ -80,12 +84,12 @@ int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
 module_param(opmode,           int, 0444);
 
 /* read-write */
-module_param_named(once,msp_once,                      bool, 0644);
-module_param_named(debug,msp_debug,                    int,  0644);
-module_param_named(stereo_threshold,msp_stereo_thresh, int,  0644);
-module_param_named(standard,msp_standard,              int,  0644);
-module_param_named(amsound,msp_amsound,                bool, 0644);
-module_param_named(dolby,msp_dolby,                    bool, 0644);
+module_param_named(once, msp_once,                      bool, 0644);
+module_param_named(debug, msp_debug,                    int,  0644);
+module_param_named(stereo_threshold, msp_stereo_thresh, int,  0644);
+module_param_named(standard, msp_standard,              int,  0644);
+module_param_named(amsound, msp_amsound,                bool, 0644);
+module_param_named(dolby, msp_dolby,                    bool, 0644);
 
 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
@@ -106,6 +110,7 @@ MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
 
 /* Addresses to scan */
 static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
+
 I2C_CLIENT_INSMOD;
 
 /* ----------------------------------------------------------------------- */
@@ -156,16 +161,16 @@ static int msp_read(struct i2c_client *client, int dev, int addr)
                        break;
                v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
                       dev, addr);
-               current->state = TASK_INTERRUPTIBLE;
-               schedule_timeout(msecs_to_jiffies(10));
+               schedule_timeout_interruptible(msecs_to_jiffies(10));
        }
        if (err == 3) {
-               v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
+               v4l_warn(client, "resetting chip, sound will go off.\n");
                msp_reset(client);
                return -1;
        }
        retval = read[0] << 8 | read[1];
-       v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
+       v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n",
+                       dev, addr, retval);
        return retval;
 }
 
@@ -190,17 +195,17 @@ static int msp_write(struct i2c_client *client, int dev, int addr, int val)
        buffer[3] = val  >> 8;
        buffer[4] = val  &  0xff;
 
-       v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
+       v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n",
+                       dev, addr, val);
        for (err = 0; err < 3; err++) {
                if (i2c_master_send(client, buffer, 5) == 5)
                        break;
                v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
                       dev, addr);
-               current->state = TASK_INTERRUPTIBLE;
-               schedule_timeout(msecs_to_jiffies(10));
+               schedule_timeout_interruptible(msecs_to_jiffies(10));
        }
        if (err == 3) {
-               v4l_warn(client, "giving up, resetting chip. Sound will go off, sorry folks :-|\n");
+               v4l_warn(client, "resetting chip, sound will go off.\n");
                msp_reset(client);
                return -1;
        }
@@ -245,71 +250,62 @@ int msp_write_dsp(struct i2c_client *client, int addr, int val)
  * ----------------------------------------------------------------------- */
 
 static int scarts[3][9] = {
-       /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
+       /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
        /* SCART DSP Input select */
-       { 0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
+       { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
        /* SCART1 Output select */
-       { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
+       { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
        /* SCART2 Output select */
-       { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
+       { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
 };
 
 static char *scart_names[] = {
-       "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
+       "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
 };
 
 void msp_set_scart(struct i2c_client *client, int in, int out)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
+       struct msp_state *state = to_state(i2c_get_clientdata(client));
 
-       state->in_scart=in;
+       state->in_scart = in;
 
-       if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
-               if (-1 == scarts[out][in])
+       if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
+               if (-1 == scarts[out][in + 1])
                        return;
 
-               state->acb &= ~scarts[out][SCART_MASK];
-               state->acb |=  scarts[out][in];
+               state->acb &= ~scarts[out][0];
+               state->acb |=  scarts[out][in + 1];
        } else
                state->acb = 0xf60; /* Mute Input and SCART 1 Output */
 
        v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
-                                               scart_names[in], out, state->acb);
+                                       scart_names[in], out, state->acb);
        msp_write_dsp(client, 0x13, state->acb);
 
        /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
-       msp_write_dem(client, 0x40, state->i2s_mode);
-}
-
-void msp_set_mute(struct i2c_client *client)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-
-       v4l_dbg(1, msp_debug, client, "mute audio\n");
-       msp_write_dsp(client, 0x0000, 0);
-       msp_write_dsp(client, 0x0007, 1);
-       if (state->has_scart2_out_volume)
-               msp_write_dsp(client, 0x0040, 1);
-       if (state->has_headphones)
-               msp_write_dsp(client, 0x0006, 0);
+       if (state->has_i2s_conf)
+               msp_write_dem(client, 0x40, state->i2s_mode);
 }
 
 void msp_set_audio(struct i2c_client *client)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
+       struct msp_state *state = to_state(i2c_get_clientdata(client));
        int bal = 0, bass, treble, loudness;
        int val = 0;
+       int reallymuted = state->muted | state->scan_in_progress;
 
-       if (!state->muted)
+       if (!reallymuted)
                val = (state->volume * 0x7f / 65535) << 8;
 
-       v4l_dbg(1, msp_debug, client, "mute=%s volume=%d\n",
-               state->muted ? "on" : "off", state->volume);
+       v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
+               state->muted ? "on" : "off",
+               state->scan_in_progress ? "yes" : "no",
+               state->volume);
 
        msp_write_dsp(client, 0x0000, val);
-       msp_write_dsp(client, 0x0007, state->muted ? 0x1 : (val | 0x1));
+       msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
        if (state->has_scart2_out_volume)
-               msp_write_dsp(client, 0x0040, state->muted ? 0x1 : (val | 0x1));
+               msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
        if (state->has_headphones)
                msp_write_dsp(client, 0x0006, val);
        if (!state->has_sound_processing)
@@ -336,47 +332,14 @@ void msp_set_audio(struct i2c_client *client)
        msp_write_dsp(client, 0x0033, loudness);
 }
 
-int msp_modus(struct i2c_client *client)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-
-       if (state->radio) {
-               v4l_dbg(1, msp_debug, client, "video mode selected to Radio\n");
-               return 0x0003;
-       }
-
-       if (state->v4l2_std & V4L2_STD_PAL) {
-               v4l_dbg(1, msp_debug, client, "video mode selected to PAL\n");
-
-#if 1
-               /* experimental: not sure this works with all chip versions */
-               return 0x7003;
-#else
-               /* previous value, try this if it breaks ... */
-               return 0x1003;
-#endif
-       }
-       if (state->v4l2_std & V4L2_STD_NTSC) {
-               v4l_dbg(1, msp_debug, client, "video mode selected to NTSC\n");
-               return 0x2003;
-       }
-       if (state->v4l2_std & V4L2_STD_SECAM) {
-               v4l_dbg(1, msp_debug, client, "video mode selected to SECAM\n");
-               return 0x0003;
-       }
-       return 0x0003;
-}
-
 /* ------------------------------------------------------------------------ */
 
-
 static void msp_wake_thread(struct i2c_client *client)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
+       struct msp_state *state = to_state(i2c_get_clientdata(client));
 
        if (NULL == state->kthread)
                return;
-       msp_set_mute(client);
        state->watch_stereo = 0;
        state->restart = 1;
        wake_up_interruptible(&state->wq);
@@ -403,20 +366,16 @@ int msp_sleep(struct msp_state *state, int timeout)
 }
 
 /* ------------------------------------------------------------------------ */
-
-static int msp_mode_v4l2_to_v4l1(int rxsubchans)
+#ifdef CONFIG_VIDEO_ALLOW_V4L1
+static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
 {
-       int mode = 0;
-
-       if (rxsubchans & V4L2_TUNER_SUB_STEREO)
-               mode |= VIDEO_SOUND_STEREO;
-       if (rxsubchans & V4L2_TUNER_SUB_LANG2)
-               mode |= VIDEO_SOUND_LANG2;
-       if (rxsubchans & V4L2_TUNER_SUB_LANG1)
-               mode |= VIDEO_SOUND_LANG1;
-       if (mode == 0)
-               mode |= VIDEO_SOUND_MONO;
-       return mode;
+       if (rxsubchans == V4L2_TUNER_SUB_MONO)
+               return VIDEO_SOUND_MONO;
+       if (rxsubchans == V4L2_TUNER_SUB_STEREO)
+               return VIDEO_SOUND_STEREO;
+       if (audmode == V4L2_TUNER_MODE_LANG2)
+               return VIDEO_SOUND_LANG2;
+       return VIDEO_SOUND_LANG1;
 }
 
 static int msp_mode_v4l1_to_v4l2(int mode)
@@ -429,102 +388,11 @@ static int msp_mode_v4l1_to_v4l2(int mode)
                return V4L2_TUNER_MODE_LANG1;
        return V4L2_TUNER_MODE_MONO;
 }
+#endif
 
-static void msp_any_detect_stereo(struct i2c_client *client)
-{
-       struct msp_state *state  = i2c_get_clientdata(client);
-
-       switch (state->opmode) {
-       case OPMODE_MANUAL:
-       case OPMODE_AUTODETECT:
-               autodetect_stereo(client);
-               break;
-       case OPMODE_AUTOSELECT:
-               msp34xxg_detect_stereo(client);
-               break;
-       }
-}
-
-static struct v4l2_queryctrl msp_qctrl_std[] = {
-       {
-               .id            = V4L2_CID_AUDIO_VOLUME,
-               .name          = "Volume",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 58880,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_MUTE,
-               .name          = "Mute",
-               .minimum       = 0,
-               .maximum       = 1,
-               .step          = 1,
-               .default_value = 1,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_BOOLEAN,
-       },
-};
-
-static struct v4l2_queryctrl msp_qctrl_sound_processing[] = {
-       {
-               .id            = V4L2_CID_AUDIO_BALANCE,
-               .name          = "Balance",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_BASS,
-               .name          = "Bass",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_TREBLE,
-               .name          = "Treble",
-               .minimum       = 0,
-               .maximum       = 65535,
-               .step          = 65535/100,
-               .default_value = 32768,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       },{
-               .id            = V4L2_CID_AUDIO_LOUDNESS,
-               .name          = "Loudness",
-               .minimum       = 0,
-               .maximum       = 1,
-               .step          = 1,
-               .default_value = 1,
-               .flags         = 0,
-               .type          = V4L2_CTRL_TYPE_BOOLEAN,
-       },
-};
-
-
-static void msp_any_set_audmode(struct i2c_client *client, int audmode)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-
-       switch (state->opmode) {
-       case OPMODE_MANUAL:
-       case OPMODE_AUTODETECT:
-               state->watch_stereo = 0;
-               msp3400c_setstereo(client, audmode);
-               break;
-       case OPMODE_AUTOSELECT:
-               msp34xxg_set_audmode(client, audmode);
-               break;
-       }
-}
-
-static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
+static int msp_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
+       struct msp_state *state = to_state(sd);
 
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_VOLUME:
@@ -565,9 +433,10 @@ static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        return 0;
 }
 
-static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
+static int msp_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_VOLUME:
@@ -613,76 +482,13 @@ static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
        return 0;
 }
 
-static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
+#ifdef CONFIG_VIDEO_ALLOW_V4L1
+static long msp_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
 {
-       struct msp_state *state = i2c_get_clientdata(client);
-       u16 *sarg = arg;
-       int scart = 0;
-
-       if (msp_debug >= 2)
-               v4l_i2c_print_ioctl(client, cmd);
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
        switch (cmd) {
-       case AUDC_SET_INPUT:
-               if (*sarg == state->input)
-                       break;
-               state->input = *sarg;
-               switch (*sarg) {
-               case AUDIO_RADIO:
-                       /* Hauppauge uses IN2 for the radio */
-                       state->mode = MSP_MODE_FM_RADIO;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_EXTERN_1:
-                       /* IN1 is often used for external input ... */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN1;
-                       break;
-               case AUDIO_EXTERN_2:
-                       /* ... sometimes it is IN2 through ;) */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_TUNER:
-                       state->mode = -1;
-                       break;
-               default:
-                       if (*sarg & AUDIO_MUTE)
-                               msp_set_scart(client, SCART_MUTE, 0);
-                       break;
-               }
-               if (scart) {
-                       state->rxsubchans = V4L2_TUNER_SUB_STEREO;
-                       msp_set_scart(client, scart, 0);
-                       msp_write_dsp(client, 0x000d, 0x1900);
-                       if (state->opmode != OPMODE_AUTOSELECT)
-                               msp3400c_setstereo(client, state->audmode);
-               }
-               msp_wake_thread(client);
-               break;
-
-       case AUDC_SET_RADIO:
-               if (state->radio)
-                       return 0;
-               state->radio = 1;
-               v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
-               state->watch_stereo = 0;
-               switch (state->opmode) {
-               case OPMODE_MANUAL:
-                       /* set msp3400 to FM radio mode */
-                       msp3400c_setmode(client, MSP_MODE_FM_RADIO);
-                       msp3400c_setcarrier(client, MSP_CARRIER(10.7),
-                                           MSP_CARRIER(10.7));
-                       msp_set_audio(client);
-                       break;
-               case OPMODE_AUTODETECT:
-               case OPMODE_AUTOSELECT:
-                       /* the thread will do for us */
-                       msp_wake_thread(client);
-                       break;
-               }
-               break;
-
        /* --- v4l ioctls --- */
        /* take care: bttv does userspace copying, we'll get a
           kernel pointer here... */
@@ -705,8 +511,8 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                if (state->radio)
                        break;
                if (state->opmode == OPMODE_AUTOSELECT)
-                       msp_any_detect_stereo(client);
-               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
+                       msp_detect_stereo(client);
+               va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
                break;
        }
 
@@ -721,8 +527,11 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                state->treble = va->treble;
                msp_set_audio(client);
 
-               if (va->mode != 0 && state->radio == 0)
-                       msp_any_set_audmode(client, msp_mode_v4l1_to_v4l2(va->mode));
+               if (va->mode != 0 && state->radio == 0 &&
+                   state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
+                       state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
+                       msp_set_audmode(client);
+               }
                break;
        }
 
@@ -751,250 +560,210 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
        }
 
        case VIDIOCSFREQ:
-       case VIDIOC_S_FREQUENCY:
        {
                /* new channel -- kick audio carrier scan */
                msp_wake_thread(client);
                break;
        }
-
-       /* msp34xx specific */
-       case MSP_SET_MATRIX:
-       {
-               struct msp_matrix *mspm = arg;
-
-               msp_set_scart(client, mspm->input, mspm->output);
-               break;
-       }
-
-       /* --- v4l2 ioctls --- */
-       case VIDIOC_S_STD:
-       {
-               v4l2_std_id *id = arg;
-               int update = state->radio || state->v4l2_std != *id;
-
-               state->v4l2_std = *id;
-               state->radio = 0;
-               if (update)
-                       msp_wake_thread(client);
-               return 0;
+       default:
+               return -ENOIOCTLCMD;
        }
+       return 0;
+}
+#endif
 
-       case VIDIOC_ENUMINPUT:
-       {
-               struct v4l2_input *i = arg;
-
-               if (i->index != 0)
-                       return -EINVAL;
+/* --- v4l2 ioctls --- */
+static int msp_s_radio(struct v4l2_subdev *sd)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               i->type = V4L2_INPUT_TYPE_TUNER;
-               switch (i->index) {
-               case AUDIO_RADIO:
-                       strcpy(i->name, "Radio");
-                       break;
-               case AUDIO_EXTERN_1:
-                       strcpy(i->name, "Extern 1");
-                       break;
-               case AUDIO_EXTERN_2:
-                       strcpy(i->name, "Extern 2");
-                       break;
-               case AUDIO_TUNER:
-                       strcpy(i->name, "Television");
-                       break;
-               default:
-                       return -EINVAL;
-               }
+       if (state->radio)
                return 0;
+       state->radio = 1;
+       v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
+       state->watch_stereo = 0;
+       switch (state->opmode) {
+       case OPMODE_MANUAL:
+               /* set msp3400 to FM radio mode */
+               msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
+               msp3400c_set_carrier(client, MSP_CARRIER(10.7),
+                               MSP_CARRIER(10.7));
+               msp_set_audio(client);
+               break;
+       case OPMODE_AUTODETECT:
+       case OPMODE_AUTOSELECT:
+               /* the thread will do for us */
+               msp_wake_thread(client);
+               break;
        }
+       return 0;
+}
 
-       case VIDIOC_G_AUDIO:
-       {
-               struct v4l2_audio *a = arg;
-
-               memset(a, 0, sizeof(*a));
-
-               switch (a->index) {
-               case AUDIO_RADIO:
-                       strcpy(a->name, "Radio");
-                       break;
-               case AUDIO_EXTERN_1:
-                       strcpy(a->name, "Extern 1");
-                       break;
-               case AUDIO_EXTERN_2:
-                       strcpy(a->name, "Extern 2");
-                       break;
-               case AUDIO_TUNER:
-                       strcpy(a->name, "Television");
-                       break;
-               default:
-                       return -EINVAL;
-               }
+static int msp_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               a->capability = V4L2_AUDCAP_STEREO;
-               a->mode = 0;  /* TODO: add support for AVL */
-               break;
-       }
+       /* new channel -- kick audio carrier scan */
+       msp_wake_thread(client);
+       return 0;
+}
 
-       case VIDIOC_S_AUDIO:
-       {
-               struct v4l2_audio *sarg = arg;
+static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       int update = state->radio || state->v4l2_std != id;
 
-               switch (sarg->index) {
-               case AUDIO_RADIO:
-                       /* Hauppauge uses IN2 for the radio */
-                       state->mode = MSP_MODE_FM_RADIO;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_EXTERN_1:
-                       /* IN1 is often used for external input ... */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN1;
-                       break;
-               case AUDIO_EXTERN_2:
-                       /* ... sometimes it is IN2 through ;) */
-                       state->mode = MSP_MODE_EXTERN;
-                       scart       = SCART_IN2;
-                       break;
-               case AUDIO_TUNER:
-                       state->mode = -1;
-                       break;
-               }
-               if (scart) {
-                       state->rxsubchans = V4L2_TUNER_SUB_STEREO;
-                       msp_set_scart(client, scart, 0);
-                       msp_write_dsp(client, 0x000d, 0x1900);
-               }
-               msp_any_set_audmode(client, state->audmode);
+       state->v4l2_std = id;
+       state->radio = 0;
+       if (update)
                msp_wake_thread(client);
-               break;
+       return 0;
+}
+
+static int msp_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *rt)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       int tuner = (rt->input >> 3) & 1;
+       int sc_in = rt->input & 0x7;
+       int sc1_out = rt->output & 0xf;
+       int sc2_out = (rt->output >> 4) & 0xf;
+       u16 val, reg;
+       int i;
+       int extern_input = 1;
+
+       if (state->routing.input == rt->input &&
+                       state->routing.output == rt->output)
+               return 0;
+       state->routing = *rt;
+       /* check if the tuner input is used */
+       for (i = 0; i < 5; i++) {
+               if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
+                       extern_input = 0;
        }
+       state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
+       state->rxsubchans = V4L2_TUNER_SUB_STEREO;
+       msp_set_scart(client, sc_in, 0);
+       msp_set_scart(client, sc1_out, 1);
+       msp_set_scart(client, sc2_out, 2);
+       msp_set_audmode(client);
+       reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
+       val = msp_read_dem(client, reg);
+       msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
+       /* wake thread when a new input is chosen */
+       msp_wake_thread(client);
+       return 0;
+}
 
-       case VIDIOC_G_TUNER:
-       {
-               struct v4l2_tuner *vt = arg;
+static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               if (state->radio)
-                       break;
-               if (state->opmode == OPMODE_AUTOSELECT)
-                       msp_any_detect_stereo(client);
-               vt->audmode    = state->audmode;
-               vt->rxsubchans = state->rxsubchans;
-               vt->capability = V4L2_TUNER_CAP_STEREO |
-                       V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
-               break;
-       }
+       if (state->radio)
+               return 0;
+       if (state->opmode == OPMODE_AUTOSELECT)
+               msp_detect_stereo(client);
+       vt->audmode    = state->audmode;
+       vt->rxsubchans = state->rxsubchans;
+       vt->capability |= V4L2_TUNER_CAP_STEREO |
+               V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
+       return 0;
+}
 
-       case VIDIOC_S_TUNER:
-       {
-               struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
+static int msp_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               if (state->radio)  /* TODO: add mono/stereo support for radio */
-                       break;
-               /* only set audmode */
-               msp_any_set_audmode(client, vt->audmode);
-               break;
-       }
+       if (state->radio)  /* TODO: add mono/stereo support for radio */
+               return 0;
+       if (state->audmode == vt->audmode)
+               return 0;
+       state->audmode = vt->audmode;
+       /* only set audmode */
+       msp_set_audmode(client);
+       return 0;
+}
 
-       case VIDIOC_G_AUDOUT:
-       {
-               struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
-               int idx = a->index;
+static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-               memset(a, 0, sizeof(*a));
+       v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", freq);
 
-               switch (idx) {
-               case 0:
-                       strcpy(a->name, "Scart1 Out");
+       switch (freq) {
+               case 1024000:
+                       state->i2s_mode = 0;
                        break;
-               case 1:
-                       strcpy(a->name, "Scart2 Out");
-                       break;
-               case 2:
-                       strcpy(a->name, "I2S Out");
+               case 2048000:
+                       state->i2s_mode = 1;
                        break;
                default:
                        return -EINVAL;
-               }
-               break;
        }
+       return 0;
+}
 
-       case VIDIOC_S_AUDOUT:
-       {
-               struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
-
-               if (a->index < 0 || a->index > 2)
-                       return -EINVAL;
-
-               v4l_dbg(1, msp_debug, client, "Setting audio out on msp34xx to input %i\n", a->index);
-               msp_set_scart(client, state->in_scart, a->index + 1);
-
-               break;
-       }
+static int msp_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
+{
+       struct msp_state *state = to_state(sd);
 
-       case VIDIOC_INT_I2S_CLOCK_FREQ:
-       {
-               u32 *a = (u32 *)arg;
-
-               v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
-
-               switch (*a) {
-                       case 1024000:
-                               state->i2s_mode = 0;
-                               break;
-                       case 2048000:
-                               state->i2s_mode = 1;
-                               break;
-                       default:
-                               return -EINVAL;
-               }
-               break;
+       switch (qc->id) {
+               case V4L2_CID_AUDIO_VOLUME:
+               case V4L2_CID_AUDIO_MUTE:
+                       return v4l2_ctrl_query_fill_std(qc);
+               default:
+                       break;
        }
-
-       case VIDIOC_QUERYCTRL:
-       {
-               struct v4l2_queryctrl *qc = arg;
-               int i;
-
-               for (i = 0; i < ARRAY_SIZE(msp_qctrl_std); i++)
-                       if (qc->id && qc->id == msp_qctrl_std[i].id) {
-                               memcpy(qc, &msp_qctrl_std[i], sizeof(*qc));
-                               return 0;
-                       }
-               if (!state->has_sound_processing)
-                       return -EINVAL;
-               for (i = 0; i < ARRAY_SIZE(msp_qctrl_sound_processing); i++)
-                       if (qc->id && qc->id == msp_qctrl_sound_processing[i].id) {
-                               memcpy(qc, &msp_qctrl_sound_processing[i], sizeof(*qc));
-                               return 0;
-                       }
+       if (!state->has_sound_processing)
                return -EINVAL;
+       switch (qc->id) {
+               case V4L2_CID_AUDIO_LOUDNESS:
+               case V4L2_CID_AUDIO_BALANCE:
+               case V4L2_CID_AUDIO_BASS:
+               case V4L2_CID_AUDIO_TREBLE:
+                       return v4l2_ctrl_query_fill_std(qc);
+               default:
+                       return -EINVAL;
        }
+       return 0;
+}
 
-       case VIDIOC_G_CTRL:
-               return msp_get_ctrl(client, arg);
-
-       case VIDIOC_S_CTRL:
-               return msp_set_ctrl(client, arg);
+static int msp_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
 
-       case VIDIOC_LOG_STATUS:
-       {
-               const char *p;
+       return v4l2_chip_ident_i2c_client(client, chip, state->ident,
+                       (state->rev1 << 16) | state->rev2);
+}
 
-               if (state->opmode == OPMODE_AUTOSELECT)
-                       msp_any_detect_stereo(client);
-               v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
-                               client->name, state->rev1, state->rev2);
-               v4l_info(client, "Audio:    volume %d%s\n",
-                               state->volume, state->muted ? " (muted)" : "");
-               if (state->has_sound_processing) {
-                       v4l_info(client, "Audio:    balance %d bass %d treble %d loudness %s\n",
-                                       state->balance, state->bass, state->treble,
-                                       state->loudness ? "on" : "off");
-               }
-               switch (state->mode) {
+static int msp_log_status(struct v4l2_subdev *sd)
+{
+       struct msp_state *state = to_state(sd);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       const char *p;
+
+       if (state->opmode == OPMODE_AUTOSELECT)
+               msp_detect_stereo(client);
+       v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
+                       client->name, state->rev1, state->rev2);
+       v4l_info(client, "Audio:    volume %d%s\n",
+                       state->volume, state->muted ? " (muted)" : "");
+       if (state->has_sound_processing) {
+               v4l_info(client, "Audio:    balance %d bass %d treble %d loudness %s\n",
+                               state->balance, state->bass,
+                               state->treble,
+                               state->loudness ? "on" : "off");
+       }
+       switch (state->mode) {
                case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
                case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
-               case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break;
+               case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono/stereo"; break;
                case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
                case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
                case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
@@ -1002,55 +771,85 @@ static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
                case MSP_MODE_BTSC: p = "BTSC"; break;
                case MSP_MODE_EXTERN: p = "External input"; break;
                default: p = "unknown"; break;
-               }
-               if (state->opmode == OPMODE_MANUAL) {
-                       v4l_info(client, "Mode:     %s (%s%s)\n", p,
+       }
+       if (state->mode == MSP_MODE_EXTERN) {
+               v4l_info(client, "Mode:     %s\n", p);
+       } else if (state->opmode == OPMODE_MANUAL) {
+               v4l_info(client, "Mode:     %s (%s%s)\n", p,
                                (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
                                (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
-               } else {
+       } else {
+               if (state->opmode == OPMODE_AUTODETECT)
                        v4l_info(client, "Mode:     %s\n", p);
-                       v4l_info(client, "Standard: %s (%s%s)\n",
+               v4l_info(client, "Standard: %s (%s%s)\n",
                                msp_standard_std_name(state->std),
                                (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
                                (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
-               }
-               v4l_info(client, "ACB:      0x%04x\n", state->acb);
-               break;
-       }
-
-       default:
-               /* unknown */
-               return -EINVAL;
        }
+       v4l_info(client, "Audmode:  0x%04x\n", state->audmode);
+       v4l_info(client, "Routing:  0x%08x (input) 0x%08x (output)\n",
+                       state->routing.input, state->routing.output);
+       v4l_info(client, "ACB:      0x%04x\n", state->acb);
        return 0;
 }
 
-static int msp_suspend(struct device * dev, pm_message_t state)
+static int msp_suspend(struct i2c_client *client, pm_message_t state)
 {
-       struct i2c_client *client = container_of(dev, struct i2c_client, dev);
-
        v4l_dbg(1, msp_debug, client, "suspend\n");
        msp_reset(client);
        return 0;
 }
 
-static int msp_resume(struct device * dev)
+static int msp_resume(struct i2c_client *client)
 {
-       struct i2c_client *client = container_of(dev, struct i2c_client, dev);
-
        v4l_dbg(1, msp_debug, client, "resume\n");
        msp_wake_thread(client);
        return 0;
 }
 
+static int msp_command(struct i2c_client *client, unsigned cmd, void *arg)
+{
+       return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
+}
+
 /* ----------------------------------------------------------------------- */
 
-static struct i2c_driver i2c_driver;
+static const struct v4l2_subdev_core_ops msp_core_ops = {
+       .log_status = msp_log_status,
+       .g_chip_ident = msp_g_chip_ident,
+       .g_ctrl = msp_g_ctrl,
+       .s_ctrl = msp_s_ctrl,
+       .queryctrl = msp_queryctrl,
+#ifdef CONFIG_VIDEO_ALLOW_V4L1
+       .ioctl = msp_ioctl,
+#endif
+};
+
+static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
+       .s_frequency = msp_s_frequency,
+       .g_tuner = msp_g_tuner,
+       .s_tuner = msp_s_tuner,
+       .s_radio = msp_s_radio,
+       .s_std = msp_s_std,
+};
+
+static const struct v4l2_subdev_audio_ops msp_audio_ops = {
+       .s_routing = msp_s_routing,
+       .s_i2s_clock_freq = msp_s_i2s_clock_freq,
+};
+
+static const struct v4l2_subdev_ops msp_ops = {
+       .core = &msp_core_ops,
+       .tuner = &msp_tuner_ops,
+       .audio = &msp_audio_ops,
+};
+
+/* ----------------------------------------------------------------------- */
 
-static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
+static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
-       struct i2c_client *client;
        struct msp_state *state;
+       struct v4l2_subdev *sd;
        int (*thread_func)(void *data) = NULL;
        int msp_hard;
        int msp_family;
@@ -1058,29 +857,21 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        int msp_product, msp_prod_hi, msp_prod_lo;
        int msp_rom;
 
-       client = kmalloc(sizeof(*client), GFP_KERNEL);
-       if (client == NULL)
-               return -ENOMEM;
-       memset(client, 0, sizeof(*client));
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &i2c_driver;
-       snprintf(client->name, sizeof(client->name) - 1, "msp3400");
+       if (!id)
+               strlcpy(client->name, "msp3400", sizeof(client->name));
 
        if (msp_reset(client) == -1) {
                v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
-               kfree(client);
-               return -1;
+               return -ENODEV;
        }
 
-       state = kmalloc(sizeof(*state), GFP_KERNEL);
-       if (state == NULL) {
-               kfree(client);
+       state = kzalloc(sizeof(*state), GFP_KERNEL);
+       if (!state)
                return -ENOMEM;
-       }
-       i2c_set_clientdata(client, state);
 
-       memset(state, 0, sizeof(*state));
+       sd = &state->sd;
+       v4l2_i2c_subdev_init(sd, client, &msp_ops);
+
        state->v4l2_std = V4L2_STD_NTSC;
        state->audmode = V4L2_TUNER_MODE_STEREO;
        state->volume = 58880;  /* 0db gain */
@@ -1092,16 +883,20 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        state->muted = 0;
        state->i2s_mode = 0;
        init_waitqueue_head(&state->wq);
+       /* These are the reset input/output positions */
+       state->routing.input = MSP_INPUT_DEFAULT;
+       state->routing.output = MSP_OUTPUT_DEFAULT;
 
        state->rev1 = msp_read_dsp(client, 0x1e);
        if (state->rev1 != -1)
                state->rev2 = msp_read_dsp(client, 0x1f);
-       v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
+       v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n",
+                       state->rev1, state->rev2);
        if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
-               v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n");
+               v4l_dbg(1, msp_debug, client,
+                               "not an msp3400 (cannot read chip version)\n");
                kfree(state);
-               kfree(client);
-               return -1;
+               return -ENODEV;
        }
 
        msp_set_audio(client);
@@ -1113,34 +908,56 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        msp_revision = (state->rev1 & 0x0f) + '@';
        msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
        msp_rom = state->rev2 & 0x1f;
-       snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
-                       msp_family, msp_product,
-                       msp_revision, msp_hard, msp_rom);
+       /* Rev B=2, C=3, D=4, G=7 */
+       state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
+                       msp_revision - '@';
 
        /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
-       state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
+       state->has_nicam =
+               msp_prod_hi == 1 || msp_prod_hi == 5;
        /* Has radio support: was added with revision G */
-       state->has_radio = msp_revision >= 'G';
+       state->has_radio =
+               msp_revision >= 'G';
        /* Has headphones output: not for stripped down products */
-       state->has_headphones = msp_prod_lo < 5;
+       state->has_headphones =
+               msp_prod_lo < 5;
+       /* Has scart2 input: not in stripped down products of the '3' family */
+       state->has_scart2 =
+               msp_family >= 4 || msp_prod_lo < 7;
+       /* Has scart3 input: not in stripped down products of the '3' family */
+       state->has_scart3 =
+               msp_family >= 4 || msp_prod_lo < 5;
        /* Has scart4 input: not in pre D revisions, not in stripped D revs */
-       state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
-       /* Has scart2 and scart3 inputs and scart2 output: not in stripped
-          down products of the '3' family */
-       state->has_scart23_in_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
+       state->has_scart4 =
+               msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
+       /* Has scart2 output: not in stripped down products of
+        * the '3' family */
+       state->has_scart2_out =
+               msp_family >= 4 || msp_prod_lo < 5;
        /* Has scart2 a volume control? Not in pre-D revisions. */
-       state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart23_in_scart2_out;
+       state->has_scart2_out_volume =
+               msp_revision > 'C' && state->has_scart2_out;
        /* Has a configurable i2s out? */
-       state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7;
-       /* Has subwoofer output: not in pre-D revs and not in stripped down products */
-       state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
-       /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
-          stripped down products */
-       state->has_sound_processing = msp_prod_lo < 7;
+       state->has_i2s_conf =
+               msp_revision >= 'G' && msp_prod_lo < 7;
+       /* Has subwoofer output: not in pre-D revs and not in stripped down
+        * products */
+       state->has_subwoofer =
+               msp_revision >= 'D' && msp_prod_lo < 5;
+       /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
+        *  not in stripped down products */
+       state->has_sound_processing =
+               msp_prod_lo < 7;
        /* Has Virtual Dolby Surround: only in msp34x1 */
-       state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
+       state->has_virtual_dolby_surround =
+               msp_revision == 'G' && msp_prod_lo == 1;
        /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
-       state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
+       state->has_dolby_pro_logic =
+               msp_revision == 'G' && msp_prod_lo == 2;
+       /* The msp343xG supports BTSC only and cannot do Automatic Standard
+        * Detection. */
+       state->force_btsc =
+               msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
 
        state->opmode = opmode;
        if (state->opmode == OPMODE_AUTO) {
@@ -1155,60 +972,52 @@ static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
        }
 
        /* hello world :-) */
-       v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
+       v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n",
+                       msp_family, msp_product,
+                       msp_revision, msp_hard, msp_rom,
+                       client->addr << 1, client->adapter->name);
        v4l_info(client, "%s ", client->name);
        if (state->has_nicam && state->has_radio)
-               printk("supports nicam and radio, ");
+               printk(KERN_CONT "supports nicam and radio, ");
        else if (state->has_nicam)
-               printk("supports nicam, ");
+               printk(KERN_CONT "supports nicam, ");
        else if (state->has_radio)
-               printk("supports radio, ");
-       printk("mode is ");
+               printk(KERN_CONT "supports radio, ");
+       printk(KERN_CONT "mode is ");
 
        /* version-specific initialization */
        switch (state->opmode) {
        case OPMODE_MANUAL:
-               printk("manual");
+               printk(KERN_CONT "manual");
                thread_func = msp3400c_thread;
                break;
        case OPMODE_AUTODETECT:
-               printk("autodetect");
+               printk(KERN_CONT "autodetect");
                thread_func = msp3410d_thread;
                break;
        case OPMODE_AUTOSELECT:
-               printk("autodetect and autoselect");
+               printk(KERN_CONT "autodetect and autoselect");
                thread_func = msp34xxg_thread;
                break;
        }
-       printk("\n");
+       printk(KERN_CONT "\n");
 
        /* startup control thread if needed */
        if (thread_func) {
                state->kthread = kthread_run(thread_func, client, "msp34xx");
 
-               if (state->kthread == NULL)
+               if (IS_ERR(state->kthread))
                        v4l_warn(client, "kernel_thread() failed\n");
                msp_wake_thread(client);
        }
-
-       /* done */
-       i2c_attach_client(client);
-
        return 0;
 }
 
-static int msp_probe(struct i2c_adapter *adapter)
+static int msp_remove(struct i2c_client *client)
 {
-       if (adapter->class & I2C_CLASS_TV_ANALOG)
-               return i2c_probe(adapter, &addr_data, msp_attach);
-       return 0;
-}
-
-static int msp_detach(struct i2c_client *client)
-{
-       struct msp_state *state = i2c_get_clientdata(client);
-       int err;
+       struct msp_state *state = to_state(i2c_get_clientdata(client));
 
+       v4l2_device_unregister_subdev(&state->sd);
        /* shutdown control thread */
        if (state->kthread) {
                state->restart = 1;
@@ -1216,43 +1025,28 @@ static int msp_detach(struct i2c_client *client)
        }
        msp_reset(client);
 
-       err = i2c_detach_client(client);
-       if (err) {
-               return err;
-       }
-
        kfree(state);
-       kfree(client);
        return 0;
 }
 
 /* ----------------------------------------------------------------------- */
 
-/* i2c implementation */
-static struct i2c_driver i2c_driver = {
-       .id             = I2C_DRIVERID_MSP3400,
-       .attach_adapter = msp_probe,
-       .detach_client  = msp_detach,
-       .command        = msp_command,
-       .driver = {
-               .name    = "msp3400",
-               .suspend = msp_suspend,
-               .resume  = msp_resume,
-       },
+static const struct i2c_device_id msp_id[] = {
+       { "msp3400", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, msp_id);
+
+static struct v4l2_i2c_driver_data v4l2_i2c_data = {
+       .name = "msp3400",
+       .driverid = I2C_DRIVERID_MSP3400,
+       .command = msp_command,
+       .probe = msp_probe,
+       .remove = msp_remove,
+       .suspend = msp_suspend,
+       .resume = msp_resume,
+       .id_table = msp_id,
 };
-
-static int __init msp3400_init_module(void)
-{
-       return i2c_add_driver(&i2c_driver);
-}
-
-static void __exit msp3400_cleanup_module(void)
-{
-       i2c_del_driver(&i2c_driver);
-}
-
-module_init(msp3400_init_module);
-module_exit(msp3400_cleanup_module);
 
 /*
  * Overrides for Emacs so that we follow Linus's tabbing style.