Fix input of size fieds and CDROM sector length in scsi2sd-util
[SCSI2SD.git] / software / scsi2sd-util / SCSI2SD_HID.cc
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
2 //
3 //      This file is part of SCSI2SD.
4 //
5 //      SCSI2SD is free software: you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation, either version 3 of the License, or
8 //      (at your option) any later version.
9 //
10 //      SCSI2SD is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
17 #include "SCSI2SD_HID.hh"
18 #include "scsi2sd.h"
19 #include "hidpacket.h"
20
21 #include <cassert>
22 #include <stdexcept>
23 #include <sstream>
24
25 #include <iostream>
26 #include <string.h> // memcpy
27
28 using namespace SCSI2SD;
29
30 HID::HID(hid_device_info* hidInfo) :
31         myHidInfo(hidInfo),
32         myConfigHandle(NULL),
33         myDebugHandle(NULL),
34         myFirmwareVersion(0),
35         mySDCapacity(0)
36 {
37
38         // hidInfo->interface_number not supported on mac, and interfaces
39         // are enumerated in a random order. :-(
40         // We rely on the watchdog value of the debug interface changing on each
41         // read to differentiate the interfaces.
42         try
43         {
44                 while (hidInfo && !(myConfigHandle && myDebugHandle))
45                 {
46                         std::stringstream msg;
47                         msg << "Error opening HID device " << hidInfo->path << std::endl;
48
49                         if ((hidInfo->interface_number == CONFIG_INTERFACE) ||
50                                 (hidInfo->usage_page == 0xFF00))
51                         {
52                                 myConfigHandle = hid_open_path(hidInfo->path);
53                                 if (!myConfigHandle) throw std::runtime_error(msg.str());
54
55                                 hidInfo = hidInfo->next;
56                         }
57                         else if ((hidInfo->interface_number == DEBUG_INTERFACE) ||
58                                 (hidInfo->usage_page == 0xFF01))
59                         {
60                                 myDebugHandle = hid_open_path(hidInfo->path);
61                                 if (!myDebugHandle) throw std::runtime_error(msg.str());
62                                 readDebugData();
63                                 hidInfo = hidInfo->next;
64                         }
65                         else if (hidInfo->interface_number == -1)
66                         {
67                                 // hidInfo->interface_number not supported on mac, and
68                                 // interfaces are enumerated in a random order. :-(
69                                 // We rely on the watchdog value of the debug interface
70                                 // changing on each read to differentiate the interfaces.
71                                 // Not necessary since firmware 3.5.2 as the usage_page can
72                                 // be used instead.
73                                 hid_device* dev = hid_open_path(hidInfo->path);
74                                 if (!dev)
75                                 {
76                                         throw std::runtime_error(msg.str());
77                                 }
78
79                                 uint8_t buf[HID_PACKET_SIZE];
80                                 int watchVal = -1;
81                                 int configIntFound = 1;
82                                 for (int i = 0; i < 4; ++i)
83                                 {
84                                         buf[0] = 0; // report id
85                                         hid_read_timeout(dev, buf, HID_PACKET_SIZE, HID_TIMEOUT_MS);
86                                         if (watchVal == -1) watchVal = buf[25];
87                                         configIntFound = configIntFound && (buf[25] == watchVal);
88                                 }
89
90                                 if (configIntFound)
91                                 {
92                                         myConfigHandle = dev;
93                                 }
94                                 else
95                                 {
96                                         myDebugHandle = dev;
97                                         readDebugData();
98                                 }
99                         }
100                 }
101         }
102         catch (std::runtime_error& e)
103         {
104                 destroy();
105                 throw e;
106         }
107 }
108
109 void
110 HID::destroy()
111 {
112         if (myConfigHandle)
113         {
114                 hid_close(myConfigHandle);
115                 myConfigHandle = NULL;
116         }
117         if (myDebugHandle)
118         {
119                 hid_close(myDebugHandle);
120                 myDebugHandle = NULL;
121         }
122
123         hid_free_enumeration(myHidInfo);
124         myHidInfo = NULL;
125 }
126
127 HID::~HID()
128 {
129         destroy();
130 }
131
132 HID*
133 HID::Open()
134 {
135         hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
136         if (dev)
137         {
138                 return new HID(dev);
139         }
140         else
141         {
142                 return NULL;
143         }
144 }
145
146 void
147 HID::enterBootloader()
148 {
149         // Reboot commands added in firmware 3.5
150         if (!myDebugHandle)
151         {
152                 throw std::runtime_error(
153                         "Cannot enter SCSI2SD bootloader: debug interface not found");
154         }
155         else if (myFirmwareVersion == 0)
156         {
157                 throw std::runtime_error(
158                         "Cannot enter SCSI2SD bootloader: old firmware version running.\n"
159                         "The SCSI2SD board cannot reset itself. Please disconnect and \n"
160                         "reconnect the USB cable.\n");
161         }
162         else
163         {
164                 uint8_t hidBuf[HID_PACKET_SIZE + 1] =
165                 {
166                         0x00, // Report ID;
167                         0x01 // Reboot command
168                         // 63 bytes unused.
169                 };
170
171                 int result = hid_write(myDebugHandle, hidBuf, sizeof(hidBuf));
172                 if (result <= 0)
173                 {
174                         const wchar_t* err = hid_error(myDebugHandle);
175                         std::stringstream ss;
176                         ss << "USB HID write failure: " << err;
177                         throw std::runtime_error(ss.str());
178                 }
179         }
180 }
181
182 void
183 HID::readFlashRow(int array, int row, std::vector<uint8_t>& out)
184 {
185         std::vector<uint8_t> cmd
186         {
187                 CONFIG_READFLASH,
188                 static_cast<uint8_t>(array),
189                 static_cast<uint8_t>(row)
190         };
191         sendHIDPacket(cmd, out, HIDPACKET_MAX_LEN / 62);
192 }
193
194 void
195 HID::writeFlashRow(int array, int row, const std::vector<uint8_t>& in)
196 {
197         std::vector<uint8_t> cmd;
198         cmd.push_back(CONFIG_WRITEFLASH);
199         cmd.insert(cmd.end(), in.begin(), in.end());
200         cmd.push_back(static_cast<uint8_t>(array));
201         cmd.push_back(static_cast<uint8_t>(row));
202         std::vector<uint8_t> out;
203         sendHIDPacket(cmd, out, 1);
204         if ((out.size() < 1) || (out[0] != CONFIG_STATUS_GOOD))
205         {
206                 std::stringstream ss;
207                 ss << "Error writing flash " << array << "/" << row;
208                 throw std::runtime_error(ss.str());
209         }
210 }
211
212 bool
213 HID::readSCSIDebugInfo(std::vector<uint8_t>& buf)
214 {
215         buf[0] = 0; // report id
216         hid_set_nonblocking(myDebugHandle, 1);
217         int result =
218                 hid_read_timeout(
219                         myDebugHandle,
220                         &buf[0],
221                         HID_PACKET_SIZE,
222                         HID_TIMEOUT_MS);
223         hid_set_nonblocking(myDebugHandle, 0);
224
225         if (result <= 0)
226         {
227                 const wchar_t* err = hid_error(myDebugHandle);
228                 std::stringstream ss;
229                 ss << "USB HID read failure: " << err;
230                 throw std::runtime_error(ss.str());
231         }
232         return result > 0;
233 }
234
235
236 void
237 HID::readHID(uint8_t* buffer, size_t len)
238 {
239         assert(len >= 0);
240         buffer[0] = 0; // report id
241
242         int result = -1;
243         for (int retry = 0; retry < 3 && result <= 0; ++retry)
244         {
245                 result = hid_read_timeout(myConfigHandle, buffer, len, HID_TIMEOUT_MS);
246         }
247
248         if (result < 0)
249         {
250                 const wchar_t* err = hid_error(myConfigHandle);
251                 std::stringstream ss;
252                 ss << "USB HID read failure: " << err;
253                 throw std::runtime_error(ss.str());
254         }
255 }
256
257 void
258 HID::readDebugData()
259 {
260         uint8_t buf[HID_PACKET_SIZE];
261         buf[0] = 0; // report id
262         int result =
263                 hid_read_timeout(
264                         myDebugHandle,
265                         buf,
266                         HID_PACKET_SIZE,
267                         HID_TIMEOUT_MS);
268
269         if (result <= 0)
270         {
271                 const wchar_t* err = hid_error(myDebugHandle);
272                 std::stringstream ss;
273                 ss << "USB HID read failure: " << err;
274                 throw std::runtime_error(ss.str());
275         }
276         myFirmwareVersion = (((uint16_t)buf[62]) << 8) | buf[63];
277
278         mySDCapacity =
279                 (((uint32_t)buf[58]) << 24) |
280                 (((uint32_t)buf[59]) << 16) |
281                 (((uint32_t)buf[60]) << 8) |
282                 ((uint32_t)buf[61]);
283 }
284
285 std::string
286 HID::getFirmwareVersionStr() const
287 {
288         if (myFirmwareVersion == 0)
289         {
290                 return "Unknown (3.0 - 3.4)";
291         }
292         else
293         {
294                 std::stringstream ver;
295                 ver << std::hex <<
296                         (myFirmwareVersion >> 8) <<
297                         '.' << ((myFirmwareVersion & 0xF0) >> 4);
298
299                 int rev = myFirmwareVersion & 0xF;
300                 if (rev)
301                 {
302                         ver << "." << rev;
303                 }
304                 return ver.str();
305         }
306 }
307
308
309 bool
310 HID::ping()
311 {
312         std::vector<uint8_t> cmd { CONFIG_PING };
313         std::vector<uint8_t> out;
314         try
315         {
316                 sendHIDPacket(cmd, out, 1);
317         }
318         catch (std::runtime_error& e)
319         {
320                 return false;
321         }
322
323         return (out.size() >= 1) && (out[0] == CONFIG_STATUS_GOOD);
324 }
325
326 std::vector<uint8_t>
327 HID::getSD_CSD()
328 {
329         std::vector<uint8_t> cmd { CONFIG_SDINFO };
330         std::vector<uint8_t> out;
331         try
332         {
333                 sendHIDPacket(cmd, out, 16);
334         }
335         catch (std::runtime_error& e)
336         {
337                 return std::vector<uint8_t>(16);
338         }
339
340         out.resize(16);
341         return out;
342 }
343
344 std::vector<uint8_t>
345 HID::getSD_CID()
346 {
347         std::vector<uint8_t> cmd { CONFIG_SDINFO };
348         std::vector<uint8_t> out;
349         try
350         {
351                 sendHIDPacket(cmd, out, 16);
352         }
353         catch (std::runtime_error& e)
354         {
355                 return std::vector<uint8_t>(16);
356         }
357
358         std::vector<uint8_t> result(16);
359         for (size_t i = 0; i < 16; ++i) result[i] = out[16 + i];
360         return result;
361 }
362
363 bool
364 HID::scsiSelfTest()
365 {
366         std::vector<uint8_t> cmd { CONFIG_SCSITEST };
367         std::vector<uint8_t> out;
368         try
369         {
370                 sendHIDPacket(cmd, out, 2);
371         }
372         catch (std::runtime_error& e)
373         {
374                 return false;
375         }
376         return (out.size() >= 1) && (out[0] == CONFIG_STATUS_GOOD);
377 }
378
379
380 void
381 HID::sendHIDPacket(
382         const std::vector<uint8_t>& cmd,
383         std::vector<uint8_t>& out,
384         size_t responseLength)
385 {
386         assert(cmd.size() <= HIDPACKET_MAX_LEN);
387         hidPacket_send(&cmd[0], cmd.size());
388
389         uint8_t hidBuf[HID_PACKET_SIZE];
390         const uint8_t* chunk = hidPacket_getHIDBytes(hidBuf);
391
392         while (chunk)
393         {
394                 uint8_t reportBuf[HID_PACKET_SIZE + 1] = { 0x00 }; // Report ID
395                 memcpy(&reportBuf[1], chunk, HID_PACKET_SIZE);
396                 int result = -1;
397                 for (int retry = 0; retry < 10 && result <= 0; ++retry)
398                 {
399                         result = hid_write(myConfigHandle, reportBuf, sizeof(reportBuf));
400                 }
401
402                 if (result <= 0)
403                 {
404                         const wchar_t* err = hid_error(myConfigHandle);
405                         std::stringstream ss;
406                         ss << "USB HID write failure: " << err;
407                         throw std::runtime_error(ss.str());
408                 }
409                 chunk = hidPacket_getHIDBytes(hidBuf);
410         }
411
412         const uint8_t* resp = NULL;
413         size_t respLen;
414         resp = hidPacket_getPacket(&respLen);
415
416         for (unsigned int retry = 0; retry < responseLength * 2 && !resp; ++retry)
417         {
418                 readHID(hidBuf, sizeof(hidBuf)); // Will block
419                 hidPacket_recv(hidBuf, HID_PACKET_SIZE);
420                 resp = hidPacket_getPacket(&respLen);
421         }
422
423         if (!resp)
424         {
425                 throw std::runtime_error("SCSI2SD config protocol error");
426         }
427         out.insert(
428                 out.end(),
429                 resp,
430                 resp + respLen);
431 }
432
433