📄 ide-cd.c
字号:
/* If we're not done filling the current buffer, complain. Otherwise, complete the command normally. */ if (rq->current_nr_sectors > 0) { printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n", drive->name, rq->current_nr_sectors); cdrom_end_request (0, drive); } else cdrom_end_request (1, drive); return; } /* Check that the drive is expecting to do the same thing we are. */ if (cdrom_read_check_ireason (drive, len, ireason)) return; /* Assume that the drive will always provide data in multiples of at least SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise. */ if ((len % SECTOR_SIZE) != 0) { printk ("%s: cdrom_read_intr: Bad transfer size %d\n", drive->name, len); if (CDROM_CONFIG_FLAGS (drive)->limit_nframes) printk (" This drive is not supported by this version of the driver\n"); else { printk (" Trying to limit transfer sizes\n"); CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1; } cdrom_end_request (0, drive); return; } /* The number of sectors we need to read from the drive. */ sectors_to_transfer = len / SECTOR_SIZE; /* First, figure out if we need to bit-bucket any of the leading sectors. */ nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)), sectors_to_transfer); while (nskip > 0) { /* We need to throw away a sector. */ char dum[SECTOR_SIZE]; atapi_input_bytes (drive, dum, sizeof (dum)); --rq->current_nr_sectors; --nskip; --sectors_to_transfer; } /* Now loop while we still have data to read from the drive. */ while (sectors_to_transfer > 0) { int this_transfer; /* If we've filled the present buffer but there's another chained buffer after it, move on. */ if (rq->current_nr_sectors == 0 && rq->nr_sectors > 0) cdrom_end_request (1, drive); /* If the buffers are full, cache the rest of the data in our internal buffer. */ if (rq->current_nr_sectors == 0) { cdrom_buffer_sectors (drive, rq->sector, sectors_to_transfer); sectors_to_transfer = 0; } else { /* Transfer data to the buffers. Figure out how many sectors we can transfer to the current buffer. */ this_transfer = MIN (sectors_to_transfer, rq->current_nr_sectors); /* Read this_transfer sectors into the current buffer. */ while (this_transfer > 0) { atapi_input_bytes (drive, rq->buffer, SECTOR_SIZE); rq->buffer += SECTOR_SIZE; --rq->nr_sectors; --rq->current_nr_sectors; ++rq->sector; --this_transfer; --sectors_to_transfer; } } } /* Done moving data! Wait for another interrupt. */ ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);}/* * Try to satisfy some of the current read request from our cached data. * Returns nonzero if the request has been completed, zero otherwise. */static int cdrom_read_from_buffer (ide_drive_t *drive){ struct cdrom_info *info = drive->driver_data; struct request *rq = HWGROUP(drive)->rq; /* Can't do anything if there's no buffer. */ if (info->sector_buffer == NULL) return 0; /* Loop while this request needs data and the next block is present in our cache. */ while (rq->nr_sectors > 0 && rq->sector >= info->sector_buffered && rq->sector < info->sector_buffered + info->nsectors_buffered) { if (rq->current_nr_sectors == 0) cdrom_end_request (1, drive); memcpy (rq->buffer, info->sector_buffer + (rq->sector - info->sector_buffered) * SECTOR_SIZE, SECTOR_SIZE); rq->buffer += SECTOR_SIZE; --rq->current_nr_sectors; --rq->nr_sectors; ++rq->sector; } /* If we've satisfied the current request, terminate it successfully. */ if (rq->nr_sectors == 0) { cdrom_end_request (1, drive); return -1; } /* Move on to the next buffer if needed. */ if (rq->current_nr_sectors == 0) cdrom_end_request (1, drive); /* If this condition does not hold, then the kluge i use to represent the number of sectors to skip at the start of a transfer will fail. I think that this will never happen, but let's be paranoid and check. */ if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) && (rq->sector % SECTORS_PER_FRAME) != 0) { printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n", drive->name, rq->sector); cdrom_end_request (0, drive); return -1; } return 0;}/* * Routine to send a read packet command to the drive. * This is usually called directly from cdrom_start_read. * However, for drq_interrupt devices, it is called from an interrupt * when the drive is ready to accept the command. */static void cdrom_start_read_continuation (ide_drive_t *drive){ struct packet_command pc; struct request *rq = HWGROUP(drive)->rq; int nsect, sector, nframes, frame, nskip; /* Number of sectors to transfer. */ nsect = rq->nr_sectors; /* Starting sector. */ sector = rq->sector; /* If the requested sector doesn't start on a cdrom block boundary, we must adjust the start of the transfer so that it does, and remember to skip the first few sectors. If the CURRENT_NR_SECTORS field is larger than the size of the buffer, it will mean that we're to skip a number of sectors equal to the amount by which CURRENT_NR_SECTORS is larger than the buffer size. */ nskip = (sector % SECTORS_PER_FRAME); if (nskip > 0) { /* Sanity check... */ if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS)) { printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n", drive->name, rq->current_nr_sectors); cdrom_end_request (0, drive); return; } sector -= nskip; nsect += nskip; rq->current_nr_sectors += nskip; } /* Convert from sectors to cdrom blocks, rounding up the transfer length if needed. */ nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME; frame = sector / SECTORS_PER_FRAME; /* Largest number of frames was can transfer at once is 64k-1. For some drives we need to limit this even more. */ nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ? (65534 / CD_FRAMESIZE) : 65535); /* Set up the command */ memset (&pc.c, 0, sizeof (pc.c)); pc.c[0] = READ_10; pc.c[7] = (nframes >> 8); pc.c[8] = (nframes & 0xff); put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]); /* Send the command to the drive and return. */ (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c), &cdrom_read_intr);}#define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */#define IDECD_SEEK_TIMER (2 * WAIT_MIN_SLEEP) /* 40 ms */#define IDECD_SEEK_TIMEOUT WAIT_CMD /* 10 sec */static void cdrom_seek_intr (ide_drive_t *drive){ struct cdrom_info *info = drive->driver_data; int stat; static int retry = 10; if (cdrom_decode_status (drive, 0, &stat)) return; CDROM_CONFIG_FLAGS(drive)->seeking = 1; if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) { if (--retry == 0) { printk ("%s: disabled DSC seek overlap\n", drive->name); drive->dsc_overlap = 0; } }}static void cdrom_start_seek_continuation (ide_drive_t *drive){ struct packet_command pc; struct request *rq = HWGROUP(drive)->rq; int sector, frame, nskip; sector = rq->sector; nskip = (sector % SECTORS_PER_FRAME); if (nskip > 0) sector -= nskip; frame = sector / SECTORS_PER_FRAME; memset (&pc.c, 0, sizeof (pc.c)); pc.c[0] = SEEK; put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]); (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c), &cdrom_seek_intr);}static void cdrom_start_seek (ide_drive_t *drive, unsigned int block){ struct cdrom_info *info = drive->driver_data; info->dma = 0; info->start_seek = jiffies; cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);}/* * Start a read request from the CD-ROM. */static void cdrom_start_read (ide_drive_t *drive, unsigned int block){ struct cdrom_info *info = drive->driver_data; struct request *rq = HWGROUP(drive)->rq; int minor = MINOR (rq->rq_dev); /* If the request is relative to a partition, fix it up to refer to the absolute address. */ if ((minor & PARTN_MASK) != 0) { rq->sector = block; minor &= ~PARTN_MASK; rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor); } /* We may be retrying this request after an error. Fix up any weirdness which might be present in the request packet. */ restore_request (rq); /* Satisfy whatever we can of this request from our cached sector. */ if (cdrom_read_from_buffer (drive)) return; /* Clear the local sector buffer. */ info->nsectors_buffered = 0; if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) && (rq->nr_sectors % SECTORS_PER_FRAME == 0)) info->dma = 1; else info->dma = 0; /* Start sending the read request to the drive. */ cdrom_start_packet_command (drive, 32768, cdrom_start_read_continuation);}/**************************************************************************** * Execute all other packet commands. *//* Forward declarations. */static intcdrom_lockdoor (ide_drive_t *drive, int lockflag, struct atapi_request_sense *reqbuf);/* Interrupt routine for packet command completion. */static void cdrom_pc_intr (ide_drive_t *drive){ int ireason, len, stat, thislen; struct request *rq = HWGROUP(drive)->rq; struct packet_command *pc = (struct packet_command *)rq->buffer; /* Check for errors. */ if (cdrom_decode_status (drive, 0, &stat)) return; /* Read the interrupt reason and the transfer length. */ ireason = IN_BYTE (IDE_NSECTOR_REG); len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG); /* If DRQ is clear, the command has completed. Complain if we still have data left to transfer. */ if ((stat & DRQ_STAT) == 0) { /* Some of the trailing request sense fields are optional, and some drives don't send them. Sigh. */ if (pc->c[0] == REQUEST_SENSE && pc->buflen > 0 && pc->buflen <= 5) { while (pc->buflen > 0) { *pc->buffer++ = 0; --pc->buflen; } } if (pc->buflen == 0) cdrom_end_request (1, drive); else { /* Comment this out, because this always happens right after a reset occurs, and it is annoying to always print expected stuff. */ /* printk ("%s: cdrom_pc_intr: data underrun %d\n", drive->name, pc->buflen); */ pc->stat = 1; cdrom_end_request (1, drive); } return; } /* Figure out how much data to transfer. */ thislen = pc->buflen; if (thislen < 0) thislen = -thislen; if (thislen > len) thislen = len; /* The drive wants to be written to. */ if ((ireason & 3) == 0) { /* Check that we want to write. */ if (pc->buflen > 0) { printk ("%s: cdrom_pc_intr: Drive wants " "to transfer data the wrong way!\n", drive->name); pc->stat = 1; thislen = 0; } /* Transfer the data. */ atapi_output_bytes (drive, pc->buffer, thislen); /* If we haven't moved enough data to satisfy the drive, add some padding. */ while (len > thislen) { int dum = 0; atapi_output_bytes (drive, &dum, sizeof (dum)); len -= sizeof (dum); } /* Keep count of how much data we've moved. */ pc->buffer += thislen; pc->buflen += thislen; } /* Same drill for reading. */ else if ((ireason & 3) == 2) { /* Check that we want to read. */ if (pc->buflen < 0) { printk ("%s: cdrom_pc_intr: Drive wants to " "transfer data the wrong way!\n", drive->name); pc->stat = 1; thislen = 0; } /* Transfer the data. */ atapi_input_bytes (drive, pc->buffer, thislen); /* If we haven't moved enough data to satisfy the drive, add some padding. */ while (len > thislen) { int dum = 0; atapi_input_bytes (drive, &dum, sizeof (dum)); len -= sizeof (dum); } /* Keep count of how much data we've moved. */ pc->buffer += thislen; pc->buflen -= thislen; } else { printk ("%s: cdrom_pc_intr: The drive " "appears confused (ireason = 0x%2x)\n", drive->name, ireason); pc->stat = 1; } /* Now we wait for another interrupt. */ ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -