[PATCH] pcmcia: default suspend and resume handling
[safe/jmp/linux-2.6] / drivers / ide / ide-cd.c
index 8b9d855..b4a41d6 100644 (file)
 #include <linux/cdrom.h>
 #include <linux/ide.h>
 #include <linux/completion.h>
+#include <linux/mutex.h>
 
 #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
 
 
 #include "ide-cd.h"
 
-static DECLARE_MUTEX(idecd_ref_sem);
+static DEFINE_MUTEX(idecd_ref_mutex);
 
 #define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref) 
 
@@ -335,11 +336,11 @@ static struct cdrom_info *ide_cd_get(struct gendisk *disk)
 {
        struct cdrom_info *cd = NULL;
 
-       down(&idecd_ref_sem);
+       mutex_lock(&idecd_ref_mutex);
        cd = ide_cd_g(disk);
        if (cd)
                kref_get(&cd->kref);
-       up(&idecd_ref_sem);
+       mutex_unlock(&idecd_ref_mutex);
        return cd;
 }
 
@@ -347,9 +348,9 @@ static void ide_cd_release(struct kref *);
 
 static void ide_cd_put(struct cdrom_info *cd)
 {
-       down(&idecd_ref_sem);
+       mutex_lock(&idecd_ref_mutex);
        kref_put(&cd->kref, ide_cd_release);
-       up(&idecd_ref_sem);
+       mutex_unlock(&idecd_ref_mutex);
 }
 
 /****************************************************************************
@@ -614,7 +615,7 @@ static void cdrom_end_request (ide_drive_t *drive, int uptodate)
                         */
                        spin_lock_irqsave(&ide_lock, flags);
                        end_that_request_chunk(failed, 0, failed->data_len);
-                       end_that_request_last(failed);
+                       end_that_request_last(failed, 0);
                        spin_unlock_irqrestore(&ide_lock, flags);
                }
 
@@ -980,7 +981,7 @@ static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
  * and attempt to recover if there are problems.  Returns  0 if everything's
  * ok; nonzero if the request has been terminated.
  */
-static inline
+static
 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
 {
        if (ireason == 2)
@@ -1292,7 +1293,6 @@ static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
        struct cdrom_info *info = drive->driver_data;
 
        info->dma = 0;
-       info->cmd = 0;
        info->start_seek = jiffies;
        return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
 }
@@ -1333,8 +1333,6 @@ static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
        if (cdrom_read_from_buffer(drive))
                return ide_stopped;
 
-       blk_attempt_remerge(drive->queue, rq);
-
        /* Clear the local sector buffer. */
        info->nsectors_buffered = 0;
 
@@ -1344,8 +1342,6 @@ static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
            (rq->nr_sectors & (sectors_per_frame - 1)))
                info->dma = 0;
 
-       info->cmd = READ;
-
        /* Start sending the read request to the drive. */
        return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
 }
@@ -1484,7 +1480,6 @@ static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
        struct cdrom_info *info = drive->driver_data;
 
        info->dma = 0;
-       info->cmd = 0;
        rq->flags &= ~REQ_FAILED;
        len = rq->data_len;
 
@@ -1545,7 +1540,7 @@ int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
 /*
  * Write handling
  */
-static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
+static int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
 {
        /* Two notes about IDE interrupt reason here - 0 means that
         * the drive wants to receive data from us, 2 means that
@@ -1739,7 +1734,7 @@ end_request:
 
        spin_lock_irqsave(&ide_lock, flags);
        blkdev_dequeue_request(rq);
-       end_that_request_last(rq);
+       end_that_request_last(rq, 1);
        HWGROUP(drive)->rq = NULL;
        spin_unlock_irqrestore(&ide_lock, flags);
        return ide_stopped;
@@ -1878,20 +1873,11 @@ static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
                return ide_stopped;
        }
 
-       /*
-        * for dvd-ram and such media, it's a really big deal to get
-        * big writes all the time. so scour the queue and attempt to
-        * remerge requests, often the plugging will not have had time
-        * to do this properly
-        */
-       blk_attempt_remerge(drive->queue, rq);
-
        info->nsectors_buffered = 0;
 
        /* use dma, if possible. we don't need to check more, since we
         * know that the transfer is always (at least!) frame aligned */
        info->dma = drive->using_dma ? 1 : 0;
-       info->cmd = WRITE;
 
        info->devinfo.media_written = 1;
 
@@ -1916,7 +1902,6 @@ static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
        rq->flags |= REQ_QUIET;
 
        info->dma = 0;
-       info->cmd = 0;
 
        /*
         * sg request
@@ -1925,7 +1910,6 @@ static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
                int mask = drive->queue->dma_alignment;
                unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
 
-               info->cmd = rq_data_dir(rq);
                info->dma = drive->using_dma;
 
                /*
@@ -2159,6 +2143,7 @@ static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
        req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
        req.data = (char *)&capbuf;
        req.data_len = sizeof(capbuf);
+       req.flags |= REQ_QUIET;
 
        stat = cdrom_queue_packet_command(drive, &req);
        if (stat == 0) {
@@ -2488,52 +2473,6 @@ static int ide_cdrom_packet(struct cdrom_device_info *cdi,
 }
 
 static
-int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
-                        unsigned int cmd, unsigned long arg)
-{
-       struct packet_command cgc;
-       char buffer[16];
-       int stat;
-
-       init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
-
-       /* These will be moved into the Uniform layer shortly... */
-       switch (cmd) {
-       case CDROMSETSPINDOWN: {
-               char spindown;
-               if (copy_from_user(&spindown, (void __user *) arg, sizeof(char)))
-                       return -EFAULT;
-                if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
-                       return stat;
-
-               buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
-
-               return cdrom_mode_select(cdi, &cgc);
-       } 
-       case CDROMGETSPINDOWN: {
-               char spindown;
-                if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
-                       return stat;
-               spindown = buffer[11] & 0x0f;
-               if (copy_to_user((void __user *) arg, &spindown, sizeof (char)))
-                       return -EFAULT;
-               return 0;
-       }
-  
-       default:
-               return -EINVAL;
-       }
-
-}
-
-static
 int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
                           unsigned int cmd, void *arg)
                           
@@ -2869,12 +2808,11 @@ static struct cdrom_device_ops ide_cdrom_dops = {
        .get_mcn                = ide_cdrom_get_mcn,
        .reset                  = ide_cdrom_reset,
        .audio_ioctl            = ide_cdrom_audio_ioctl,
-       .dev_ioctl              = ide_cdrom_dev_ioctl,
        .capability             = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
                                CDC_SELECT_SPEED | CDC_SELECT_DISC |
                                CDC_MULTI_SESSION | CDC_MCN |
                                CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
-                               CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
+                               CDC_DRIVE_STATUS | CDC_CD_R |
                                CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
                                CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
                                CDC_MRW_W | CDC_RAM,
@@ -2912,6 +2850,8 @@ static int ide_cdrom_register (ide_drive_t *drive, int nslots)
                devinfo->mask |= CDC_CLOSE_TRAY;
        if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
                devinfo->mask |= CDC_MO_DRIVE;
+       if (!CDROM_CONFIG_FLAGS(drive)->ram)
+               devinfo->mask |= CDC_RAM;
 
        devinfo->disk = info->disk;
        return register_cdrom(devinfo);
@@ -3271,9 +3211,8 @@ sector_t ide_cdrom_capacity (ide_drive_t *drive)
 }
 #endif
 
-static int ide_cd_remove(struct device *dev)
+static void ide_cd_remove(ide_drive_t *drive)
 {
-       ide_drive_t *drive = to_ide_device(dev);
        struct cdrom_info *info = drive->driver_data;
 
        ide_unregister_subdriver(drive, info->driver);
@@ -3281,8 +3220,6 @@ static int ide_cd_remove(struct device *dev)
        del_gendisk(info->disk);
 
        ide_cd_put(info);
-
-       return 0;
 }
 
 static void ide_cd_release(struct kref *kref)
@@ -3292,12 +3229,9 @@ static void ide_cd_release(struct kref *kref)
        ide_drive_t *drive = info->drive;
        struct gendisk *g = info->disk;
 
-       if (info->buffer != NULL)
-               kfree(info->buffer);
-       if (info->toc != NULL)
-               kfree(info->toc);
-       if (info->changer_info != NULL)
-               kfree(info->changer_info);
+       kfree(info->buffer);
+       kfree(info->toc);
+       kfree(info->changer_info);
        if (devinfo->handle == drive && unregister_cdrom(devinfo))
                printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
                                "driver.\n", __FUNCTION__, drive->name);
@@ -3309,7 +3243,7 @@ static void ide_cd_release(struct kref *kref)
        kfree(info);
 }
 
-static int ide_cd_probe(struct device *);
+static int ide_cd_probe(ide_drive_t *);
 
 #ifdef CONFIG_PROC_FS
 static int proc_idecd_read_capacity
@@ -3331,13 +3265,13 @@ static ide_proc_entry_t idecd_proc[] = {
 #endif
 
 static ide_driver_t ide_cdrom_driver = {
-       .owner                  = THIS_MODULE,
        .gen_driver = {
+               .owner          = THIS_MODULE,
                .name           = "ide-cdrom",
                .bus            = &ide_bus_type,
-               .probe          = ide_cd_probe,
-               .remove         = ide_cd_remove,
        },
+       .probe                  = ide_cd_probe,
+       .remove                 = ide_cd_remove,
        .version                = IDECD_VERSION,
        .media                  = ide_cdrom,
        .supports_dsc_overlap   = 1,
@@ -3388,6 +3322,45 @@ static int idecd_release(struct inode * inode, struct file * file)
        return 0;
 }
 
+static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
+{
+       struct packet_command cgc;
+       char buffer[16];
+       int stat;
+       char spindown;
+
+       if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
+               return -EFAULT;
+
+       init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
+
+       stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
+       if (stat)
+               return stat;
+
+       buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
+       return cdrom_mode_select(cdi, &cgc);
+}
+
+static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
+{
+       struct packet_command cgc;
+       char buffer[16];
+       int stat;
+       char spindown;
+
+       init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
+
+       stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
+       if (stat)
+               return stat;
+
+       spindown = buffer[11] & 0x0f;
+       if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
+               return -EFAULT;
+       return 0;
+}
+
 static int idecd_ioctl (struct inode *inode, struct file *file,
                        unsigned int cmd, unsigned long arg)
 {
@@ -3395,7 +3368,16 @@ static int idecd_ioctl (struct inode *inode, struct file *file,
        struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
        int err;
 
-       err  = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
+       switch (cmd) {
+       case CDROMSETSPINDOWN:
+               return idecd_set_spindown(&info->devinfo, arg);
+       case CDROMGETSPINDOWN:
+               return idecd_get_spindown(&info->devinfo, arg);
+       default:
+               break;
+       }
+
+       err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
        if (err == -EINVAL)
                err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
 
@@ -3431,9 +3413,8 @@ static char *ignore = NULL;
 module_param(ignore, charp, 0400);
 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
 
-static int ide_cd_probe(struct device *dev)
+static int ide_cd_probe(ide_drive_t *drive)
 {
-       ide_drive_t *drive = to_ide_device(dev);
        struct cdrom_info *info;
        struct gendisk *g;
        struct request_sense sense;
@@ -3455,7 +3436,7 @@ static int ide_cd_probe(struct device *dev)
                printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
                goto failed;
        }
-       info = kmalloc(sizeof(struct cdrom_info), GFP_KERNEL);
+       info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
        if (info == NULL) {
                printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
                goto failed;
@@ -3469,8 +3450,6 @@ static int ide_cd_probe(struct device *dev)
 
        ide_register_subdriver(drive, &ide_cdrom_driver);
 
-       memset(info, 0, sizeof (struct cdrom_info));
-
        kref_init(&info->kref);
 
        info->drive = drive;
@@ -3489,12 +3468,9 @@ static int ide_cd_probe(struct device *dev)
        if (ide_cdrom_setup(drive)) {
                struct cdrom_device_info *devinfo = &info->devinfo;
                ide_unregister_subdriver(drive, &ide_cdrom_driver);
-               if (info->buffer != NULL)
-                       kfree(info->buffer);
-               if (info->toc != NULL)
-                       kfree(info->toc);
-               if (info->changer_info != NULL)
-                       kfree(info->changer_info);
+               kfree(info->buffer);
+               kfree(info->toc);
+               kfree(info->changer_info);
                if (devinfo->handle == drive && unregister_cdrom(devinfo))
                        printk (KERN_ERR "%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
                kfree(info);
@@ -3518,12 +3494,13 @@ static void __exit ide_cdrom_exit(void)
 {
        driver_unregister(&ide_cdrom_driver.gen_driver);
 }
-static int ide_cdrom_init(void)
+
+static int __init ide_cdrom_init(void)
 {
        return driver_register(&ide_cdrom_driver.gen_driver);
 }
 
+MODULE_ALIAS("ide:*m-cdrom*");
 module_init(ide_cdrom_init);
 module_exit(ide_cdrom_exit);
 MODULE_LICENSE("GPL");