⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ide-cd.c

📁 讲述linux的初始化过程
💻 C
📖 第 1 页 / 共 5 页
字号:
 * the buffer is cleared.) */static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,                                  int sectors_to_transfer){	struct cdrom_info *info = drive->driver_data;	/* Number of sectors to read into the buffer. */	int sectors_to_buffer = MIN (sectors_to_transfer,				     (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -				       info->nsectors_buffered);	char *dest;	/* If we couldn't get a buffer, don't try to buffer anything... */	if (info->buffer == NULL)		sectors_to_buffer = 0;	/* If this is the first sector in the buffer, remember its number. */	if (info->nsectors_buffered == 0)		info->sector_buffered = sector;	/* Read the data into the buffer. */	dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;	while (sectors_to_buffer > 0) {		atapi_input_bytes (drive, dest, SECTOR_SIZE);		--sectors_to_buffer;		--sectors_to_transfer;		++info->nsectors_buffered;		dest += SECTOR_SIZE;	}	/* Throw away any remaining data. */	while (sectors_to_transfer > 0) {		char dum[SECTOR_SIZE];		atapi_input_bytes (drive, dum, sizeof (dum));		--sectors_to_transfer;	}}/* * Check the contents of the interrupt reason register from the cdrom * and attempt to recover if there are problems.  Returns  0 if everything's * ok; nonzero if the request has been terminated. */static inlineint cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason){	ireason &= 3;	if (ireason == 2) return 0;	if (ireason == 0) {		/* Whoops... The drive is expecting to receive data from us! */		printk ("%s: cdrom_read_intr: "			"Drive wants to transfer data the wrong way!\n",			drive->name);		/* Throw some data at the drive so it doesn't hang		   and quit this request. */		while (len > 0) {			int dum = 0;			atapi_output_bytes (drive, &dum, sizeof (dum));			len -= sizeof (dum);		}	} else  if (ireason == 1) {		/* Some drives (ASUS) seem to tell us that status		 * info is available. just get it and ignore.		 */		GET_STAT();		return 0;	} else {		/* Drive wants a command packet, or invalid ireason... */		printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",			drive->name, ireason);	}	cdrom_end_request (0, drive);	return -1;}/* * Interrupt routine.  Called when a read request has completed. */static ide_startstop_t cdrom_read_intr (ide_drive_t *drive){	int stat;	int ireason, len, sectors_to_transfer, nskip;	struct cdrom_info *info = drive->driver_data;	int i, dma = info->dma, dma_error = 0;	ide_startstop_t startstop;	struct request *rq = HWGROUP(drive)->rq;	/* Check for errors. */	if (dma) {		info->dma = 0;		if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))			HWIF(drive)->dmaproc(ide_dma_off, drive);	}	if (cdrom_decode_status (&startstop, drive, 0, &stat))		return startstop; 	if (dma) {		if (!dma_error) {			for (i = rq->nr_sectors; i > 0;) {				i -= rq->current_nr_sectors;				ide_end_request(1, HWGROUP(drive));			}			return ide_stopped;		} else			return ide_error (drive, "dma error", stat);	}	/* 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. */	if ((stat & DRQ_STAT) == 0) {		/* 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 ide_stopped;	}	/* Check that the drive is expecting to do the same thing we are. */	if (cdrom_read_check_ireason (drive, len, ireason))		return ide_stopped;	/* 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 ide_stopped;	}	/* 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, NULL);	return ide_started;}/* * 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->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->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 ide_startstop_t 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) &&			(rq->sector % CD_FRAMESIZE != 0)) {			printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",				drive->name, rq->current_nr_sectors);			cdrom_end_request (0, drive);			return ide_stopped;		}		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] = GPCMD_READ_10;	pc.c[7] = (nframes >> 8);	pc.c[8] = (nframes & 0xff);	put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);	pc.timeout = WAIT_CMD;	/* Send the command to the drive and return. */	return cdrom_transfer_packet_command(drive, &pc, &cdrom_read_intr);}#define IDECD_SEEK_THRESHOLD	(1000)			/* 1000 blocks */#define IDECD_SEEK_TIMER	(5 * WAIT_MIN_SLEEP)	/* 100 ms */#define IDECD_SEEK_TIMEOUT     WAIT_CMD			/* 10 sec */static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive){	struct cdrom_info *info = drive->driver_data;	int stat;	static int retry = 10;	ide_startstop_t startstop;	if (cdrom_decode_status (&startstop, drive, 0, &stat))		return startstop;	CDROM_CONFIG_FLAGS(drive)->seeking = 1;	if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {		if (--retry == 0) {			/*			 * this condition is far too common, to bother			 * users about it			 */#if 0			printk("%s: disabled DSC seek overlap\n", drive->name);#endif			drive->dsc_overlap = 0;		}	}	return ide_stopped;}static ide_startstop_t 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] = GPCMD_SEEK;	put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);	pc.timeout = WAIT_CMD;	return cdrom_transfer_packet_command(drive, &pc, &cdrom_seek_intr);}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);}/* Fix up a possibly partially-processed request so that we can   start it over entirely, or even put it back on the request queue. */static void restore_request (struct request *rq){	if (rq->buffer != rq->bh->b_data) {		int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;		rq->buffer = rq->bh->b_data;		rq->nr_sectors += n;		rq->sector -= n;	}	rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;}/* * Start a read request from the CD-ROM. */static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -