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