-static ide_startstop_t cdrom_rw_intr(ide_drive_t *drive)
-{
- struct cdrom_info *info = drive->driver_data;
- struct request *rq = HWGROUP(drive)->rq;
- xfer_func_t *xferfunc;
- int stat, ireason, len, thislen, uptodate, nskip;
- int dma_error = 0, dma = info->dma, write = rq_data_dir(rq) == WRITE;
- u8 lowcyl = 0, highcyl = 0;
-
- /* Check for errors. */
- if (dma) {
- info->dma = 0;
- dma_error = HWIF(drive)->ide_dma_end(drive);
- if (dma_error) {
- printk(KERN_ERR "%s: DMA %s error\n", drive->name,
- write ? "write" : "read");
- ide_dma_off(drive);
- }
- }
-
- if (cdrom_decode_status(drive, 0, &stat))
- return ide_stopped;
-
- /*
- * using dma, transfer is complete now
- */
- if (dma) {
- if (dma_error)
- return ide_error(drive, "dma error", stat);
-
- ide_end_request(drive, 1, rq->nr_sectors);
- return ide_stopped;
- }
-
- /* Read the interrupt reason and the transfer length. */
- ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
- lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
- highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
-
- len = lowcyl + (256 * highcyl);
-
- /* If DRQ is clear, the command has completed. */
- if ((stat & DRQ_STAT) == 0) {
- /*
- * If we're not done reading/writing, complain.
- * Otherwise, complete the command normally.
- */
- uptodate = 1;
- if (rq->current_nr_sectors > 0) {
- printk(KERN_ERR "%s: %s: data underrun (%d blocks)\n",
- drive->name, __FUNCTION__,
- rq->current_nr_sectors);
- if (!write)
- rq->cmd_flags |= REQ_FAILED;
- uptodate = 0;
- }
- cdrom_end_request(drive, uptodate);
- return ide_stopped;
- }
-
- thislen = len;
-
- /* Check that the drive is expecting to do the same thing we are. */
- if (write) {
- if (cdrom_write_check_ireason(drive, len, ireason))
- return ide_stopped;
-
- xferfunc = HWIF(drive)->atapi_output_bytes;
- } else {
- if (cdrom_read_check_ireason(drive, len, ireason))
- return ide_stopped;
-
- if (ide_cd_check_transfer_size(drive, len)) {
- cdrom_end_request(drive, 0);
- return ide_stopped;
- }
-
- /*
- * First, figure out if we need to bit-bucket
- * any of the leading sectors.
- */
- nskip = min_t(int, rq->current_nr_sectors
- - bio_cur_sectors(rq->bio),
- thislen >> 9);
-
- if (nskip > 0) {
- ide_cd_drain_data(drive, nskip);
- rq->current_nr_sectors -= nskip;
- thislen -= (nskip << 9);
- }
-
- xferfunc = HWIF(drive)->atapi_input_bytes;
- }
-
- /*
- * now loop and read/write the data
- */
- while (thislen > 0) {
- u8 *ptr = NULL;
- int blen;
-
- if (rq->bio) {
- ptr = rq->buffer;
- blen = rq->current_nr_sectors << 9;
- }
-
- if (!ptr) {
- if (!write)
- /*
- * If the buffers are full, cache the rest
- * of the data in our internal buffer.
- */
- cdrom_buffer_sectors(drive, rq->sector,
- thislen >> 9);
- else {
- printk(KERN_ERR "%s: %s: confused, missing "
- "data\n",
- drive->name, __FUNCTION__);
- blk_dump_rq_flags(rq, "cdrom_rw_intr, write");
- }
- break;
- }
-
- /*
- * Figure out how many sectors we can transfer
- */
- if (blen > thislen)
- blen = thislen;
-
- xferfunc(drive, ptr, blen);
-
- thislen -= blen;
- rq->buffer += blen;
- rq->nr_sectors -= (blen >> 9);
- rq->current_nr_sectors -= (blen >> 9);
- rq->sector += (blen >> 9);
-
- /*
- * current buffer complete, move on
- */
- if (rq->current_nr_sectors == 0 && rq->nr_sectors)
- cdrom_end_request(drive, 1);
- }
-
- /* re-arm handler */
- ide_set_handler(drive, cdrom_rw_intr, ATAPI_WAIT_PC, NULL);
- return ide_started;
-}
-