00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00059 #include <errno.h>
00060
00061 #include <fs/fs.h>
00062
00063 #include <fs/phatfs.h>
00064 #include <fs/phatvol.h>
00065 #include <dev/blockdev.h>
00066 #include <fs/phatio.h>
00067
00068 #include <sys/event.h>
00069
00070 #if 0
00071
00072 #define NUTDEBUG
00073 #include <stdio.h>
00074 #endif
00075
00080
00091 int PhatSectorFlush(NUTDEVICE * dev, int bufnum)
00092 {
00093 BLKPAR_SEEK pars;
00094 int sbn;
00095 PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00096 NUTFILE *blkmnt = dev->dev_icb;
00097 NUTDEVICE *blkdev = blkmnt->nf_dev;
00098
00099 if (bufnum < 0) {
00100 sbn = 0;
00101 #if PHAT_SECTOR_BUFFERS
00102 bufnum = PHAT_SECTOR_BUFFERS - 1;
00103 #else
00104 bufnum = 0;
00105 #endif
00106 }
00107 else {
00108 sbn = bufnum;
00109 }
00110
00111 while (sbn <= bufnum) {
00112 if (vol->vol_buf[sbn].sect_dirty) {
00113 pars.par_nfp = blkmnt;
00114 pars.par_blknum = vol->vol_buf[sbn].sect_num;
00115 if ((*blkdev->dev_ioctl) (blkdev, NUTBLKDEV_SEEK, &pars)) {
00116 errno = EIO;
00117 return -1;
00118 }
00119 if ((*blkdev->dev_write) (blkmnt, vol->vol_buf[sbn].sect_data, 1) != 1) {
00120 errno = EIO;
00121 return -1;
00122 }
00123 vol->vol_buf[sbn].sect_dirty = 0;
00124 }
00125 sbn++;
00126 }
00127 return 0;
00128 }
00129
00141 int PhatSectorRead(NUTFILE * blkmnt, u_long sect, u_char * buf)
00142 {
00143 BLKPAR_SEEK pars;
00144 NUTDEVICE *blkdev = blkmnt->nf_dev;
00145
00146
00147 pars.par_nfp = blkmnt;
00148 pars.par_blknum = sect;
00149 if ((*blkdev->dev_ioctl) (blkdev, NUTBLKDEV_SEEK, &pars)) {
00150 errno = EIO;
00151 return -1;
00152 }
00153
00154
00155 if ((*blkdev->dev_read) (blkmnt, buf, 1) != 1) {
00156 errno = EIO;
00157 return -1;
00158 }
00159 return 0;
00160 }
00161
00162
00163
00164
00165
00166 int PhatSectorLoad(NUTDEVICE * dev, u_long sect)
00167 {
00168 int sbn;
00169 PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
00170
00171
00172 NutEventWait(&vol->vol_iomutex, 0);
00173
00174 #if PHAT_SECTOR_BUFFERS
00175 for (sbn = 0; sbn < PHAT_SECTOR_BUFFERS; sbn++) {
00176 if (vol->vol_buf[sbn].sect_num == sect) {
00177 vol->vol_usenext = sbn;
00178
00179 NutEventPostAsync(&vol->vol_iomutex);
00180 return sbn;
00181 }
00182 }
00183
00184
00185 vol->vol_usenext++;
00186 if (vol->vol_usenext >= PHAT_SECTOR_BUFFERS) {
00187 vol->vol_usenext = 0;
00188 }
00189 sbn = vol->vol_usenext;
00190 #else
00191 sbn = 0;
00192 if (vol->vol_buf[sbn].sect_num == sect) {
00193
00194 NutEventPostAsync(&vol->vol_iomutex);
00195 return sbn;
00196 }
00197 #endif
00198
00199 if (PhatSectorFlush(dev, sbn)) {
00200 sbn = -1;
00201 }
00202 else if (PhatSectorRead(dev->dev_icb, sect, vol->vol_buf[sbn].sect_data)) {
00203 sbn = -1;
00204 }
00205 else {
00206 vol->vol_buf[sbn].sect_num = sect;
00207 }
00208
00209
00210 NutEventPostAsync(&vol->vol_iomutex);
00211
00212 return sbn;
00213 }
00214