summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/drivers/usb/fusb_msc.h
blob: bc0a1989b952bf03c3e49a58700a3d980db26865 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fusb_msc.h
 * Date: 2022-02-11 13:33:09
 * LastEditTime: 2022-02-17 17:50:46
 * Description:  This files is for definition of USB mass storage function
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 * 1.0   Zhugengyu  2022/2/7    init commit
 */

#ifndef  DRIVERS_USB_MSC_H
#define  DRIVERS_USB_MSC_H

#ifdef __cplusplus
extern "C"
{
#endif

/***************************** Include Files *********************************/

#include "fusb.h"

/************************** Constant Definitions *****************************/
/* Possible values for quirks field. */
enum
{
    /* Don't check for LUNs (force assumption that there's only one LUN). */
    FUSB_MSC_QUIRK_NO_LUNS  = 1 << 0,
    /* Never do a BULK_ONLY reset, just continue. This means that the device
       cannot recover from phase errors and won't detach automatically for
       unrecoverable errors. Do not use unless you have to. */
    FUSB_MSC_QUIRK_NO_RESET = 1 << 1,
};

/* Possible values for ready field. */
enum
{
    FUSB_MSC_DETACHED = -1, /* Disk detached or out to lunch. */
    FUSB_MSC_NOT_READY = 0, /* Disk not ready yet -- empty card reader */
    FUSB_MSC_READY = 1,     /* Disk ready to communicate. */
};

enum
{
    FUSB_MSC_SUBCLASS_ATAPI_8020 = 0x2,
    FUSB_MSC_SUBCLASS_ATAPI_8070 = 0x5,
    FUSB_MSC_SUBCLASS_SCSI = 0x6
};

/* Protocols of MSC */
enum
{
    FUSB_MSC_PROTOCOL_BULK_ONLY = 0x50 /* Usb bulk-only transfer protocol */
};

typedef enum
{
    FUSB_DIR_DATA_IN = 0x80, /* data from the device to the host */
    FUSB_DIR_DATA_OUT = 0 /* data from the host to the device */
} FUsbMassStorageDirection;

/**************************** Type Definitions *******************************/
typedef struct
{
    unsigned int blocksize;
    unsigned int numblocks;
    FUsbEndpoint *bulk_in;
    FUsbEndpoint *bulk_out;
    u8 quirks       : 7;
    u8 usbdisk_created  : 1;
    s8 ready;
    u8 lun;
    u8 num_luns;
    void *data; /* For use by consumers of libpayload. */
} FUsbMassStorage;

/************************** Variable Definitions *****************************/

/***************** Macros (Inline Functions) Definitions *********************/
#define MSC_INST(dev) ((FUsbMassStorage*)(dev)->data)

/**
 * @name: FUsbMscGetCapcityMB
 * @msg: 获取USB大容量存储设备的容量,单位MB
 * @return {fsize_t} 容量,MB
 * @param {FUsbDev} *dev, USB大容量存储设备实例
 */
static inline u32 FUsbMscGetCapcityMB(FUsbDev *dev)
{
    FASSERT(dev);
    return (u32)(MSC_INST(dev)->numblocks > 1000000
                 ? (MSC_INST(dev)->numblocks / 1024) * MSC_INST(dev)->blocksize / 1024
                 : MSC_INST(dev)->numblocks * MSC_INST(dev)->blocksize / 1024 / 1024);
}

/**
 * @name: FUsbMscGetBlkSize
 * @msg: 获取USB大容量存储设备的块大小
 * @return {*} 块大小,字节数
 * @param {FUsbDev} *dev, USB大容量存储设备实例
 */
static inline u32 FUsbMscGetBlkSize(FUsbDev *dev)
{
    FASSERT(dev);
    return (u32)MSC_INST(dev)->blocksize;
}

/**
 * @name: FUsbMscGetBlkNum
 * @msg: 获取USB大容量存储设备块数目
 * @return {*} 块数目
 * @param {FUsbDev} *dev, USB大容量存储设备实例
 */
static inline u32 FUsbMscGetBlkNum(FUsbDev *dev)
{
    FASSERT(dev);
    return (u32)MSC_INST(dev)->numblocks;
}

/************************** Function Prototypes ******************************/
/* 读写USB大容量存储设备,以512字节为一块 */
int FUsbMscRwBlk512(FUsbDev *dev, int start, int n, FUsbMassStorageDirection dir, u8 *buf);

/* USB大容量存储设备的初始化函数,由应用程序注册到FUSB框架中 */
void FUsbMassStorageInit(FUsbDev *dev);

/**
 * To be implemented by application. It's called by the USB stack
 * when a new USB storage device is found, so the client has the chance
 * to know about it.
 *
 * @param dev descriptor for the USB storage device
 */
void __attribute__((weak)) FUsbDiskCreate(FUsbDev *dev);

/**
 * To be implemented by application. It's called by the USB stack
 * when it finds out that a USB storage device is removed.
 *
 * @param dev descriptor for the USB storage device
 */
void __attribute__((weak)) FUsbDiskRemove(FUsbDev *dev);

#ifdef __cplusplus
}
#endif


#endif