📄 sramdrv.c
字号:
/* sramDrv.c - PCMCIA SRAM device driver *//* Copyright 1984-1996 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01e,21jun00,rsh upgrade to dosFs 2.001d,16jan97,hdn added pCtrl->memBase.01c,28mar96,jdi doc: cleaned up language and format.01b,22feb96,hdn cleaned up01a,28feb95,hdn written based on memDrv.c and ramDrv.c.*//*DESCRIPTIONThis is a device driver for the SRAM PC card. The memory location and sizeare specified when the "disk" is created.USER-CALLABLE ROUTINESMost of the routines in this driver are accessible only through the I/Osystem. However, two routines must be called directly: sramDrv() toinitialize the driver, and sramDevCreate() to create block devices.Additionally, the sramMap() routine is called directly to map the PCMCIAmemory onto the ISA address space. Note that this routine does not useany mutual exclusion or synchronization mechanism; thus, special care mustbe taken in the multitasking environment.Before using this driver, it must be initialized by calling sramDrv(). Thisroutine should be called only once, before any reads, writes, or calls tosramDevCreate() or sramMap(). It can be called from usrRoot() in usrConfig.cor at some later point.SEE ALSO:.pG "I/O System"LINTLIBRARY*/#include "vxWorks.h"#include "ioLib.h"#include "memLib.h"#include "errnoLib.h"#include "string.h"#include "stdlib.h"#include "stdio.h"#include "sysLib.h"#include "logLib.h"#include "private/semLibP.h"#include "drv/pcmcia/pcmciaLib.h"#include "drv/pcmcia/sramDrv.h"/* defines */#define SRAM_WINDOW 1/* imports */IMPORT PCMCIA_CTRL pcmciaCtrl;IMPORT SRAM_RESOURCE sramResources[];/* globals */SRAM_CTRL sramCtrl;int sramResourceNumEnt;/* locals */LOCAL BOOL sramDrvInstalled = FALSE;LOCAL PCMCIA_MEMWIN sramMemwin = {1,0,0,0,0,0};/* forward declarations */LOCAL STATUS sramRead (SRAM_DEV *pSramDev, int startBlk, int numBlks, char *pChar);LOCAL STATUS sramWrite (SRAM_DEV *pSramDev, int startBlk, int numBlks, char *pChar);LOCAL STATUS sramIoctl (SRAM_DEV *pSramDev, int function, int arg);LOCAL STATUS sramStatusChk (SRAM_DEV *pSramDev);/********************************************************************************* sramDrv - install a PCMCIA SRAM memory driver** This routine initializes a PCMCIA SRAM memory driver. It must be called once,* before any other routines in the driver.** RETURNS:* OK, or ERROR if the I/O system cannot install the driver.*/STATUS sramDrv ( int sock /* socket no. */ ) { PCMCIA_CTRL *pCtrl = &pcmciaCtrl; PCMCIA_CHIP *pChip = &pCtrl->chip; PCMCIA_CARD *pCard = &pCtrl->card[sock]; SEM_ID muteSemID = &sramCtrl.muteSem[sock]; SEM_ID syncSemID = &sramCtrl.syncSem[sock]; int ix; if ((!pChip->installed) || (!pCard->detected)) return (ERROR); if (!sramDrvInstalled) { for (ix = 0; ix < MAX_SOCKETS; ix++) { semBInit (&sramCtrl.syncSem[ix], SEM_Q_FIFO, SEM_EMPTY); semMInit (&sramCtrl.muteSem[ix], SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE); } sramDrvInstalled = TRUE; } if (!pCard->installed) { semBInit (syncSemID, SEM_Q_FIFO, SEM_EMPTY); semMInit (muteSemID, SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE); } return (OK); }/********************************************************************************* sramMap - map PCMCIA memory onto a specified ISA address space** This routine maps PCMCIA memory onto a specified ISA address space.** RETURNS:* OK, or ERROR if the memory cannot be mapped.*/STATUS sramMap ( int sock, /* socket no. */ int type, /* 0: common 1: attribute */ int start, /* ISA start address */ int stop, /* ISA stop address */ int offset, /* card offset address */ int extraws /* extra wait state */ ) { PCMCIA_CTRL *pCtrl = &pcmciaCtrl; PCMCIA_CHIP *pChip = &pCtrl->chip; PCMCIA_MEMWIN memwin; if ((!pChip->installed) || (sock >= pChip->socks)) return (ERROR); memwin.window = SRAM_WINDOW; memwin.flags = MAP_ACTIVE | MAP_16BIT; if (type == 1) memwin.flags = MAP_ACTIVE | MAP_16BIT | MAP_ATTRIB; memwin.extraws = extraws; memwin.start = start; memwin.stop = stop; memwin.cardstart = offset & 0xfffff000; if ((* pChip->memwinSet)(sock, &memwin) != OK) return (ERROR); return (OK); }/********************************************************************************* sramDevCreate - create a PCMCIA memory disk device** This routine creates a PCMCIA memory disk device.** RETURNS:* A pointer to a block device structure (BLK_DEV), or NULL if memory cannot* be allocated for the device structure.** SEE ALSO: ramDevCreate()*/BLK_DEV *sramDevCreate ( int sock, /* socket no. */ int bytesPerBlk, /* number of bytes per block */ int blksPerTrack, /* number of blocks per track */ int nBlocks, /* number of blocks on this device */ int blkOffset /* no. of blks to skip at start of device */ ) { PCMCIA_CTRL *pCtrl = &pcmciaCtrl; PCMCIA_CHIP *pChip = &pCtrl->chip; PCMCIA_CARD *pCard = &pCtrl->card[sock]; SRAM_RESOURCE *pSram = &sramResources[sock]; PCCARD_RESOURCE *pResource = &pSram->resource; SRAM_DEV *pSramDev; /* ptr to created SRAM_DEV struct */ BLK_DEV *pBlkDev; /* ptr to BLK_DEV struct in SRAM_DEV */ if ((!pChip->installed) || (!pCard->installed) || (sock >= sramResourceNumEnt)) return (NULL); /* Set up defaults for any values not specified */ if (bytesPerBlk == 0) bytesPerBlk = DEFAULT_SEC_SIZE; if (nBlocks == 0) nBlocks = DEFAULT_DISK_SIZE / bytesPerBlk; if (blksPerTrack == 0) blksPerTrack = nBlocks; if ((bytesPerBlk * nBlocks) >= pResource->memLength) return (NULL); /* Allocate a SRAM_DEV structure for device */ pSramDev = (SRAM_DEV *) malloc (sizeof (SRAM_DEV)); if (pSramDev == NULL) return (NULL); /* no memory */ /* Initialize BLK_DEV structure (in SRAM_DEV) */ pBlkDev = &pSramDev->blkDev; pBlkDev->bd_nBlocks = nBlocks; /* number of blocks */ pBlkDev->bd_bytesPerBlk = bytesPerBlk; /* bytes per block */ pBlkDev->bd_blksPerTrack = blksPerTrack; /* blocks per track */ pBlkDev->bd_nHeads = 1; /* one "head" */ pBlkDev->bd_removable = TRUE; /* removable */ pBlkDev->bd_retry = 1; /* retry count */ pBlkDev->bd_readyChanged = TRUE; /* new ready status */ pBlkDev->bd_mode = O_RDWR; /* initial mode for device */ pBlkDev->bd_blkRd = sramRead; /* read block function */ pBlkDev->bd_blkWrt = sramWrite; /* write block function */ pBlkDev->bd_ioctl = sramIoctl; /* ioctl function */ pBlkDev->bd_reset = NULL; /* no reset function */ pBlkDev->bd_statusChk = sramStatusChk; /* check-status function */ pBlkDev->bd_statusChk = NULL; /* check-status function */ /* Initialize remainder of device struct */ pSramDev->blkOffset = blkOffset; /* block offset */ pSramDev->sock = sock; /* socket no. */ return (&pSramDev->blkDev); }/********************************************************************************* sramRead - read one or more blocks from a PCMCIA memory disk volume** This routine reads one or more blocks from the specified volume,* starting with the specified block number. The byte offset is* calculated and the PCMCIA memory disk data is copied to the specified buffer.** If any block offset was specified during sramDevCreate(), it is added* to <startBlk> before the transfer takes place.** RETURNS: OK, or ERROR if mapping or read fails.*/LOCAL STATUS sramRead ( SRAM_DEV *pSramDev, /* pointer to device desriptor */ int startBlk, /* starting block number to read */ int numBlks, /* number of blocks to read */ char *pChar /* pointer to buffer to receive data */ ) { PCMCIA_CTRL *pCtrl = &pcmciaCtrl; PCMCIA_CHIP *pChip = &pCtrl->chip; PCMCIA_CARD *pCard = &pCtrl->card[pSramDev->sock]; SRAM_RESOURCE *pSram = &sramResources[pSramDev->sock]; PCCARD_RESOURCE *pResource = &pSram->resource; SEM_ID muteSemID = &sramCtrl.muteSem[pSramDev->sock]; SEM_ID syncSemID = &sramCtrl.syncSem[pSramDev->sock]; u_int bytesPerBlk = pSramDev->blkDev.bd_bytesPerBlk; u_int windowSize = pResource->memStop - pResource->memStart + 1; u_int nbytes = bytesPerBlk * numBlks; u_int offset = (u_int)pResource->memOffset + ((startBlk + pSramDev->blkOffset) * bytesPerBlk); u_int copiedBytes = 0; u_int length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -