rk fb: from rk3368 fb only need to reserved 1 framebuffer
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / sr.c
index 4778e27071689593ec4fdffdd1107018c2f7ebe6..119d67f9c47edef7517e120f510169df4b105316 100644 (file)
@@ -45,6 +45,7 @@
 #include <linux/blkdev.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include <scsi/scsi.h>
@@ -79,6 +80,11 @@ static DEFINE_MUTEX(sr_mutex);
 static int sr_probe(struct device *);
 static int sr_remove(struct device *);
 static int sr_done(struct scsi_cmnd *);
+static int sr_runtime_suspend(struct device *dev);
+
+static struct dev_pm_ops sr_pm_ops = {
+       .runtime_suspend        = sr_runtime_suspend,
+};
 
 static struct scsi_driver sr_template = {
        .owner                  = THIS_MODULE,
@@ -86,6 +92,7 @@ static struct scsi_driver sr_template = {
                .name           = "sr",
                .probe          = sr_probe,
                .remove         = sr_remove,
+               .pm             = &sr_pm_ops,
        },
        .done                   = sr_done,
 };
@@ -131,6 +138,16 @@ static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
        return container_of(disk->private_data, struct scsi_cd, driver);
 }
 
+static int sr_runtime_suspend(struct device *dev)
+{
+       struct scsi_cd *cd = dev_get_drvdata(dev);
+
+       if (cd->media_present)
+               return -EBUSY;
+       else
+               return 0;
+}
+
 /*
  * The get and put routines for the struct scsi_cd.  Note this entity
  * has a scsi_device pointer and owns a reference to this.
@@ -146,7 +163,8 @@ static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
        kref_get(&cd->kref);
        if (scsi_device_get(cd->device))
                goto out_put;
-       goto out;
+       if (!scsi_autopm_get_device(cd->device))
+               goto out;
 
  out_put:
        kref_put(&cd->kref, sr_kref_release);
@@ -162,6 +180,7 @@ static void scsi_cd_put(struct scsi_cd *cd)
 
        mutex_lock(&sr_ref_mutex);
        kref_put(&cd->kref, sr_kref_release);
+       scsi_autopm_put_device(sdev);
        scsi_device_put(sdev);
        mutex_unlock(&sr_ref_mutex);
 }
@@ -221,14 +240,33 @@ static unsigned int sr_check_events(struct cdrom_device_info *cdi,
                return 0;
 
        events = sr_get_events(cd->device);
+       cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
+
+       /*
+        * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
+        * for several times in a row.  We rely on TUR only for this likely
+        * broken device, to prevent generating incorrect media changed
+        * events for every open().
+        */
+       if (cd->ignore_get_event) {
+               events &= ~DISK_EVENT_MEDIA_CHANGE;
+               goto do_tur;
+       }
+
        /*
         * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
         * is being cleared.  Note that there are devices which hang
         * if asked to execute TUR repeatedly.
         */
-       if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
-               goto skip_tur;
+       if (cd->device->changed) {
+               events |= DISK_EVENT_MEDIA_CHANGE;
+               cd->device->changed = 0;
+               cd->tur_changed = true;
+       }
 
+       if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
+               return events;
+do_tur:
        /* let's see whether the media is there with TUR */
        last_present = cd->media_present;
        ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
@@ -242,12 +280,31 @@ static unsigned int sr_check_events(struct cdrom_device_info *cdi,
                (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
 
        if (last_present != cd->media_present)
-               events |= DISK_EVENT_MEDIA_CHANGE;
-skip_tur:
+               cd->device->changed = 1;
+
        if (cd->device->changed) {
                events |= DISK_EVENT_MEDIA_CHANGE;
                cd->device->changed = 0;
+               cd->tur_changed = true;
+       }
+
+       if (cd->ignore_get_event)
+               return events;
+
+       /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
+       if (!cd->tur_changed) {
+               if (cd->get_event_changed) {
+                       if (cd->tur_mismatch++ > 8) {
+                               sdev_printk(KERN_WARNING, cd->device,
+                                           "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
+                               cd->ignore_get_event = true;
+                       }
+               } else {
+                       cd->tur_mismatch = 0;
+               }
        }
+       cd->tur_changed = false;
+       cd->get_event_changed = false;
 
        return events;
 }
@@ -484,14 +541,13 @@ static int sr_block_open(struct block_device *bdev, fmode_t mode)
        return ret;
 }
 
-static int sr_block_release(struct gendisk *disk, fmode_t mode)
+static void sr_block_release(struct gendisk *disk, fmode_t mode)
 {
        struct scsi_cd *cd = scsi_cd(disk);
        mutex_lock(&sr_mutex);
        cdrom_release(&cd->cdi, mode);
        scsi_cd_put(cd);
        mutex_unlock(&sr_mutex);
-       return 0;
 }
 
 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
@@ -502,6 +558,8 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
        void __user *argp = (void __user *)arg;
        int ret;
 
+       scsi_autopm_get_device(cd->device);
+
        mutex_lock(&sr_mutex);
 
        /*
@@ -533,6 +591,7 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
 
 out:
        mutex_unlock(&sr_mutex);
+       scsi_autopm_put_device(cd->device);
        return ret;
 }
 
@@ -540,7 +599,17 @@ static unsigned int sr_block_check_events(struct gendisk *disk,
                                          unsigned int clearing)
 {
        struct scsi_cd *cd = scsi_cd(disk);
-       return cdrom_check_events(&cd->cdi, clearing);
+       unsigned int ret;
+
+       if (atomic_read(&cd->device->disk_events_disable_depth) == 0) {
+               scsi_autopm_get_device(cd->device);
+               ret = cdrom_check_events(&cd->cdi, clearing);
+               scsi_autopm_put_device(cd->device);
+       } else {
+               ret = 0;
+       }
+
+       return ret;
 }
 
 static int sr_block_revalidate_disk(struct gendisk *disk)
@@ -548,12 +617,16 @@ static int sr_block_revalidate_disk(struct gendisk *disk)
        struct scsi_cd *cd = scsi_cd(disk);
        struct scsi_sense_hdr sshdr;
 
+       scsi_autopm_get_device(cd->device);
+
        /* if the unit is not ready, nothing more to do */
        if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
-               return 0;
+               goto out;
 
        sr_cd_check(&cd->cdi);
        get_sectorsize(cd);
+out:
+       scsi_autopm_put_device(cd->device);
        return 0;
 }
 
@@ -680,6 +753,8 @@ static int sr_probe(struct device *dev)
 
        sdev_printk(KERN_DEBUG, sdev,
                    "Attached scsi CD-ROM %s\n", cd->cdi.name);
+       scsi_autopm_put_device(cd->device);
+
        return 0;
 
 fail_put:
@@ -927,6 +1002,8 @@ static int sr_remove(struct device *dev)
 {
        struct scsi_cd *cd = dev_get_drvdata(dev);
 
+       scsi_autopm_get_device(cd->device);
+
        blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
        del_gendisk(cd->disk);