1 #ifndef __USBH_MTP_H__
2 #define __USBH_MTP_H__
3 
4 #include "usbh_mtp_ptp.h"
5 
6 
7 typedef enum {
8     USBH_MTP_IDLE = 0,
9     USBH_MTP_GETDEVICEINFO,
10     USBH_MTP_OPENSESSION,
11     USBH_MTP_GETSTORAGEIDS,
12     USBH_MTP_GETSTORAGEINFO,
13     USBH_MTP_TRANSFER,
14     USBH_MTP_EVENT,
15     USBH_MTP_EVENT_WAIT,
16 
17     /* Events may be interleaved within a data stream during a transaction.
18        It may be assumed that the Operation Request Phase and Response Phase are atomic, but the Data Phase
19        must allow for events to be communicated in either direction without interrupting data transfer.
20     */
21     USBH_MTP_EVENT_CHECK,
22 } USBH_MTP_State;
23 
24 typedef enum {
25     USBH_MTP_OP_IDLE = 0,
26     USBH_MTP_OP_SEND,
27     USBH_MTP_OP_WAIT,
28     USBH_MTP_OP_ERROR,
29 } USBH_MTP_OpState;
30 
31 typedef enum {
32     USBH_MTP_XFER_IDLE = 0,
33     USBH_MTP_XFER_OP_REQ,
34     USBH_MTP_XFER_OP_REQ_WAIT,
35     USBH_MTP_XFER_DATA_OUT,
36     USBH_MTP_XFER_DATA_OUT_WAIT,
37     USBH_MTP_XFER_DATA_IN,
38     USBH_MTP_XFER_DATA_IN_WAIT,
39     USBH_MTP_XFER_RESP,
40     USBH_MTP_XFER_RESP_WAIT,
41     USBH_MTP_XFER_ERROR,
42 } USBH_MTP_XferState;
43 
44 
45 typedef struct {
46     uint32_t timer;
47     uint16_t poll;
48     PTP_EventContainer_t container;
49 } MTP_EventHandle_t;
50 
51 
52 typedef struct {
53     uint8_t  InEp;
54     uint8_t  OutEp;
55     uint8_t  NotifyEp;
56     uint16_t InEpSize;
57     uint16_t OutEpSize;
58     uint16_t NotifyEpSize;
59     uint8_t  InEpDATAX;
60     uint8_t  OutEpDATAX;
61     uint8_t  NotifyEpDATAX;
62 
63     USBH_MTP_State      state;
64     USBH_MTP_State      stateReq;
65     USBH_MTP_State      stateBkp;
66 
67     USBH_MTP_OpState    OpState;
68     USBH_MTP_XferState  XferState;
69 
70     USBH_Status         XferStatus;
71 
72     PTP_OpContainer_t   op_container;
73     PTP_DataContainer_t data_container;
74     PTP_RespContainer_t resp_container;
75 
76     uint32_t  session_id;
77     uint32_t  transaction_id;
78 
79     uint32_t  flags;
80 
81     uint8_t  *data_ptr;
82     uint32_t  data_len;
83     uint8_t   first_packet;     // 1 数据第一帧   3 数据第一帧,且需丢弃 header 不存储
84 
85     PTP_DeviceInfo_t    devinfo;
86     PTP_StorageIDs_t    storids;
87     PTP_StorageInfo_t   storinfo[PTP_MAX_STORAGE_UNITS_NBR];
88 
89     MTP_EventHandle_t   events;
90 
91     uint32_t CurrentStorage;
92     uint32_t is_ready;
93 } USBH_MTP_Info_t;
94 
95 extern USBH_MTP_Info_t USBH_MTP_Info;
96 
97 
USBH_MTP_Ready(void)98 static uint32_t USBH_MTP_Ready(void)
99 {
100     return USBH_MTP_Info.is_ready;
101 }
102 
USBH_MTP_StorageCount(void)103 static uint32_t USBH_MTP_StorageCount(void)
104 {
105     return USBH_MTP_Info.storids.n;
106 }
107 
USBH_MTP_Storage(uint32_t index)108 static uint32_t USBH_MTP_Storage(uint32_t index)
109 {
110     return USBH_MTP_Info.storids.Storage[index];
111 }
112 
113 
114 USBH_Status USBH_MTP_GetDeviceInfo(USBH_Info_t *phost, PTP_DeviceInfo_t *dev_info);
115 
116 USBH_Status USBH_MTP_OpenSession(USBH_Info_t *phost, uint32_t session);
117 
118 USBH_Status USBH_MTP_GetStorageIds(USBH_Info_t *phost, PTP_StorageIDs_t *storage_ids);
119 USBH_Status USBH_MTP_GetStorageInfo(USBH_Info_t *phost, uint32_t storage_id, PTP_StorageInfo_t *storage_info);
120 
121 USBH_Status USBH_MTP_GetNumObjects(USBH_Info_t *phost, uint32_t storage_id, uint32_t format, uint32_t folder, uint32_t *numobs);
122 USBH_Status USBH_MTP_GetObjectHandles(USBH_Info_t *phost, uint32_t storage_id, uint32_t format, uint32_t folder, PTP_ObjectHandles_t *handles);
123 
124 USBH_Status USBH_MTP_GetObjectInfo(USBH_Info_t *phost, uint32_t handle, PTP_ObjectInfo_t *object_info);
125 
126 USBH_Status USBH_MTP_GetObject(USBH_Info_t *phost, uint32_t handle, uint8_t *object);
127 USBH_Status USBH_MTP_GetPartialObject(USBH_Info_t *phost, uint32_t handle, uint32_t offset, uint32_t maxbytes, uint8_t *object, uint32_t *len);
128 
129 USBH_Status USBH_MTP_DeleteObject(USBH_Info_t *phost, uint32_t handle, uint32_t format);
130 USBH_Status USBH_MTP_SendObject(USBH_Info_t *phost, uint32_t handle, uint8_t *object, uint32_t size);
131 
132 USBH_Status USBH_MTP_GetDevicePropDesc(USBH_Info_t *phost, uint16_t propcode, PTP_DevicePropDesc_t *devicepropertydesc);
133 
134 USBH_Status USBH_MTP_GetObjectPropsSupported(USBH_Info_t *phost, uint16_t ofc, uint32_t *propnum, uint16_t *props);
135 USBH_Status USBH_MTP_GetObjectPropDesc(USBH_Info_t *phost, uint16_t opc, uint16_t ofc, PTP_ObjectPropDesc_t *opd);
136 USBH_Status USBH_MTP_GetObjectPropList(USBH_Info_t *phost, uint32_t handle, MTP_Properties_t *pprops, uint32_t *nrofprops);
137 
138 
139 void USBH_MTP_EventsCallback(USBH_Info_t *phost, uint32_t event, uint32_t param);
140 
141 
142 #define USBH_TObreak(ms)                            \
143     {                                               \
144         static int start;                           \
145         start = USBH->FRAMENR;                      \
146         if(abs((int)USBH->FRAMENR - start) > ms)    \
147         {                                           \
148             break;                                  \
149         }                                           \
150     }
151 
152 
153 #endif // __USBH_MTP_H__
154