-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSCSI2SD_HID.cc
More file actions
244 lines (217 loc) · 5.27 KB
/
Copy pathSCSI2SD_HID.cc
File metadata and controls
244 lines (217 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
//
// This file is part of SCSI2SD.
//
// SCSI2SD is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SCSI2SD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
#include "SCSI2SD_HID.hh"
#include <cassert>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <string.h> // memcpy
using namespace SCSI2SD;
HID::HID(hid_device_info* hidInfo) :
myHidInfo(hidInfo),
myConfigHandle(NULL),
myDebugHandle(NULL),
myFirmwareVersion(0),
mySDCapacity(0)
{
// hidInfo->interface_number not supported on mac, and interfaces
// are enumerated in a random order. :-(
// We rely on the watchdog value of the debug interface changing on each
// read to differentiate the interfaces.
while (hidInfo && !(myConfigHandle && myDebugHandle))
{
if ((hidInfo->interface_number == CONFIG_INTERFACE) ||
(hidInfo->usage_page == 0xFF00))
{
myConfigHandle = hid_open_path(hidInfo->path);
hidInfo = hidInfo->next;
}
else if ((hidInfo->interface_number == DEBUG_INTERFACE) ||
(hidInfo->usage_page == 0xFF01))
{
myDebugHandle = hid_open_path(hidInfo->path);
readDebugData();
hidInfo = hidInfo->next;
}
else if (hidInfo->interface_number == -1)
{
// hidInfo->interface_number not supported on mac, and
// interfaces are enumerated in a random order. :-(
// We rely on the watchdog value of the debug interface
// changing on each read to differentiate the interfaces.
// Not necessary since firmware 3.5.2 as the usage_page can
// be used instead.
hid_device* dev = hid_open_path(hidInfo->path);
if (!dev)
{
hidInfo = hidInfo->next;
continue;
}
uint8_t buf[HID_PACKET_SIZE];
int watchVal = -1;
int configIntFound = 1;
for (int i = 0; i < 4; ++i)
{
buf[0] = 0; // report id
hid_read(dev, buf, HID_PACKET_SIZE);
if (watchVal == -1) watchVal = buf[25];
configIntFound = configIntFound && (buf[25] == watchVal);
}
if (configIntFound)
{
myConfigHandle = dev;
}
else
{
myDebugHandle = dev;
readDebugData();
}
}
}
}
HID::~HID()
{
if (myConfigHandle)
{
hid_close(myConfigHandle);
}
if (myDebugHandle)
{
hid_close(myDebugHandle);
}
hid_free_enumeration(myHidInfo);
}
HID*
HID::Open()
{
hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
if (dev)
{
return new HID(dev);
}
else
{
return NULL;
}
}
void
HID::enterBootloader()
{
// Reboot commands added in firmware 3.5
if (!myDebugHandle)
{
throw std::runtime_error(
"Cannot enter SCSI2SD bootloader: debug interface not found");
}
else if (myFirmwareVersion == 0)
{
throw std::runtime_error(
"Cannot enter SCSI2SD bootloader: old firmware version running.\n"
"The SCSI2SD board cannot reset itself. Please disconnect and \n"
"reconnect the USB cable.\n");
}
else
{
uint8_t hidBuf[HID_PACKET_SIZE + 1] =
{
0x00, // Report ID;
0x01 // Reboot command
// 63 bytes unused.
};
int result = hid_write(myDebugHandle, hidBuf, sizeof(hidBuf));
if (result < 0)
{
const wchar_t* err = hid_error(myDebugHandle);
std::stringstream ss;
ss << "USB HID write failure: " << err;
throw std::runtime_error(ss.str());
}
}
}
void
HID::readConfig(uint8_t* buffer, size_t len)
{
assert(len >= 0);
buffer[0] = 0; // report id
int result = hid_read(myConfigHandle, buffer, len);
if (result < 0)
{
const wchar_t* err = hid_error(myConfigHandle);
std::stringstream ss;
ss << "USB HID read failure: " << err;
throw std::runtime_error(ss.str());
}
}
void
HID::saveConfig(uint8_t* buffer, size_t len)
{
assert(len >= 0 && len <= HID_PACKET_SIZE);
uint8_t hidBuf[HID_PACKET_SIZE + 1] =
{
0x00, // Report ID;
};
memcpy(&hidBuf[1], buffer, len);
int result = hid_write(myConfigHandle, hidBuf, len + 1);
if (result < 0)
{
const wchar_t* err = hid_error(myConfigHandle);
std::stringstream ss;
ss << "USB HID write failure: " << err;
throw std::runtime_error(ss.str());
}
}
void
HID::readDebugData()
{
uint8_t buf[HID_PACKET_SIZE];
buf[0] = 0; // report id
int result = hid_read(myDebugHandle, buf, HID_PACKET_SIZE);
if (result < 0)
{
const wchar_t* err = hid_error(myDebugHandle);
std::stringstream ss;
ss << "USB HID read failure: " << err;
throw std::runtime_error(ss.str());
}
myFirmwareVersion = (((uint16_t)buf[62]) << 8) | buf[63];
mySDCapacity =
(((uint32_t)buf[58]) << 24) |
(((uint32_t)buf[59]) << 16) |
(((uint32_t)buf[60]) << 8) |
((uint32_t)buf[61]);
}
std::string
HID::getFirmwareVersionStr() const
{
if (myFirmwareVersion == 0)
{
return "Unknown (3.0 - 3.4)";
}
else
{
std::stringstream ver;
ver << std::hex <<
(myFirmwareVersion >> 8) <<
'.' << ((myFirmwareVersion & 0xF0) >> 4);
int rev = myFirmwareVersion & 0xF;
if (rev)
{
ver << "." << rev;
}
return ver.str();
}
}