2 * Acess2 USB Stack Mass Storage Driver
3 * - By John Hodge (thePowersGang)
9 #define VERSION VER2(0,1)
13 #include "msc_proto.h"
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);
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,
40 int MSC_Initialise(char **Arguments)
42 USB_RegisterDriver( &gMSC_Driver_BulkOnly );
46 void MSC_Cleanup(void)
50 void MSC_DeviceConnected(tUSBInterface *Dev, void *Descriptors, size_t DescriptorsLen)
55 info = malloc( sizeof(*info) );
56 USB_SetDeviceDataPtr(Dev, info);
58 // Get the class code (and hence what command set is in use)
59 Uint8 sc = (USB_GetInterfaceClass(Dev) & 0x00FF00) >> 8;
62 // SCSI Transparent Command Set
64 info->BlockCount = MSC_SCSI_GetSize(Dev, &info->BlockSize);
65 vt = &gMSC_SCSI_VolType;
67 // Unknown, prepare to chuck sad
69 Log_Error("USB MSC", "Unknown sub-class 0x%02x", sc);
70 USB_SetDeviceDataPtr(Dev, NULL);
75 // Create device name from Vendor ID, Device ID and Serial Number
78 USB_GetDeviceVendor(Dev, &vendor, &devid);
79 serial_number = USB_GetSerialNumber(Dev);
80 if( !serial_number ) {
81 // No serial number - this breaks spec, but it's easy to handle
82 Log_Warning("USB MSC", "Device does not have a serial number, using a random one");
83 serial_number = malloc(4+8+1);
84 sprintf(serial_number, "rand%08x", rand());
86 char name[4 + 4 + 1 + 4 + 1 + strlen(serial_number) + 1];
87 sprintf(name, "usb-%04x:%04x-%s", vendor, devid, serial_number);
90 // Pass the buck to LVM
91 LOG("Device '%s' has 0x%llx blocks of 0x%x bytes", name, info->BlockCount, info->BlockSize);
92 LVM_AddVolume(vt, name, Dev, info->BlockSize, info->BlockCount);
95 void MSC_DataIn(tUSBInterface *Dev, int EndPt, int Length, void *Data)
97 // Will this ever be needed?
101 * \brief Create a CBW structure
103 int MSC_int_CreateCBW(tMSC_CBW *Cbw, int bInput, size_t CmdLen, const void *CmdData, size_t DataLen)
106 Log_Error("MSC", "Zero length commands are invalid");
111 Log_Error("MSC", "Command data >16 bytes (%i)", CmdLen);
115 Cbw->dCBWSignature = LittleEndian32(0x43425355);
116 Cbw->dCBWTag = giMSC_NextTag ++;
117 Cbw->dCBWDataTransferLength = LittleEndian32(DataLen);
118 Cbw->bmCBWFlags = (bInput ? 0x80 : 0x00);
119 Cbw->bCBWLUN = 0; // TODO: Logical Unit Number (param from proto)
120 Cbw->bCBWLength = CmdLen;
121 memcpy(Cbw->CBWCB, CmdData, CmdLen);
126 void MSC_SendData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, const void *Data)
130 const int endpoint_out = 2;
131 const int endpoint_in = 1;
133 if( MSC_int_CreateCBW(&cbw, 0, CmdLen, CmdData, DataLen) )
137 USB_SendData(Dev, endpoint_out, sizeof(cbw), &cbw);
140 USB_SendData(Dev, endpoint_out, DataLen, Data);
143 USB_RecvData(Dev, endpoint_in, sizeof(csw), &csw);
144 // TODO: Validate CSW
147 void MSC_RecvData(tUSBInterface *Dev, size_t CmdLen, const void *CmdData, size_t DataLen, void *Data)
151 const int endpoint_out = 2;
152 const int endpoint_in = 1;
154 if( MSC_int_CreateCBW(&cbw, 1, CmdLen, CmdData, DataLen) )
158 USB_SendData(Dev, endpoint_out, sizeof(cbw), &cbw);
161 // TODO: use async version and wait for the transaction to complete
162 USB_RecvData(Dev, endpoint_in, DataLen, Data);
165 USB_RecvData(Dev, endpoint_in, sizeof(csw), &csw);
166 // TODO: Validate CSW