Merge to kernel-2.6.20-1.2949.fc6.vs2.2.0.1
[linux-2.6.git] / drivers / media / video / bt8xx / bttv-driver.c
similarity index 92%
rename from drivers/media/video/bttv-driver.c
rename to drivers/media/video/bt8xx/bttv-driver.c
index 578b200..4887eee 100644 (file)
 #include <linux/kdev_t.h>
 #include "bttvp.h"
 #include <media/v4l2-common.h>
+#include <media/tvaudio.h>
+#include <media/msp3400.h>
 
 #include <linux/dma-mapping.h>
 
 #include <asm/io.h>
 #include <asm/byteorder.h>
 
-#include "rds.h"
+#include <media/rds.h>
 
 
 unsigned int bttv_num;                 /* number of Bt848s in use */
@@ -153,6 +155,36 @@ static ssize_t show_card(struct class_device *cd, char *buf)
 }
 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
 
+/* ----------------------------------------------------------------------- */
+/* dvb auto-load setup                                                     */
+#if defined(CONFIG_MODULES) && defined(MODULE)
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
+static void request_module_async(void *ptr)
+{
+#else
+static void request_module_async(struct work_struct *work)
+{
+#endif
+       request_module("dvb-bt8xx");
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
+#define request_modules(dev)
+#else
+static void request_modules(struct bttv *dev)
+{
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
+       INIT_WORK(&dev->request_module_wk, request_module_async, (void*)dev);
+#else
+       INIT_WORK(&dev->request_module_wk, request_module_async);
+#endif
+       schedule_work(&dev->request_module_wk);
+}
+#endif
+#else
+#define request_modules(dev)
+#endif /* CONFIG_MODULES */
+
 /* ----------------------------------------------------------------------- */
 /* static data                                                             */
 
@@ -926,56 +958,106 @@ video_mux(struct bttv *btv, unsigned int input)
 
 static char *audio_modes[] = {
        "audio: tuner", "audio: radio", "audio: extern",
-       "audio: intern", "audio: off"
+       "audio: intern", "audio: mute"
 };
 
 static int
-audio_mux(struct bttv *btv, int mode)
+audio_mux(struct bttv *btv, int input, int mute)
 {
-       int val,mux,i2c_mux,signal;
+       int gpio_val, signal;
+       struct v4l2_control ctrl;
+       struct i2c_client *c;
 
        gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
                   bttv_tvcards[btv->c.type].gpiomask);
        signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
 
-       switch (mode) {
-       case AUDIO_MUTE:
-               btv->audio |= AUDIO_MUTE;
-               break;
-       case AUDIO_UNMUTE:
-               btv->audio &= ~AUDIO_MUTE;
-               break;
-       case AUDIO_TUNER:
-       case AUDIO_RADIO:
-       case AUDIO_EXTERN:
-       case AUDIO_INTERN:
-               btv->audio &= AUDIO_MUTE;
-               btv->audio |= mode;
-       }
-       i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
-       if (btv->opt_automute && !signal && !btv->radio_user)
-               mux = AUDIO_OFF;
-
-       val = bttv_tvcards[btv->c.type].audiomux[mux];
-       gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
+       btv->mute = mute;
+       btv->audio = input;
+
+       /* automute */
+       mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
+
+       if (mute)
+               gpio_val = bttv_tvcards[btv->c.type].gpiomute;
+       else
+               gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
+
+       gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
        if (bttv_gpio)
-               bttv_gpio_tracking(btv,audio_modes[mux]);
-       if (!in_interrupt())
-               bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
+               bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
+       if (in_interrupt())
+               return 0;
+
+       ctrl.id = V4L2_CID_AUDIO_MUTE;
+       ctrl.value = btv->mute;
+       bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
+       c = btv->i2c_msp34xx_client;
+       if (c) {
+               struct v4l2_routing route;
+
+               /* Note: the inputs tuner/radio/extern/intern are translated
+                  to msp routings. This assumes common behavior for all msp3400
+                  based TV cards. When this assumption fails, then the
+                  specific MSP routing must be added to the card table.
+                  For now this is sufficient. */
+               switch (input) {
+               case TVAUDIO_INPUT_RADIO:
+                       route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
+                                   MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
+                       break;
+               case TVAUDIO_INPUT_EXTERN:
+                       route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
+                                   MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
+                       break;
+               case TVAUDIO_INPUT_INTERN:
+                       /* Yes, this is the same input as for RADIO. I doubt
+                          if this is ever used. The only board with an INTERN
+                          input is the BTTV_BOARD_AVERMEDIA98. I wonder how
+                          that was tested. My guess is that the whole INTERN
+                          input does not work. */
+                       route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
+                                   MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
+                       break;
+               case TVAUDIO_INPUT_TUNER:
+               default:
+                       route.input = MSP_INPUT_DEFAULT;
+                       break;
+               }
+               route.output = MSP_OUTPUT_DEFAULT;
+               c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
+       }
+       c = btv->i2c_tvaudio_client;
+       if (c) {
+               struct v4l2_routing route;
+
+               route.input = input;
+               route.output = 0;
+               c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
+       }
        return 0;
 }
 
+static inline int
+audio_mute(struct bttv *btv, int mute)
+{
+       return audio_mux(btv, btv->audio, mute);
+}
+
+static inline int
+audio_input(struct bttv *btv, int input)
+{
+       return audio_mux(btv, input, btv->mute);
+}
+
 static void
 i2c_vidiocschan(struct bttv *btv)
 {
-       struct video_channel c;
+       v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
 
-       memset(&c,0,sizeof(c));
-       c.norm    = btv->tvnorm;
-       c.channel = btv->input;
-       bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
+       bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
        if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
-               bttv_tda9880_setnorm(btv,c.norm);
+               bttv_tda9880_setnorm(btv,btv->tvnorm);
 }
 
 static int
@@ -1023,8 +1105,8 @@ set_input(struct bttv *btv, unsigned int input)
        } else {
                video_mux(btv,input);
        }
-       audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
-                      AUDIO_TUNER : AUDIO_EXTERN));
+       audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
+                      TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
        set_tvnorm(btv,btv->tvnorm);
        i2c_vidiocschan(btv);
 }
@@ -1129,11 +1211,27 @@ static int get_control(struct bttv *btv, struct v4l2_control *c)
                        break;
        if (i == BTTV_CTLS)
                return -EINVAL;
-       if (i >= 4 && i <= 8) {
+       if (btv->audio_hook && i >= 4 && i <= 8) {
                memset(&va,0,sizeof(va));
-               bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
-               if (btv->audio_hook)
-                       btv->audio_hook(btv,&va,0);
+               btv->audio_hook(btv,&va,0);
+               switch (c->id) {
+               case V4L2_CID_AUDIO_MUTE:
+                       c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
+                       break;
+               case V4L2_CID_AUDIO_VOLUME:
+                       c->value = va.volume;
+                       break;
+               case V4L2_CID_AUDIO_BALANCE:
+                       c->value = va.balance;
+                       break;
+               case V4L2_CID_AUDIO_BASS:
+                       c->value = va.bass;
+                       break;
+               case V4L2_CID_AUDIO_TREBLE:
+                       c->value = va.treble;
+                       break;
+               }
+               return 0;
        }
        switch (c->id) {
        case V4L2_CID_BRIGHTNESS:
@@ -1150,19 +1248,11 @@ static int get_control(struct bttv *btv, struct v4l2_control *c)
                break;
 
        case V4L2_CID_AUDIO_MUTE:
-               c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
-               break;
        case V4L2_CID_AUDIO_VOLUME:
-               c->value = va.volume;
-               break;
        case V4L2_CID_AUDIO_BALANCE:
-               c->value = va.balance;
-               break;
        case V4L2_CID_AUDIO_BASS:
-               c->value = va.bass;
-               break;
        case V4L2_CID_AUDIO_TREBLE:
-               c->value = va.treble;
+               bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
                break;
 
        case V4L2_CID_PRIVATE_CHROMA_AGC:
@@ -1214,11 +1304,35 @@ static int set_control(struct bttv *btv, struct v4l2_control *c)
                        break;
        if (i == BTTV_CTLS)
                return -EINVAL;
-       if (i >= 4 && i <= 8) {
+       if (btv->audio_hook && i >= 4 && i <= 8) {
                memset(&va,0,sizeof(va));
-               bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
-               if (btv->audio_hook)
-                       btv->audio_hook(btv,&va,0);
+               btv->audio_hook(btv,&va,0);
+               switch (c->id) {
+               case V4L2_CID_AUDIO_MUTE:
+                       if (c->value) {
+                               va.flags |= VIDEO_AUDIO_MUTE;
+                               audio_mute(btv, 1);
+                       } else {
+                               va.flags &= ~VIDEO_AUDIO_MUTE;
+                               audio_mute(btv, 0);
+                       }
+                       break;
+
+               case V4L2_CID_AUDIO_VOLUME:
+                       va.volume = c->value;
+                       break;
+               case V4L2_CID_AUDIO_BALANCE:
+                       va.balance = c->value;
+                       break;
+               case V4L2_CID_AUDIO_BASS:
+                       va.bass = c->value;
+                       break;
+               case V4L2_CID_AUDIO_TREBLE:
+                       va.treble = c->value;
+                       break;
+               }
+               btv->audio_hook(btv,&va,1);
+               return 0;
        }
        switch (c->id) {
        case V4L2_CID_BRIGHTNESS:
@@ -1234,26 +1348,13 @@ static int set_control(struct bttv *btv, struct v4l2_control *c)
                bt848_sat(btv,c->value);
                break;
        case V4L2_CID_AUDIO_MUTE:
-               if (c->value) {
-                       va.flags |= VIDEO_AUDIO_MUTE;
-                       audio_mux(btv, AUDIO_MUTE);
-               } else {
-                       va.flags &= ~VIDEO_AUDIO_MUTE;
-                       audio_mux(btv, AUDIO_UNMUTE);
-               }
-               break;
-
+               audio_mute(btv, c->value);
+               /* fall through */
        case V4L2_CID_AUDIO_VOLUME:
-               va.volume = c->value;
-               break;
        case V4L2_CID_AUDIO_BALANCE:
-               va.balance = c->value;
-               break;
        case V4L2_CID_AUDIO_BASS:
-               va.bass = c->value;
-               break;
        case V4L2_CID_AUDIO_TREBLE:
-               va.treble = c->value;
+               bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
                break;
 
        case V4L2_CID_PRIVATE_CHROMA_AGC:
@@ -1309,11 +1410,6 @@ static int set_control(struct bttv *btv, struct v4l2_control *c)
        default:
                return -EINVAL;
        }
-       if (i >= 4 && i <= 8) {
-               bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
-               if (btv->audio_hook)
-                       btv->audio_hook(btv,&va,1);
-       }
        return 0;
 }
 
@@ -1397,7 +1493,7 @@ bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
                free_btres(btv,fh,RESOURCE_OVERLAY);
        if (NULL != old) {
                dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
-               bttv_dma_free(btv, old);
+               bttv_dma_free(&fh->cap,btv, old);
                kfree(old);
        }
        dprintk("switch_overlay: done\n");
@@ -1407,7 +1503,8 @@ bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
 /* ----------------------------------------------------------------------- */
 /* video4linux (1) interface                                               */
 
-static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
+static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
+                              struct bttv_buffer *buf,
                               const struct bttv_format *fmt,
                               unsigned int width, unsigned int height,
                               enum v4l2_field field)
@@ -1450,7 +1547,7 @@ static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
        /* alloc risc memory */
        if (STATE_NEEDS_INIT == buf->vb.state) {
                redo_dma_risc = 1;
-               if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
+               if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
                        goto fail;
        }
 
@@ -1462,7 +1559,7 @@ static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
        return 0;
 
  fail:
-       bttv_dma_free(btv,buf);
+       bttv_dma_free(q,btv,buf);
        return rc;
 }
 
@@ -1486,7 +1583,7 @@ buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
        struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
        struct bttv_fh *fh = q->priv_data;
 
-       return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
+       return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
                                   fh->width, fh->height, field);
 }
 
@@ -1510,7 +1607,7 @@ static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
        struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
        struct bttv_fh *fh = q->priv_data;
 
-       bttv_dma_free(fh->btv,buf);
+       bttv_dma_free(&fh->cap,fh->btv,buf);
 }
 
 static struct videobuf_queue_ops bttv_video_qops = {
@@ -1535,12 +1632,16 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
        }
        case VIDIOCSFREQ:
        {
-               unsigned long *freq = arg;
+               struct v4l2_frequency freq;
+
+               memset(&freq, 0, sizeof(freq));
+               freq.frequency = *(unsigned long *)arg;
                mutex_lock(&btv->lock);
-               btv->freq=*freq;
-               bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
+               freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
+               btv->freq = *(unsigned long *)arg;
+               bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
                if (btv->has_matchbox && btv->radio_user)
-                       tea5757_set_freq(btv,*freq);
+                       tea5757_set_freq(btv,*(unsigned long *)arg);
                mutex_unlock(&btv->lock);
                return 0;
        }
@@ -1653,7 +1754,7 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
                        return -EINVAL;
 
                mutex_lock(&btv->lock);
-               audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
+               audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
                bttv_call_i2c_clients(btv,cmd,v);
 
                /* card specific hooks */
@@ -1722,7 +1823,7 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
                memset(i,0,sizeof(*i));
                i->index    = n;
                i->type     = V4L2_INPUT_TYPE_CAMERA;
-               i->audioset = 0;
+               i->audioset = 1;
                if (i->index == bttv_tvcards[btv->c.type].tuner) {
                        sprintf(i->name, "Television");
                        i->type  = V4L2_INPUT_TYPE_TUNER;
@@ -1771,33 +1872,26 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
                        return -EINVAL;
                mutex_lock(&btv->lock);
                memset(t,0,sizeof(*t));
+               t->rxsubchans = V4L2_TUNER_SUB_MONO;
+               bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
                strcpy(t->name, "Television");
-               t->type       = V4L2_TUNER_ANALOG_TV;
                t->capability = V4L2_TUNER_CAP_NORM;
-               t->rxsubchans = V4L2_TUNER_SUB_MONO;
+               t->type       = V4L2_TUNER_ANALOG_TV;
                if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
                        t->signal = 0xffff;
-               {
-                       struct video_tuner tuner;
-
-                       memset(&tuner, 0, sizeof (tuner));
-                       tuner.rangehigh = 0xffffffffUL;
-                       bttv_call_i2c_clients(btv, VIDIOCGTUNER, &tuner);
-                       t->rangelow = tuner.rangelow;
-                       t->rangehigh = tuner.rangehigh;
-               }
-               {
+
+               if (btv->audio_hook) {
                        /* Hmmm ... */
                        struct video_audio va;
                        memset(&va, 0, sizeof(struct video_audio));
-                       bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
-                       if (btv->audio_hook)
-                               btv->audio_hook(btv,&va,0);
+                       btv->audio_hook(btv,&va,0);
+                       t->audmode    = V4L2_TUNER_MODE_MONO;
+                       t->rxsubchans = V4L2_TUNER_SUB_MONO;
                        if(va.mode & VIDEO_SOUND_STEREO) {
-                               t->audmode     = V4L2_TUNER_MODE_STEREO;
-                               t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
+                               t->audmode    = V4L2_TUNER_MODE_STEREO;
+                               t->rxsubchans = V4L2_TUNER_SUB_STEREO;
                        }
-                       if(va.mode & VIDEO_SOUND_LANG1) {
+                       if(va.mode & VIDEO_SOUND_LANG2) {
                                t->audmode    = V4L2_TUNER_MODE_LANG1;
                                t->rxsubchans = V4L2_TUNER_SUB_LANG1
                                        | V4L2_TUNER_SUB_LANG2;
@@ -1816,21 +1910,20 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
                if (0 != t->index)
                        return -EINVAL;
                mutex_lock(&btv->lock);
-               {
+               bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
+               if (btv->audio_hook) {
                        struct video_audio va;
                        memset(&va, 0, sizeof(struct video_audio));
-                       bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
                        if (t->audmode == V4L2_TUNER_MODE_MONO)
                                va.mode = VIDEO_SOUND_MONO;
-                       else if (t->audmode == V4L2_TUNER_MODE_STEREO)
+                       else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
+                                t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
                                va.mode = VIDEO_SOUND_STEREO;
                        else if (t->audmode == V4L2_TUNER_MODE_LANG1)
                                va.mode = VIDEO_SOUND_LANG1;
                        else if (t->audmode == V4L2_TUNER_MODE_LANG2)
                                va.mode = VIDEO_SOUND_LANG2;
-                       bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
-                       if (btv->audio_hook)
-                               btv->audio_hook(btv,&va,1);
+                       btv->audio_hook(btv,&va,1);
                }
                mutex_unlock(&btv->lock);
                return 0;
@@ -1855,7 +1948,7 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
                        return -EINVAL;
                mutex_lock(&btv->lock);
                btv->freq = f->frequency;
-               bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
+               bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
                if (btv->has_matchbox && btv->radio_user)
                        tea5757_set_freq(btv,btv->freq);
                mutex_unlock(&btv->lock);
@@ -1863,7 +1956,9 @@ static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
        }
        case VIDIOC_LOG_STATUS:
        {
+               printk(KERN_INFO "bttv%d: =================  START STATUS CARD #%d  =================\n", btv->c.nr, btv->c.nr);
                bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
+               printk(KERN_INFO "bttv%d: ==================  END STATUS CARD #%d  ==================\n", btv->c.nr, btv->c.nr);
                return 0;
        }
 
@@ -1965,7 +2060,7 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
                BUG();
        }
 
-       down(&fh->cap.lock);
+       mutex_lock(&fh->cap.lock);
                kfree(fh->ov.clips);
        fh->ov.clips    = clips;
        fh->ov.nclips   = n;
@@ -1986,7 +2081,7 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
                bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
                retval = bttv_switch_overlay(btv,fh,new);
        }
-       up(&fh->cap.lock);
+       mutex_unlock(&fh->cap.lock);
        return retval;
 }
 
@@ -2166,7 +2261,7 @@ static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
                fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 
                /* update our state informations */
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                fh->fmt              = fmt;
                fh->cap.field        = f->fmt.pix.field;
                fh->cap.last         = V4L2_FIELD_NONE;
@@ -2175,7 +2270,7 @@ static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
                btv->init.fmt        = fmt;
                btv->init.width      = f->fmt.pix.width;
                btv->init.height     = f->fmt.pix.height;
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
 
                return 0;
        }
@@ -2282,7 +2377,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                fmt = format_by_palette(pic->palette);
                if (NULL == fmt)
                        return -EINVAL;
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                if (fmt->depth != pic->depth) {
                        retval = -EINVAL;
                        goto fh_unlock_and_return;
@@ -2313,7 +2408,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                bt848_contrast(btv,pic->contrast);
                bt848_hue(btv,pic->hue);
                bt848_sat(btv,pic->colour);
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return 0;
        }
 
@@ -2366,6 +2461,14 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                fbuf->bytesperline  = btv->fbuf.fmt.bytesperline;
                if (fh->ovfmt)
                        fbuf->depth = fh->ovfmt->depth;
+               else {
+                       if (fbuf->width)
+                               fbuf->depth   = ((fbuf->bytesperline<<3)
+                                                 + (fbuf->width-1) )
+                                                 /fbuf->width;
+                       else
+                               fbuf->depth = 0;
+               }
                return 0;
        }
        case VIDIOCSFBUF:
@@ -2379,7 +2482,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                        return -EPERM;
                end = (unsigned long)fbuf->base +
                        fbuf->height * fbuf->bytesperline;
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                retval = -EINVAL;
 
                switch (fbuf->depth) {
@@ -2417,7 +2520,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                        btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
                else
                        btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return 0;
        }
 
@@ -2440,7 +2543,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
                        return -EBUSY;
 
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                if (*on) {
                        fh->ov.tvnorm = btv->tvnorm;
                        new = videobuf_alloc(sizeof(*new));
@@ -2451,7 +2554,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
 
                /* switch over */
                retval = bttv_switch_overlay(btv,fh,new);
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return retval;
        }
 
@@ -2460,7 +2563,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                struct video_mbuf *mbuf = arg;
                unsigned int i;
 
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
                                             V4L2_MEMORY_MMAP);
                if (retval < 0)
@@ -2470,7 +2573,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                mbuf->size   = gbuffers * gbufsize;
                for (i = 0; i < gbuffers; i++)
                        mbuf->offsets[i] = i * gbufsize;
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return 0;
        }
        case VIDIOCMCAPTURE:
@@ -2482,7 +2585,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                if (vm->frame >= VIDEO_MAX_FRAME)
                        return -EINVAL;
 
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                retval = -EINVAL;
                buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
                if (NULL == buf)
@@ -2496,7 +2599,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
                        ? V4L2_FIELD_INTERLACED
                        : V4L2_FIELD_BOTTOM;
-               retval = bttv_prepare_buffer(btv,buf,
+               retval = bttv_prepare_buffer(&fh->cap,btv,buf,
                                             format_by_palette(vm->format),
                                             vm->width,vm->height,field);
                if (0 != retval)
@@ -2504,7 +2607,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                spin_lock_irqsave(&btv->s_lock,flags);
                buffer_queue(&fh->cap,&buf->vb);
                spin_unlock_irqrestore(&btv->s_lock,flags);
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return 0;
        }
        case VIDIOCSYNC:
@@ -2515,7 +2618,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                if (*frame >= VIDEO_MAX_FRAME)
                        return -EINVAL;
 
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                retval = -EINVAL;
                buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
                if (NULL == buf)
@@ -2528,14 +2631,14 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                        retval = -EIO;
                        /* fall through */
                case STATE_DONE:
-                       videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
-                       bttv_dma_free(btv,buf);
+                       videobuf_dma_sync(&fh->cap,&buf->vb.dma);
+                       bttv_dma_free(&fh->cap,btv,buf);
                        break;
                default:
                        retval = -EINVAL;
                        break;
                }
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return retval;
        }
 
@@ -2719,7 +2822,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
                        return -EINVAL;
 
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                retval = -EINVAL;
                if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
                        if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
@@ -2759,7 +2862,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                                retval = bttv_switch_overlay(btv,fh,new);
                        }
                }
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                return retval;
        }
 
@@ -2813,12 +2916,10 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
                        return 0;
                }
                *c = bttv_ctls[i];
-               if (i >= 4 && i <= 8) {
+               if (btv->audio_hook && i >= 4 && i <= 8) {
                        struct video_audio va;
                        memset(&va,0,sizeof(va));
-                       bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
-                       if (btv->audio_hook)
-                               btv->audio_hook(btv,&va,0);
+                       btv->audio_hook(btv,&va,0);
                        switch (bttv_ctls[i].id) {
                        case V4L2_CID_AUDIO_VOLUME:
                                if (!(va.flags & VIDEO_AUDIO_VOLUME))
@@ -2890,7 +2991,7 @@ static int bttv_do_ioctl(struct inode *inode, struct file *file,
        return 0;
 
  fh_unlock_and_return:
-       up(&fh->cap.lock);
+       mutex_unlock(&fh->cap.lock);
        return retval;
 }
 
@@ -2957,16 +3058,16 @@ static unsigned int bttv_poll(struct file *file, poll_table *wait)
                buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
        } else {
                /* read() capture */
-               down(&fh->cap.lock);
+               mutex_lock(&fh->cap.lock);
                if (NULL == fh->cap.read_buf) {
                        /* need to capture a new frame */
                        if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
-                               up(&fh->cap.lock);
+                               mutex_unlock(&fh->cap.lock);
                                return POLLERR;
                        }
                        fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
                        if (NULL == fh->cap.read_buf) {
-                               up(&fh->cap.lock);
+                               mutex_unlock(&fh->cap.lock);
                                return POLLERR;
                        }
                        fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
@@ -2974,13 +3075,13 @@ static unsigned int bttv_poll(struct file *file, poll_table *wait)
                        if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
                                kfree (fh->cap.read_buf);
                                fh->cap.read_buf = NULL;
-                               up(&fh->cap.lock);
+                               mutex_unlock(&fh->cap.lock);
                                return POLLERR;
                        }
                        fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
                        fh->cap.read_off = 0;
                }
-               up(&fh->cap.lock);
+               mutex_unlock(&fh->cap.lock);
                buf = (struct bttv_buffer*)fh->cap.read_buf;
        }
 
@@ -3162,8 +3263,8 @@ static int radio_open(struct inode *inode, struct file *file)
 
        file->private_data = btv;
 
-       bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
-       audio_mux(btv,AUDIO_RADIO);
+       bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
+       audio_input(btv,TVAUDIO_INPUT_RADIO);
 
        mutex_unlock(&btv->lock);
        return 0;
@@ -3682,7 +3783,7 @@ bttv_irq_switch_vbi(struct bttv *btv)
        spin_unlock(&btv->s_lock);
 }
 
-static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
+static irqreturn_t bttv_irq(int irq, void *dev_id)
 {
        u32 stat,astat;
        u32 dstat;
@@ -3749,7 +3850,7 @@ static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
                        bttv_irq_switch_video(btv);
 
                if ((astat & BT848_INT_HLOCK)  &&  btv->opt_automute)
-                       audio_mux(btv, -1);
+                       audio_mute(btv, btv->mute);  /* trigger automute */
 
                if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
                        printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
@@ -3860,7 +3961,12 @@ static int __devinit bttv_register_video(struct bttv *btv)
                goto err;
        printk(KERN_INFO "bttv%d: registered device video%d\n",
               btv->c.nr,btv->video_dev->minor & 0x1f);
-       video_device_create_file(btv->video_dev, &class_device_attr_card);
+       if (class_device_create_file(&btv->video_dev->class_dev,
+                                    &class_device_attr_card)<0) {
+               printk(KERN_ERR "bttv%d: class_device_create_file 'card' "
+                      "failed\n", btv->c.nr);
+               goto err;
+       }
 
        /* vbi */
        btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
@@ -3956,8 +4062,9 @@ static int __devinit bttv_probe(struct pci_dev *dev,
        if (!request_mem_region(pci_resource_start(dev,0),
                                pci_resource_len(dev,0),
                                btv->c.name)) {
-               printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
-                      btv->c.nr, pci_resource_start(dev,0));
+               printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
+                      btv->c.nr,
+                      (unsigned long long)pci_resource_start(dev,0));
                return -EBUSY;
        }
        pci_set_master(dev);
@@ -3968,12 +4075,13 @@ static int __devinit bttv_probe(struct pci_dev *dev,
        pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
        printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
               bttv_num,btv->id, btv->revision, pci_name(dev));
-       printk("irq: %d, latency: %d, mmio: 0x%lx\n",
-              btv->c.pci->irq, lat, pci_resource_start(dev,0));
+       printk("irq: %d, latency: %d, mmio: 0x%llx\n",
+              btv->c.pci->irq, lat,
+              (unsigned long long)pci_resource_start(dev,0));
        schedule();
 
-       btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
-       if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
+       btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
+       if (NULL == btv->bt848_mmio) {
                printk("bttv%d: ioremap() failed\n", btv->c.nr);
                result = -EIO;
                goto fail1;
@@ -3985,7 +4093,7 @@ static int __devinit bttv_probe(struct pci_dev *dev,
        /* disable irqs, register irq handler */
        btwrite(0, BT848_INT_MASK);
        result = request_irq(btv->c.pci->irq, bttv_irq,
-                            SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
+                            IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
        if (result < 0) {
                printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
                       bttv_num,btv->c.pci->irq);
@@ -4050,13 +4158,15 @@ static int __devinit bttv_probe(struct pci_dev *dev,
                bt848_contrast(btv,32768);
                bt848_hue(btv,32768);
                bt848_sat(btv,32768);
-               audio_mux(btv,AUDIO_MUTE);
+               audio_mute(btv, 1);
                set_input(btv,0);
        }
 
-       /* add subdevices */
-       if (bttv_tvcards[btv->c.type].has_dvb)
+       /* add subdevices and autoload dvb-bt8xx if needed */
+       if (bttv_tvcards[btv->c.type].has_dvb) {
                bttv_sub_add_device(&btv->c, "dvb");
+               request_modules(btv);
+       }
 
        bttv_input_init(btv);
 
@@ -4116,6 +4226,7 @@ static void __devexit bttv_remove(struct pci_dev *pci_dev)
        return;
 }
 
+#ifdef CONFIG_PM
 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
 {
        struct bttv *btv = pci_get_drvdata(pci_dev);
@@ -4196,6 +4307,7 @@ static int bttv_resume(struct pci_dev *pci_dev)
        spin_unlock_irqrestore(&btv->s_lock,flags);
        return 0;
 }
+#endif
 
 static struct pci_device_id bttv_pci_tbl[] = {
        {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
@@ -4216,12 +4328,16 @@ static struct pci_driver bttv_pci_driver = {
        .id_table = bttv_pci_tbl,
        .probe    = bttv_probe,
        .remove   = __devexit_p(bttv_remove),
+#ifdef CONFIG_PM
        .suspend  = bttv_suspend,
        .resume   = bttv_resume,
+#endif
 };
 
 static int bttv_init_module(void)
 {
+       int ret;
+
        bttv_num = 0;
 
        printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
@@ -4243,7 +4359,11 @@ static int bttv_init_module(void)
 
        bttv_check_chipset();
 
-       bus_register(&bttv_sub_bus_type);
+       ret = bus_register(&bttv_sub_bus_type);
+       if (ret < 0) {
+               printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
+               return ret;
+       }
        return pci_register_driver(&bttv_pci_driver);
 }