The Pedigree Project 0.1
UsbMassStorageDevice.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef USBMASSSTORAGEDEVICE_H
21#define USBMASSSTORAGEDEVICE_H
22#include "pedigree/kernel/compiler.h"
23#include "pedigree/kernel/processor/types.h"
24#include "pedigree/kernel/utilities/String.h"
25#include "pedigree/kernel/utilities/utility.h"
26
27#include <config.h>
28
29#include "modules/drivers/common/scsi/ScsiController.h"
30#include "modules/system/usb/UsbConstants.h"
31#include "modules/system/usb/UsbDevice.h"
32
33class Device;
34
35#if (HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS) || PEDIGREE_USB_SMOKE_TESTS
37#endif
38
40 public:
42 virtual ~UsbMassStorageDevice();
43
44 virtual void initialiseDriver();
45 void prepareForDisconnection() override;
46
47 virtual bool sendCommand(size_t nUnit, uintptr_t pCommand, uint8_t nCommandSize,
48 uintptr_t pRespBuffer, uint16_t nRespBytes, bool bWrite);
49
50 virtual void getUsbDeviceName(String& str) {
51 str.assign("USB Mass Storage Device", 24);
52 }
53
54 virtual bool hasSubtree() const {
55 return true;
56 }
57
58 virtual Device* getDevice() {
59 return this;
60 }
61
62 private:
63#if (HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS) || PEDIGREE_USB_SMOKE_TESTS
64 friend class UsbMassStorageBotTestAccess;
65#endif
66
67 enum class BotStatus { Passed, Failed, RecoveryRequired };
68
69 bool massStorageReset();
70 MUST_USE_RESULT bool performResetRecovery();
71 MUST_USE_RESULT BotStatus readStatus(uint32_t tag, uint32_t expectedBytes);
72
73 enum MassStorageRequests {
74 MassStorageRequest = static_cast<uint8_t>(static_cast<uint8_t>(UsbRequestType::Class) |
75 static_cast<uint8_t>(UsbRequestRecipient::Interface)),
76
77 MassStorageReset = 0xFF,
78 MassStorageGetMaxLUN = 0xFE
79 };
80
81 enum MassStorageSigs {
82 CbwSig = HOST_TO_LITTLE32(0x43425355), // USBC
83 CswSig = HOST_TO_LITTLE32(0x53425355) // USBS
84 };
85
86 struct Cbw {
87 uint32_t nSig;
88 uint32_t nTag;
89 uint32_t nDataBytes;
90 uint8_t nFlags;
91 uint8_t nLUN;
92 uint8_t nCommandSize;
93 uint8_t pCommand[16];
94 } PACKED;
95
96 struct Csw {
97 uint32_t nSig;
98 uint32_t nTag;
99 uint32_t nResidue;
100 uint8_t nStatus;
101 } PACKED;
102
103 static_assert(sizeof(Cbw) == 31, "BOT CBW wire size changed");
104 static_assert(sizeof(Csw) == 13, "BOT CSW wire size changed");
105
106 Mutex m_CommandLock;
107 size_t m_nUnits;
108 Endpoint* m_pInEndpoint;
109 Endpoint* m_pOutEndpoint;
110 uint32_t m_NextTag;
111 bool m_ResetRecoveryRequired;
112 bool m_DeviceAvailable;
113
114 protected:
115 virtual size_t getNumUnits() {
116 return m_nUnits;
117 }
118};
119
120#endif
Definition Mutex.h:56
virtual Device * getDevice()
virtual bool hasSubtree() const
Do we expose our own Device tree?
virtual void initialiseDriver()
Implemented by the driver class, initialises driver-specific stuff.
void prepareForDisconnection() override