/*---------------------------------------------------------------------------*
  Project:      NAND flash library
  File:         nand.h
  Programmer:   HIRATSU Daisuke

  Copyright (C) 2005-2006 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: nand.h,v $
  Revision 1.14  2006/03/20 10:55:35  hiratsu
  Implemented NANDMove[Async] instead of NANDRename[Async].

  Revision 1.13  2006/03/17 04:41:56  hiratsu
  Added NAND_MAX_NAME macro.
  File and directory name length is limited by this value.

  Revision 1.12  2006/03/16 11:42:36  hiratsu
  New error code is added.

  Revision 1.11  2006/03/11 09:45:13  hiratsu
  Changed value of NAND_RESULT_UNKNOWN.

  Revision 1.10  2006/03/02 05:17:39  hiratsu
  Added macro for maximum file descriptor.

  Revision 1.9  2006/02/16 13:03:35  hiratsu
  Refined API names.

  Revision 1.8  2006/02/13 12:38:26  hiratsu
  Some macro definitions are modified in order to match ISFS API.
  Sorted APIs.
  Misc APIs are added, but not implemented.

  Revision 1.7  2006/02/11 08:52:51  hiratsu
  Renamed APIs.

  Revision 1.6  2006/02/08 12:57:47  hiratsu
  Directory APIs are added.  But it is not tested very well.

  Revision 1.5  2006/02/07 06:56:50  hiratsu
  Added directory API.

  Revision 1.4  2006/02/06 00:47:51  hiratsu
  Added Async APIs and seek API.

 *---------------------------------------------------------------------------*/

#ifndef __NAND_H__
#define __NAND_H__


#include <revolution/types.h>


#ifdef __cplusplus
extern "C" {
#endif


#define NAND_RESULT_OK                0 
#define NAND_RESULT_ACCESS         ( -1)
#define NAND_RESULT_BUSY           ( -2)
#define NAND_RESULT_CORRUPT        ( -3)
#define NAND_RESULT_ECC_CRIT       ( -4)
#define NAND_RESULT_EXISTS         ( -5)
#define NAND_RESULT_INVALID        ( -6)
#define NAND_RESULT_MAXBLOCKS      ( -7)
#define NAND_RESULT_MAXFD          ( -8)
#define NAND_RESULT_MAXFILES       ( -9)
#define NAND_RESULT_NOEXISTS       (-10)
#define NAND_RESULT_NOTEMPTY       (-11)
#define NAND_RESULT_NOTREADY       (-12)
#define NAND_RESULT_OPENFD         (-13)
#define NAND_RESULT_UNKNOWN        (-64)
#define NAND_RESULT_FATAL_ERROR   (-128)

#define NAND_PERM_OTHER_READ   0x01
#define NAND_PERM_OTHER_WRITE  0x02
#define NAND_PERM_GROUP_READ   0x04
#define NAND_PERM_GROUP_WRITE  0x08
#define NAND_PERM_OWNER_READ   0x10
#define NAND_PERM_OWNER_WRITE  0x20

#define NAND_PERM_OWNER_MASK   (NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE)
#define NAND_PERM_GROUP_MASK   (NAND_PERM_GROUP_READ | NAND_PERM_GROUP_WRITE)
#define NAND_PERM_OTHER_MASK   (NAND_PERM_OTHER_READ | NAND_PERM_OTHER_WRITE)

#define NAND_ATTR_NO_COPY      0x01
#define NAND_ATTR_NO_RENAME    0x02

#define NAND_ACCESS_READ       0x01
#define NAND_ACCESS_WRITE      0x02
#define NAND_ACCESS_RW         (NAND_ACCESS_READ | NAND_ACCESS_WRITE)

#define NAND_SEEK_SET          0
#define NAND_SEEK_CUR          1
#define NAND_SEEK_END          2

#define NAND_MAX_CAPACITY      (512*1024*1024)
#define NAND_MAX_INODES        6143
#define NAND_MAX_PATH          64
#define NAND_MAX_FD            16
#define NAND_MAX_NAME          12


typedef struct NANDFileInfo
{
    s32 fileDescriptor;
} NANDFileInfo;


typedef struct NANDStatus
{
    u32 ownerId;
    u16 groupId;
    u8  attribute;
    u8  permission;
} NANDStatus;


typedef void (*NANDCallback)( s32 result );

void NANDInit(void);

//   File access API
s32  NANDCreate         (const char *path, u8 perm, u8 attr                 );
s32  NANDCreateAsync    (const char *path, u8 perm, u8 attr, NANDCallback cb);
s32  NANDOpen           (const char *path, NANDFileInfo *info, u8 accType                 );
s32  NANDOpenAsync      (const char *path, NANDFileInfo *info, u8 accType, NANDCallback cb);
s32  NANDClose          (NANDFileInfo *info                 );
s32  NANDCloseAsync     (NANDFileInfo *info, NANDCallback cb);
s32  NANDRead           (NANDFileInfo *info,       void *buf, u32 length                 );
s32  NANDReadAsync      (NANDFileInfo *info,       void *buf, u32 length, NANDCallback cb);
s32  NANDWrite          (NANDFileInfo *info, const void *buf, u32 length                 );
s32  NANDWriteAsync     (NANDFileInfo *info, const void *buf, u32 length, NANDCallback cb);
s32  NANDSeek           (NANDFileInfo *info, s32 offset, s32 whence                 );
s32  NANDSeekAsync      (NANDFileInfo *info, s32 offset, s32 whence, NANDCallback cb);
s32  NANDGetSize        (NANDFileInfo *info, u32 *size                   );
s32  NANDGetSizeAsync   (NANDFileInfo *info, u32 *size,   NANDCallback cb);
s32  NANDGetOffset      (NANDFileInfo *info, u32 *offset                 );
s32  NANDGetOffsetAsync (NANDFileInfo *info, u32 *offset, NANDCallback cb);

//   Directory access API
s32  NANDReadDir        (const char *path, char *nameList, u32 *num                 );
s32  NANDReadDirAsync   (const char *path, char *nameList, u32 *num, NANDCallback cb);
s32  NANDCreateDir      (const char *path, u8 perm, u8 attr                 );
s32  NANDCreateDirAsync (const char *path, u8 perm, u8 attr, NANDCallback cb);
s32  NANDChangeDir      (const char *path                 );
s32  NANDChangeDirAsync (const char *path, NANDCallback cb);
s32  NANDGetCurrentDir  (      char  path[NAND_MAX_PATH]  );

//   File or Directory access API
s32  NANDDelete         (const char *path                 );
s32  NANDDeleteAsync    (const char *path, NANDCallback cb);
s32  NANDMove           (const char *path, const char *destDir                 );
s32  NANDMoveAsync      (const char *path, const char *destDir, NANDCallback cb);
s32  NANDGetStatus      (const char *path,       NANDStatus *stat                 );
s32  NANDGetStatusAsync (const char *path,       NANDStatus *stat, NANDCallback cb);
s32  NANDSetStatus      (const char *path, const NANDStatus *stat                 );
s32  NANDSetStatusAsync (const char *path, const NANDStatus *stat, NANDCallback cb); 

//   Misc APIs.
s32  NANDFreeBlocks     (u32 *bytes, u32 *inodes                 );
s32  NANDFreeBlocksAsync(u32 *bytes, u32 *inodes, NANDCallback cb);


#ifdef __cplusplus
}
#endif


#endif // end of __NAND_H__
