]> Pileus Git - ~andy/linux/blobdiff - drivers/media/video/cx88/cx88-mpeg.c
V4L/DVB (7201): cx88-mpeg: Fix race condition in variable access
[~andy/linux] / drivers / media / video / cx88 / cx88-mpeg.c
index 0aedbeaf94cd7edfacc59c3271e3166f3dd75cb1..e357f415db06601ceab9333994d90aa4d51f4bfa 100644 (file)
@@ -609,13 +609,19 @@ static int cx8802_request_acquire(struct cx8802_driver *drv)
        struct cx88_core *core = drv->core;
 
        /* Fail a request for hardware if the device is busy. */
-       if (core->active_type_id != CX88_BOARD_NONE)
+       if (core->active_type_id != CX88_BOARD_NONE &&
+           core->active_type_id != drv->type_id)
                return -EBUSY;
 
        if (drv->advise_acquire)
        {
-               core->active_type_id = drv->type_id;
-               drv->advise_acquire(drv);
+               mutex_lock(&drv->core->lock);
+               core->active_ref++;
+               if (core->active_type_id == CX88_BOARD_NONE) {
+                       core->active_type_id = drv->type_id;
+                       drv->advise_acquire(drv);
+               }
+               mutex_unlock(&drv->core->lock);
 
                mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
        }
@@ -628,12 +634,14 @@ static int cx8802_request_release(struct cx8802_driver *drv)
 {
        struct cx88_core *core = drv->core;
 
-       if (drv->advise_release)
+       mutex_lock(&drv->core->lock);
+       if (drv->advise_release && --core->active_ref == 0)
        {
                drv->advise_release(drv);
                core->active_type_id = CX88_BOARD_NONE;
                mpeg_dbg(1,"%s() Post release GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
        }
+       mutex_unlock(&drv->core->lock);
 
        return 0;
 }