linux/drivers/staging/keucr/smilmain.c
<<
>>
Prefs
   1#include <linux/slab.h>
   2#include "usb.h"
   3#include "scsiglue.h"
   4#include "smcommon.h"
   5#include "smil.h"
   6
   7int         Check_D_LogCHS              (WORD *,BYTE *,BYTE *);
   8void        Initialize_D_Media          (void);
   9void        PowerOff_D_Media            (void);
  10int         Check_D_MediaPower          (void);
  11int         Check_D_MediaExist          (void);
  12int         Check_D_MediaWP             (void);
  13int         Check_D_MediaFmt            (struct us_data *);
  14int         Check_D_MediaFmtForEraseAll (struct us_data *);
  15int         Conv_D_MediaAddr            (struct us_data *, DWORD);
  16int         Inc_D_MediaAddr             (struct us_data *);
  17int         Check_D_FirstSect           (void);
  18int         Check_D_LastSect            (void);
  19int         Media_D_ReadOneSect         (struct us_data *, WORD, BYTE *);
  20int         Media_D_WriteOneSect        (struct us_data *, WORD, BYTE *);
  21int         Media_D_CopyBlockHead       (struct us_data *);
  22int         Media_D_CopyBlockTail       (struct us_data *);
  23int         Media_D_EraseOneBlock       (void);
  24int         Media_D_EraseAllBlock       (void);
  25
  26int  Copy_D_BlockAll             (struct us_data *, DWORD);
  27int  Copy_D_BlockHead            (struct us_data *);
  28int  Copy_D_BlockTail            (struct us_data *);
  29int  Reassign_D_BlockHead        (struct us_data *);
  30
  31int  Assign_D_WriteBlock         (void);
  32int  Release_D_ReadBlock         (struct us_data *);
  33int  Release_D_WriteBlock        (struct us_data *);
  34int  Release_D_CopySector        (struct us_data *);
  35
  36int  Copy_D_PhyOneSect           (struct us_data *);
  37int  Read_D_PhyOneSect           (struct us_data *, WORD, BYTE *);
  38int  Write_D_PhyOneSect          (struct us_data *, WORD, BYTE *);
  39int  Erase_D_PhyOneBlock         (struct us_data *);
  40
  41int  Set_D_PhyFmtValue           (struct us_data *);
  42int  Search_D_CIS                (struct us_data *);
  43int  Make_D_LogTable             (struct us_data *);
  44void Check_D_BlockIsFull         (void);
  45
  46int  MarkFail_D_PhyOneBlock      (struct us_data *);
  47
  48DWORD ErrXDCode;
  49DWORD ErrCode;
  50//BYTE  SectBuf[SECTSIZE];
  51static BYTE  WorkBuf[SECTSIZE];
  52static BYTE  Redundant[REDTSIZE];
  53static BYTE  WorkRedund[REDTSIZE];
  54//WORD  Log2Phy[MAX_ZONENUM][MAX_LOGBLOCK];
  55static WORD  *Log2Phy[MAX_ZONENUM];                 // 128 x 1000,   Log2Phy[MAX_ZONENUM][MAX_LOGBLOCK];
  56static BYTE  Assign[MAX_ZONENUM][MAX_BLOCKNUM/8];
  57static WORD  AssignStart[MAX_ZONENUM];
  58WORD  ReadBlock;
  59WORD  WriteBlock;
  60DWORD MediaChange;
  61static DWORD SectCopyMode;
  62
  63//BIT Control Macro
  64static BYTE BitData[] = { 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 } ;
  65#define Set_D_Bit(a,b)    (a[(BYTE)((b)/8)]|= BitData[(b)%8])
  66#define Clr_D_Bit(a,b)    (a[(BYTE)((b)/8)]&=~BitData[(b)%8])
  67#define Chk_D_Bit(a,b)    (a[(BYTE)((b)/8)] & BitData[(b)%8])
  68
  69//extern PBYTE    SMHostAddr;
  70BYTE     IsSSFDCCompliance;
  71BYTE     IsXDCompliance;
  72
  73
  74//
  75////Power Control & Media Exist Check Function
  76////----- Init_D_SmartMedia() --------------------------------------------
  77//int Init_D_SmartMedia(void)
  78//{
  79//    int     i;
  80//
  81//    EMCR_Print("Init_D_SmartMedia start\n");
  82//    for (i=0; i<MAX_ZONENUM; i++)
  83//    {
  84//        if (Log2Phy[i]!=NULL)
  85//        {
  86//            EMCR_Print("ExFreePool Zone = %x, Addr = %x\n", i, Log2Phy[i]);
  87//            ExFreePool(Log2Phy[i]);
  88//            Log2Phy[i] = NULL;
  89//        }
  90//    }
  91//
  92//    Initialize_D_Media();
  93//    return(NO_ERROR);
  94//}
  95
  96//----- SM_FreeMem() -------------------------------------------------
  97int SM_FreeMem(void)
  98{
  99        int     i;
 100
 101        pr_info("SM_FreeMem start\n");
 102        for (i=0; i<MAX_ZONENUM; i++)
 103        {
 104                if (Log2Phy[i]!=NULL)
 105                {
 106                        pr_info("Free Zone = %x, Addr = %p\n", i, Log2Phy[i]);
 107                        kfree(Log2Phy[i]);
 108                        Log2Phy[i] = NULL;
 109                }
 110        }
 111        return(NO_ERROR);
 112}
 113
 114////----- Pwoff_D_SmartMedia() -------------------------------------------
 115//int Pwoff_D_SmartMedia(void)
 116//{
 117//    PowerOff_D_Media();
 118//    return(NO_ERROR);
 119//}
 120//
 121////----- Check_D_SmartMedia() -------------------------------------------
 122//int Check_D_SmartMedia(void)
 123//{
 124//    if (Check_D_MediaExist())
 125//        return(ErrCode);
 126//
 127//    return(NO_ERROR);
 128//}
 129//
 130////----- Check_D_Parameter() --------------------------------------------
 131//int Check_D_Parameter(PFDO_DEVICE_EXTENSION fdoExt,WORD *pcyl,BYTE *phead,BYTE *psect)
 132//{
 133//    if (Check_D_MediaPower())
 134//        return(ErrCode);
 135//
 136//    if (Check_D_MediaFmt(fdoExt))
 137//        return(ErrCode);
 138//
 139//    if (Check_D_LogCHS(pcyl,phead,psect))
 140//        return(ErrCode);
 141//
 142//    return(NO_ERROR);
 143//}
 144
 145//SmartMedia Read/Write/Erase Function
 146//----- Media_D_ReadSector() -------------------------------------------
 147int Media_D_ReadSector(struct us_data *us, DWORD start,WORD count,BYTE *buf)
 148{
 149        WORD len, bn;
 150
 151        //if (Check_D_MediaPower())        ; ¦b 6250 don't care
 152        //    return(ErrCode);
 153        //if (Check_D_MediaFmt(fdoExt))    ;
 154        //    return(ErrCode);
 155        if (Conv_D_MediaAddr(us, start))
 156                return(ErrCode);
 157
 158        while(1)
 159        {
 160                len = Ssfdc.MaxSectors - Media.Sector;
 161                if (count > len)
 162                        bn = len;
 163                else
 164                        bn = count;
 165                //if (Media_D_ReadOneSect(fdoExt, SectBuf))
 166                //if (Media_D_ReadOneSect(fdoExt, count, buf))
 167                if (Media_D_ReadOneSect(us, bn, buf))
 168                {
 169                        ErrCode = ERR_EccReadErr;
 170                        return(ErrCode);
 171                }
 172
 173                Media.Sector += bn;
 174                count -= bn;
 175
 176                if (count<=0)
 177                        break;
 178
 179                buf += bn * SECTSIZE;
 180
 181                if (Inc_D_MediaAddr(us))
 182                        return(ErrCode);
 183        }
 184
 185        return(NO_ERROR);
 186}
 187// here
 188//----- Media_D_CopySector() ------------------------------------------
 189int Media_D_CopySector(struct us_data *us, DWORD start,WORD count,BYTE *buf)
 190{
 191        //DWORD mode;
 192        //int i;
 193        WORD len, bn;
 194        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 195        //ADDRESS_T   bb = (ADDRESS_T) &Media;
 196
 197        /* pr_info("Media_D_CopySector !!!\n"); */
 198        if (Conv_D_MediaAddr(us, start))
 199                return(ErrCode);
 200
 201        while(1)
 202        {
 203                if (Assign_D_WriteBlock())
 204                        return(ERROR);
 205
 206                len = Ssfdc.MaxSectors - Media.Sector;
 207                if (count > len)
 208                        bn = len;
 209                else
 210                bn = count;
 211
 212                //if (Ssfdc_D_CopyBlock(fdoExt,count,buf,Redundant))
 213                if (Ssfdc_D_CopyBlock(us,bn,buf,Redundant))
 214                {
 215                        ErrCode = ERR_WriteFault;
 216                        return(ErrCode);
 217                }
 218
 219                Media.Sector = 0x1F;
 220                //if (Release_D_ReadBlock(fdoExt))
 221                if (Release_D_CopySector(us))
 222                {
 223                        if (ErrCode==ERR_HwError)
 224                        {
 225                                ErrCode = ERR_WriteFault;
 226                                return(ErrCode);
 227                        }
 228                }
 229                count -= bn;
 230
 231                if (count<=0)
 232                        break;
 233
 234                buf += bn * SECTSIZE;
 235
 236                if (Inc_D_MediaAddr(us))
 237                        return(ErrCode);
 238
 239        }
 240        return(NO_ERROR);
 241}
 242
 243//----- Release_D_CopySector() ------------------------------------------
 244int Release_D_CopySector(struct us_data *us)
 245{
 246        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 247        //ADDRESS_T   bb = (ADDRESS_T) &Media;
 248
 249        Log2Phy[Media.Zone][Media.LogBlock]=WriteBlock;
 250        Media.PhyBlock=ReadBlock;
 251
 252        if (Media.PhyBlock==NO_ASSIGN)
 253        {
 254                Media.PhyBlock=WriteBlock;
 255                return(SMSUCCESS);
 256        }
 257
 258        Clr_D_Bit(Assign[Media.Zone],Media.PhyBlock);
 259        Media.PhyBlock=WriteBlock;
 260
 261        return(SMSUCCESS);
 262}
 263/*
 264//----- Media_D_WriteSector() ------------------------------------------
 265int Media_D_WriteSector(PFDO_DEVICE_EXTENSION fdoExt, DWORD start,WORD count,BYTE *buf)
 266{
 267    int i;
 268    WORD len, bn;
 269    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 270    ADDRESS_T   bb = (ADDRESS_T) &Media;
 271
 272    //if (Check_D_MediaPower())
 273    //    return(ErrCode);
 274    //
 275    //if (Check_D_MediaFmt(fdoExt))
 276    //    return(ErrCode);
 277    //
 278    //if (Check_D_MediaWP())
 279    //    return(ErrCode);
 280
 281    if (Conv_D_MediaAddr(fdoExt, start))
 282        return(ErrCode);
 283
 284    //ENE_Print("Media_D_WriteSector --- Sector = %x\n", Media.Sector);
 285    if (Check_D_FirstSect())
 286    {
 287        if (Media_D_CopyBlockHead(fdoExt))
 288        {
 289            ErrCode = ERR_WriteFault;
 290            return(ErrCode);
 291        }
 292    }
 293
 294    while(1)
 295    {
 296        if (!Check_D_FirstSect())
 297        {
 298            if (Assign_D_WriteBlock())
 299                return(ErrCode);
 300        }
 301
 302        len = Ssfdc.MaxSectors - Media.Sector;
 303        if (count > len)
 304           bn = len;
 305        else
 306           bn = count;
 307        //for(i=0;i<SECTSIZE;i++)
 308        //    SectBuf[i]=*buf++;
 309
 310        //if (Media_D_WriteOneSect(fdoExt, SectBuf))
 311        if (Media_D_WriteOneSect(fdoExt, bn, buf))
 312        {
 313            ErrCode = ERR_WriteFault;
 314            return(ErrCode);
 315        }
 316
 317        Media.Sector += bn - 1;
 318
 319        if (!Check_D_LastSect())
 320        {
 321            if (Release_D_ReadBlock(fdoExt))
 322
 323            {    if (ErrCode==ERR_HwError)
 324                {
 325                    ErrCode = ERR_WriteFault;
 326                    return(ErrCode);
 327                }
 328            }
 329        }
 330
 331        count -= bn;
 332
 333        if (count<=0)
 334            break;
 335
 336        buf += bn * SECTSIZE;
 337
 338        //if (--count<=0)
 339        //    break;
 340
 341        if (Inc_D_MediaAddr(fdoExt))
 342            return(ErrCode);
 343    }
 344
 345    if (!Check_D_LastSect())
 346        return(NO_ERROR);
 347
 348    if (Inc_D_MediaAddr(fdoExt))
 349        return(ErrCode);
 350
 351    if (Media_D_CopyBlockTail(fdoExt))
 352    {
 353        ErrCode = ERR_WriteFault;
 354        return(ErrCode);
 355    }
 356
 357    return(NO_ERROR);
 358}
 359//
 360////----- Media_D_EraseBlock() -------------------------------------------
 361//int Media_D_EraseBlock(PFDO_DEVICE_EXTENSION fdoExt, DWORD start,WORD count)
 362//{
 363//    if (Check_D_MediaPower())
 364//        return(ErrCode);
 365//
 366//    if (Check_D_MediaFmt(fdoExt))
 367//        return(ErrCode);
 368//
 369//    if (Check_D_MediaWP())
 370//        return(ErrCode);
 371//
 372//    if (Conv_D_MediaAddr(start))
 373//        return(ErrCode);
 374//
 375//    while(Check_D_FirstSect()) {
 376//        if (Inc_D_MediaAddr(fdoExt))
 377//            return(ErrCode);
 378//
 379//        if (--count<=0)
 380//            return(NO_ERROR);
 381//    }
 382//
 383//    while(1) {
 384//        if (!Check_D_LastSect())
 385//            if (Media_D_EraseOneBlock())
 386//                if (ErrCode==ERR_HwError)
 387//                {
 388//                    ErrCode = ERR_WriteFault;
 389//                    return(ErrCode);
 390//                }
 391//
 392//        if (Inc_D_MediaAddr(fdoExt))
 393//            return(ErrCode);
 394//
 395//        if (--count<=0)
 396//            return(NO_ERROR);
 397//    }
 398//}
 399//
 400////----- Media_D_EraseAll() ---------------------------------------------
 401//int Media_D_EraseAll(PFDO_DEVICE_EXTENSION fdoExt)
 402//{
 403//    if (Check_D_MediaPower())
 404//        return(ErrCode);
 405//
 406//    if (Check_D_MediaFmtForEraseAll(fdoExt))
 407//        return(ErrCode);
 408//
 409//    if (Check_D_MediaWP())
 410//        return(ErrCode);
 411//
 412//    if (Media_D_EraseAllBlock())
 413//        return(ErrCode);
 414//
 415//    return(NO_ERROR);
 416//}
 417
 418//SmartMedia Write Function for One Sector Write Mode
 419//----- Media_D_OneSectWriteStart() ------------------------------------
 420int Media_D_OneSectWriteStart(PFDO_DEVICE_EXTENSION fdoExt,DWORD start,BYTE *buf)
 421{
 422//  int i;
 423//  SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 424//  ADDRESS_T   bb = (ADDRESS_T) &Media;
 425//
 426//  //if (Check_D_MediaPower())
 427//  //    return(ErrCode);
 428//  //if (Check_D_MediaFmt(fdoExt))
 429//  //    return(ErrCode);
 430//  //if (Check_D_MediaWP())
 431//  //    return(ErrCode);
 432//  if (Conv_D_MediaAddr(fdoExt, start))
 433//      return(ErrCode);
 434//
 435//  if (Check_D_FirstSect())
 436//      if (Media_D_CopyBlockHead(fdoExt))
 437//      {
 438//          ErrCode = ERR_WriteFault;
 439//          return(ErrCode);
 440//      }
 441//
 442//  if (!Check_D_FirstSect())
 443//      if (Assign_D_WriteBlock())
 444//          return(ErrCode);
 445//
 446//  //for(i=0;i<SECTSIZE;i++)
 447//  //    SectBuf[i]=*buf++;
 448//
 449//  //if (Media_D_WriteOneSect(fdoExt, SectBuf))
 450//  if (Media_D_WriteOneSect(fdoExt, buf))
 451//  {
 452//      ErrCode = ERR_WriteFault;
 453//      return(ErrCode);
 454//  }
 455//
 456//  if (!Check_D_LastSect())
 457//  {
 458//      if (Release_D_ReadBlock(fdoExt))
 459//          if (ErrCode==ERR_HwError)
 460//          {
 461//              ErrCode = ERR_WriteFault;
 462//              return(ErrCode);
 463//          }
 464//  }
 465
 466    return(NO_ERROR);
 467}
 468
 469//----- Media_D_OneSectWriteNext() -------------------------------------
 470int Media_D_OneSectWriteNext(PFDO_DEVICE_EXTENSION fdoExt, BYTE *buf)
 471{
 472//  int i;
 473//  SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 474//  ADDRESS_T   bb = (ADDRESS_T) &Media;
 475//
 476//  if (Inc_D_MediaAddr(fdoExt))
 477//      return(ErrCode);
 478//
 479//  if (!Check_D_FirstSect())
 480//    if (Assign_D_WriteBlock())
 481//      return(ErrCode);
 482//
 483//  //for(i=0;i<SECTSIZE;i++)
 484//  //    SectBuf[i]=*buf++;
 485//
 486//  //if (Media_D_WriteOneSect(fdoExt, SectBuf))
 487//  if (Media_D_WriteOneSect(fdoExt, buf))
 488//  {
 489//      ErrCode = ERR_WriteFault;
 490//      return(ErrCode);
 491//  }
 492//
 493//  if (!Check_D_LastSect())
 494//  {
 495//      if (Release_D_ReadBlock(fdoExt))
 496//          if (ErrCode==ERR_HwError)
 497//          {
 498//              ErrCode = ERR_WriteFault;
 499//              return(ErrCode);
 500//          }
 501//  }
 502
 503    return(NO_ERROR);
 504}
 505
 506//----- Media_D_OneSectWriteFlush() ------------------------------------
 507int Media_D_OneSectWriteFlush(PFDO_DEVICE_EXTENSION fdoExt)
 508{
 509    if (!Check_D_LastSect())
 510        return(NO_ERROR);
 511
 512    if (Inc_D_MediaAddr(fdoExt))
 513        return(ErrCode);
 514
 515    if (Media_D_CopyBlockTail(fdoExt))
 516    {
 517        ErrCode = ERR_WriteFault;
 518        return(ErrCode);
 519    }
 520
 521    return(NO_ERROR);
 522}
 523//
 524////LED Tern On/Off Subroutine
 525////----- SM_EnableLED() -----------------------------------------------
 526//void SM_EnableLED(PFDO_DEVICE_EXTENSION fdoExt, BOOLEAN enable)
 527//{
 528//    if (fdoExt->Drive_IsSWLED)
 529//    {
 530//        if (enable)
 531//           Led_D_TernOn();
 532//        else
 533//           Led_D_TernOff();
 534//    }
 535//}
 536//
 537////----- Led_D_TernOn() -------------------------------------------------
 538//void Led_D_TernOn(void)
 539//{
 540//    if (Check_D_CardStsChg())
 541//        MediaChange=ERROR;
 542//
 543//    Cnt_D_LedOn();
 544//}
 545//
 546////----- Led_D_TernOff() ------------------------------------------------
 547//void Led_D_TernOff(void)
 548//{
 549//    if (Check_D_CardStsChg())
 550//        MediaChange=ERROR;
 551//
 552//    Cnt_D_LedOff();
 553//}
 554//
 555////SmartMedia Logical Format Subroutine
 556////----- Check_D_LogCHS() -----------------------------------------------
 557//int Check_D_LogCHS(WORD *c,BYTE *h,BYTE *s)
 558//{
 559//    switch(Ssfdc.Model) {
 560//        case SSFDC1MB:   *c=125; *h= 4; *s= 4; break;
 561//        case SSFDC2MB:   *c=125; *h= 4; *s= 8; break;
 562//        case SSFDC4MB:   *c=250; *h= 4; *s= 8; break;
 563//        case SSFDC8MB:   *c=250; *h= 4; *s=16; break;
 564//        case SSFDC16MB:  *c=500; *h= 4; *s=16; break;
 565//        case SSFDC32MB:  *c=500; *h= 8; *s=16; break;
 566//        case SSFDC64MB:  *c=500; *h= 8; *s=32; break;
 567//        case SSFDC128MB: *c=500; *h=16; *s=32; break;
 568//        default:         *c= 0;  *h= 0; *s= 0; ErrCode = ERR_NoSmartMedia;    return(ERROR);
 569//    }
 570//
 571//    return(SMSUCCESS);
 572//}
 573//
 574////Power Control & Media Exist Check Subroutine
 575////----- Initialize_D_Media() -------------------------------------------
 576//void Initialize_D_Media(void)
 577//{
 578//    ErrCode      = NO_ERROR;
 579//    MediaChange  = ERROR;
 580//    SectCopyMode = COMPLETED;
 581//    Cnt_D_Reset();
 582//}
 583//
 584////----- PowerOff_D_Media() ---------------------------------------------
 585//void PowerOff_D_Media(void)
 586//{
 587//    Cnt_D_PowerOff();
 588//}
 589//
 590////----- Check_D_MediaPower() -------------------------------------------
 591//int Check_D_MediaPower(void)
 592//{
 593//    //usleep(56*1024);
 594//    if (Check_D_CardStsChg())
 595//        MediaChange = ERROR;
 596//    //usleep(56*1024);
 597//    if ((!Check_D_CntPower())&&(!MediaChange))  // ¦³ power & Media ¨S³Q change, «h return success
 598//        return(SMSUCCESS);
 599//    //usleep(56*1024);
 600//
 601//    if (Check_D_CardExist())                    // Check if card is not exist, return err
 602//    {
 603//        ErrCode        = ERR_NoSmartMedia;
 604//        MediaChange = ERROR;
 605//        return(ERROR);
 606//    }
 607//    //usleep(56*1024);
 608//    if (Cnt_D_PowerOn())
 609//    {
 610//        ErrCode        = ERR_NoSmartMedia;
 611//        MediaChange = ERROR;
 612//        return(ERROR);
 613//    }
 614//    //usleep(56*1024);
 615//    Ssfdc_D_Reset(fdoExt);
 616//    //usleep(56*1024);
 617//    return(SMSUCCESS);
 618//}
 619//
 620////-----Check_D_MediaExist() --------------------------------------------
 621//int Check_D_MediaExist(void)
 622//{
 623//    if (Check_D_CardStsChg())
 624//        MediaChange = ERROR;
 625//
 626//    if (!Check_D_CardExist())
 627//    {
 628//        if (!MediaChange)
 629//            return(SMSUCCESS);
 630//
 631//        ErrCode = ERR_ChangedMedia;
 632//        return(ERROR);
 633//    }
 634//
 635//    ErrCode = ERR_NoSmartMedia;
 636//
 637//    return(ERROR);
 638//}
 639//
 640////----- Check_D_MediaWP() ----------------------------------------------
 641//int Check_D_MediaWP(void)
 642//{
 643//    if (Ssfdc.Attribute &MWP)
 644//    {
 645//        ErrCode = ERR_WrtProtect;
 646//        return(ERROR);
 647//    }
 648//
 649//    return(SMSUCCESS);
 650//}
 651*/
 652//SmartMedia Physical Format Test Subroutine
 653//----- Check_D_MediaFmt() ---------------------------------------------
 654int Check_D_MediaFmt(struct us_data *us)
 655{
 656        pr_info("Check_D_MediaFmt\n");
 657        //ULONG i,j, result=FALSE, zone,block;
 658
 659        //usleep(56*1024);
 660        if (!MediaChange)
 661                return(SMSUCCESS);
 662
 663        MediaChange  = ERROR;
 664        SectCopyMode = COMPLETED;
 665
 666        //usleep(56*1024);
 667        if (Set_D_PhyFmtValue(us))
 668        {
 669                ErrCode = ERR_UnknownMedia;
 670                return(ERROR);
 671        }
 672        
 673        //usleep(56*1024);
 674        if (Search_D_CIS(us))
 675        {
 676                ErrCode = ERR_IllegalFmt;
 677                return(ERROR);
 678        }
 679
 680
 681    MediaChange = SMSUCCESS;
 682    return(SMSUCCESS);
 683}
 684/*
 685////----- Check_D_BlockIsFull() ----------------------------------
 686//void Check_D_BlockIsFull()
 687//{
 688//    ULONG i, block;
 689//
 690//    if (IsXDCompliance || IsSSFDCCompliance)
 691//    {
 692//       // If the blocks are full then return write-protect.
 693//       block = Ssfdc.MaxBlocks/8;
 694//       for (Media.Zone=0; Media.Zone<Ssfdc.MaxZones; Media.Zone++)
 695//       {
 696//           if (Log2Phy[Media.Zone]==NULL)
 697//           {
 698//               if (Make_D_LogTable())
 699//               {
 700//                   ErrCode = ERR_IllegalFmt;
 701//                   return;
 702//               }
 703//           }
 704//
 705//           for (i=0; i<block; i++)
 706//           {
 707//               if (Assign[Media.Zone][i] != 0xFF)
 708//                  return;
 709//           }
 710//       }
 711//       Ssfdc.Attribute |= WP;
 712//    }
 713//}
 714//
 715//
 716////----- Check_D_MediaFmtForEraseAll() ----------------------------------
 717//int Check_D_MediaFmtForEraseAll(PFDO_DEVICE_EXTENSION fdoExt)
 718//{
 719//    MediaChange  = ERROR;
 720//    SectCopyMode = COMPLETED;
 721//
 722//    if (Set_D_PhyFmtValue(fdoExt))
 723//    {
 724//        ErrCode = ERR_UnknownMedia;
 725//        return(ERROR);
 726//    }
 727//
 728//    if (Search_D_CIS(fdoExt))
 729//    {
 730//        ErrCode = ERR_IllegalFmt;
 731//        return(ERROR);
 732//    }
 733//
 734//    return(SMSUCCESS);
 735//}
 736*/
 737//SmartMedia Physical Address Control Subroutine
 738//----- Conv_D_MediaAddr() ---------------------------------------------
 739int Conv_D_MediaAddr(struct us_data *us, DWORD addr)
 740{
 741        DWORD temp;
 742        //ULONG  zz;
 743        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 744        //ADDRESS_T   bb = (ADDRESS_T) &Media;
 745
 746        temp           = addr/Ssfdc.MaxSectors;
 747        Media.Zone     = (BYTE) (temp/Ssfdc.MaxLogBlocks);
 748
 749        if (Log2Phy[Media.Zone]==NULL)
 750        {
 751                if (Make_D_LogTable(us))
 752                {
 753                        ErrCode = ERR_IllegalFmt;
 754                        return(ERROR);
 755                }
 756        }
 757
 758        Media.Sector   = (BYTE) (addr%Ssfdc.MaxSectors);
 759        Media.LogBlock = (WORD) (temp%Ssfdc.MaxLogBlocks);
 760
 761        if (Media.Zone<Ssfdc.MaxZones)
 762        {
 763                Clr_D_RedundantData(Redundant);
 764                Set_D_LogBlockAddr(Redundant);
 765                Media.PhyBlock = Log2Phy[Media.Zone][Media.LogBlock];
 766                return(SMSUCCESS);
 767        }
 768
 769        ErrCode = ERR_OutOfLBA;
 770        return(ERROR);
 771}
 772
 773//----- Inc_D_MediaAddr() ----------------------------------------------
 774int Inc_D_MediaAddr(struct us_data *us)
 775{
 776        WORD        LogBlock = Media.LogBlock;
 777        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 778        //ADDRESS_T   bb = (ADDRESS_T) &Media;
 779
 780        if (++Media.Sector<Ssfdc.MaxSectors)
 781                return(SMSUCCESS);
 782
 783        if (Log2Phy[Media.Zone]==NULL)
 784        {
 785                if (Make_D_LogTable(us))
 786                {
 787                        ErrCode = ERR_IllegalFmt;
 788                        return(ERROR);
 789                }
 790        }
 791
 792        Media.Sector=0;
 793        Media.LogBlock = LogBlock;
 794
 795        if (++Media.LogBlock<Ssfdc.MaxLogBlocks)
 796        {
 797                Clr_D_RedundantData(Redundant);
 798                Set_D_LogBlockAddr(Redundant);
 799                Media.PhyBlock=Log2Phy[Media.Zone][Media.LogBlock];
 800                return(SMSUCCESS);
 801        }
 802
 803        Media.LogBlock=0;
 804
 805        if (++Media.Zone<Ssfdc.MaxZones)
 806        {
 807                if (Log2Phy[Media.Zone]==NULL)
 808                {
 809                        if (Make_D_LogTable(us))
 810                        {
 811                                ErrCode = ERR_IllegalFmt;
 812                                return(ERROR);
 813                        }
 814                }
 815
 816                Media.LogBlock = 0;
 817
 818                Clr_D_RedundantData(Redundant);
 819                Set_D_LogBlockAddr(Redundant);
 820                Media.PhyBlock=Log2Phy[Media.Zone][Media.LogBlock];
 821                return(SMSUCCESS);
 822        }
 823
 824        Media.Zone=0;
 825        ErrCode = ERR_OutOfLBA;
 826
 827        return(ERROR);
 828}
 829/*
 830//----- Check_D_FirstSect() --------------------------------------------
 831int Check_D_FirstSect(void)
 832{
 833    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 834    ADDRESS_T   bb = (ADDRESS_T) &Media;
 835
 836    if (!Media.Sector)
 837        return(SMSUCCESS);
 838
 839    return(ERROR);
 840}
 841
 842//----- Check_D_LastSect() ---------------------------------------------
 843int Check_D_LastSect(void)
 844{
 845    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 846    ADDRESS_T   bb = (ADDRESS_T) &Media;
 847
 848    if (Media.Sector<(Ssfdc.MaxSectors-1))
 849        return(ERROR);
 850
 851    return(SMSUCCESS);
 852}
 853*/
 854//SmartMedia Read/Write Subroutine with Retry
 855//----- Media_D_ReadOneSect() ------------------------------------------
 856int Media_D_ReadOneSect(struct us_data *us, WORD count, BYTE *buf)
 857{
 858        DWORD err, retry;
 859
 860        if (!Read_D_PhyOneSect(us, count, buf))
 861                return(SMSUCCESS);
 862        if (ErrCode==ERR_HwError)
 863                return(ERROR);
 864        if (ErrCode==ERR_DataStatus)
 865                return(ERROR);
 866
 867#ifdef RDERR_REASSIGN
 868        if (Ssfdc.Attribute &MWP)
 869        {
 870                if (ErrCode==ERR_CorReadErr)
 871                        return(SMSUCCESS);
 872                return(ERROR);
 873        }
 874
 875        err=ErrCode;
 876        for(retry=0; retry<2; retry++)
 877        {
 878                if (Copy_D_BlockAll(us, (err==ERR_EccReadErr)?REQ_FAIL:REQ_ERASE))
 879                {
 880                        if (ErrCode==ERR_HwError)
 881                                return(ERROR);
 882                        continue;
 883                }
 884
 885                ErrCode = err;
 886                if (ErrCode==ERR_CorReadErr)
 887                        return(SMSUCCESS);
 888                return(ERROR);
 889        }
 890
 891        MediaChange = ERROR;
 892#else
 893        if (ErrCode==ERR_CorReadErr) return(SMSUCCESS);
 894#endif
 895
 896        return(ERROR);
 897}
 898/*
 899//----- Media_D_WriteOneSect() -----------------------------------------
 900int Media_D_WriteOneSect(PFDO_DEVICE_EXTENSION fdoExt, WORD count, BYTE *buf)
 901{
 902    DWORD retry;
 903    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 904    ADDRESS_T   bb = (ADDRESS_T) &Media;
 905
 906    if (!Write_D_PhyOneSect(fdoExt, count, buf))
 907        return(SMSUCCESS);
 908    if (ErrCode==ERR_HwError)
 909        return(ERROR);
 910
 911    for(retry=1; retry<2; retry++)
 912    {
 913        if (Reassign_D_BlockHead(fdoExt))
 914        {
 915            if (ErrCode==ERR_HwError)
 916                return(ERROR);
 917            continue;
 918        }
 919
 920        if (!Write_D_PhyOneSect(fdoExt, count, buf))
 921            return(SMSUCCESS);
 922        if (ErrCode==ERR_HwError)
 923            return(ERROR);
 924    }
 925
 926    if (Release_D_WriteBlock(fdoExt))
 927        return(ERROR);
 928
 929    ErrCode        = ERR_WriteFault;
 930    MediaChange = ERROR;
 931    return(ERROR);
 932}
 933
 934//SmartMedia Data Copy Subroutine with Retry
 935//----- Media_D_CopyBlockHead() ----------------------------------------
 936int Media_D_CopyBlockHead(PFDO_DEVICE_EXTENSION fdoExt)
 937{
 938    DWORD retry;
 939
 940    for(retry=0; retry<2; retry++)
 941    {
 942        if (!Copy_D_BlockHead(fdoExt))
 943            return(SMSUCCESS);
 944        if (ErrCode==ERR_HwError)
 945            return(ERROR);
 946    }
 947
 948    MediaChange = ERROR;
 949    return(ERROR);
 950}
 951
 952//----- Media_D_CopyBlockTail() ----------------------------------------
 953int Media_D_CopyBlockTail(PFDO_DEVICE_EXTENSION fdoExt)
 954{
 955    DWORD retry;
 956
 957    if (!Copy_D_BlockTail(fdoExt))
 958        return(SMSUCCESS);
 959    if (ErrCode==ERR_HwError)
 960        return(ERROR);
 961
 962    for(retry=1; retry<2; retry++)
 963    {
 964        if (Reassign_D_BlockHead(fdoExt))
 965        {
 966            if (ErrCode==ERR_HwError)
 967                return(ERROR);
 968            continue;
 969        }
 970
 971        if (!Copy_D_BlockTail(fdoExt))
 972            return(SMSUCCESS);
 973        if (ErrCode==ERR_HwError)
 974            return(ERROR);
 975    }
 976
 977    if (Release_D_WriteBlock(fdoExt))
 978        return(ERROR);
 979
 980    ErrCode        = ERR_WriteFault;
 981    MediaChange = ERROR;
 982    return(ERROR);
 983}
 984//
 985////----- Media_D_EraseOneBlock() ----------------------------------------
 986//int Media_D_EraseOneBlock(void)
 987//{
 988//    WORD        LogBlock = Media.LogBlock;
 989//    WORD        PhyBlock = Media.PhyBlock;
 990//    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
 991//    ADDRESS_T   bb = (ADDRESS_T) &Media;
 992//
 993//    if (Media.PhyBlock==NO_ASSIGN)
 994//        return(SMSUCCESS);
 995//
 996//    if (Log2Phy[Media.Zone]==NULL)
 997//    {
 998//        if (Make_D_LogTable())
 999//        {
1000//            ErrCode = ERR_IllegalFmt;
1001//            return(ERROR);
1002//        }
1003//    }
1004//    Media.LogBlock = LogBlock;
1005//    Media.PhyBlock = PhyBlock;
1006//
1007//    Log2Phy[Media.Zone][Media.LogBlock]=NO_ASSIGN;
1008//
1009//    if (Erase_D_PhyOneBlock(fdoExt))
1010//    {
1011//        if (ErrCode==ERR_HwError)
1012//            return(ERROR);
1013//        if (MarkFail_D_PhyOneBlock())
1014//            return(ERROR);
1015//
1016//        ErrCode = ERR_WriteFault;
1017//        return(ERROR);
1018//    }
1019//
1020//    Clr_D_Bit(Assign[Media.Zone],Media.PhyBlock);
1021//    Media.PhyBlock=NO_ASSIGN;
1022//    return(SMSUCCESS);
1023//}
1024//
1025////SmartMedia Erase Subroutine
1026////----- Media_D_EraseAllBlock() ----------------------------------------
1027//int Media_D_EraseAllBlock(void)
1028//{
1029//    WORD cis=0;
1030//
1031//    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1032//    ADDRESS_T   bb = (ADDRESS_T) &Media;
1033//
1034//    MediaChange = ERROR;
1035//    Media.Sector   = 0;
1036//
1037//    for(Media.Zone=0; Media.Zone<Ssfdc.MaxZones; Media.Zone++)
1038//        for(Media.PhyBlock=0; Media.PhyBlock<Ssfdc.MaxBlocks; Media.PhyBlock++) {
1039//            if (Ssfdc_D_ReadRedtData(Redundant))
1040//            {
1041//                Ssfdc_D_Reset(fdoExt);
1042//                return(ERROR);
1043//            }
1044//
1045//            Ssfdc_D_Reset(fdoExt);
1046//            if (!Check_D_FailBlock(Redundant))
1047//            {
1048//                if (cis)
1049//                {
1050//                    if (Ssfdc_D_EraseBlock(fdoExt))
1051//                    {
1052//                        ErrCode = ERR_HwError;
1053//                        return(ERROR);
1054//                    }
1055//
1056//                    if (Ssfdc_D_CheckStatus())
1057//                    {
1058//                        if (MarkFail_D_PhyOneBlock())
1059//                            return(ERROR);
1060//                    }
1061//
1062//                    continue;
1063//                }
1064//
1065//                if (Media.PhyBlock!=CisArea.PhyBlock)
1066//                {
1067//                    ErrCode = ERR_IllegalFmt;
1068//                    return(ERROR);
1069//                }
1070//
1071//                cis++;
1072//            }
1073//
1074//        }
1075//    return(SMSUCCESS);
1076//}
1077*/
1078//SmartMedia Physical Sector Data Copy Subroutine
1079//----- Copy_D_BlockAll() ----------------------------------------------
1080int Copy_D_BlockAll(struct us_data *us, DWORD mode)
1081{
1082        BYTE sect;
1083        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1084        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1085
1086        sect=Media.Sector;
1087
1088        if (Assign_D_WriteBlock())
1089                return(ERROR);
1090        if (mode==REQ_FAIL)
1091                SectCopyMode=REQ_FAIL;
1092
1093        for(Media.Sector=0; Media.Sector<Ssfdc.MaxSectors; Media.Sector++)
1094        {
1095                if (Copy_D_PhyOneSect(us))
1096                {
1097                        if (ErrCode==ERR_HwError)
1098                                return(ERROR);
1099                        if (Release_D_WriteBlock(us))
1100                                return(ERROR);
1101
1102                        ErrCode = ERR_WriteFault;
1103                        Media.PhyBlock=ReadBlock;
1104                        Media.Sector=sect;
1105
1106                        return(ERROR);
1107                }
1108        }
1109
1110        if (Release_D_ReadBlock(us))
1111                return(ERROR);
1112
1113        Media.PhyBlock=WriteBlock;
1114        Media.Sector=sect;
1115        return(SMSUCCESS);
1116}
1117/*
1118//----- Copy_D_BlockHead() ---------------------------------------------
1119int Copy_D_BlockHead(PFDO_DEVICE_EXTENSION fdoExt)
1120{
1121    BYTE sect;
1122    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1123    ADDRESS_T   bb = (ADDRESS_T) &Media;
1124
1125    sect=Media.Sector;
1126    if (Assign_D_WriteBlock())
1127        return(ERROR);
1128
1129    for(Media.Sector=0; Media.Sector<sect; Media.Sector++)
1130    {
1131        if (Copy_D_PhyOneSect(fdoExt))
1132        {
1133            if (ErrCode==ERR_HwError)
1134                return(ERROR);
1135            if (Release_D_WriteBlock(fdoExt))
1136                return(ERROR);
1137
1138            ErrCode = ERR_WriteFault;
1139            Media.PhyBlock=ReadBlock;
1140            Media.Sector=sect;
1141
1142            return(ERROR);
1143        }
1144    }
1145
1146    Media.PhyBlock=WriteBlock;
1147    Media.Sector=sect;
1148    return(SMSUCCESS);
1149}
1150
1151//----- Copy_D_BlockTail() ---------------------------------------------
1152int Copy_D_BlockTail(PFDO_DEVICE_EXTENSION fdoExt)
1153{
1154    BYTE sect;
1155    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1156    ADDRESS_T   bb = (ADDRESS_T) &Media;
1157
1158    for(sect=Media.Sector; Media.Sector<Ssfdc.MaxSectors; Media.Sector++)
1159    {
1160        if (Copy_D_PhyOneSect(fdoExt))
1161        {
1162            if (ErrCode==ERR_HwError)
1163                return(ERROR);
1164
1165            Media.PhyBlock=WriteBlock;
1166            Media.Sector=sect;
1167
1168            return(ERROR);
1169        }
1170    }
1171
1172    if (Release_D_ReadBlock(fdoExt))
1173        return(ERROR);
1174
1175    Media.PhyBlock=WriteBlock;
1176    Media.Sector=sect;
1177    return(SMSUCCESS);
1178}
1179
1180//----- Reassign_D_BlockHead() -----------------------------------------
1181int Reassign_D_BlockHead(PFDO_DEVICE_EXTENSION fdoExt)
1182{
1183    DWORD  mode;
1184    WORD   block;
1185    BYTE   sect;
1186    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1187    ADDRESS_T   bb = (ADDRESS_T) &Media;
1188
1189    mode=SectCopyMode;
1190    block=ReadBlock;
1191    sect=Media.Sector;
1192
1193    if (Assign_D_WriteBlock())
1194        return(ERROR);
1195
1196    SectCopyMode=REQ_FAIL;
1197
1198    for(Media.Sector=0; Media.Sector<sect; Media.Sector++)
1199    {
1200        if (Copy_D_PhyOneSect(fdoExt))
1201        {
1202            if (ErrCode==ERR_HwError)
1203                return(ERROR);
1204            if (Release_D_WriteBlock(fdoExt))
1205                return(ERROR);
1206
1207            ErrCode = ERR_WriteFault;
1208            SectCopyMode=mode;
1209            WriteBlock=ReadBlock;
1210            ReadBlock=block;
1211            Media.Sector=sect;
1212            Media.PhyBlock=WriteBlock;
1213
1214            return(ERROR);
1215        }
1216    }
1217
1218    if (Release_D_ReadBlock(fdoExt))
1219        return(ERROR);
1220
1221    SectCopyMode=mode;
1222    ReadBlock=block;
1223    Media.Sector=sect;
1224    Media.PhyBlock=WriteBlock;
1225    return(SMSUCCESS);
1226}
1227*/
1228//SmartMedia Physical Block Assign/Release Subroutine
1229//----- Assign_D_WriteBlock() ------------------------------------------
1230int Assign_D_WriteBlock(void)
1231{
1232        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1233        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1234        ReadBlock=Media.PhyBlock;
1235
1236        for(WriteBlock=AssignStart[Media.Zone]; WriteBlock<Ssfdc.MaxBlocks; WriteBlock++)
1237        {
1238                if (!Chk_D_Bit(Assign[Media.Zone],WriteBlock))
1239                {
1240                        Set_D_Bit(Assign[Media.Zone],WriteBlock);
1241                        AssignStart[Media.Zone]=WriteBlock+1;
1242                        Media.PhyBlock=WriteBlock;
1243                        SectCopyMode=REQ_ERASE;
1244                        //ErrXDCode = NO_ERROR;
1245                        return(SMSUCCESS);
1246                }
1247        }
1248
1249        for(WriteBlock=0; WriteBlock<AssignStart[Media.Zone]; WriteBlock++)
1250        {
1251                if (!Chk_D_Bit(Assign[Media.Zone],WriteBlock))
1252                {
1253                        Set_D_Bit(Assign[Media.Zone],WriteBlock);
1254                        AssignStart[Media.Zone]=WriteBlock+1;
1255                        Media.PhyBlock=WriteBlock;
1256                        SectCopyMode=REQ_ERASE;
1257                        //ErrXDCode = NO_ERROR;
1258                        return(SMSUCCESS);
1259                }
1260        }
1261
1262        WriteBlock=NO_ASSIGN;
1263        ErrCode = ERR_WriteFault;
1264        // For xD test
1265        //Ssfdc.Attribute |= WP;
1266        //ErrXDCode = ERR_WrtProtect;
1267        return(ERROR);
1268}
1269
1270//----- Release_D_ReadBlock() ------------------------------------------
1271int Release_D_ReadBlock(struct us_data *us)
1272{
1273        DWORD mode;
1274        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1275        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1276
1277        mode=SectCopyMode;
1278        SectCopyMode=COMPLETED;
1279
1280        if (mode==COMPLETED)
1281                return(SMSUCCESS);
1282
1283        Log2Phy[Media.Zone][Media.LogBlock]=WriteBlock;
1284        Media.PhyBlock=ReadBlock;
1285
1286        if (Media.PhyBlock==NO_ASSIGN)
1287        {
1288                Media.PhyBlock=WriteBlock;
1289                return(SMSUCCESS);
1290        }
1291
1292        if (mode==REQ_ERASE)
1293        {
1294                if (Erase_D_PhyOneBlock(us))
1295                {
1296                        if (ErrCode==ERR_HwError) return(ERROR);
1297                        if (MarkFail_D_PhyOneBlock(us)) return(ERROR);
1298                }
1299                else
1300                        Clr_D_Bit(Assign[Media.Zone],Media.PhyBlock);
1301        }
1302        else if (MarkFail_D_PhyOneBlock(us))
1303                return(ERROR);
1304
1305        Media.PhyBlock=WriteBlock;
1306        return(SMSUCCESS);
1307}
1308
1309//----- Release_D_WriteBlock() -----------------------------------------
1310int Release_D_WriteBlock(struct us_data *us)
1311{
1312        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1313        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1314        SectCopyMode=COMPLETED;
1315        Media.PhyBlock=WriteBlock;
1316
1317        if (MarkFail_D_PhyOneBlock(us))
1318                return(ERROR);
1319
1320        Media.PhyBlock=ReadBlock;
1321        return(SMSUCCESS);
1322}
1323
1324//SmartMedia Physical Sector Data Copy Subroutine
1325//----- Copy_D_PhyOneSect() --------------------------------------------
1326int Copy_D_PhyOneSect(struct us_data *us)
1327{
1328        int           i;
1329        DWORD  err, retry;
1330        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1331        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1332
1333        /* pr_info("Copy_D_PhyOneSect --- Secotr = %x\n", Media.Sector); */
1334        if (ReadBlock!=NO_ASSIGN)
1335        {
1336                Media.PhyBlock=ReadBlock;
1337                for(retry=0; retry<2; retry++)
1338                {
1339                        if (retry!=0)
1340                        {
1341                                Ssfdc_D_Reset(us);
1342                                if (Ssfdc_D_ReadCisSect(us,WorkBuf,WorkRedund))
1343                                { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1344
1345                                if (Check_D_CISdata(WorkBuf,WorkRedund))
1346                                { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1347                        }
1348
1349                        if (Ssfdc_D_ReadSect(us,WorkBuf,WorkRedund))
1350                        { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1351                        if (Check_D_DataStatus(WorkRedund))
1352                        { err=ERROR; break; }
1353                        if (!Check_D_ReadError(WorkRedund))
1354                        { err=SMSUCCESS; break; }
1355                        if (!Check_D_Correct(WorkBuf,WorkRedund))
1356                        { err=SMSUCCESS; break; }
1357
1358                        err=ERROR;
1359                        SectCopyMode=REQ_FAIL;
1360                }
1361        }
1362        else
1363        {
1364                err=SMSUCCESS;
1365                for(i=0; i<SECTSIZE; i++)
1366                        WorkBuf[i]=DUMMY_DATA;
1367                Clr_D_RedundantData(WorkRedund);
1368        }
1369
1370        Set_D_LogBlockAddr(WorkRedund);
1371        if (err==ERROR)
1372        {
1373                Set_D_RightECC(WorkRedund);
1374                Set_D_DataStaus(WorkRedund);
1375        }
1376
1377        Media.PhyBlock=WriteBlock;
1378
1379        if (Ssfdc_D_WriteSectForCopy(us, WorkBuf, WorkRedund))
1380        { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1381        if (Ssfdc_D_CheckStatus())
1382        { ErrCode = ERR_WriteFault; return(ERROR); }
1383
1384        Media.PhyBlock=ReadBlock;
1385        return(SMSUCCESS);
1386}
1387
1388//SmartMedia Physical Sector Read/Write/Erase Subroutine
1389//----- Read_D_PhyOneSect() --------------------------------------------
1390int Read_D_PhyOneSect(struct us_data *us, WORD count, BYTE *buf)
1391{
1392        int           i;
1393        DWORD  retry;
1394        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1395        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1396
1397        if (Media.PhyBlock==NO_ASSIGN)
1398        {
1399                for(i=0; i<SECTSIZE; i++)
1400                        *buf++=DUMMY_DATA;
1401                return(SMSUCCESS);
1402        }
1403
1404        for(retry=0; retry<2; retry++)
1405        {
1406                if (retry!=0)
1407                {
1408                        Ssfdc_D_Reset(us);
1409
1410                        if (Ssfdc_D_ReadCisSect(us,WorkBuf,WorkRedund))
1411                        { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1412                        if (Check_D_CISdata(WorkBuf,WorkRedund))
1413                        { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1414                }
1415
1416                //if (Ssfdc_D_ReadSect(fdoExt,buf,Redundant))
1417                if (Ssfdc_D_ReadBlock(us,count,buf,Redundant))
1418                { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1419                if (Check_D_DataStatus(Redundant))
1420                { ErrCode = ERR_DataStatus; return(ERROR); }
1421
1422                if (!Check_D_ReadError(Redundant))
1423                        return(SMSUCCESS);
1424
1425                if (!Check_D_Correct(buf,Redundant))
1426                { ErrCode = ERR_CorReadErr; return(ERROR); }
1427        }
1428
1429        ErrCode = ERR_EccReadErr;
1430        return(ERROR);
1431}
1432/*
1433//----- Write_D_PhyOneSect() -------------------------------------------
1434int Write_D_PhyOneSect(PFDO_DEVICE_EXTENSION fdoExt, WORD count, BYTE *buf)
1435{
1436    SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1437    ADDRESS_T   bb = (ADDRESS_T) &Media;
1438
1439    //if (Ssfdc_D_WriteSect(fdoExt,buf,Redundant))
1440    if (Ssfdc_D_WriteBlock(fdoExt,count,buf,Redundant))
1441    { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1442    if (Ssfdc_D_CheckStatus())
1443    { ErrCode = ERR_WriteFault; return(ERROR); }
1444
1445    return(SMSUCCESS);
1446}
1447*/
1448//----- Erase_D_PhyOneBlock() ------------------------------------------
1449int Erase_D_PhyOneBlock(struct us_data *us)
1450{
1451        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1452        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1453
1454        if (Ssfdc_D_EraseBlock(us))
1455        { ErrCode = ERR_HwError; MediaChange=ERROR; return(ERROR); }
1456        if (Ssfdc_D_CheckStatus())
1457        { ErrCode = ERR_WriteFault; return(ERROR); }
1458
1459        return(SMSUCCESS);
1460}
1461
1462//SmartMedia Physical Format Check Local Subroutine
1463//----- Set_D_PhyFmtValue() --------------------------------------------
1464int Set_D_PhyFmtValue(struct us_data *us)
1465{
1466//    PPDO_DEVICE_EXTENSION   pdoExt;
1467//    BYTE      idcode[4];
1468//    DWORD     UserDefData_1, UserDefData_2, Data, mask;
1469//
1470//    //if (!fdoExt->ChildDeviceObject)       return(ERROR);
1471//    //pdoExt = fdoExt->ChildDeviceObject->DeviceExtension;
1472//
1473//    Ssfdc_D_ReadID(idcode, READ_ID_1);
1474//
1475    //if (Set_D_SsfdcModel(idcode[1]))
1476    if (Set_D_SsfdcModel(us->SM_DeviceID))
1477        return(ERROR);
1478
1479//    //Use Multi-function pin to differentiate SM and xD.
1480//    UserDefData_1 = ReadPCIReg(fdoExt->BusID, fdoExt->DevID, fdoExt->FuncID, PCI_REG_USER_DEF) & 0x80;
1481//    if (UserDefData_1)
1482//    {
1483//       if ( READ_PORT_BYTE(SM_REG_INT_STATUS) & 0x80 )      fdoExt->DiskType = DISKTYPE_XD;
1484//       if ( READ_PORT_BYTE(SM_REG_INT_STATUS) & 0x40 )      fdoExt->DiskType = DISKTYPE_SM;
1485//
1486//       if ( IsXDCompliance && (fdoExt->DiskType == DISKTYPE_XD) )
1487//       {
1488//          Ssfdc_D_ReadID(idcode, READ_ID_3);
1489//          if (idcode[2] != 0xB5)
1490//             return(ERROR);
1491//       }
1492//    }
1493//
1494//    //Use GPIO to differentiate SM and xD.
1495//    UserDefData_2 = ReadPCIReg(fdoExt->BusID, fdoExt->DevID, fdoExt->FuncID, PCI_REG_USER_DEF) >> 8;
1496//    if ( UserDefData_2 )
1497//    {
1498//       Data = ReadPCIReg(fdoExt->BusID, fdoExt->DevID, 0, 0xAC);
1499//
1500//       mask = 1 << (UserDefData_2-1);
1501//       // 1 : xD , 0 : SM
1502//       if ( Data & mask)
1503//          fdoExt->DiskType = DISKTYPE_XD;
1504//       else
1505//          fdoExt->DiskType = DISKTYPE_SM;
1506//
1507//       if ( IsXDCompliance && (fdoExt->DiskType == DISKTYPE_XD) )
1508//       {
1509//          Ssfdc_D_ReadID(idcode, READ_ID_3);
1510//          if (idcode[2] != 0xB5)
1511//             return(ERROR);
1512//       }
1513//    }
1514//
1515//    if ( !(UserDefData_1 | UserDefData_2) )
1516//    {
1517//      // Use UserDefine Register to differentiate SM and xD.
1518//      Ssfdc_D_ReadID(idcode, READ_ID_3);
1519//
1520//      if (idcode[2] == 0xB5)
1521//         fdoExt->DiskType = DISKTYPE_XD;
1522//      else
1523//      {
1524//          if (!IsXDCompliance)
1525//             fdoExt->DiskType = DISKTYPE_SM;
1526//          else
1527//             return(ERROR);
1528//      }
1529//
1530//      if (fdoExt->UserDef_DiskType == 0x04)  fdoExt->DiskType = DISKTYPE_XD;
1531//      if (fdoExt->UserDef_DiskType == 0x08)  fdoExt->DiskType = DISKTYPE_SM;
1532//    }
1533//
1534//    if (!fdoExt->UserDef_DisableWP)
1535//    {
1536//       if (fdoExt->DiskType == DISKTYPE_SM)
1537//       {
1538//           if (Check_D_SsfdcWP())
1539//              Ssfdc.Attribute|=WP;
1540//       }
1541//    }
1542
1543    return(SMSUCCESS);
1544}
1545
1546//----- Search_D_CIS() -------------------------------------------------
1547int Search_D_CIS(struct us_data *us)
1548{
1549        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1550        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1551
1552        Media.Zone=0; Media.Sector=0;
1553
1554        for (Media.PhyBlock=0; Media.PhyBlock<(Ssfdc.MaxBlocks-Ssfdc.MaxLogBlocks-1); Media.PhyBlock++)
1555        {
1556                if (Ssfdc_D_ReadRedtData(us, Redundant))
1557                {
1558                        Ssfdc_D_Reset(us);
1559                        return(ERROR);
1560                }
1561
1562                if (!Check_D_FailBlock(Redundant))
1563                        break;
1564        }
1565
1566        if (Media.PhyBlock==(Ssfdc.MaxBlocks-Ssfdc.MaxLogBlocks-1))
1567        {
1568                Ssfdc_D_Reset(us);
1569                return(ERROR);
1570        }
1571
1572        while (Media.Sector<CIS_SEARCH_SECT)
1573        {
1574                if (Media.Sector)
1575                {
1576                        if (Ssfdc_D_ReadRedtData(us, Redundant))
1577                        {
1578                                Ssfdc_D_Reset(us);
1579                                return(ERROR);
1580                        }
1581                }
1582                if (!Check_D_DataStatus(Redundant))
1583                {
1584                        if (Ssfdc_D_ReadSect(us,WorkBuf,Redundant))
1585                        {
1586                                Ssfdc_D_Reset(us);
1587                                return(ERROR);
1588                        }
1589
1590                        if (Check_D_CISdata(WorkBuf,Redundant))
1591                        {
1592                                Ssfdc_D_Reset(us);
1593                                return(ERROR);
1594                        }
1595
1596                        CisArea.PhyBlock=Media.PhyBlock;
1597                        CisArea.Sector=Media.Sector;
1598                        Ssfdc_D_Reset(us);
1599                        return(SMSUCCESS);
1600                }
1601
1602                Media.Sector++;
1603        }
1604
1605        Ssfdc_D_Reset(us);
1606        return(ERROR);
1607}
1608
1609//----- Make_D_LogTable() ----------------------------------------------
1610int Make_D_LogTable(struct us_data *us)
1611{
1612        WORD  phyblock,logblock;
1613        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1614        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1615
1616        if (Log2Phy[Media.Zone]==NULL)
1617        {
1618                Log2Phy[Media.Zone] = kmalloc(MAX_LOGBLOCK*sizeof(WORD), GFP_KERNEL);
1619                /* pr_info("ExAllocatePool Zone = %x, Addr = %x\n",
1620                                Media.Zone, Log2Phy[Media.Zone]); */
1621                if (Log2Phy[Media.Zone]==NULL)
1622                        return(ERROR);
1623        }
1624
1625        Media.Sector=0;
1626
1627        //for(Media.Zone=0; Media.Zone<MAX_ZONENUM; Media.Zone++)
1628        //for(Media.Zone=0; Media.Zone<Ssfdc.MaxZones; Media.Zone++)
1629        {
1630                /* pr_info("Make_D_LogTable --- MediaZone = 0x%x\n",
1631                                                        Media.Zone); */
1632                for(Media.LogBlock=0; Media.LogBlock<Ssfdc.MaxLogBlocks; Media.LogBlock++)
1633                        Log2Phy[Media.Zone][Media.LogBlock]=NO_ASSIGN;
1634
1635                for(Media.PhyBlock=0; Media.PhyBlock<(MAX_BLOCKNUM/8); Media.PhyBlock++)
1636                        Assign[Media.Zone][Media.PhyBlock]=0x00;
1637
1638                for(Media.PhyBlock=0; Media.PhyBlock<Ssfdc.MaxBlocks; Media.PhyBlock++)
1639                {
1640                        if ((!Media.Zone) && (Media.PhyBlock<=CisArea.PhyBlock))
1641                        {
1642                                Set_D_Bit(Assign[Media.Zone],Media.PhyBlock);
1643                                continue;
1644                        }
1645
1646                        if (Ssfdc_D_ReadRedtData(us, Redundant))
1647                        { Ssfdc_D_Reset(us); return(ERROR); }
1648
1649                        if (!Check_D_DataBlank(Redundant))
1650                                continue;
1651
1652                        Set_D_Bit(Assign[Media.Zone],Media.PhyBlock);
1653
1654                        if (Check_D_FailBlock(Redundant))
1655                                continue;
1656
1657                        //if (Check_D_DataStatus(Redundant))
1658                        //    continue;
1659
1660                        if (Load_D_LogBlockAddr(Redundant))
1661                                continue;
1662
1663                        if (Media.LogBlock>=Ssfdc.MaxLogBlocks)
1664                                continue;
1665
1666                        if (Log2Phy[Media.Zone][Media.LogBlock]==NO_ASSIGN)
1667                        {
1668                                Log2Phy[Media.Zone][Media.LogBlock]=Media.PhyBlock;
1669                                continue;
1670                        }
1671
1672                        phyblock     = Media.PhyBlock;
1673                        logblock     = Media.LogBlock;
1674                        Media.Sector = (BYTE)(Ssfdc.MaxSectors-1);
1675
1676                        if (Ssfdc_D_ReadRedtData(us, Redundant))
1677                        { Ssfdc_D_Reset(us); return(ERROR); }
1678
1679                        if (!Load_D_LogBlockAddr(Redundant))
1680                        {
1681                                if (Media.LogBlock==logblock)
1682                                {
1683                                        Media.PhyBlock=Log2Phy[Media.Zone][logblock];
1684
1685                                        if (Ssfdc_D_ReadRedtData(us, Redundant))
1686                                        { Ssfdc_D_Reset(us); return(ERROR); }
1687
1688                                        Media.PhyBlock=phyblock;
1689
1690                                        if (!Load_D_LogBlockAddr(Redundant))
1691                                        {
1692                                                if (Media.LogBlock!=logblock)
1693                                                {
1694                                                        Media.PhyBlock=Log2Phy[Media.Zone][logblock];
1695                                                        Log2Phy[Media.Zone][logblock]=phyblock;
1696                                                }
1697                                        }
1698                                        else
1699                                        {
1700                                                Media.PhyBlock=Log2Phy[Media.Zone][logblock];
1701                                                Log2Phy[Media.Zone][logblock]=phyblock;
1702                                        }
1703                                }
1704                        }
1705
1706                        Media.Sector=0;
1707
1708// here Not yet
1709//#ifdef L2P_ERR_ERASE
1710//                      if (!(Ssfdc.Attribute &MWP))
1711//                      {
1712//                              Ssfdc_D_Reset(fdoExt);
1713//                              if (Ssfdc_D_EraseBlock(fdoExt))
1714//                                      return(ERROR);
1715//
1716//                              if (Ssfdc_D_CheckStatus())
1717//                              {
1718//                                      if (MarkFail_D_PhyOneBlock())
1719//                                              return(ERROR);
1720//                              }
1721//                              else
1722//                                      Clr_D_Bit(Assign[Media.Zone],Media.PhyBlock);
1723//                      }
1724//#else
1725//                      Ssfdc.Attribute|=MWP;
1726//#endif
1727                        Media.PhyBlock=phyblock;
1728
1729                } // End for (Media.PhyBlock<Ssfdc.MaxBlocks)
1730
1731                AssignStart[Media.Zone]=0;
1732
1733        } // End for (Media.Zone<MAX_ZONENUM)
1734
1735        Ssfdc_D_Reset(us);
1736        return(SMSUCCESS);
1737}
1738
1739//----- MarkFail_D_PhyOneBlock() ---------------------------------------
1740int MarkFail_D_PhyOneBlock(struct us_data *us)
1741{
1742        BYTE sect;
1743        //SSFDCTYPE_T aa = (SSFDCTYPE_T ) &Ssfdc;
1744        //ADDRESS_T   bb = (ADDRESS_T) &Media;
1745
1746        sect=Media.Sector;
1747        Set_D_FailBlock(WorkRedund);
1748        //Ssfdc_D_WriteRedtMode();
1749
1750        for(Media.Sector=0; Media.Sector<Ssfdc.MaxSectors; Media.Sector++)
1751        {
1752                if (Ssfdc_D_WriteRedtData(us, WorkRedund))
1753                {
1754                        Ssfdc_D_Reset(us);
1755                        Media.Sector   = sect;
1756                        ErrCode        = ERR_HwError;
1757                        MediaChange = ERROR;
1758                        return(ERROR);
1759                } // NO Status Check
1760        }
1761
1762        Ssfdc_D_Reset(us);
1763        Media.Sector=sect;
1764        return(SMSUCCESS);
1765}
1766/*
1767//
1768////----- SM_Init() ----------------------------------------------------
1769//void SM_Init(void)
1770//{
1771//    _Hw_D_ClrIntCardChg();
1772//    _Hw_D_SetIntMask();
1773//    // For DMA Interrupt
1774//    _Hw_D_ClrDMAIntCardChg();
1775//    _Hw_D_SetDMAIntMask();
1776//}
1777//
1778////----- Media_D_EraseAllRedtData() -----------------------------------
1779//int Media_D_EraseAllRedtData(DWORD Index, BOOLEAN CheckBlock)
1780//{
1781//    BYTE    i;
1782//
1783//    if (Check_D_MediaPower())
1784//        return(ErrCode);
1785//
1786//    if (Check_D_MediaWP())
1787//        return(ErrCode);
1788//
1789//    for (i=0; i<REDTSIZE; i++)
1790//        WorkRedund[i] = 0xFF;
1791//
1792//    Media.Zone = (BYTE)Index;
1793//    for (Media.PhyBlock=0; Media.PhyBlock<Ssfdc.MaxBlocks; Media.PhyBlock++)
1794//    {
1795//        if ((!Media.Zone) && (Media.PhyBlock<=CisArea.PhyBlock))
1796//            continue;
1797//
1798//        if (Ssfdc_D_EraseBlock(fdoExt))
1799//        {
1800//            ErrCode = ERR_HwError;
1801//            return(ERROR);
1802//        }
1803//
1804//        for(Media.Sector=0; Media.Sector<Ssfdc.MaxSectors; Media.Sector++)
1805//        {
1806//            Ssfdc_D_WriteRedtMode();
1807//
1808//            if (Ssfdc_D_WriteRedtData(WorkRedund))
1809//            {
1810//                Ssfdc_D_Reset(fdoExt);
1811//                ErrCode        = ERR_HwError;
1812//                MediaChange    = ERROR;
1813//                return(ERROR);
1814//            } // NO Status Check
1815//        }
1816//
1817//        Ssfdc_D_Reset(fdoExt);
1818//    }
1819//
1820//    Ssfdc_D_Reset(fdoExt);
1821//
1822//    return(SMSUCCESS);
1823//}
1824//
1825////----- Media_D_GetMediaInfo() ---------------------------------------
1826//DWORD Media_D_GetMediaInfo(PFDO_DEVICE_EXTENSION fdoExt, PIOCTL_MEDIA_INFO_IN pParamIn, PIOCTL_MEDIA_INFO_OUT pParamOut)
1827//{
1828//    pParamOut->ErrCode = STATUS_CMD_FAIL;
1829//
1830//    Init_D_SmartMedia();
1831//
1832//    if (Check_D_MediaPower())
1833//        return (ErrCode==ERR_NoSmartMedia) ? STATUS_CMD_NO_MEDIA : STATUS_CMD_FAIL;
1834//
1835//    if (Set_D_PhyFmtValue(fdoExt))
1836//        return STATUS_CMD_FAIL;
1837//
1838//    //usleep(56*1024);
1839//    if (Search_D_CIS(fdoExt))
1840//        return STATUS_CMD_FAIL;
1841//
1842//    if (Check_D_MediaWP())
1843//        return STATUS_CMD_MEDIA_WP;
1844//
1845//    pParamOut->PageSize  = Ssfdc.MaxSectors;
1846//    pParamOut->BlockSize = Ssfdc.MaxBlocks;
1847//    pParamOut->ZoneSize  = Ssfdc.MaxZones;
1848//
1849//    return STATUS_CMD_SUCCESS;
1850//}*/
1851