Modules/USB MSC - Can now read on qemu (dunno how reliable)
[tpg/acess2.git] / KernelLand / Modules / USB / MSC / main.c
1 /*
2  * Acess2 USB Stack Mass Storage Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Driver Core
7  */
8 #define DEBUG   0
9 #define VERSION VER2(0,1)
10 #include <acess.h>
11 #include <modules.h>
12 #include "common.h"
13 #include "msc_proto.h"
14
15 // === PROTOTYPES ===
16  int    MSC_Initialise(char **Arguments);
17 void    MSC_Cleanup(void);
18 void    MSC_DeviceConnected(tUSBInterface *Dev, void *Descriptors, size_t DescriptorsLen);
19 void    MSC_DataIn(tUSBInterface *Dev, int EndPt, int Length, void *Data);
20 // --- Internal Helpers
21  int    MSC_int_CreateCBW(tMSC_CBW *Cbw, int bInput, size_t CmdLen, const void *CmdData, size_t DataLen);
22 void    MSC_SendData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, const void *Data);
23 void    MSC_RecvData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, void *Data);
24
25 // === GLOBALS ===
26 MODULE_DEFINE(0, VERSION, USB_MSC, MSC_Initialise, MSC_Cleanup, "USB_Core", NULL);
27 tUSBDriver      gMSC_Driver_BulkOnly = {
28         .Name = "MSC_BulkOnly",
29         .Match = {.Class = {0x080050, 0xFF00FF}},
30         .Connected = MSC_DeviceConnected,
31         .MaxEndpoints = 2,
32         .Endpoints = {
33                 {0x82, MSC_DataIn},
34                 {0x02, NULL},
35                 }
36         };
37 int giMSC_NextTag;
38
39 // === CODE ===
40 int MSC_Initialise(char **Arguments)
41 {
42         USB_RegisterDriver( &gMSC_Driver_BulkOnly );
43         return 0;
44 }
45
46 void MSC_Cleanup(void)
47 {
48 }
49
50 void MSC_DeviceConnected(tUSBInterface *Dev, void *Descriptors, size_t DescriptorsLen)
51 {
52         tMSCInfo        *info;
53         // TODO: Get the class code (and hence what command set is in use)
54         // TODO: Get the maximum packet size too
55
56         info = malloc( sizeof(*info) );
57         USB_SetDeviceDataPtr(Dev, info);        
58
59         info->BlockCount = MSC_SCSI_GetSize(Dev, &info->BlockSize);
60
61         LOG("Device has 0x%llx blocks of 0x%x bytes",
62                 info->BlockCount, info->BlockSize);
63         LVM_AddVolume(&gMSC_SCSI_VolType, "0", Dev, info->BlockSize, info->BlockCount);
64 }
65
66 void MSC_DataIn(tUSBInterface *Dev, int EndPt, int Length, void *Data)
67 {
68         // Will this ever be needed?
69 }
70
71 int MSC_int_CreateCBW(tMSC_CBW *Cbw, int bInput, size_t CmdLen, const void *CmdData, size_t DataLen)
72 {
73         if( CmdLen == 0 ) {
74                 Log_Error("MSC", "Zero length commands are invalid");
75                 return -1;
76         }
77         
78         if( CmdLen > 16 ) {
79                 Log_Error("MSC", "Command data >16 bytes (%i)", CmdLen);
80                 return -1;
81         }
82         
83         Cbw->dCBWSignature = LittleEndian32(0x43425355);
84         Cbw->dCBWTag    = giMSC_NextTag ++;
85         Cbw->dCBWDataTransferLength = LittleEndian32(DataLen);
86         Cbw->bmCBWFlags = (bInput ? 0x80 : 0x00);
87         Cbw->bCBWLUN    = 0;    // TODO: Logical Unit Number (param from proto)
88         Cbw->bCBWLength = CmdLen;
89         memcpy(Cbw->CBWCB, CmdData, CmdLen);
90         
91         return 0;
92 }
93
94 void MSC_SendData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, const void *Data)
95 {
96         tMSC_CBW        cbw;
97         tMSC_CSW        csw;
98         const int       endpoint_out = 2;
99         const int       endpoint_in = 1;
100         const int       max_packet_size = 64;
101         
102         if( MSC_int_CreateCBW(&cbw, 0, CmdLen, CmdData, DataLen) )
103                 return ;
104         
105         // Send CBW
106         USB_SendData(Dev, endpoint_out, sizeof(cbw), &cbw);
107         
108         // Send Data
109         for( size_t ofs = 0; ofs < DataLen; ofs += max_packet_size )
110         {
111                 USB_SendData(Dev, endpoint_out, MIN(max_packet_size, DataLen - ofs), Data + ofs);
112         }
113         
114         // Read CSW
115         USB_RecvData(Dev, endpoint_in, sizeof(csw), &csw);
116         // TODO: Validate CSW
117 }
118
119 void MSC_RecvData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, void *Data)
120 {
121         tMSC_CBW        cbw;
122         tMSC_CSW        csw;
123         const int       endpoint_out = 2;
124         const int       endpoint_in = 1;
125         const int       max_packet_size = 64;
126                 
127         if( MSC_int_CreateCBW(&cbw, 1, CmdLen, CmdData, DataLen) )
128                 return ;
129         
130         // Send CBW
131         LOG("Send CBW");
132         USB_SendData(Dev, endpoint_out, sizeof(cbw), &cbw);
133         
134         // Read Data
135         for( size_t ofs = 0; ofs < DataLen; ofs += max_packet_size )
136         {
137                 LOG("Read bytes 0x%x+0x%x", ofs, MIN(max_packet_size, DataLen - ofs));
138                 // TODO: use async version and wait for the transaction to complete
139                 USB_RecvData(Dev, endpoint_in, MIN(max_packet_size, DataLen - ofs), Data + ofs);
140         }
141         
142         // Read CSW
143         LOG("Read CSW");
144         USB_RecvData(Dev, endpoint_in, sizeof(csw), &csw);
145         // TODO: Validate CSW
146 }
147

UCC git Repository :: git.ucc.asn.au