diff --git a/include/filesys/block/sdmm_vfs.c b/include/filesys/block/sdmm_vfs.c new file mode 100644 index 000000000..5e7708fb4 --- /dev/null +++ b/include/filesys/block/sdmm_vfs.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include + +vfs_file_t * +_sdmm_open(int pclk, int pss, int pdi, int pdo) +{ + int r; + int drv = 0; + struct __using("filesys/block/sdmm.cc") *SDMM; + unsigned long long pmask; + vfs_file_t *handle; + + SDMM = _gc_alloc_managed(sizeof(*SDMM)); + +#ifdef _DEBUG + __builtin_printf("sdmm_open: using pins: %d %d %d %d\n", pclk, pss, pdi, pdo); +#endif + pmask = (1ULL << pclk) | (1ULL << pss) | (1ULL << pdi) | (1ULL << pdo); + if (!_usepins(pmask)) { + _gc_free(SDMM); + _seterror(EBUSY); + return 0; + } + SDMM->f_pinmask = pmask; + r = SDMM->disk_setpins(drv, pclk, pss, pdi, pdo); + if (r == 0) + r = SDMM->disk_initialize(0); + if (r != 0) { +#ifdef _DEBUG + __builtin_printf("sd card initialize: result=[%d]\n", r); + _waitms(1000); +#endif + goto cleanup_and_out; + } + handle = _get_vfs_file_handle(); + if (!handle) goto cleanup_and_out; + + handle->flags = O_RDWR; + handle->bufmode = _IONBF; + handle->state = _VFS_STATE_INUSE | _VFS_STATE_WROK | _VFS_STATE_RDOK; + handle->read = &SDMM->v_read; + handle->write = &SDMM->v_write; + handle->close = &SDMM->v_close; + handle->ioctl = &SDMM->v_ioctl; + handle->flush = &SDMM->v_flush; + handle->lseek = &SDMM->v_lseek; + handle->putcf = &SDMM->v_putc; + handle->getcf = &SDMM->v_getc; + return handle; +cleanup_and_out: + _freepins(pmask); + _gc_free(SDMM); + _seterror(EIO); + return 0; +} + +struct vfs * +_vfs_open_sdcardx(int pclk, int pss, int pdi, int pdo) +{ + vfs_file_t *handle; + struct vfs *result; + + handle = _sdmm_open(pclk, pss, pdi, pdo); + if (!handle) return 0; + result = _vfs_open_fat_handle(handle); + if (!result) { + /* go ahead and close the handle */ + handle->close(handle); + } + return result; +} + +struct vfs * +_vfs_open_sdcard() +{ + return _vfs_open_sdcardx(61, 60, 59, 58); +} + diff --git a/include/filesys/fatfs/fatfs_vfs.c b/include/filesys/fatfs/fatfs_vfs.c index ff08c697f..f8f670813 100644 --- a/include/filesys/fatfs/fatfs_vfs.c +++ b/include/filesys/fatfs/fatfs_vfs.c @@ -7,80 +7,6 @@ #include #include "ff.h" -vfs_file_t * -_sdmm_open(int pclk, int pss, int pdi, int pdo) -{ - int r; - int drv = 0; - struct __using("filesys/block/sdmm.cc") *SDMM; - unsigned long long pmask; - vfs_file_t *handle; - - SDMM = _gc_alloc_managed(sizeof(*SDMM)); - -#ifdef _DEBUG - __builtin_printf("sdmm_open: using pins: %d %d %d %d\n", pclk, pss, pdi, pdo); -#endif - pmask = (1ULL << pclk) | (1ULL << pss) | (1ULL << pdi) | (1ULL << pdo); - if (!_usepins(pmask)) { - _gc_free(SDMM); - _seterror(EBUSY); - return 0; - } - SDMM->f_pinmask = pmask; - r = SDMM->disk_setpins(drv, pclk, pss, pdi, pdo); - if (r == 0) - r = SDMM->disk_initialize(0); - if (r != 0) { -#ifdef _DEBUG - __builtin_printf("sd card initialize: result=[%d]\n", r); - _waitms(1000); -#endif - goto cleanup_and_out; - } - handle = _get_vfs_file_handle(); - if (!handle) goto cleanup_and_out; - - handle->flags = O_RDWR; - handle->bufmode = _IONBF; - handle->state = _VFS_STATE_INUSE | _VFS_STATE_WROK | _VFS_STATE_RDOK; - handle->read = &SDMM->v_read; - handle->write = &SDMM->v_write; - handle->close = &SDMM->v_close; - handle->ioctl = &SDMM->v_ioctl; - handle->flush = &SDMM->v_flush; - handle->lseek = &SDMM->v_lseek; - handle->putcf = &SDMM->v_putc; - handle->getcf = &SDMM->v_getc; - return handle; -cleanup_and_out: - _freepins(pmask); - _gc_free(SDMM); - _seterror(EIO); - return 0; -} - -struct vfs * -_vfs_open_sdcardx(int pclk, int pss, int pdi, int pdo) -{ - vfs_file_t *handle; - struct vfs *result; - - handle = _sdmm_open(pclk, pss, pdi, pdo); - if (!handle) return 0; - result = _vfs_open_fat_handle(handle); - if (!result) { - /* go ahead and close the handle */ - handle->close(handle); - } - return result; -} - -struct vfs * -_vfs_open_sdcard() -{ - return _vfs_open_sdcardx(61, 60, 59, 58); -} struct vfs * _vfs_open_fat_handle(vfs_file_t *fhandle) diff --git a/include/libc.a b/include/libc.a index 7ca9f1a80..53dbad8fb 100644 --- a/include/libc.a +++ b/include/libc.a @@ -8,12 +8,14 @@ #define DIR struct _dir #define DIRENT struct dirent +/* filesys/block/sdmm_vfs.c */ +_FILE *_sdmm_open(int pclk, int pss, int pdi, int pdo) __fromfile("filesys/block/sdmm_vfs.c"); +struct vfs *_vfs_open_sdcard(void) __fromfile("filesys/block/sdmm_vfs.c"); +struct vfs *_vfs_open_sdcardx(int pclk, int pss, int pdi, int pdo) __fromfile("filesys/block/sdmm_vfs.c"); + /* filesys/fatfs/fatfs_vfs.c */ -_FILE *_sdmm_open(int pclk, int pss, int pdi, int pdo) __fromfile("filesys/fatfs/fatfs_vfs.c"); struct vfs *_vfs_open_fat_handle(_FILE *fhandle) __fromfile("filesys/fatfs/fatfs_vfs.c"); struct vfs *_vfs_open_fat_file(const char *name) __fromfile("filesys/fatfs/fatfs_vfs.c"); -struct vfs *_vfs_open_sdcard(void) __fromfile("filesys/fatfs/fatfs_vfs.c"); -struct vfs *_vfs_open_sdcardx(int pclk, int pss, int pdi, int pdo) __fromfile("filesys/fatfs/fatfs_vfs.c"); /* filesys/fs9p/fs9p_vfs.c */ struct vfs *_vfs_open_host(void) __fromfile("filesys/fs9p/fs9p_vfs.c"); diff --git a/include/sys/vfs.h b/include/sys/vfs.h index cf6d14613..41c818067 100644 --- a/include/sys/vfs.h +++ b/include/sys/vfs.h @@ -56,8 +56,8 @@ struct vfs *_vfs_open_fat_handle(vfs_file_t *handle) _IMPL("filesys/fatfs/fatfs_ struct vfs *_vfs_open_fat_file(const char *name) _IMPL("filesys/fatfs/fatfs_vfs.c"); /* legacy calls */ -struct vfs *_vfs_open_sdcard(void) _IMPL("filesys/fatfs/fatfs_vfs.c"); -struct vfs *_vfs_open_sdcardx(int pclk = 61, int pss = 60, int pdi = 59, int pdo = 58) _IMPL("filesys/fatfs/fatfs_vfs.c"); +struct vfs *_vfs_open_sdcard(void) _IMPL("filesys/block/sdmm_vfs.c"); +struct vfs *_vfs_open_sdcardx(int pclk = 61, int pss = 60, int pdi = 59, int pdo = 58) _IMPL("filesys/block/sdmm_vfs.c"); /* parallax file system */ struct vfs *_vfs_open_parallaxfs(void) _IMPL("filesys/parallax/parallaxfs_vfs.c");