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

📄 ide-cd.c

📁 GNU Mach 微内核源代码, 基于美国卡内基美隆大学的 Mach 研究项目
💻 C
📖 第 1 页 / 共 5 页
字号:
	char *dest;	/* If we don't yet have a sector buffer, try to allocate one.	   If we can't get one atomically, it's not fatal -- we'll just throw	   the data away rather than caching it. */	if (info->sector_buffer == NULL) {		info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,							GFP_ATOMIC);		/* If we couldn't get a buffer,		   don't try to buffer anything... */		if (info->sector_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->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;	while (sectors_to_buffer > 0) {		cdrom_in_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];		cdrom_in_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;			cdrom_out_bytes (drive, &dum, sizeof (dum));			len -= sizeof (dum);		}	} 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 void cdrom_read_intr (ide_drive_t *drive){	int stat;	int ireason, len, sectors_to_transfer, nskip;	struct request *rq = HWGROUP(drive)->rq;	/* 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. */	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;	}	/* 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);		printk ("  This drive is not supported by this version of the driver\n");		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];		cdrom_in_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) {				cdrom_in_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->cdrom_info;	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;#if !STANDARD_ATAPI	if (nsect > drive->cdrom_info.max_sectors)		nsect = drive->cdrom_info.max_sectors;#endif /* not STANDARD_ATAPI */	/* 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. */	nframes = MIN (nframes, 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);}/* * Start a read request from the CD-ROM. */static void cdrom_start_read (ide_drive_t *drive, unsigned int block){	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. */	drive->cdrom_info.nsectors_buffered = 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 {			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. */		cdrom_out_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;			cdrom_out_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. */		cdrom_in_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;			cdrom_in_bytes (drive, &dum, sizeof (dum));			len -= sizeof (dum);		}		/* Keep count of how much data we've moved. */

⌨️ 快捷键说明

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