1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using Antmicro.Renode.Utilities; 10 11 namespace Antmicro.Renode.Peripherals.USBDeprecated 12 { 13 public abstract class USBDescriptor 14 { 15 USBDescriptor()16 public USBDescriptor() 17 { 18 19 } 20 21 private byte length; 22 23 public byte Length 24 { 25 get 26 { 27 return length; 28 } 29 set 30 { 31 length = value; 32 array = new byte[value]; 33 } 34 } 35 36 public DescriptorType Type{ get; set; } 37 38 byte[] array; 39 ToArray()40 public virtual byte[] ToArray() 41 { 42 array[0x0] = Length; 43 array[0x1] = (byte)Type; 44 return array; 45 } 46 } 47 48 public class StandardUSBDescriptor : USBDescriptor 49 { StandardUSBDescriptor()50 public StandardUSBDescriptor() 51 { 52 Type = DescriptorType.Device; 53 Length = 0x12; 54 } 55 56 public ushort USB{ get; set; } 57 58 public byte DeviceClass{ get; set; } 59 60 public byte DeviceSubClass{ get; set; } 61 62 public byte DeviceProtocol{ get; set; } 63 64 public byte MaxPacketSize{ get; set; } 65 66 public ushort VendorId{ get; set; } 67 68 public ushort ProductId{ get; set; } 69 70 public ushort Device{ get; set; } 71 72 public byte ManufacturerIndex{ get; set; } 73 74 public byte ProductIndex{ get; set; } 75 76 public byte SerialNumberIndex{ get; set; } 77 78 public byte NumberOfConfigurations{ get; set; } 79 ToArray()80 public override byte[] ToArray() 81 { 82 var arr = base.ToArray(); 83 arr[0x2] = USB.LoByte(); 84 arr[0x3] = USB.HiByte(); 85 arr[0x4] = DeviceClass; 86 arr[0x5] = DeviceSubClass; 87 arr[0x6] = DeviceProtocol; 88 arr[0x7] = MaxPacketSize; 89 arr[0x8] = VendorId.LoByte(); 90 arr[0x9] = VendorId.HiByte(); 91 arr[0xA] = ProductId.LoByte(); 92 arr[0xB] = ProductId.HiByte(); 93 arr[0xC] = Device.LoByte(); 94 arr[0xD] = Device.HiByte(); 95 arr[0xE] = ManufacturerIndex; 96 arr[0xF] = ProductIndex; 97 arr[0x10] = SerialNumberIndex; 98 arr[0x11] = NumberOfConfigurations; 99 return arr; 100 } 101 } 102 103 public class DeviceQualifierUSBDescriptor:USBDescriptor 104 { DeviceQualifierUSBDescriptor()105 public DeviceQualifierUSBDescriptor() 106 { 107 Type = DescriptorType.DeviceQualifier; 108 Length = 0xA; 109 } 110 111 public ushort USB{ get; set; } 112 113 public byte DeviceClass{ get; set; } 114 115 public byte DeviceSubClass{ get; set; } 116 117 public byte DeviceProtocol{ get; set; } 118 119 public byte MaxPacketSize{ get; set; } 120 121 public byte NumberOfConfigurations{ get; set; } 122 //+1 byte reserved 123 ToArray()124 public override byte[] ToArray() 125 { 126 var arr = base.ToArray(); 127 arr[0x2] = USB.LoByte(); 128 arr[0x3] = USB.HiByte(); 129 arr[0x4] = DeviceClass; 130 arr[0x5] = DeviceSubClass; 131 arr[0x6] = DeviceProtocol; 132 arr[0x7] = MaxPacketSize; 133 arr[0x8] = NumberOfConfigurations; 134 arr[0x9] = 0; 135 //Reserved 136 return arr; 137 } 138 } 139 140 public class ConfigurationUSBDescriptor:USBDescriptor 141 { ConfigurationUSBDescriptor()142 public ConfigurationUSBDescriptor() 143 { 144 Type = DescriptorType.Configuration; 145 Length = 0x9; 146 } 147 148 public ushort TotalLength{ get; set; } 149 150 public byte NumberOfInterfaces{ get; set; } 151 152 public byte ConfigurationValue{ get; set; } 153 154 public byte ConfigurationIndex{ get; set; } 155 156 public bool SelfPowered { get; set; } 157 158 public bool RemoteWakeup{ get; set; } 159 160 public byte Attributes 161 { 162 get 163 { 164 return (byte)((1 << 7) | ((SelfPowered ? 1 : 0) << 6) | ((RemoteWakeup ? 1 : 0) << 5)); 165 } 166 set 167 { 168 169 SelfPowered = ((value >> 6) & 1) != 0; 170 RemoteWakeup = ((value >> 5) & 1) != 0; 171 } 172 } 173 174 public byte MaxPower{ get; set; } 175 176 public InterfaceUSBDescriptor[] InterfaceDescriptor; 177 ToArray()178 public override byte[] ToArray() 179 { 180 TotalLength = Length; 181 for(int i=0; i<NumberOfInterfaces; i++) 182 { 183 TotalLength += InterfaceDescriptor[i].Length; 184 for(int j=0; j<InterfaceDescriptor[i].NumberOfEndpoints; j++) 185 { 186 TotalLength += InterfaceDescriptor[i].EndpointDescriptor[j].Length; 187 } 188 } 189 190 var arr = new byte[TotalLength]; 191 var offset = Length; 192 arr[0x0] = Length; 193 arr[0x1] = (byte)Type; 194 arr[0x2] = TotalLength.LoByte(); 195 arr[0x3] = TotalLength.HiByte(); 196 arr[0x4] = NumberOfInterfaces; 197 arr[0x5] = ConfigurationValue; 198 arr[0x6] = ConfigurationIndex; 199 arr[0x7] = Attributes; 200 arr[0x8] = MaxPower; 201 202 for(int i=0; i<NumberOfInterfaces; i++) 203 { 204 InterfaceDescriptor[i].ToArray().CopyTo(arr, offset); 205 offset += InterfaceDescriptor[i].Length; 206 for(int j=0; j<InterfaceDescriptor[i].NumberOfEndpoints; j++) 207 { 208 InterfaceDescriptor[i].EndpointDescriptor[j].ToArray().CopyTo(arr, offset); 209 offset += InterfaceDescriptor[i].EndpointDescriptor[j].Length; 210 } 211 } 212 return arr; 213 } 214 } 215 216 public class InterfaceUSBDescriptor:USBDescriptor 217 { InterfaceUSBDescriptor()218 public InterfaceUSBDescriptor() 219 { 220 Type = DescriptorType.Interface; 221 Length = 0x9; 222 } 223 224 public byte InterfaceNumber{ get; set; } 225 226 public byte AlternateSetting{ get; set; } 227 228 public byte NumberOfEndpoints{ get; set; } 229 230 public byte InterfaceClass{ get; set; } 231 232 public byte InterfaceSubClass{ get; set; } 233 234 public byte InterfaceProtocol{ get; set; } 235 236 public byte InterfaceIndex{ get; set; } 237 238 public EndpointUSBDescriptor[] EndpointDescriptor; 239 ToArray()240 public override byte[] ToArray() 241 { 242 var arr = base.ToArray(); 243 arr[0x2] = InterfaceNumber; 244 arr[0x3] = AlternateSetting; 245 arr[0x4] = NumberOfEndpoints; 246 arr[0x5] = InterfaceClass; 247 arr[0x6] = InterfaceSubClass; 248 arr[0x7] = InterfaceProtocol; 249 arr[0x8] = InterfaceIndex; 250 return arr; 251 } 252 } 253 254 public class EndpointUSBDescriptor:USBDescriptor 255 { EndpointUSBDescriptor()256 public EndpointUSBDescriptor() 257 { 258 Type = DescriptorType.Endpoint; 259 Length = 7; 260 } 261 262 public byte EndpointNumber{ get; set; } 263 264 public bool InEnpoint{ get; set; } 265 266 public byte EndpointAddress 267 { 268 get 269 { 270 return (byte)(((InEnpoint ? 1 : 0) << 7) | (EndpointNumber & 7)); 271 //6..4 reserved 272 } 273 set 274 { 275 EndpointNumber = (byte)(value & 7); 276 InEnpoint = ((value >> 7) & 1) != 0; 277 } 278 } 279 280 public TransferTypeEnum TransferType{ get; set; } 281 282 public SynchronizationTypeEnum SynchronizationType{ get; set; } 283 284 public UsageTypeEnum UsageType{ get; set; } 285 286 public byte Attributes 287 { 288 get 289 { 290 return (byte)((((byte)UsageType & 3) << 4) | ((byte)SynchronizationType & 3) << 2 | ((byte)TransferType & 3)); 291 } 292 set 293 { 294 TransferType = (TransferTypeEnum)(value & 3); 295 SynchronizationType = (SynchronizationTypeEnum)((value >> 2) & 3); 296 UsageType = (UsageTypeEnum)((value >> 4) & 3); 297 } 298 } 299 300 public ushort MaxPacketSize{ get; set; } 301 302 public byte Interval{ get; set; } 303 ToArray()304 public override byte[] ToArray() 305 { 306 var arr = base.ToArray(); 307 arr[0x2] = EndpointAddress; 308 arr[0x3] = Attributes; 309 arr[0x4] = MaxPacketSize.LoByte(); 310 arr[0x5] = MaxPacketSize.HiByte(); 311 arr[0x6] = Interval; 312 313 return arr; 314 } 315 public enum TransferTypeEnum : byte 316 { 317 Control = 0x00, 318 Isochronous = 0x01, 319 Bulk = 0x2, 320 Interrupt = 0x03 321 } 322 323 public enum SynchronizationTypeEnum : byte 324 { 325 NoSynchronization = 0x00, 326 Asynchronous = 0x01, 327 Adaptive = 0x02, 328 Synchronous = 0x03 329 } 330 331 public enum UsageTypeEnum : byte 332 { 333 Data = 0x00, 334 Feedback = 0x01, 335 ImplicitFeedbackData = 0x02 336 } 337 } 338 339 public class StringUSBDescriptor : USBDescriptor 340 { StringUSBDescriptor(string value)341 public StringUSBDescriptor(string value) 342 { 343 Type = DescriptorType.String; 344 StringValue = value; 345 Length = (byte)(2 + System.Text.ASCIIEncoding.Unicode.GetByteCount(value)); 346 } 347 StringUSBDescriptor(uint numberOfLangs)348 public StringUSBDescriptor(uint numberOfLangs) 349 { 350 LangId = new ushort[numberOfLangs]; 351 Length = (byte)(numberOfLangs * 2 + 2); 352 Type = DescriptorType.String; 353 } 354 355 public string StringValue { get; set; } 356 357 public ushort[] LangId{ get; set; } 358 ToArray()359 public override byte[] ToArray() 360 { 361 var arr = base.ToArray(); 362 int i = 0x2; 363 if(StringValue != null) 364 { 365 var bytes = System.Text.ASCIIEncoding.Unicode.GetBytes(StringValue); 366 367 foreach(var byt in bytes) 368 { 369 arr[i] = byt; 370 ++i; 371 } 372 } 373 else 374 { 375 foreach(var id in LangId) 376 { 377 arr[i] = id.LoByte(); 378 ++i; 379 380 arr[i] = id.HiByte(); 381 ++i; 382 } 383 } 384 385 return arr; 386 } 387 } 388 } 389 390