aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/bt8xx
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-10-28 12:35:11 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-10-28 12:35:11 -0400
commit0851668fdd97e526b2a41f794b785c204dd3d3e0 (patch)
tree4ef7c20a8be8393006c6fe9627eb29dd30877d61 /drivers/media/video/bt8xx
parent00ebb6382b8d9c7c15b5f8ad230670d8161d38dd (diff)
parent7655e594945289b418af39f6669fea4666a7b520 (diff)
Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-2.6
* 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/linux-2.6: (505 commits) [media] af9015: Fix max I2C message size when used with tda18271 [media] IR: initialize ir_raw_event in few more drivers [media] Guard a divide in v4l1 compat layer [media] imon: fix nomouse modprobe option [media] imon: remove redundant change_protocol call [media] imon: fix my egregious brown paper bag w/rdev/idev split [media] cafe_ccic: Configure ov7670 correctly [media] ov7670: allow configuration of image size, clock speed, and I/O method [media] af9015: support for DigitalNow TinyTwin v3 [1f4d:9016] [media] af9015: map DigitalNow TinyTwin v2 remote [media] DigitalNow TinyTwin remote controller [media] af9015: RC fixes and improvements videodev2.h.xml: Update to reflect the latest changes at videodev2.h [media] v4l: document new Bayer and monochrome pixel formats [media] DocBook/v4l: Add missing formats used on gspca cpia1 and sn9c2028 [media] firedtv: add parameter to fake ca_system_ids in CA_INFO [media] tm6000: fix a macro coding style issue tm6000: Remove some ugly debug code [media] Nova-S-Plus audio line input [media] [RFC,1/1] V4L2: Use new CAP bits in existing RDS capable drivers ...
Diffstat (limited to 'drivers/media/video/bt8xx')
-rw-r--r--drivers/media/video/bt8xx/bttv-cards.c22
-rw-r--r--drivers/media/video/bt8xx/bttv-driver.c273
-rw-r--r--drivers/media/video/bt8xx/bttv-i2c.c43
-rw-r--r--drivers/media/video/bt8xx/bttv-input.c84
-rw-r--r--drivers/media/video/bt8xx/bttv-risc.c2
-rw-r--r--drivers/media/video/bt8xx/bttv.h1
-rw-r--r--drivers/media/video/bt8xx/bttvp.h13
7 files changed, 279 insertions, 159 deletions
diff --git a/drivers/media/video/bt8xx/bttv-cards.c b/drivers/media/video/bt8xx/bttv-cards.c
index 7af56cde0c79..87d8b006ef77 100644
--- a/drivers/media/video/bt8xx/bttv-cards.c
+++ b/drivers/media/video/bt8xx/bttv-cards.c
@@ -3529,7 +3529,7 @@ void __devinit bttv_init_card2(struct bttv *btv)
3529 struct v4l2_subdev *sd; 3529 struct v4l2_subdev *sd;
3530 3530
3531 sd = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3531 sd = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3532 &btv->c.i2c_adap, "saa6588", "saa6588", 0, addrs); 3532 &btv->c.i2c_adap, NULL, "saa6588", 0, addrs);
3533 btv->has_saa6588 = (sd != NULL); 3533 btv->has_saa6588 = (sd != NULL);
3534 } 3534 }
3535 3535
@@ -3554,7 +3554,7 @@ void __devinit bttv_init_card2(struct bttv *btv)
3554 }; 3554 };
3555 3555
3556 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3556 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3557 &btv->c.i2c_adap, "msp3400", "msp3400", 0, addrs); 3557 &btv->c.i2c_adap, NULL, "msp3400", 0, addrs);
3558 if (btv->sd_msp34xx) 3558 if (btv->sd_msp34xx)
3559 return; 3559 return;
3560 goto no_audio; 3560 goto no_audio;
@@ -3568,7 +3568,7 @@ void __devinit bttv_init_card2(struct bttv *btv)
3568 }; 3568 };
3569 3569
3570 if (v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3570 if (v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3571 &btv->c.i2c_adap, "tda7432", "tda7432", 0, addrs)) 3571 &btv->c.i2c_adap, NULL, "tda7432", 0, addrs))
3572 return; 3572 return;
3573 goto no_audio; 3573 goto no_audio;
3574 } 3574 }
@@ -3576,7 +3576,7 @@ void __devinit bttv_init_card2(struct bttv *btv)
3576 case 3: { 3576 case 3: {
3577 /* The user specified that we should probe for tvaudio */ 3577 /* The user specified that we should probe for tvaudio */
3578 btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3578 btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3579 &btv->c.i2c_adap, "tvaudio", "tvaudio", 0, tvaudio_addrs()); 3579 &btv->c.i2c_adap, NULL, "tvaudio", 0, tvaudio_addrs());
3580 if (btv->sd_tvaudio) 3580 if (btv->sd_tvaudio)
3581 return; 3581 return;
3582 goto no_audio; 3582 goto no_audio;
@@ -3596,11 +3596,11 @@ void __devinit bttv_init_card2(struct bttv *btv)
3596 found is really something else (e.g. a tea6300). */ 3596 found is really something else (e.g. a tea6300). */
3597 if (!bttv_tvcards[btv->c.type].no_msp34xx) { 3597 if (!bttv_tvcards[btv->c.type].no_msp34xx) {
3598 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3598 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3599 &btv->c.i2c_adap, "msp3400", "msp3400", 3599 &btv->c.i2c_adap, NULL, "msp3400",
3600 0, I2C_ADDRS(I2C_ADDR_MSP3400 >> 1)); 3600 0, I2C_ADDRS(I2C_ADDR_MSP3400 >> 1));
3601 } else if (bttv_tvcards[btv->c.type].msp34xx_alt) { 3601 } else if (bttv_tvcards[btv->c.type].msp34xx_alt) {
3602 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3602 btv->sd_msp34xx = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3603 &btv->c.i2c_adap, "msp3400", "msp3400", 3603 &btv->c.i2c_adap, NULL, "msp3400",
3604 0, I2C_ADDRS(I2C_ADDR_MSP3400_ALT >> 1)); 3604 0, I2C_ADDRS(I2C_ADDR_MSP3400_ALT >> 1));
3605 } 3605 }
3606 3606
@@ -3616,13 +3616,13 @@ void __devinit bttv_init_card2(struct bttv *btv)
3616 }; 3616 };
3617 3617
3618 if (v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3618 if (v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3619 &btv->c.i2c_adap, "tda7432", "tda7432", 0, addrs)) 3619 &btv->c.i2c_adap, NULL, "tda7432", 0, addrs))
3620 return; 3620 return;
3621 } 3621 }
3622 3622
3623 /* Now see if we can find one of the tvaudio devices. */ 3623 /* Now see if we can find one of the tvaudio devices. */
3624 btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3624 btv->sd_tvaudio = v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3625 &btv->c.i2c_adap, "tvaudio", "tvaudio", 0, tvaudio_addrs()); 3625 &btv->c.i2c_adap, NULL, "tvaudio", 0, tvaudio_addrs());
3626 if (btv->sd_tvaudio) 3626 if (btv->sd_tvaudio)
3627 return; 3627 return;
3628 3628
@@ -3646,13 +3646,13 @@ void __devinit bttv_init_tuner(struct bttv *btv)
3646 /* Load tuner module before issuing tuner config call! */ 3646 /* Load tuner module before issuing tuner config call! */
3647 if (bttv_tvcards[btv->c.type].has_radio) 3647 if (bttv_tvcards[btv->c.type].has_radio)
3648 v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3648 v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3649 &btv->c.i2c_adap, "tuner", "tuner", 3649 &btv->c.i2c_adap, NULL, "tuner",
3650 0, v4l2_i2c_tuner_addrs(ADDRS_RADIO)); 3650 0, v4l2_i2c_tuner_addrs(ADDRS_RADIO));
3651 v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3651 v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3652 &btv->c.i2c_adap, "tuner", "tuner", 3652 &btv->c.i2c_adap, NULL, "tuner",
3653 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD)); 3653 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
3654 v4l2_i2c_new_subdev(&btv->c.v4l2_dev, 3654 v4l2_i2c_new_subdev(&btv->c.v4l2_dev,
3655 &btv->c.i2c_adap, "tuner", "tuner", 3655 &btv->c.i2c_adap, NULL, "tuner",
3656 0, v4l2_i2c_tuner_addrs(ADDRS_TV_WITH_DEMOD)); 3656 0, v4l2_i2c_tuner_addrs(ADDRS_TV_WITH_DEMOD));
3657 3657
3658 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV; 3658 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
diff --git a/drivers/media/video/bt8xx/bttv-driver.c b/drivers/media/video/bt8xx/bttv-driver.c
index 38c7f78ad9cf..3da6e80e1041 100644
--- a/drivers/media/video/bt8xx/bttv-driver.c
+++ b/drivers/media/video/bt8xx/bttv-driver.c
@@ -842,7 +842,7 @@ static const struct v4l2_queryctrl *ctrl_by_id(int id)
842 RESOURCE_OVERLAY) 842 RESOURCE_OVERLAY)
843 843
844static 844static
845int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit) 845int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
846{ 846{
847 int xbits; /* mutual exclusive resources */ 847 int xbits; /* mutual exclusive resources */
848 848
@@ -935,7 +935,7 @@ disclaim_video_lines(struct bttv *btv)
935} 935}
936 936
937static 937static
938void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits) 938void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
939{ 939{
940 if ((fh->resources & bits) != bits) { 940 if ((fh->resources & bits) != bits) {
941 /* trying to free ressources not allocated by us ... */ 941 /* trying to free ressources not allocated by us ... */
@@ -1682,7 +1682,7 @@ bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1682 kfree(old); 1682 kfree(old);
1683 } 1683 }
1684 if (NULL == new) 1684 if (NULL == new)
1685 free_btres(btv,fh,RESOURCE_OVERLAY); 1685 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1686 dprintk("switch_overlay: done\n"); 1686 dprintk("switch_overlay: done\n");
1687 return retval; 1687 return retval;
1688} 1688}
@@ -1859,21 +1859,25 @@ static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1859 unsigned int i; 1859 unsigned int i;
1860 int err; 1860 int err;
1861 1861
1862 mutex_lock(&btv->lock);
1862 err = v4l2_prio_check(&btv->prio, fh->prio); 1863 err = v4l2_prio_check(&btv->prio, fh->prio);
1863 if (0 != err) 1864 if (err)
1864 return err; 1865 goto err;
1865 1866
1866 for (i = 0; i < BTTV_TVNORMS; i++) 1867 for (i = 0; i < BTTV_TVNORMS; i++)
1867 if (*id & bttv_tvnorms[i].v4l2_id) 1868 if (*id & bttv_tvnorms[i].v4l2_id)
1868 break; 1869 break;
1869 if (i == BTTV_TVNORMS) 1870 if (i == BTTV_TVNORMS) {
1870 return -EINVAL; 1871 err = -EINVAL;
1872 goto err;
1873 }
1871 1874
1872 mutex_lock(&btv->lock);
1873 set_tvnorm(btv, i); 1875 set_tvnorm(btv, i);
1876
1877err:
1874 mutex_unlock(&btv->lock); 1878 mutex_unlock(&btv->lock);
1875 1879
1876 return 0; 1880 return err;
1877} 1881}
1878 1882
1879static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id) 1883static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
@@ -1893,10 +1897,13 @@ static int bttv_enum_input(struct file *file, void *priv,
1893{ 1897{
1894 struct bttv_fh *fh = priv; 1898 struct bttv_fh *fh = priv;
1895 struct bttv *btv = fh->btv; 1899 struct bttv *btv = fh->btv;
1896 int n; 1900 int rc = 0;
1897 1901
1898 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) 1902 mutex_lock(&btv->lock);
1899 return -EINVAL; 1903 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1904 rc = -EINVAL;
1905 goto err;
1906 }
1900 1907
1901 i->type = V4L2_INPUT_TYPE_CAMERA; 1908 i->type = V4L2_INPUT_TYPE_CAMERA;
1902 i->audioset = 1; 1909 i->audioset = 1;
@@ -1919,10 +1926,12 @@ static int bttv_enum_input(struct file *file, void *priv,
1919 i->status |= V4L2_IN_ST_NO_H_LOCK; 1926 i->status |= V4L2_IN_ST_NO_H_LOCK;
1920 } 1927 }
1921 1928
1922 for (n = 0; n < BTTV_TVNORMS; n++) 1929 i->std = BTTV_NORMS;
1923 i->std |= bttv_tvnorms[n].v4l2_id;
1924 1930
1925 return 0; 1931err:
1932 mutex_unlock(&btv->lock);
1933
1934 return rc;
1926} 1935}
1927 1936
1928static int bttv_g_input(struct file *file, void *priv, unsigned int *i) 1937static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
@@ -1930,7 +1939,10 @@ static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1930 struct bttv_fh *fh = priv; 1939 struct bttv_fh *fh = priv;
1931 struct bttv *btv = fh->btv; 1940 struct bttv *btv = fh->btv;
1932 1941
1942 mutex_lock(&btv->lock);
1933 *i = btv->input; 1943 *i = btv->input;
1944 mutex_unlock(&btv->lock);
1945
1934 return 0; 1946 return 0;
1935} 1947}
1936 1948
@@ -1941,15 +1953,19 @@ static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1941 1953
1942 int err; 1954 int err;
1943 1955
1956 mutex_lock(&btv->lock);
1944 err = v4l2_prio_check(&btv->prio, fh->prio); 1957 err = v4l2_prio_check(&btv->prio, fh->prio);
1945 if (0 != err) 1958 if (unlikely(err))
1946 return err; 1959 goto err;
1947 1960
1948 if (i > bttv_tvcards[btv->c.type].video_inputs) 1961 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1949 return -EINVAL; 1962 err = -EINVAL;
1963 goto err;
1964 }
1950 1965
1951 mutex_lock(&btv->lock);
1952 set_input(btv, i, btv->tvnorm); 1966 set_input(btv, i, btv->tvnorm);
1967
1968err:
1953 mutex_unlock(&btv->lock); 1969 mutex_unlock(&btv->lock);
1954 return 0; 1970 return 0;
1955} 1971}
@@ -1961,22 +1977,25 @@ static int bttv_s_tuner(struct file *file, void *priv,
1961 struct bttv *btv = fh->btv; 1977 struct bttv *btv = fh->btv;
1962 int err; 1978 int err;
1963 1979
1964 err = v4l2_prio_check(&btv->prio, fh->prio); 1980 if (unlikely(0 != t->index))
1965 if (0 != err)
1966 return err;
1967
1968 if (btv->tuner_type == TUNER_ABSENT)
1969 return -EINVAL;
1970
1971 if (0 != t->index)
1972 return -EINVAL; 1981 return -EINVAL;
1973 1982
1974 mutex_lock(&btv->lock); 1983 mutex_lock(&btv->lock);
1984 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1985 err = -EINVAL;
1986 goto err;
1987 }
1988
1989 err = v4l2_prio_check(&btv->prio, fh->prio);
1990 if (unlikely(err))
1991 goto err;
1992
1975 bttv_call_all(btv, tuner, s_tuner, t); 1993 bttv_call_all(btv, tuner, s_tuner, t);
1976 1994
1977 if (btv->audio_mode_gpio) 1995 if (btv->audio_mode_gpio)
1978 btv->audio_mode_gpio(btv, t, 1); 1996 btv->audio_mode_gpio(btv, t, 1);
1979 1997
1998err:
1980 mutex_unlock(&btv->lock); 1999 mutex_unlock(&btv->lock);
1981 2000
1982 return 0; 2001 return 0;
@@ -1988,8 +2007,10 @@ static int bttv_g_frequency(struct file *file, void *priv,
1988 struct bttv_fh *fh = priv; 2007 struct bttv_fh *fh = priv;
1989 struct bttv *btv = fh->btv; 2008 struct bttv *btv = fh->btv;
1990 2009
2010 mutex_lock(&btv->lock);
1991 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 2011 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1992 f->frequency = btv->freq; 2012 f->frequency = btv->freq;
2013 mutex_unlock(&btv->lock);
1993 2014
1994 return 0; 2015 return 0;
1995} 2016}
@@ -2001,21 +2022,26 @@ static int bttv_s_frequency(struct file *file, void *priv,
2001 struct bttv *btv = fh->btv; 2022 struct bttv *btv = fh->btv;
2002 int err; 2023 int err;
2003 2024
2004 err = v4l2_prio_check(&btv->prio, fh->prio);
2005 if (0 != err)
2006 return err;
2007
2008 if (unlikely(f->tuner != 0)) 2025 if (unlikely(f->tuner != 0))
2009 return -EINVAL; 2026 return -EINVAL;
2010 if (unlikely(f->type != (btv->radio_user 2027
2011 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
2012 return -EINVAL;
2013 mutex_lock(&btv->lock); 2028 mutex_lock(&btv->lock);
2029 err = v4l2_prio_check(&btv->prio, fh->prio);
2030 if (unlikely(err))
2031 goto err;
2032
2033 if (unlikely(f->type != (btv->radio_user
2034 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2035 err = -EINVAL;
2036 goto err;
2037 }
2014 btv->freq = f->frequency; 2038 btv->freq = f->frequency;
2015 bttv_call_all(btv, tuner, s_frequency, f); 2039 bttv_call_all(btv, tuner, s_frequency, f);
2016 if (btv->has_matchbox && btv->radio_user) 2040 if (btv->has_matchbox && btv->radio_user)
2017 tea5757_set_freq(btv, btv->freq); 2041 tea5757_set_freq(btv, btv->freq);
2042err:
2018 mutex_unlock(&btv->lock); 2043 mutex_unlock(&btv->lock);
2044
2019 return 0; 2045 return 0;
2020} 2046}
2021 2047
@@ -2124,7 +2150,7 @@ bttv_crop_adjust (struct bttv_crop * c,
2124 also adjust the current cropping parameters to get closer to the 2150 also adjust the current cropping parameters to get closer to the
2125 desired image size. */ 2151 desired image size. */
2126static int 2152static int
2127limit_scaled_size (struct bttv_fh * fh, 2153limit_scaled_size_lock (struct bttv_fh * fh,
2128 __s32 * width, 2154 __s32 * width,
2129 __s32 * height, 2155 __s32 * height,
2130 enum v4l2_field field, 2156 enum v4l2_field field,
@@ -2238,7 +2264,7 @@ limit_scaled_size (struct bttv_fh * fh,
2238 may also adjust the current cropping parameters to get closer 2264 may also adjust the current cropping parameters to get closer
2239 to the desired window size. */ 2265 to the desired window size. */
2240static int 2266static int
2241verify_window (struct bttv_fh * fh, 2267verify_window_lock (struct bttv_fh * fh,
2242 struct v4l2_window * win, 2268 struct v4l2_window * win,
2243 int adjust_size, 2269 int adjust_size,
2244 int adjust_crop) 2270 int adjust_crop)
@@ -2257,7 +2283,9 @@ verify_window (struct bttv_fh * fh,
2257 if (V4L2_FIELD_ANY == field) { 2283 if (V4L2_FIELD_ANY == field) {
2258 __s32 height2; 2284 __s32 height2;
2259 2285
2286 mutex_lock(&fh->btv->lock);
2260 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1; 2287 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2288 mutex_unlock(&fh->btv->lock);
2261 field = (win->w.height > height2) 2289 field = (win->w.height > height2)
2262 ? V4L2_FIELD_INTERLACED 2290 ? V4L2_FIELD_INTERLACED
2263 : V4L2_FIELD_TOP; 2291 : V4L2_FIELD_TOP;
@@ -2292,7 +2320,7 @@ verify_window (struct bttv_fh * fh,
2292 win->w.width -= win->w.left & ~width_mask; 2320 win->w.width -= win->w.left & ~width_mask;
2293 win->w.left = (win->w.left - width_mask - 1) & width_mask; 2321 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2294 2322
2295 rc = limit_scaled_size(fh, &win->w.width, &win->w.height, 2323 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2296 field, width_mask, 2324 field, width_mask,
2297 /* width_bias: round down */ 0, 2325 /* width_bias: round down */ 0,
2298 adjust_size, adjust_crop); 2326 adjust_size, adjust_crop);
@@ -2303,7 +2331,7 @@ verify_window (struct bttv_fh * fh,
2303 return 0; 2331 return 0;
2304} 2332}
2305 2333
2306static int setup_window(struct bttv_fh *fh, struct bttv *btv, 2334static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2307 struct v4l2_window *win, int fixup) 2335 struct v4l2_window *win, int fixup)
2308{ 2336{
2309 struct v4l2_clip *clips = NULL; 2337 struct v4l2_clip *clips = NULL;
@@ -2313,7 +2341,7 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2313 return -EINVAL; 2341 return -EINVAL;
2314 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED)) 2342 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2315 return -EINVAL; 2343 return -EINVAL;
2316 retval = verify_window(fh, win, 2344 retval = verify_window_lock(fh, win,
2317 /* adjust_size */ fixup, 2345 /* adjust_size */ fixup,
2318 /* adjust_crop */ fixup); 2346 /* adjust_crop */ fixup);
2319 if (0 != retval) 2347 if (0 != retval)
@@ -2332,6 +2360,8 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2332 return -EFAULT; 2360 return -EFAULT;
2333 } 2361 }
2334 } 2362 }
2363
2364 mutex_lock(&fh->cap.vb_lock);
2335 /* clip against screen */ 2365 /* clip against screen */
2336 if (NULL != btv->fbuf.base) 2366 if (NULL != btv->fbuf.base)
2337 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height, 2367 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
@@ -2354,7 +2384,6 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2354 BUG(); 2384 BUG();
2355 } 2385 }
2356 2386
2357 mutex_lock(&fh->cap.vb_lock);
2358 kfree(fh->ov.clips); 2387 kfree(fh->ov.clips);
2359 fh->ov.clips = clips; 2388 fh->ov.clips = clips;
2360 fh->ov.nclips = n; 2389 fh->ov.nclips = n;
@@ -2362,6 +2391,14 @@ static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2362 fh->ov.w = win->w; 2391 fh->ov.w = win->w;
2363 fh->ov.field = win->field; 2392 fh->ov.field = win->field;
2364 fh->ov.setup_ok = 1; 2393 fh->ov.setup_ok = 1;
2394
2395 /*
2396 * FIXME: btv is protected by btv->lock mutex, while btv->init
2397 * is protected by fh->cap.vb_lock. This seems to open the
2398 * possibility for some race situations. Maybe the better would
2399 * be to unify those locks or to use another way to store the
2400 * init values that will be consumed by videobuf callbacks
2401 */
2365 btv->init.ov.w.width = win->w.width; 2402 btv->init.ov.w.width = win->w.width;
2366 btv->init.ov.w.height = win->w.height; 2403 btv->init.ov.w.height = win->w.height;
2367 btv->init.ov.field = win->field; 2404 btv->init.ov.field = win->field;
@@ -2490,7 +2527,9 @@ static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2490 if (V4L2_FIELD_ANY == field) { 2527 if (V4L2_FIELD_ANY == field) {
2491 __s32 height2; 2528 __s32 height2;
2492 2529
2530 mutex_lock(&btv->lock);
2493 height2 = btv->crop[!!fh->do_crop].rect.height >> 1; 2531 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2532 mutex_unlock(&btv->lock);
2494 field = (f->fmt.pix.height > height2) 2533 field = (f->fmt.pix.height > height2)
2495 ? V4L2_FIELD_INTERLACED 2534 ? V4L2_FIELD_INTERLACED
2496 : V4L2_FIELD_BOTTOM; 2535 : V4L2_FIELD_BOTTOM;
@@ -2516,7 +2555,7 @@ static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2516 width = f->fmt.pix.width; 2555 width = f->fmt.pix.width;
2517 height = f->fmt.pix.height; 2556 height = f->fmt.pix.height;
2518 2557
2519 rc = limit_scaled_size(fh, &width, &height, field, 2558 rc = limit_scaled_size_lock(fh, &width, &height, field,
2520 /* width_mask: 4 pixels */ ~3, 2559 /* width_mask: 4 pixels */ ~3,
2521 /* width_bias: nearest */ 2, 2560 /* width_bias: nearest */ 2,
2522 /* adjust_size */ 1, 2561 /* adjust_size */ 1,
@@ -2536,7 +2575,7 @@ static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2536{ 2575{
2537 struct bttv_fh *fh = priv; 2576 struct bttv_fh *fh = priv;
2538 2577
2539 return verify_window(fh, &f->fmt.win, 2578 return verify_window_lock(fh, &f->fmt.win,
2540 /* adjust_size */ 1, 2579 /* adjust_size */ 1,
2541 /* adjust_crop */ 0); 2580 /* adjust_crop */ 0);
2542} 2581}
@@ -2563,7 +2602,7 @@ static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2563 height = f->fmt.pix.height; 2602 height = f->fmt.pix.height;
2564 field = f->fmt.pix.field; 2603 field = f->fmt.pix.field;
2565 2604
2566 retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field, 2605 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2567 /* width_mask: 4 pixels */ ~3, 2606 /* width_mask: 4 pixels */ ~3,
2568 /* width_bias: nearest */ 2, 2607 /* width_bias: nearest */ 2,
2569 /* adjust_size */ 1, 2608 /* adjust_size */ 1,
@@ -2601,7 +2640,7 @@ static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2601 return -EINVAL; 2640 return -EINVAL;
2602 } 2641 }
2603 2642
2604 return setup_window(fh, btv, &f->fmt.win, 1); 2643 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2605} 2644}
2606 2645
2607#ifdef CONFIG_VIDEO_V4L1_COMPAT 2646#ifdef CONFIG_VIDEO_V4L1_COMPAT
@@ -2651,11 +2690,15 @@ static int bttv_querycap(struct file *file, void *priv,
2651 V4L2_CAP_VBI_CAPTURE | 2690 V4L2_CAP_VBI_CAPTURE |
2652 V4L2_CAP_READWRITE | 2691 V4L2_CAP_READWRITE |
2653 V4L2_CAP_STREAMING; 2692 V4L2_CAP_STREAMING;
2654 if (btv->has_saa6588)
2655 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2656 if (no_overlay <= 0) 2693 if (no_overlay <= 0)
2657 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY; 2694 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2658 2695
2696 /*
2697 * No need to lock here: those vars are initialized during board
2698 * probe and remains untouched during the rest of the driver lifecycle
2699 */
2700 if (btv->has_saa6588)
2701 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2659 if (btv->tuner_type != TUNER_ABSENT) 2702 if (btv->tuner_type != TUNER_ABSENT)
2660 cap->capabilities |= V4L2_CAP_TUNER; 2703 cap->capabilities |= V4L2_CAP_TUNER;
2661 return 0; 2704 return 0;
@@ -2730,19 +2773,25 @@ static int bttv_overlay(struct file *file, void *f, unsigned int on)
2730 struct bttv_fh *fh = f; 2773 struct bttv_fh *fh = f;
2731 struct bttv *btv = fh->btv; 2774 struct bttv *btv = fh->btv;
2732 struct bttv_buffer *new; 2775 struct bttv_buffer *new;
2733 int retval; 2776 int retval = 0;
2734 2777
2735 if (on) { 2778 if (on) {
2779 mutex_lock(&fh->cap.vb_lock);
2736 /* verify args */ 2780 /* verify args */
2737 if (NULL == btv->fbuf.base) 2781 if (unlikely(!btv->fbuf.base)) {
2782 mutex_unlock(&fh->cap.vb_lock);
2738 return -EINVAL; 2783 return -EINVAL;
2739 if (!fh->ov.setup_ok) { 2784 }
2785 if (unlikely(!fh->ov.setup_ok)) {
2740 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr); 2786 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2741 return -EINVAL; 2787 retval = -EINVAL;
2742 } 2788 }
2789 if (retval)
2790 return retval;
2791 mutex_unlock(&fh->cap.vb_lock);
2743 } 2792 }
2744 2793
2745 if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY)) 2794 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2746 return -EBUSY; 2795 return -EBUSY;
2747 2796
2748 mutex_lock(&fh->cap.vb_lock); 2797 mutex_lock(&fh->cap.vb_lock);
@@ -2785,7 +2834,7 @@ static int bttv_s_fbuf(struct file *file, void *f,
2785 __s32 width = fb->fmt.width; 2834 __s32 width = fb->fmt.width;
2786 __s32 height = fb->fmt.height; 2835 __s32 height = fb->fmt.height;
2787 2836
2788 retval = limit_scaled_size(fh, &width, &height, 2837 retval = limit_scaled_size_lock(fh, &width, &height,
2789 V4L2_FIELD_INTERLACED, 2838 V4L2_FIELD_INTERLACED,
2790 /* width_mask */ ~3, 2839 /* width_mask */ ~3,
2791 /* width_bias */ 2, 2840 /* width_bias */ 2,
@@ -2852,7 +2901,7 @@ static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2852 struct bttv *btv = fh->btv; 2901 struct bttv *btv = fh->btv;
2853 int res = bttv_resource(fh); 2902 int res = bttv_resource(fh);
2854 2903
2855 if (!check_alloc_btres(btv, fh, res)) 2904 if (!check_alloc_btres_lock(btv, fh, res))
2856 return -EBUSY; 2905 return -EBUSY;
2857 2906
2858 return videobuf_qbuf(bttv_queue(fh), b); 2907 return videobuf_qbuf(bttv_queue(fh), b);
@@ -2872,7 +2921,7 @@ static int bttv_streamon(struct file *file, void *priv,
2872 struct bttv *btv = fh->btv; 2921 struct bttv *btv = fh->btv;
2873 int res = bttv_resource(fh); 2922 int res = bttv_resource(fh);
2874 2923
2875 if (!check_alloc_btres(btv, fh, res)) 2924 if (!check_alloc_btres_lock(btv, fh, res))
2876 return -EBUSY; 2925 return -EBUSY;
2877 return videobuf_streamon(bttv_queue(fh)); 2926 return videobuf_streamon(bttv_queue(fh));
2878} 2927}
@@ -2890,7 +2939,7 @@ static int bttv_streamoff(struct file *file, void *priv,
2890 retval = videobuf_streamoff(bttv_queue(fh)); 2939 retval = videobuf_streamoff(bttv_queue(fh));
2891 if (retval < 0) 2940 if (retval < 0)
2892 return retval; 2941 return retval;
2893 free_btres(btv, fh, res); 2942 free_btres_lock(btv, fh, res);
2894 return 0; 2943 return 0;
2895} 2944}
2896 2945
@@ -2907,6 +2956,7 @@ static int bttv_queryctrl(struct file *file, void *priv,
2907 c->id >= V4L2_CID_PRIVATE_LASTP1)) 2956 c->id >= V4L2_CID_PRIVATE_LASTP1))
2908 return -EINVAL; 2957 return -EINVAL;
2909 2958
2959 mutex_lock(&btv->lock);
2910 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME)) 2960 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2911 *c = no_ctl; 2961 *c = no_ctl;
2912 else { 2962 else {
@@ -2914,6 +2964,7 @@ static int bttv_queryctrl(struct file *file, void *priv,
2914 2964
2915 *c = (NULL != ctrl) ? *ctrl : no_ctl; 2965 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2916 } 2966 }
2967 mutex_unlock(&btv->lock);
2917 2968
2918 return 0; 2969 return 0;
2919} 2970}
@@ -2924,8 +2975,11 @@ static int bttv_g_parm(struct file *file, void *f,
2924 struct bttv_fh *fh = f; 2975 struct bttv_fh *fh = f;
2925 struct bttv *btv = fh->btv; 2976 struct bttv *btv = fh->btv;
2926 2977
2978 mutex_lock(&btv->lock);
2927 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id, 2979 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2928 &parm->parm.capture.timeperframe); 2980 &parm->parm.capture.timeperframe);
2981 mutex_unlock(&btv->lock);
2982
2929 return 0; 2983 return 0;
2930} 2984}
2931 2985
@@ -2961,7 +3015,9 @@ static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2961 struct bttv_fh *fh = f; 3015 struct bttv_fh *fh = f;
2962 struct bttv *btv = fh->btv; 3016 struct bttv *btv = fh->btv;
2963 3017
3018 mutex_lock(&btv->lock);
2964 *p = v4l2_prio_max(&btv->prio); 3019 *p = v4l2_prio_max(&btv->prio);
3020 mutex_unlock(&btv->lock);
2965 3021
2966 return 0; 3022 return 0;
2967} 3023}
@@ -2971,8 +3027,13 @@ static int bttv_s_priority(struct file *file, void *f,
2971{ 3027{
2972 struct bttv_fh *fh = f; 3028 struct bttv_fh *fh = f;
2973 struct bttv *btv = fh->btv; 3029 struct bttv *btv = fh->btv;
3030 int rc;
2974 3031
2975 return v4l2_prio_change(&btv->prio, &fh->prio, prio); 3032 mutex_lock(&btv->lock);
3033 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
3034 mutex_unlock(&btv->lock);
3035
3036 return rc;
2976} 3037}
2977 3038
2978static int bttv_cropcap(struct file *file, void *priv, 3039static int bttv_cropcap(struct file *file, void *priv,
@@ -2985,7 +3046,9 @@ static int bttv_cropcap(struct file *file, void *priv,
2985 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 3046 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2986 return -EINVAL; 3047 return -EINVAL;
2987 3048
3049 mutex_lock(&btv->lock);
2988 *cap = bttv_tvnorms[btv->tvnorm].cropcap; 3050 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3051 mutex_unlock(&btv->lock);
2989 3052
2990 return 0; 3053 return 0;
2991} 3054}
@@ -3003,7 +3066,9 @@ static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3003 inconsistent with fh->width or fh->height and apps 3066 inconsistent with fh->width or fh->height and apps
3004 do not expect a change here. */ 3067 do not expect a change here. */
3005 3068
3069 mutex_lock(&btv->lock);
3006 crop->c = btv->crop[!!fh->do_crop].rect; 3070 crop->c = btv->crop[!!fh->do_crop].rect;
3071 mutex_unlock(&btv->lock);
3007 3072
3008 return 0; 3073 return 0;
3009} 3074}
@@ -3024,14 +3089,15 @@ static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3024 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY) 3089 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3025 return -EINVAL; 3090 return -EINVAL;
3026 3091
3027 retval = v4l2_prio_check(&btv->prio, fh->prio);
3028 if (0 != retval)
3029 return retval;
3030
3031 /* Make sure tvnorm, vbi_end and the current cropping 3092 /* Make sure tvnorm, vbi_end and the current cropping
3032 parameters remain consistent until we're done. Note 3093 parameters remain consistent until we're done. Note
3033 read() may change vbi_end in check_alloc_btres(). */ 3094 read() may change vbi_end in check_alloc_btres_lock(). */
3034 mutex_lock(&btv->lock); 3095 mutex_lock(&btv->lock);
3096 retval = v4l2_prio_check(&btv->prio, fh->prio);
3097 if (0 != retval) {
3098 mutex_unlock(&btv->lock);
3099 return retval;
3100 }
3035 3101
3036 retval = -EBUSY; 3102 retval = -EBUSY;
3037 3103
@@ -3128,17 +3194,17 @@ static ssize_t bttv_read(struct file *file, char __user *data,
3128 3194
3129 switch (fh->type) { 3195 switch (fh->type) {
3130 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 3196 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3131 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) { 3197 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3132 /* VIDEO_READ in use by another fh, 3198 /* VIDEO_READ in use by another fh,
3133 or VIDEO_STREAM by any fh. */ 3199 or VIDEO_STREAM by any fh. */
3134 return -EBUSY; 3200 return -EBUSY;
3135 } 3201 }
3136 retval = videobuf_read_one(&fh->cap, data, count, ppos, 3202 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3137 file->f_flags & O_NONBLOCK); 3203 file->f_flags & O_NONBLOCK);
3138 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ); 3204 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3139 break; 3205 break;
3140 case V4L2_BUF_TYPE_VBI_CAPTURE: 3206 case V4L2_BUF_TYPE_VBI_CAPTURE:
3141 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI)) 3207 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3142 return -EBUSY; 3208 return -EBUSY;
3143 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1, 3209 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3144 file->f_flags & O_NONBLOCK); 3210 file->f_flags & O_NONBLOCK);
@@ -3157,20 +3223,19 @@ static unsigned int bttv_poll(struct file *file, poll_table *wait)
3157 unsigned int rc = POLLERR; 3223 unsigned int rc = POLLERR;
3158 3224
3159 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { 3225 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3160 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI)) 3226 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3161 return POLLERR; 3227 return POLLERR;
3162 return videobuf_poll_stream(file, &fh->vbi, wait); 3228 return videobuf_poll_stream(file, &fh->vbi, wait);
3163 } 3229 }
3164 3230
3231 mutex_lock(&fh->cap.vb_lock);
3165 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) { 3232 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3166 mutex_lock(&fh->cap.vb_lock);
3167 /* streaming capture */ 3233 /* streaming capture */
3168 if (list_empty(&fh->cap.stream)) 3234 if (list_empty(&fh->cap.stream))
3169 goto err; 3235 goto err;
3170 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream); 3236 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3171 } else { 3237 } else {
3172 /* read() capture */ 3238 /* read() capture */
3173 mutex_lock(&fh->cap.vb_lock);
3174 if (NULL == fh->cap.read_buf) { 3239 if (NULL == fh->cap.read_buf) {
3175 /* need to capture a new frame */ 3240 /* need to capture a new frame */
3176 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) 3241 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
@@ -3188,7 +3253,6 @@ static unsigned int bttv_poll(struct file *file, poll_table *wait)
3188 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf); 3253 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3189 fh->cap.read_off = 0; 3254 fh->cap.read_off = 0;
3190 } 3255 }
3191 mutex_unlock(&fh->cap.vb_lock);
3192 buf = (struct bttv_buffer*)fh->cap.read_buf; 3256 buf = (struct bttv_buffer*)fh->cap.read_buf;
3193 } 3257 }
3194 3258
@@ -3221,21 +3285,32 @@ static int bttv_open(struct file *file)
3221 return -ENODEV; 3285 return -ENODEV;
3222 } 3286 }
3223 3287
3224 lock_kernel();
3225
3226 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n", 3288 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3227 btv->c.nr,v4l2_type_names[type]); 3289 btv->c.nr,v4l2_type_names[type]);
3228 3290
3229 /* allocate per filehandle data */ 3291 /* allocate per filehandle data */
3230 fh = kmalloc(sizeof(*fh),GFP_KERNEL); 3292 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3231 if (NULL == fh) { 3293 if (unlikely(!fh))
3232 unlock_kernel();
3233 return -ENOMEM; 3294 return -ENOMEM;
3234 }
3235 file->private_data = fh; 3295 file->private_data = fh;
3296
3297 /*
3298 * btv is protected by btv->lock mutex, while btv->init and other
3299 * streaming vars are protected by fh->cap.vb_lock. We need to take
3300 * care of both locks to avoid troubles. However, vb_lock is used also
3301 * inside videobuf, without calling buf->lock. So, it is a very bad
3302 * idea to hold both locks at the same time.
3303 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3304 * with the rest of init, holding btv->lock.
3305 */
3306 mutex_lock(&fh->cap.vb_lock);
3236 *fh = btv->init; 3307 *fh = btv->init;
3308 mutex_unlock(&fh->cap.vb_lock);
3309
3237 fh->type = type; 3310 fh->type = type;
3238 fh->ov.setup_ok = 0; 3311 fh->ov.setup_ok = 0;
3312
3313 mutex_lock(&btv->lock);
3239 v4l2_prio_open(&btv->prio, &fh->prio); 3314 v4l2_prio_open(&btv->prio, &fh->prio);
3240 3315
3241 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops, 3316 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
@@ -3243,13 +3318,13 @@ static int bttv_open(struct file *file)
3243 V4L2_BUF_TYPE_VIDEO_CAPTURE, 3318 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3244 V4L2_FIELD_INTERLACED, 3319 V4L2_FIELD_INTERLACED,
3245 sizeof(struct bttv_buffer), 3320 sizeof(struct bttv_buffer),
3246 fh); 3321 fh, NULL);
3247 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops, 3322 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3248 &btv->c.pci->dev, &btv->s_lock, 3323 &btv->c.pci->dev, &btv->s_lock,
3249 V4L2_BUF_TYPE_VBI_CAPTURE, 3324 V4L2_BUF_TYPE_VBI_CAPTURE,
3250 V4L2_FIELD_SEQ_TB, 3325 V4L2_FIELD_SEQ_TB,
3251 sizeof(struct bttv_buffer), 3326 sizeof(struct bttv_buffer),
3252 fh); 3327 fh, NULL);
3253 set_tvnorm(btv,btv->tvnorm); 3328 set_tvnorm(btv,btv->tvnorm);
3254 set_input(btv, btv->input, btv->tvnorm); 3329 set_input(btv, btv->input, btv->tvnorm);
3255 3330
@@ -3272,7 +3347,7 @@ static int bttv_open(struct file *file)
3272 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm); 3347 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3273 3348
3274 bttv_field_count(btv); 3349 bttv_field_count(btv);
3275 unlock_kernel(); 3350 mutex_unlock(&btv->lock);
3276 return 0; 3351 return 0;
3277} 3352}
3278 3353
@@ -3281,6 +3356,7 @@ static int bttv_release(struct file *file)
3281 struct bttv_fh *fh = file->private_data; 3356 struct bttv_fh *fh = file->private_data;
3282 struct bttv *btv = fh->btv; 3357 struct bttv *btv = fh->btv;
3283 3358
3359 mutex_lock(&btv->lock);
3284 /* turn off overlay */ 3360 /* turn off overlay */
3285 if (check_btres(fh, RESOURCE_OVERLAY)) 3361 if (check_btres(fh, RESOURCE_OVERLAY))
3286 bttv_switch_overlay(btv,fh,NULL); 3362 bttv_switch_overlay(btv,fh,NULL);
@@ -3288,25 +3364,32 @@ static int bttv_release(struct file *file)
3288 /* stop video capture */ 3364 /* stop video capture */
3289 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) { 3365 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3290 videobuf_streamoff(&fh->cap); 3366 videobuf_streamoff(&fh->cap);
3291 free_btres(btv,fh,RESOURCE_VIDEO_STREAM); 3367 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3292 } 3368 }
3293 if (fh->cap.read_buf) { 3369 if (fh->cap.read_buf) {
3294 buffer_release(&fh->cap,fh->cap.read_buf); 3370 buffer_release(&fh->cap,fh->cap.read_buf);
3295 kfree(fh->cap.read_buf); 3371 kfree(fh->cap.read_buf);
3296 } 3372 }
3297 if (check_btres(fh, RESOURCE_VIDEO_READ)) { 3373 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3298 free_btres(btv, fh, RESOURCE_VIDEO_READ); 3374 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3299 } 3375 }
3300 3376
3301 /* stop vbi capture */ 3377 /* stop vbi capture */
3302 if (check_btres(fh, RESOURCE_VBI)) { 3378 if (check_btres(fh, RESOURCE_VBI)) {
3303 videobuf_stop(&fh->vbi); 3379 videobuf_stop(&fh->vbi);
3304 free_btres(btv,fh,RESOURCE_VBI); 3380 free_btres_lock(btv,fh,RESOURCE_VBI);
3305 } 3381 }
3306 3382
3307 /* free stuff */ 3383 /* free stuff */
3384
3385 /*
3386 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3387 * otherwise we may have dead lock conditions
3388 */
3389 mutex_unlock(&btv->lock);
3308 videobuf_mmap_free(&fh->cap); 3390 videobuf_mmap_free(&fh->cap);
3309 videobuf_mmap_free(&fh->vbi); 3391 videobuf_mmap_free(&fh->vbi);
3392 mutex_lock(&btv->lock);
3310 v4l2_prio_close(&btv->prio, fh->prio); 3393 v4l2_prio_close(&btv->prio, fh->prio);
3311 file->private_data = NULL; 3394 file->private_data = NULL;
3312 kfree(fh); 3395 kfree(fh);
@@ -3316,6 +3399,7 @@ static int bttv_release(struct file *file)
3316 3399
3317 if (!btv->users) 3400 if (!btv->users)
3318 audio_mute(btv, 1); 3401 audio_mute(btv, 1);
3402 mutex_unlock(&btv->lock);
3319 3403
3320 return 0; 3404 return 0;
3321} 3405}
@@ -3333,13 +3417,13 @@ bttv_mmap(struct file *file, struct vm_area_struct *vma)
3333 3417
3334static const struct v4l2_file_operations bttv_fops = 3418static const struct v4l2_file_operations bttv_fops =
3335{ 3419{
3336 .owner = THIS_MODULE, 3420 .owner = THIS_MODULE,
3337 .open = bttv_open, 3421 .open = bttv_open,
3338 .release = bttv_release, 3422 .release = bttv_release,
3339 .ioctl = video_ioctl2, 3423 .unlocked_ioctl = video_ioctl2,
3340 .read = bttv_read, 3424 .read = bttv_read,
3341 .mmap = bttv_mmap, 3425 .mmap = bttv_mmap,
3342 .poll = bttv_poll, 3426 .poll = bttv_poll,
3343}; 3427};
3344 3428
3345static const struct v4l2_ioctl_ops bttv_ioctl_ops = { 3429static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
@@ -3412,21 +3496,19 @@ static int radio_open(struct file *file)
3412 3496
3413 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev)); 3497 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
3414 3498
3415 lock_kernel();
3416
3417 dprintk("bttv%d: open called (radio)\n",btv->c.nr); 3499 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3418 3500
3419 /* allocate per filehandle data */ 3501 /* allocate per filehandle data */
3420 fh = kmalloc(sizeof(*fh), GFP_KERNEL); 3502 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3421 if (NULL == fh) { 3503 if (unlikely(!fh))
3422 unlock_kernel();
3423 return -ENOMEM; 3504 return -ENOMEM;
3424 }
3425 file->private_data = fh; 3505 file->private_data = fh;
3506 mutex_lock(&fh->cap.vb_lock);
3426 *fh = btv->init; 3507 *fh = btv->init;
3427 v4l2_prio_open(&btv->prio, &fh->prio); 3508 mutex_unlock(&fh->cap.vb_lock);
3428 3509
3429 mutex_lock(&btv->lock); 3510 mutex_lock(&btv->lock);
3511 v4l2_prio_open(&btv->prio, &fh->prio);
3430 3512
3431 btv->radio_user++; 3513 btv->radio_user++;
3432 3514
@@ -3434,7 +3516,6 @@ static int radio_open(struct file *file)
3434 audio_input(btv,TVAUDIO_INPUT_RADIO); 3516 audio_input(btv,TVAUDIO_INPUT_RADIO);
3435 3517
3436 mutex_unlock(&btv->lock); 3518 mutex_unlock(&btv->lock);
3437 unlock_kernel();
3438 return 0; 3519 return 0;
3439} 3520}
3440 3521
@@ -3444,6 +3525,7 @@ static int radio_release(struct file *file)
3444 struct bttv *btv = fh->btv; 3525 struct bttv *btv = fh->btv;
3445 struct rds_command cmd; 3526 struct rds_command cmd;
3446 3527
3528 mutex_lock(&btv->lock);
3447 v4l2_prio_close(&btv->prio, fh->prio); 3529 v4l2_prio_close(&btv->prio, fh->prio);
3448 file->private_data = NULL; 3530 file->private_data = NULL;
3449 kfree(fh); 3531 kfree(fh);
@@ -3451,6 +3533,7 @@ static int radio_release(struct file *file)
3451 btv->radio_user--; 3533 btv->radio_user--;
3452 3534
3453 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd); 3535 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
3536 mutex_unlock(&btv->lock);
3454 3537
3455 return 0; 3538 return 0;
3456} 3539}
diff --git a/drivers/media/video/bt8xx/bttv-i2c.c b/drivers/media/video/bt8xx/bttv-i2c.c
index 685d6597ee79..d49b675045fe 100644
--- a/drivers/media/video/bt8xx/bttv-i2c.c
+++ b/drivers/media/video/bt8xx/bttv-i2c.c
@@ -121,9 +121,8 @@ bttv_i2c_wait_done(struct bttv *btv)
121 121
122 /* timeout */ 122 /* timeout */
123 if (wait_event_interruptible_timeout(btv->i2c_queue, 123 if (wait_event_interruptible_timeout(btv->i2c_queue,
124 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS) 124 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
125 125 rc = -EIO;
126 rc = -EIO;
127 126
128 if (btv->i2c_done & BT848_INT_RACK) 127 if (btv->i2c_done & BT848_INT_RACK)
129 rc = 1; 128 rc = 1;
@@ -390,41 +389,3 @@ int __devinit init_bttv_i2c(struct bttv *btv)
390 389
391 return btv->i2c_rc; 390 return btv->i2c_rc;
392} 391}
393
394/* Instantiate the I2C IR receiver device, if present */
395void __devinit init_bttv_i2c_ir(struct bttv *btv)
396{
397 if (0 == btv->i2c_rc) {
398 struct i2c_board_info info;
399 /* The external IR receiver is at i2c address 0x34 (0x35 for
400 reads). Future Hauppauge cards will have an internal
401 receiver at 0x30 (0x31 for reads). In theory, both can be
402 fitted, and Hauppauge suggest an external overrides an
403 internal.
404
405 That's why we probe 0x1a (~0x34) first. CB
406 */
407 const unsigned short addr_list[] = {
408 0x1a, 0x18, 0x4b, 0x64, 0x30, 0x71,
409 I2C_CLIENT_END
410 };
411
412 memset(&info, 0, sizeof(struct i2c_board_info));
413 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
414 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
415 }
416}
417
418int __devexit fini_bttv_i2c(struct bttv *btv)
419{
420 if (0 != btv->i2c_rc)
421 return 0;
422
423 return i2c_del_adapter(&btv->c.i2c_adap);
424}
425
426/*
427 * Local variables:
428 * c-basic-offset: 8
429 * End:
430 */
diff --git a/drivers/media/video/bt8xx/bttv-input.c b/drivers/media/video/bt8xx/bttv-input.c
index f68717a4bdec..6bf05a7dc5f9 100644
--- a/drivers/media/video/bt8xx/bttv-input.c
+++ b/drivers/media/video/bt8xx/bttv-input.c
@@ -245,6 +245,83 @@ static void bttv_ir_stop(struct bttv *btv)
245 } 245 }
246} 246}
247 247
248/*
249 * Get_key functions used by I2C remotes
250 */
251
252static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
253{
254 unsigned char b;
255
256 /* poll IR chip */
257 if (1 != i2c_master_recv(ir->c, &b, 1)) {
258 dprintk(KERN_INFO DEVNAME ": read error\n");
259 return -EIO;
260 }
261
262 /* ignore 0xaa */
263 if (b==0xaa)
264 return 0;
265 dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
266
267 *ir_key = b;
268 *ir_raw = b;
269 return 1;
270}
271
272/* Instantiate the I2C IR receiver device, if present */
273void __devinit init_bttv_i2c_ir(struct bttv *btv)
274{
275 const unsigned short addr_list[] = {
276 0x1a, 0x18, 0x64, 0x30, 0x71,
277 I2C_CLIENT_END
278 };
279 struct i2c_board_info info;
280
281 if (0 != btv->i2c_rc)
282 return;
283
284 memset(&info, 0, sizeof(struct i2c_board_info));
285 memset(&btv->init_data, 0, sizeof(btv->init_data));
286 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
287
288 switch (btv->c.type) {
289 case BTTV_BOARD_PV951:
290 btv->init_data.name = "PV951";
291 btv->init_data.get_key = get_key_pv951;
292 btv->init_data.ir_codes = RC_MAP_PV951;
293 btv->init_data.type = IR_TYPE_OTHER;
294 info.addr = 0x4b;
295 break;
296 default:
297 /*
298 * The external IR receiver is at i2c address 0x34 (0x35 for
299 * reads). Future Hauppauge cards will have an internal
300 * receiver at 0x30 (0x31 for reads). In theory, both can be
301 * fitted, and Hauppauge suggest an external overrides an
302 * internal.
303 * That's why we probe 0x1a (~0x34) first. CB
304 */
305
306 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
307 return;
308 }
309
310 if (btv->init_data.name)
311 info.platform_data = &btv->init_data;
312 i2c_new_device(&btv->c.i2c_adap, &info);
313
314 return;
315}
316
317int __devexit fini_bttv_i2c(struct bttv *btv)
318{
319 if (0 != btv->i2c_rc)
320 return 0;
321
322 return i2c_del_adapter(&btv->c.i2c_adap);
323}
324
248int bttv_input_init(struct bttv *btv) 325int bttv_input_init(struct bttv *btv)
249{ 326{
250 struct card_ir *ir; 327 struct card_ir *ir;
@@ -420,10 +497,3 @@ void bttv_input_fini(struct bttv *btv)
420 kfree(btv->remote); 497 kfree(btv->remote);
421 btv->remote = NULL; 498 btv->remote = NULL;
422} 499}
423
424
425/*
426 * Local variables:
427 * c-basic-offset: 8
428 * End:
429 */
diff --git a/drivers/media/video/bt8xx/bttv-risc.c b/drivers/media/video/bt8xx/bttv-risc.c
index 0fa9f39f37a3..9b57d091da48 100644
--- a/drivers/media/video/bt8xx/bttv-risc.c
+++ b/drivers/media/video/bt8xx/bttv-risc.c
@@ -582,7 +582,7 @@ bttv_dma_free(struct videobuf_queue *q,struct bttv *btv, struct bttv_buffer *buf
582 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 582 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
583 583
584 BUG_ON(in_interrupt()); 584 BUG_ON(in_interrupt());
585 videobuf_waiton(&buf->vb,0,0); 585 videobuf_waiton(q, &buf->vb, 0, 0);
586 videobuf_dma_unmap(q->dev, dma); 586 videobuf_dma_unmap(q->dev, dma);
587 videobuf_dma_free(dma); 587 videobuf_dma_free(dma);
588 btcx_riscmem_free(btv->c.pci,&buf->bottom); 588 btcx_riscmem_free(btv->c.pci,&buf->bottom);
diff --git a/drivers/media/video/bt8xx/bttv.h b/drivers/media/video/bt8xx/bttv.h
index 3ec2402c6b4a..6fd2a8ebda1e 100644
--- a/drivers/media/video/bt8xx/bttv.h
+++ b/drivers/media/video/bt8xx/bttv.h
@@ -18,7 +18,6 @@
18#include <linux/i2c.h> 18#include <linux/i2c.h>
19#include <media/v4l2-device.h> 19#include <media/v4l2-device.h>
20#include <media/ir-common.h> 20#include <media/ir-common.h>
21#include <media/ir-kbd-i2c.h>
22#include <media/i2c-addr.h> 21#include <media/i2c-addr.h>
23#include <media/tuner.h> 22#include <media/tuner.h>
24 23
diff --git a/drivers/media/video/bt8xx/bttvp.h b/drivers/media/video/bt8xx/bttvp.h
index 6cccc2a17eee..d1e26a448ed2 100644
--- a/drivers/media/video/bt8xx/bttvp.h
+++ b/drivers/media/video/bt8xx/bttvp.h
@@ -42,7 +42,7 @@
42#include <media/videobuf-dma-sg.h> 42#include <media/videobuf-dma-sg.h>
43#include <media/tveeprom.h> 43#include <media/tveeprom.h>
44#include <media/ir-common.h> 44#include <media/ir-common.h>
45 45#include <media/ir-kbd-i2c.h>
46 46
47#include "bt848.h" 47#include "bt848.h"
48#include "bttv.h" 48#include "bttv.h"
@@ -271,6 +271,12 @@ int bttv_sub_del_devices(struct bttv_core *core);
271extern int no_overlay; 271extern int no_overlay;
272 272
273/* ---------------------------------------------------------- */ 273/* ---------------------------------------------------------- */
274/* bttv-input.c */
275
276extern void init_bttv_i2c_ir(struct bttv *btv);
277extern int fini_bttv_i2c(struct bttv *btv);
278
279/* ---------------------------------------------------------- */
274/* bttv-driver.c */ 280/* bttv-driver.c */
275 281
276/* insmod options */ 282/* insmod options */
@@ -279,8 +285,6 @@ extern unsigned int bttv_debug;
279extern unsigned int bttv_gpio; 285extern unsigned int bttv_gpio;
280extern void bttv_gpio_tracking(struct bttv *btv, char *comment); 286extern void bttv_gpio_tracking(struct bttv *btv, char *comment);
281extern int init_bttv_i2c(struct bttv *btv); 287extern int init_bttv_i2c(struct bttv *btv);
282extern void init_bttv_i2c_ir(struct bttv *btv);
283extern int fini_bttv_i2c(struct bttv *btv);
284 288
285#define bttv_printk if (bttv_verbose) printk 289#define bttv_printk if (bttv_verbose) printk
286#define dprintk if (bttv_debug >= 1) printk 290#define dprintk if (bttv_debug >= 1) printk
@@ -366,6 +370,9 @@ struct bttv {
366 int has_remote; 370 int has_remote;
367 struct card_ir *remote; 371 struct card_ir *remote;
368 372
373 /* I2C remote data */
374 struct IR_i2c_init_data init_data;
375
369 /* locking */ 376 /* locking */
370 spinlock_t s_lock; 377 spinlock_t s_lock;
371 struct mutex lock; 378 struct mutex lock;