V4L/DVB (10987): cx23885: fix crash on non-netup cards
[safe/jmp/linux-2.6] / drivers / ide / ide-atapi.c
index 608c5ba..6adc5b4 100644 (file)
@@ -3,6 +3,7 @@
  */
 
 #include <linux/kernel.h>
+#include <linux/cdrom.h>
 #include <linux/delay.h>
 #include <linux/ide.h>
 #include <scsi/scsi.h>
 #define debug_log(fmt, args...) do {} while (0)
 #endif
 
+#define ATAPI_MIN_CDB_BYTES    12
+
+static inline int dev_is_idecd(ide_drive_t *drive)
+{
+       return drive->media == ide_cdrom || drive->media == ide_optical;
+}
+
 /*
  * Check whether we can support a device,
  * based on the ATAPI IDENTIFY command results.
@@ -124,20 +132,28 @@ EXPORT_SYMBOL_GPL(ide_init_pc);
  * the current request, so that it will be processed immediately, on the next
  * pass through the driver.
  */
-void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
-                      struct ide_atapi_pc *pc, struct request *rq)
+static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
+                             struct ide_atapi_pc *pc, struct request *rq)
 {
        blk_rq_init(NULL, rq);
        rq->cmd_type = REQ_TYPE_SPECIAL;
        rq->cmd_flags |= REQ_PREEMPT;
        rq->buffer = (char *)pc;
        rq->rq_disk = disk;
+
+       if (pc->req_xfer) {
+               rq->data = pc->buf;
+               rq->data_len = pc->req_xfer;
+       }
+
        memcpy(rq->cmd, pc->c, 12);
        if (drive->media == ide_tape)
                rq->cmd[13] = REQ_IDETAPE_PC1;
-       ide_do_drive_cmd(drive, rq);
+
+       drive->hwif->rq = NULL;
+
+       elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
 }
-EXPORT_SYMBOL_GPL(ide_queue_pc_head);
 
 /*
  * Add a special packet command request to the tail of the request queue,
@@ -152,6 +168,12 @@ int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
        rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
        rq->cmd_type = REQ_TYPE_SPECIAL;
        rq->buffer = (char *)pc;
+
+       if (pc->req_xfer) {
+               rq->data = pc->buf;
+               rq->data_len = pc->req_xfer;
+       }
+
        memcpy(rq->cmd, pc->c, 12);
        if (drive->media == ide_tape)
                rq->cmd[13] = REQ_IDETAPE_PC1;
@@ -192,7 +214,7 @@ int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
 {
        struct ide_atapi_pc pc;
 
-       if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK)
+       if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
                return 0;
 
        ide_init_pc(&pc);
@@ -203,25 +225,119 @@ int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
 }
 EXPORT_SYMBOL_GPL(ide_set_media_lock);
 
-/* TODO: unify the code thus making some arguments go away */
-ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
-       ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
-       void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
-       void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
-       int (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
+void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
+{
+       ide_init_pc(pc);
+       pc->c[0] = REQUEST_SENSE;
+       if (drive->media == ide_floppy) {
+               pc->c[4] = 255;
+               pc->req_xfer = 18;
+       } else {
+               pc->c[4] = 20;
+               pc->req_xfer = 20;
+       }
+}
+EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
+
+/*
+ * Called when an error was detected during the last packet command.
+ * We queue a request sense packet command in the head of the request list.
+ */
+void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
+{
+       struct request *rq = &drive->request_sense_rq;
+       struct ide_atapi_pc *pc = &drive->request_sense_pc;
+
+       (void)ide_read_error(drive);
+       ide_create_request_sense_cmd(drive, pc);
+       if (drive->media == ide_tape)
+               set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
+       ide_queue_pc_head(drive, disk, pc, rq);
+}
+EXPORT_SYMBOL_GPL(ide_retry_pc);
+
+int ide_cd_expiry(ide_drive_t *drive)
+{
+       struct request *rq = drive->hwif->rq;
+       unsigned long wait = 0;
+
+       debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
+
+       /*
+        * Some commands are *slow* and normally take a long time to complete.
+        * Usually we can use the ATAPI "disconnect" to bypass this, but not all
+        * commands/drives support that. Let ide_timer_expiry keep polling us
+        * for these.
+        */
+       switch (rq->cmd[0]) {
+       case GPCMD_BLANK:
+       case GPCMD_FORMAT_UNIT:
+       case GPCMD_RESERVE_RZONE_TRACK:
+       case GPCMD_CLOSE_TRACK:
+       case GPCMD_FLUSH_CACHE:
+               wait = ATAPI_WAIT_PC;
+               break;
+       default:
+               if (!(rq->cmd_flags & REQ_QUIET))
+                       printk(KERN_INFO "cmd 0x%x timed out\n",
+                                        rq->cmd[0]);
+               wait = 0;
+               break;
+       }
+       return wait;
+}
+EXPORT_SYMBOL_GPL(ide_cd_expiry);
+
+int ide_cd_get_xferlen(struct request *rq)
+{
+       if (blk_fs_request(rq))
+               return 32768;
+       else if (blk_sense_request(rq) || blk_pc_request(rq) ||
+                        rq->cmd_type == REQ_TYPE_ATA_PC)
+               return rq->data_len;
+       else
+               return 0;
+}
+EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
+
+void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
+{
+       ide_task_t task;
+
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
+                       IDE_TFLAG_IN_NSECT;
+
+       drive->hwif->tp_ops->tf_read(drive, &task);
+
+       *bcount = (task.tf.lbah << 8) | task.tf.lbam;
+       *ireason = task.tf.nsect & 3;
+}
+EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
+
+/*
+ * This is the usual interrupt handler which will be called during a packet
+ * command.  We will transfer some of the data (as requested by the drive)
+ * and will re-point interrupt handler to us.
+ */
+static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
 {
+       struct ide_atapi_pc *pc = drive->pc;
        ide_hwif_t *hwif = drive->hwif;
-       struct request *rq = hwif->hwgroup->rq;
+       struct request *rq = hwif->rq;
        const struct ide_tp_ops *tp_ops = hwif->tp_ops;
        xfer_func_t *xferfunc;
-       unsigned int temp;
+       unsigned int timeout, temp;
        u16 bcount;
-       u8 stat, ireason, scsi = drive->scsi;
+       u8 stat, ireason, dsc = 0;
 
        debug_log("Enter %s - interrupt handler\n", __func__);
 
+       timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
+                                              : WAIT_TAPE_CMD;
+
        if (pc->flags & PC_FLAG_TIMEDOUT) {
-               drive->pc_callback(drive);
+               drive->pc_callback(drive, 0);
                return ide_stopped;
        }
 
@@ -230,16 +346,16 @@ ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
 
        if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
                if (hwif->dma_ops->dma_end(drive) ||
-                   (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
-                       if (drive->media == ide_floppy && !scsi)
+                   (drive->media == ide_tape && (stat & ATA_ERR))) {
+                       if (drive->media == ide_floppy)
                                printk(KERN_ERR "%s: DMA %s error\n",
                                        drive->name, rq_data_dir(pc->rq)
                                                     ? "write" : "read");
                        pc->flags |= PC_FLAG_DMA_ERROR;
                } else {
                        pc->xferred = pc->req_xfer;
-                       if (update_buffers)
-                               update_buffers(drive, pc);
+                       if (drive->pc_update_buffers)
+                               drive->pc_update_buffers(drive, pc);
                }
                debug_log("%s: DMA finished\n", drive->name);
        }
@@ -253,7 +369,7 @@ ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
 
                local_irq_enable_in_hardirq();
 
-               if (drive->media == ide_tape && !scsi &&
+               if (drive->media == ide_tape &&
                    (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
                        stat &= ~ATA_ERR;
 
@@ -261,11 +377,8 @@ ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
                        /* Error detected */
                        debug_log("%s: I/O error\n", drive->name);
 
-                       if (drive->media != ide_tape || scsi) {
+                       if (drive->media != ide_tape)
                                pc->rq->errors++;
-                               if (scsi)
-                                       goto cmd_finished;
-                       }
 
                        if (rq->cmd[0] == REQUEST_SENSE) {
                                printk(KERN_ERR "%s: I/O error in request sense"
@@ -276,21 +389,18 @@ ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
                        debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
 
                        /* Retry operation */
-                       retry_pc(drive);
+                       ide_retry_pc(drive, rq->rq_disk);
 
                        /* queued, but not started */
                        return ide_stopped;
                }
-cmd_finished:
                pc->error = 0;
-               if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
-                   (stat & ATA_DSC) == 0) {
-                       dsc_handle(drive);
-                       return ide_stopped;
-               }
+
+               if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
+                       dsc = 1;
 
                /* Command finished - Call the callback function */
-               drive->pc_callback(drive);
+               drive->pc_callback(drive, dsc);
 
                return ide_stopped;
        }
@@ -330,27 +440,9 @@ cmd_finished:
                                                "us more data than expected - "
                                                "discarding data\n",
                                                drive->name);
-                               if (scsi)
-                                       temp = pc->buf_size - pc->xferred;
-                               else
-                                       temp = 0;
-                               if (temp) {
-                                       if (pc->sg)
-                                               io_buffers(drive, pc, temp, 0);
-                                       else
-                                               tp_ops->input_data(drive, NULL,
-                                                       pc->cur_pos, temp);
-                                       printk(KERN_ERR "%s: transferred %d of "
-                                                       "%d bytes\n",
-                                                       drive->name,
-                                                       temp, bcount);
-                               }
-                               pc->xferred += temp;
-                               pc->cur_pos += temp;
-                               ide_pad_transfer(drive, 0, bcount - temp);
-                               ide_set_handler(drive, handler, timeout,
-                                               expiry);
-                               return ide_started;
+
+                               ide_pad_transfer(drive, 0, bcount);
+                               goto next_irq;
                        }
                        debug_log("The device wants to send us more data than "
                                  "expected - allowing transfer\n");
@@ -359,14 +451,13 @@ cmd_finished:
        } else
                xferfunc = tp_ops->output_data;
 
-       if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
-           (drive->media == ide_tape && !scsi && pc->bh) ||
-           (scsi && pc->sg)) {
-               int done = io_buffers(drive, pc, bcount,
+       if ((drive->media == ide_floppy && !pc->buf) ||
+           (drive->media == ide_tape && pc->bh)) {
+               int done = drive->pc_io_buffers(drive, pc, bcount,
                                  !!(pc->flags & PC_FLAG_WRITING));
 
                /* FIXME: don't do partial completions */
-               if (drive->media == ide_floppy && !scsi)
+               if (drive->media == ide_floppy)
                        ide_end_request(drive, 1, done >> 9);
        } else
                xferfunc(drive, NULL, pc->cur_pos, bcount);
@@ -377,12 +468,30 @@ cmd_finished:
 
        debug_log("[cmd %x] transferred %d bytes on that intr.\n",
                  rq->cmd[0], bcount);
-
+next_irq:
        /* And set the interrupt handler again */
-       ide_set_handler(drive, handler, timeout, expiry);
+       ide_set_handler(drive, ide_pc_intr, timeout, NULL);
        return ide_started;
 }
-EXPORT_SYMBOL_GPL(ide_pc_intr);
+
+static void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount)
+{
+       ide_hwif_t *hwif = drive->hwif;
+       ide_task_t task;
+       u8 dma = drive->dma;
+
+       memset(&task, 0, sizeof(task));
+       task.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
+                       IDE_TFLAG_OUT_FEATURE | tf_flags;
+       task.tf.feature = dma;          /* Use PIO/DMA */
+       task.tf.lbam    = bcount & 0xff;
+       task.tf.lbah    = (bcount >> 8) & 0xff;
+
+       ide_tf_dump(drive->name, &task.tf);
+       hwif->tp_ops->set_irq(hwif, 1);
+       SELECT_MASK(drive, 0);
+       hwif->tp_ops->tf_load(drive, &task);
+}
 
 static u8 ide_read_ireason(ide_drive_t *drive)
 {
@@ -418,12 +527,23 @@ static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
        return ireason;
 }
 
-ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
-                               ide_handler_t *handler, unsigned int timeout,
-                               ide_expiry_t *expiry)
+static int ide_delayed_transfer_pc(ide_drive_t *drive)
 {
+       /* Send the actual packet */
+       drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
+
+       /* Timeout for the packet command */
+       return WAIT_FLOPPY_CMD;
+}
+
+static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
+{
+       struct ide_atapi_pc *uninitialized_var(pc);
        ide_hwif_t *hwif = drive->hwif;
-       struct request *rq = hwif->hwgroup->rq;
+       struct request *rq = hwif->rq;
+       ide_expiry_t *expiry;
+       unsigned int timeout;
+       int cmd_len;
        ide_startstop_t startstop;
        u8 ireason;
 
@@ -433,78 +553,131 @@ ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
                return startstop;
        }
 
-       ireason = ide_read_ireason(drive);
-       if (drive->media == ide_tape && !drive->scsi)
-               ireason = ide_wait_ireason(drive, ireason);
+       if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
+               if (drive->dma)
+                       drive->waiting_for_dma = 1;
+       }
 
-       if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
-               printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
-                               "a packet command\n", drive->name);
-               return ide_do_reset(drive);
+       if (dev_is_idecd(drive)) {
+               /* ATAPI commands get padded out to 12 bytes minimum */
+               cmd_len = COMMAND_SIZE(rq->cmd[0]);
+               if (cmd_len < ATAPI_MIN_CDB_BYTES)
+                       cmd_len = ATAPI_MIN_CDB_BYTES;
+
+               timeout = rq->timeout;
+               expiry  = ide_cd_expiry;
+       } else {
+               pc = drive->pc;
+
+               cmd_len = ATAPI_MIN_CDB_BYTES;
+
+               /*
+                * If necessary schedule the packet transfer to occur 'timeout'
+                * miliseconds later in ide_delayed_transfer_pc() after the
+                * device says it's ready for a packet.
+                */
+               if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
+                       timeout = drive->pc_delay;
+                       expiry = &ide_delayed_transfer_pc;
+               } else {
+                       timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
+                                                              : WAIT_TAPE_CMD;
+                       expiry = NULL;
+               }
+
+               ireason = ide_read_ireason(drive);
+               if (drive->media == ide_tape)
+                       ireason = ide_wait_ireason(drive, ireason);
+
+               if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
+                       printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
+                                       "a packet command\n", drive->name);
+
+                       return ide_do_reset(drive);
+               }
        }
 
        /* Set the interrupt routine */
-       ide_set_handler(drive, handler, timeout, expiry);
+       ide_set_handler(drive,
+                       (dev_is_idecd(drive) ? drive->irq_handler
+                                            : ide_pc_intr),
+                       timeout, expiry);
 
        /* Begin DMA, if necessary */
-       if (pc->flags & PC_FLAG_DMA_OK) {
-               pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
-               hwif->dma_ops->dma_start(drive);
+       if (dev_is_idecd(drive)) {
+               if (drive->dma)
+                       hwif->dma_ops->dma_start(drive);
+       } else {
+               if (pc->flags & PC_FLAG_DMA_OK) {
+                       pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
+                       hwif->dma_ops->dma_start(drive);
+               }
        }
 
        /* Send the actual packet */
        if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
-               hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
+               hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
 
        return ide_started;
 }
-EXPORT_SYMBOL_GPL(ide_transfer_pc);
 
-ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
-                            ide_handler_t *handler, unsigned int timeout,
-                            ide_expiry_t *expiry)
+ide_startstop_t ide_issue_pc(ide_drive_t *drive)
 {
+       struct ide_atapi_pc *pc;
        ide_hwif_t *hwif = drive->hwif;
+       ide_expiry_t *expiry = NULL;
+       unsigned int timeout;
+       u32 tf_flags;
        u16 bcount;
-       u8 dma = 0;
 
-       /* We haven't transferred any data yet */
-       pc->xferred = 0;
-       pc->cur_pos = pc->buf;
+       if (dev_is_idecd(drive)) {
+               tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
+               bcount = ide_cd_get_xferlen(hwif->rq);
+               expiry = ide_cd_expiry;
+               timeout = ATAPI_WAIT_PC;
 
-       /* Request to transfer the entire buffer at once */
-       if (drive->media == ide_tape && !drive->scsi)
-               bcount = pc->req_xfer;
-       else
-               bcount = min(pc->req_xfer, 63 * 1024);
+               if (drive->dma)
+                       drive->dma = !hwif->dma_ops->dma_setup(drive);
+       } else {
+               pc = drive->pc;
 
-       if (pc->flags & PC_FLAG_DMA_ERROR) {
-               pc->flags &= ~PC_FLAG_DMA_ERROR;
-               ide_dma_off(drive);
-       }
+               /* We haven't transferred any data yet */
+               pc->xferred = 0;
+               pc->cur_pos = pc->buf;
 
-       if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
-               if (drive->scsi)
-                       hwif->sg_mapped = 1;
-               dma = !hwif->dma_ops->dma_setup(drive);
-               if (drive->scsi)
-                       hwif->sg_mapped = 0;
-       }
+               tf_flags = IDE_TFLAG_OUT_DEVICE;
+               bcount = ((drive->media == ide_tape) ?
+                               pc->req_xfer :
+                               min(pc->req_xfer, 63 * 1024));
 
-       if (!dma)
-               pc->flags &= ~PC_FLAG_DMA_OK;
+               if (pc->flags & PC_FLAG_DMA_ERROR) {
+                       pc->flags &= ~PC_FLAG_DMA_ERROR;
+                       ide_dma_off(drive);
+               }
+
+               if ((pc->flags & PC_FLAG_DMA_OK) &&
+                    (drive->dev_flags & IDE_DFLAG_USING_DMA))
+                       drive->dma = !hwif->dma_ops->dma_setup(drive);
+
+               if (!drive->dma)
+                       pc->flags &= ~PC_FLAG_DMA_OK;
+
+               timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
+                                                      : WAIT_TAPE_CMD;
+       }
 
-       ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
-                          bcount, dma);
+       ide_pktcmd_tf_load(drive, tf_flags, bcount);
 
        /* Issue the packet command */
        if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
-               ide_execute_command(drive, ATA_CMD_PACKET, handler,
-                                   timeout, NULL);
+               if (drive->dma)
+                       drive->waiting_for_dma = 0;
+               ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
+                                   timeout, expiry);
                return ide_started;
        } else {
                ide_execute_pkt_cmd(drive);
-               return (*handler)(drive);
+               return ide_transfer_pc(drive);
        }
 }
 EXPORT_SYMBOL_GPL(ide_issue_pc);