forked from waninkoko/dip-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdhc.c
99 lines (73 loc) · 1.76 KB
/
sdhc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "syscalls.h"
#include "types.h"
#include "sdhc.h"
#include <stdio.h>
#include <string.h>
/* IOCTL commands */
#define IOCTL_SDHC_INIT 0x01
#define IOCTL_SDHC_READ 0x02
#define IOCTL_SDHC_WRITE 0x03
#define IOCTL_SDHC_ISINSERTED 0x04
/* Constants */
#define SDHC_SECTOR_SIZE 0x200
/* Variables */
static char fs[] ATTRIBUTE_ALIGN(32) = "/dev/sdio/sdhc";
static ioctlv io_vector[3] ATTRIBUTE_ALIGN(32);
static u32 io_buffer[3] ATTRIBUTE_ALIGN(32);
static s32 fd = -1;
static u32 sectorSz = SDHC_SECTOR_SIZE;
int sdhc_Init(void)
{
//s32 ret;
/* Already open */
if (fd >= 0)
return 0;
/* Open USB device */
fd = os_open(fs, 1);
if (fd < 0)
return -11;
/* Initialize USB storage */
os_ioctlv(fd, IOCTL_SDHC_INIT, 0, 0, NULL);
sectorSz = SDHC_SECTOR_SIZE;
return 0;
}
bool sdhc_Read(u32 sector, u32 numSectors, void *buffer)
{
u32 cnt;
s32 ret;
/* Device not opened */
if (fd < 0)
return false;
/* Sector info */
io_buffer[0] = sector;
io_buffer[1] = numSectors;
/* Setup io_vector */
io_vector[0].data = &io_buffer[0];
io_vector[0].len = sizeof(u32);
io_vector[1].data = &io_buffer[1];
io_vector[1].len = sizeof(u32);
io_vector[2].data = buffer;
io_vector[2].len = (sectorSz * numSectors);
/* Flush cache */
for (cnt = 0; cnt < 3; cnt++)
os_sync_after_write(io_vector[cnt].data, io_vector[cnt].len);
os_sync_after_write(io_vector, sizeof(ioctlv) * 3);
/* Read data */
ret = os_ioctlv(fd, IOCTL_SDHC_READ, 2, 1, io_vector);
if (ret < 0)
return false;
/* Invalidate cache */
for (cnt = 0; cnt < 3; cnt++)
os_sync_before_read(io_vector[cnt].data, io_vector[cnt].len);
return true;
}
bool sdhc_Shutdown(void)
{
if (fd >= 0) {
/* Close USB device */
os_close(fd);
/* Remove descriptor */
fd = -1;
}
return true;
}