| 1 |
/* |
| 2 |
* dfu-programmer |
| 3 |
* |
| 4 |
* $Id$ |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdarg.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stddef.h> |
| 26 |
#include <libusb-1.0/libusb.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <stdbool.h> |
| 29 |
|
| 30 |
#include "dfu.h" |
| 31 |
#include "util.h" |
| 32 |
|
| 33 |
// cSpell:words DNBUSY |
| 34 |
|
| 35 |
/* DFU commands */ |
| 36 |
#define DFU_DETACH 0 |
| 37 |
#define DFU_DNLOAD 1 |
| 38 |
#define DFU_UPLOAD 2 |
| 39 |
#define DFU_GETSTATUS 3 |
| 40 |
#define DFU_CLRSTATUS 4 |
| 41 |
#define DFU_GETSTATE 5 |
| 42 |
#define DFU_ABORT 6 |
| 43 |
|
| 44 |
#define USB_CLASS_APP_SPECIFIC 0xfe |
| 45 |
#define DFU_SUBCLASS 0x01 |
| 46 |
|
| 47 |
/* Wait for 20 seconds before a timeout since erasing/flashing can take some time. |
| 48 |
* The longest erase cycle is for the AT32UC3A0512-TA automotive part, |
| 49 |
* which needs a timeout of at least 19 seconds to erase the whole flash. */ |
| 50 |
#define DFU_TIMEOUT 20000 |
| 51 |
|
| 52 |
/* Time (in ms) for the device to wait for the usb reset after being told to detach |
| 53 |
* before the giving up going into dfu mode. */ |
| 54 |
#define DFU_DETACH_TIMEOUT 1000 |
| 55 |
|
| 56 |
#define DFU_DEBUG_THRESHOLD 100 |
| 57 |
#define DFU_TRACE_THRESHOLD 200 |
| 58 |
#define DFU_MESSAGE_DEBUG_THRESHOLD 300 |
| 59 |
|
| 60 |
#define DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \ |
| 61 |
DFU_DEBUG_THRESHOLD, __VA_ARGS__ ) |
| 62 |
#define TRACE(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \ |
| 63 |
DFU_TRACE_THRESHOLD, __VA_ARGS__ ) |
| 64 |
#define MSG_DEBUG(...) dfu_debug( __FILE__, __FUNCTION__, __LINE__, \ |
| 65 |
DFU_MESSAGE_DEBUG_THRESHOLD, __VA_ARGS__ ) |
| 66 |
|
| 67 |
// ________ P R O T O T Y P E S _______________________________ |
| 68 |
static int32_t dfu_find_interface( struct libusb_device *device, |
| 69 |
const bool honor_interfaceclass, |
| 70 |
const uint8_t bNumConfigurations); |
| 71 |
/* Used to find the dfu interface for a device if there is one. |
| 72 |
* |
| 73 |
* device - the device to search |
| 74 |
* honor_interfaceclass - if the actual interface class information |
| 75 |
* should be checked, or ignored (bug in device DFU code) |
| 76 |
* |
| 77 |
* returns the interface number if found, < 0 otherwise |
| 78 |
*/ |
| 79 |
|
| 80 |
static int32_t dfu_make_idle( dfu_device_t *device, const bool initial_abort ); |
| 81 |
/* Gets the device into the dfuIDLE state if possible. |
| 82 |
* |
| 83 |
* device - the dfu device to communicate with |
| 84 |
* |
| 85 |
* returns 0 on success, 1 if device was reset, error otherwise |
| 86 |
*/ |
| 87 |
|
| 88 |
static int32_t dfu_transfer_out( dfu_device_t *device, |
| 89 |
uint8_t request, |
| 90 |
const int32_t value, |
| 91 |
uint8_t* data, |
| 92 |
const size_t length ); |
| 93 |
|
| 94 |
static int32_t dfu_transfer_in( dfu_device_t *device, |
| 95 |
uint8_t request, |
| 96 |
const int32_t value, |
| 97 |
uint8_t* data, |
| 98 |
const size_t length ); |
| 99 |
|
| 100 |
static void dfu_msg_response_output( const char *function, const int32_t result ); |
| 101 |
/* Used to output the response from our USB request in a human readable |
| 102 |
* form. |
| 103 |
* |
| 104 |
* function - the calling function to output on behalf of |
| 105 |
* result - the result to interpret |
| 106 |
*/ |
| 107 |
|
| 108 |
// ________ F U N C T I O N S _______________________________ |
| 109 |
void dfu_set_transaction_num( dfu_device_t *device, uint16_t newNum ) { |
| 110 |
TRACE( "%s( %u )\n", __FUNCTION__, newNum ); |
| 111 |
device->transaction = newNum; |
| 112 |
DEBUG("wValue set to %d\n", device->transaction); |
| 113 |
} |
| 114 |
|
| 115 |
uint16_t dfu_get_transaction_num( dfu_device_t *device ) { |
| 116 |
TRACE( "%s( %u )\n", __FUNCTION__ ); |
| 117 |
return device->transaction; |
| 118 |
} |
| 119 |
|
| 120 |
int32_t dfu_detach( dfu_device_t *device, const int32_t timeout ) { |
| 121 |
int32_t result; |
| 122 |
|
| 123 |
TRACE( "%s( %p, %d )\n", __FUNCTION__, device, timeout ); |
| 124 |
|
| 125 |
if( (NULL == device) || (NULL == device->handle) || (timeout < 0) ) { |
| 126 |
DEBUG( "Invalid parameter\n" ); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
result = dfu_transfer_out( device, DFU_DETACH, timeout, NULL, 0 ); |
| 131 |
|
| 132 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 133 |
|
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
int32_t dfu_download( dfu_device_t *device, |
| 138 |
const size_t length, |
| 139 |
uint8_t* data ) { |
| 140 |
int32_t result; |
| 141 |
|
| 142 |
TRACE( "%s( %p, %u, %p )\n", __FUNCTION__, device, length, data ); |
| 143 |
|
| 144 |
/* Sanity checks */ |
| 145 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 146 |
DEBUG( "Invalid parameter\n" ); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
if( (0 != length) && (NULL == data) ) { |
| 151 |
DEBUG( "data was NULL, but length != 0\n" ); |
| 152 |
return -2; |
| 153 |
} |
| 154 |
|
| 155 |
if( (0 == length) && (NULL != data) ) { |
| 156 |
DEBUG( "data was not NULL, but length == 0\n" ); |
| 157 |
return -3; |
| 158 |
} |
| 159 |
|
| 160 |
{ |
| 161 |
size_t i; |
| 162 |
for( i = 0; i < length; i++ ) { |
| 163 |
MSG_DEBUG( "Message: m[%u] = 0x%02x\n", i, data[i] ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
result = dfu_transfer_out( device, DFU_DNLOAD, device->transaction++, data, length ); |
| 168 |
|
| 169 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 170 |
|
| 171 |
return result; |
| 172 |
} |
| 173 |
|
| 174 |
int32_t dfu_upload( dfu_device_t *device, const size_t length, uint8_t* data ) { |
| 175 |
int32_t result; |
| 176 |
|
| 177 |
TRACE( "%s( %p, %u, %p )\n", __FUNCTION__, device, length, data ); |
| 178 |
|
| 179 |
/* Sanity checks */ |
| 180 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 181 |
DEBUG( "Invalid parameter\n" ); |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
if( (0 == length) || (NULL == data) ) { |
| 186 |
DEBUG( "data was NULL, or length is 0\n" ); |
| 187 |
return -2; |
| 188 |
} |
| 189 |
|
| 190 |
result = dfu_transfer_in( device, DFU_UPLOAD, device->transaction++, data, length ); |
| 191 |
|
| 192 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 193 |
|
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
int32_t dfu_get_status( dfu_device_t *device, dfu_status_t *status ) { |
| 198 |
uint8_t buffer[6]; |
| 199 |
int32_t result; |
| 200 |
|
| 201 |
TRACE( "%s( %p, %p )\n", __FUNCTION__, device, status ); |
| 202 |
|
| 203 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 204 |
DEBUG( "Invalid parameter\n" ); |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
/* Initialize the status data structure */ |
| 209 |
status->bStatus = DFU_STATUS_ERROR_UNKNOWN; |
| 210 |
status->bwPollTimeout = 0; |
| 211 |
status->bState = STATE_DFU_ERROR; |
| 212 |
status->iString = 0; |
| 213 |
|
| 214 |
result = dfu_transfer_in( device, DFU_GETSTATUS, 0, buffer, sizeof(buffer) ); |
| 215 |
|
| 216 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 217 |
|
| 218 |
if( 6 == result ) { |
| 219 |
status->bStatus = buffer[0]; |
| 220 |
status->bwPollTimeout = ((0xff & buffer[3]) << 16) | |
| 221 |
((0xff & buffer[2]) << 8) | |
| 222 |
(0xff & buffer[1]); |
| 223 |
|
| 224 |
status->bState = buffer[4]; |
| 225 |
status->iString = buffer[5]; |
| 226 |
|
| 227 |
DEBUG( "==============================\n" ); |
| 228 |
DEBUG( "status->bStatus: %s (0x%02x)\n", |
| 229 |
dfu_status_to_string(status->bStatus), status->bStatus ); |
| 230 |
DEBUG( "status->bwPollTimeout: 0x%04x ms\n", status->bwPollTimeout ); |
| 231 |
DEBUG( "status->bState: %s (0x%02x)\n", |
| 232 |
dfu_state_to_string(status->bState), status->bState ); |
| 233 |
DEBUG( "status->iString: 0x%02x\n", status->iString ); |
| 234 |
DEBUG( "------------------------------\n" ); |
| 235 |
} else { |
| 236 |
if( 0 < result ) { |
| 237 |
/* There was an error, we didn't get the entire message. */ |
| 238 |
DEBUG( "result: %d\n", result ); |
| 239 |
return -2; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
int32_t dfu_clear_status( dfu_device_t *device ) { |
| 247 |
int32_t result; |
| 248 |
|
| 249 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 250 |
|
| 251 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 252 |
DEBUG( "Invalid parameter\n" ); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
result = dfu_transfer_out( device, DFU_CLRSTATUS, 0, NULL, 0 ); |
| 257 |
|
| 258 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 259 |
|
| 260 |
return result; |
| 261 |
} |
| 262 |
|
| 263 |
int32_t dfu_get_state( dfu_device_t *device ) { |
| 264 |
int32_t result; |
| 265 |
uint8_t buffer[1]; |
| 266 |
|
| 267 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 268 |
|
| 269 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 270 |
DEBUG( "Invalid parameter\n" ); |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
result = dfu_transfer_in( device, DFU_GETSTATE, 0, buffer, sizeof(buffer) ); |
| 275 |
|
| 276 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 277 |
|
| 278 |
/* Return the error if there is one. */ |
| 279 |
if( result < 1 ) { |
| 280 |
return result; |
| 281 |
} |
| 282 |
|
| 283 |
/* Return the state. */ |
| 284 |
return buffer[0]; |
| 285 |
} |
| 286 |
|
| 287 |
int32_t dfu_abort( dfu_device_t *device ) { |
| 288 |
int32_t result; |
| 289 |
|
| 290 |
TRACE( "%s( %p )\n", __FUNCTION__, device ); |
| 291 |
|
| 292 |
if( (NULL == device) || (NULL == device->handle) ) { |
| 293 |
DEBUG( "Invalid parameter\n" ); |
| 294 |
return -1; |
| 295 |
} |
| 296 |
|
| 297 |
result = dfu_transfer_out( device, DFU_ABORT, 0, NULL, 0 ); |
| 298 |
|
| 299 |
dfu_msg_response_output( __FUNCTION__, result ); |
| 300 |
|
| 301 |
return result; |
| 302 |
} |
| 303 |
|
| 304 |
struct libusb_device *dfu_device_init( const uint32_t vendor, |
| 305 |
const uint32_t product, |
| 306 |
const uint32_t bus_number, |
| 307 |
const uint32_t device_address, |
| 308 |
dfu_device_t *dfu_device, |
| 309 |
const bool initial_abort, |
| 310 |
const bool honor_interfaceclass, |
| 311 |
libusb_context *usb_context |
| 312 |
) { |
| 313 |
libusb_device **list; |
| 314 |
size_t i,deviceCount; |
| 315 |
int32_t retries = 4; |
| 316 |
|
| 317 |
TRACE( "%s( %u, %u, %p, %s, %s )\n", __FUNCTION__, vendor, product, |
| 318 |
dfu_device, ((true == initial_abort) ? "true" : "false"), |
| 319 |
((true == honor_interfaceclass) ? "true" : "false") ); |
| 320 |
|
| 321 |
DEBUG( "%s(%08x, %08x)\n",__FUNCTION__, vendor, product ); |
| 322 |
|
| 323 |
retry: |
| 324 |
deviceCount = libusb_get_device_list( usb_context, &list ); |
| 325 |
|
| 326 |
for( i = 0; i < deviceCount; i++ ) { |
| 327 |
libusb_device *device = list[i]; |
| 328 |
struct libusb_device_descriptor descriptor; |
| 329 |
|
| 330 |
if( libusb_get_device_descriptor(device, &descriptor) ) { |
| 331 |
DEBUG( "Failed in libusb_get_device_descriptor\n" ); |
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
DEBUG( "%2d: 0x%04x, 0x%04x\n", (int) i, descriptor.idVendor, descriptor.idProduct ); |
| 336 |
|
| 337 |
if( vendor != descriptor.idVendor) continue; |
| 338 |
if( product != descriptor.idProduct) continue; |
| 339 |
|
| 340 |
if( bus_number != 0 ) { |
| 341 |
if ( libusb_get_bus_number(device) != bus_number ) continue; |
| 342 |
if ( libusb_get_device_address(device) != device_address ) continue; |
| 343 |
} |
| 344 |
|
| 345 |
int32_t tmp; |
| 346 |
DEBUG( "found device at USB:%d,%d\n", libusb_get_bus_number(device), libusb_get_device_address(device) ); |
| 347 |
// We found a device that looks like it matches... |
| 348 |
// Let's try to find the DFU interface, open the device and claim it. |
| 349 |
|
| 350 |
tmp = dfu_find_interface( device, honor_interfaceclass, descriptor.bNumConfigurations ); |
| 351 |
|
| 352 |
if (tmp < 0) { |
| 353 |
/* The interface is invalid. */ |
| 354 |
DEBUG( "Failed to find interface.\n" ); |
| 355 |
continue; |
| 356 |
} |
| 357 |
|
| 358 |
dfu_device->interface = tmp; |
| 359 |
|
| 360 |
DEBUG( "opening interface %d...\n", tmp ); |
| 361 |
|
| 362 |
tmp = libusb_open( device, &dfu_device->handle ); |
| 363 |
|
| 364 |
DEBUG( "returned %d...\n", tmp ); |
| 365 |
|
| 366 |
if (tmp) { |
| 367 |
DEBUG( "failed to open device\n" ); |
| 368 |
continue; |
| 369 |
} |
| 370 |
|
| 371 |
DEBUG( "opened interface %d...\n", tmp ); |
| 372 |
|
| 373 |
tmp = libusb_set_configuration( dfu_device->handle, 1 ); |
| 374 |
|
| 375 |
if (tmp) { |
| 376 |
DEBUG( "Failed to set configuration.\n" ); |
| 377 |
goto done; |
| 378 |
} |
| 379 |
|
| 380 |
DEBUG( "set configuration %d...\n", 1 ); |
| 381 |
|
| 382 |
tmp = libusb_claim_interface( dfu_device->handle, dfu_device->interface ); |
| 383 |
|
| 384 |
if (tmp) { |
| 385 |
DEBUG( "Failed to claim the DFU interface.\n" ); |
| 386 |
goto done; |
| 387 |
} |
| 388 |
|
| 389 |
DEBUG( "claimed interface %d...\n", dfu_device->interface ); |
| 390 |
|
| 391 |
tmp = dfu_make_idle( dfu_device, initial_abort ); |
| 392 |
|
| 393 |
if (tmp == 0 || tmp == 1) { |
| 394 |
libusb_free_device_list( list, 1 ); |
| 395 |
if (!tmp) { |
| 396 |
return device; |
| 397 |
} |
| 398 |
|
| 399 |
retries--; |
| 400 |
goto retry; |
| 401 |
} |
| 402 |
|
| 403 |
DEBUG( "Failed to put the device in dfuIDLE mode.\n" ); |
| 404 |
libusb_release_interface( dfu_device->handle, dfu_device->interface ); |
| 405 |
retries = 4; |
| 406 |
|
| 407 |
done: |
| 408 |
libusb_close( dfu_device->handle ); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
libusb_free_device_list( list, 1 ); |
| 413 |
dfu_device->handle = NULL; |
| 414 |
dfu_device->interface = 0; |
| 415 |
|
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
char* dfu_state_to_string( const int32_t state ) { |
| 420 |
char *message = "unknown state"; |
| 421 |
|
| 422 |
switch( state ) { |
| 423 |
case STATE_APP_IDLE: |
| 424 |
message = "appIDLE"; |
| 425 |
break; |
| 426 |
case STATE_APP_DETACH: |
| 427 |
message = "appDETACH"; |
| 428 |
break; |
| 429 |
case STATE_DFU_IDLE: |
| 430 |
message = "dfuIDLE"; |
| 431 |
break; |
| 432 |
case STATE_DFU_DOWNLOAD_SYNC: |
| 433 |
message = "dfuDNLOAD-SYNC"; |
| 434 |
break; |
| 435 |
case STATE_DFU_DOWNLOAD_BUSY: |
| 436 |
message = "dfuDNBUSY"; |
| 437 |
break; |
| 438 |
case STATE_DFU_DOWNLOAD_IDLE: |
| 439 |
message = "dfuDNLOAD-IDLE"; |
| 440 |
break; |
| 441 |
case STATE_DFU_MANIFEST_SYNC: |
| 442 |
message = "dfuMANIFEST-SYNC"; |
| 443 |
break; |
| 444 |
case STATE_DFU_MANIFEST: |
| 445 |
message = "dfuMANIFEST"; |
| 446 |
break; |
| 447 |
case STATE_DFU_MANIFEST_WAIT_RESET: |
| 448 |
message = "dfuMANIFEST-WAIT-RESET"; |
| 449 |
break; |
| 450 |
case STATE_DFU_UPLOAD_IDLE: |
| 451 |
message = "dfuUPLOAD-IDLE"; |
| 452 |
break; |
| 453 |
case STATE_DFU_ERROR: |
| 454 |
message = "dfuERROR"; |
| 455 |
break; |
| 456 |
} |
| 457 |
|
| 458 |
return message; |
| 459 |
} |
| 460 |
|
| 461 |
char* dfu_status_to_string( const int32_t status ) { |
| 462 |
char *message = "unknown status"; |
| 463 |
|
| 464 |
switch( status ) { |
| 465 |
case DFU_STATUS_OK: |
| 466 |
message = "OK"; |
| 467 |
break; |
| 468 |
case DFU_STATUS_ERROR_TARGET: |
| 469 |
message = "errTARGET"; |
| 470 |
break; |
| 471 |
case DFU_STATUS_ERROR_FILE: |
| 472 |
message = "errFILE"; |
| 473 |
break; |
| 474 |
case DFU_STATUS_ERROR_WRITE: |
| 475 |
message = "errWRITE"; |
| 476 |
break; |
| 477 |
case DFU_STATUS_ERROR_ERASE: |
| 478 |
message = "errERASE"; |
| 479 |
break; |
| 480 |
case DFU_STATUS_ERROR_CHECK_ERASED: |
| 481 |
message = "errCHECK_ERASED"; |
| 482 |
break; |
| 483 |
case DFU_STATUS_ERROR_PROG: |
| 484 |
message = "errPROG"; |
| 485 |
break; |
| 486 |
case DFU_STATUS_ERROR_VERIFY: |
| 487 |
message = "errVERIFY"; |
| 488 |
break; |
| 489 |
case DFU_STATUS_ERROR_ADDRESS: |
| 490 |
message = "errADDRESS"; |
| 491 |
break; |
| 492 |
case DFU_STATUS_ERROR_NOTDONE: |
| 493 |
message = "errNOTDONE"; |
| 494 |
break; |
| 495 |
case DFU_STATUS_ERROR_FIRMWARE: |
| 496 |
message = "errFIRMWARE"; |
| 497 |
break; |
| 498 |
case DFU_STATUS_ERROR_VENDOR: |
| 499 |
message = "errVENDOR"; |
| 500 |
break; |
| 501 |
case DFU_STATUS_ERROR_USBR: |
| 502 |
message = "errUSBR"; |
| 503 |
break; |
| 504 |
case DFU_STATUS_ERROR_POR: |
| 505 |
message = "errPOR"; |
| 506 |
break; |
| 507 |
case DFU_STATUS_ERROR_UNKNOWN: |
| 508 |
message = "errUNKNOWN"; |
| 509 |
break; |
| 510 |
case DFU_STATUS_ERROR_STALLEDPKT: |
| 511 |
message = "errSTALLEDPKT"; |
| 512 |
break; |
| 513 |
} |
| 514 |
return message; |
| 515 |
} |
| 516 |
|
| 517 |
static int32_t dfu_find_interface( struct libusb_device *device, |
| 518 |
const bool honor_interfaceclass, |
| 519 |
const uint8_t bNumConfigurations) { |
| 520 |
int32_t c,i,s; |
| 521 |
|
| 522 |
TRACE( "%s()\n", __FUNCTION__ ); |
| 523 |
|
| 524 |
/* Loop through all of the configurations */ |
| 525 |
for( c = 0; c < bNumConfigurations; c++ ) { |
| 526 |
struct libusb_config_descriptor *config; |
| 527 |
|
| 528 |
if( libusb_get_config_descriptor(device, c, &config) ) { |
| 529 |
DEBUG( "can't get_config_descriptor: %d\n", c ); |
| 530 |
return -1; |
| 531 |
} |
| 532 |
DEBUG( "config %d: MaxPower=%d*2 mA\n", c, config->MaxPower ); |
| 533 |
|
| 534 |
/* Loop through all of the interfaces */ |
| 535 |
for( i = 0; i < config->bNumInterfaces; i++ ) { |
| 536 |
struct libusb_interface interface; |
| 537 |
|
| 538 |
interface = config->interface[i]; |
| 539 |
DEBUG( "interface %d\n", i ); |
| 540 |
|
| 541 |
/* Loop through all of the settings */ |
| 542 |
for( s = 0; s < interface.num_altsetting; s++ ) { |
| 543 |
struct libusb_interface_descriptor setting; |
| 544 |
|
| 545 |
setting = interface.altsetting[s]; |
| 546 |
DEBUG( "setting %d: class:%d, subclass %d, protocol:%d\n", s, |
| 547 |
setting.bInterfaceClass, setting.bInterfaceSubClass, |
| 548 |
setting.bInterfaceProtocol ); |
| 549 |
|
| 550 |
if( honor_interfaceclass ) { |
| 551 |
/* Check if the interface is a DFU interface */ |
| 552 |
if( (USB_CLASS_APP_SPECIFIC == setting.bInterfaceClass) |
| 553 |
&& (DFU_SUBCLASS == setting.bInterfaceSubClass) ) |
| 554 |
{ |
| 555 |
DEBUG( "Found DFU Interface: %d\n", setting.bInterfaceNumber ); |
| 556 |
return setting.bInterfaceNumber; |
| 557 |
} |
| 558 |
} else { |
| 559 |
/* If there is a bug in the DFU firmware, return the first |
| 560 |
* found interface. */ |
| 561 |
DEBUG( "Found DFU Interface: %d\n", setting.bInterfaceNumber ); |
| 562 |
return setting.bInterfaceNumber; |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
libusb_free_config_descriptor( config ); |
| 568 |
} |
| 569 |
|
| 570 |
return -1; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
static int32_t dfu_make_idle( dfu_device_t *device, |
| 575 |
const bool initial_abort ) { |
| 576 |
dfu_status_t status; |
| 577 |
int32_t retries = 4; |
| 578 |
|
| 579 |
if( true == initial_abort ) { |
| 580 |
dfu_abort( device ); |
| 581 |
} |
| 582 |
|
| 583 |
while( 0 < retries ) { |
| 584 |
if( 0 != dfu_get_status(device, &status) ) { |
| 585 |
dfu_clear_status( device ); |
| 586 |
continue; |
| 587 |
} |
| 588 |
|
| 589 |
DEBUG( "State: %s (%d)\n", dfu_state_to_string(status.bState), status.bState ); |
| 590 |
|
| 591 |
switch( status.bState ) { |
| 592 |
case STATE_DFU_IDLE: |
| 593 |
if( DFU_STATUS_OK == status.bStatus ) { |
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
/* We need the device to have the DFU_STATUS_OK status. */ |
| 598 |
dfu_clear_status( device ); |
| 599 |
break; |
| 600 |
|
| 601 |
case STATE_DFU_DOWNLOAD_SYNC: /* abort -> idle */ |
| 602 |
case STATE_DFU_DOWNLOAD_IDLE: /* abort -> idle */ |
| 603 |
case STATE_DFU_MANIFEST_SYNC: /* abort -> idle */ |
| 604 |
case STATE_DFU_UPLOAD_IDLE: /* abort -> idle */ |
| 605 |
case STATE_DFU_DOWNLOAD_BUSY: /* abort -> error */ |
| 606 |
case STATE_DFU_MANIFEST: /* abort -> error */ |
| 607 |
dfu_abort( device ); |
| 608 |
break; |
| 609 |
|
| 610 |
case STATE_DFU_ERROR: |
| 611 |
dfu_clear_status( device ); |
| 612 |
break; |
| 613 |
|
| 614 |
case STATE_APP_IDLE: |
| 615 |
dfu_detach( device, DFU_DETACH_TIMEOUT ); |
| 616 |
break; |
| 617 |
|
| 618 |
case STATE_APP_DETACH: |
| 619 |
case STATE_DFU_MANIFEST_WAIT_RESET: |
| 620 |
DEBUG( "Resetting the device\n" ); |
| 621 |
libusb_reset_device( device->handle ); |
| 622 |
return 1; |
| 623 |
} |
| 624 |
|
| 625 |
retries--; |
| 626 |
} |
| 627 |
|
| 628 |
DEBUG( "Not able to transition the device into the dfuIDLE state.\n" ); |
| 629 |
return -2; |
| 630 |
} |
| 631 |
|
| 632 |
static int32_t dfu_transfer_out( dfu_device_t *device, |
| 633 |
uint8_t request, |
| 634 |
const int32_t value, |
| 635 |
uint8_t* data, |
| 636 |
const size_t length ) { |
| 637 |
return libusb_control_transfer( device->handle, |
| 638 |
/* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 639 |
/* bRequest */ request, |
| 640 |
/* wValue */ value, |
| 641 |
/* wIndex */ device->interface, |
| 642 |
/* Data */ data, |
| 643 |
/* wLength */ length, |
| 644 |
DFU_TIMEOUT ); |
| 645 |
} |
| 646 |
|
| 647 |
static int32_t dfu_transfer_in( dfu_device_t *device, |
| 648 |
uint8_t request, |
| 649 |
const int32_t value, |
| 650 |
uint8_t* data, |
| 651 |
const size_t length ) { |
| 652 |
return libusb_control_transfer( device->handle, |
| 653 |
/* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 654 |
/* bRequest */ request, |
| 655 |
/* wValue */ value, |
| 656 |
/* wIndex */ device->interface, |
| 657 |
/* Data */ data, |
| 658 |
/* wLength */ length, |
| 659 |
DFU_TIMEOUT ); |
| 660 |
} |
| 661 |
|
| 662 |
static void dfu_msg_response_output( const char *function, |
| 663 |
const int32_t result ) { |
| 664 |
char *msg = NULL; |
| 665 |
|
| 666 |
if( 0 <= result ) { |
| 667 |
msg = "No error."; |
| 668 |
} else { |
| 669 |
switch( result ) { |
| 670 |
case LIBUSB_ERROR_IO : |
| 671 |
msg = "LIBUSB_ERROR_IO: Input/output error."; |
| 672 |
break; |
| 673 |
case LIBUSB_ERROR_INVALID_PARAM : |
| 674 |
msg = "LIBUSB_ERROR_INVALID_PARAM: Invalid parameter."; |
| 675 |
break; |
| 676 |
case LIBUSB_ERROR_ACCESS : |
| 677 |
msg = "LIBUSB_ERROR_ACCESS: Access denied (insufficient permissions)"; |
| 678 |
break; |
| 679 |
case LIBUSB_ERROR_NO_DEVICE : |
| 680 |
msg = "LIBUSB_ERROR_NO_DEVICE: No such device (it may have been disconnected)"; |
| 681 |
break; |
| 682 |
case LIBUSB_ERROR_NOT_FOUND : |
| 683 |
msg = "LIBUSB_ERROR_NOT_FOUND: Entity not found."; |
| 684 |
break; |
| 685 |
case LIBUSB_ERROR_BUSY : |
| 686 |
msg = "LIBUSB_ERROR_BUSY: Resource busy."; |
| 687 |
break; |
| 688 |
case LIBUSB_ERROR_TIMEOUT : |
| 689 |
msg = "LIBUSB_ERROR_TIMEOUT: Operation timed out."; |
| 690 |
break; |
| 691 |
case LIBUSB_ERROR_OVERFLOW : |
| 692 |
msg = "LIBUSB_ERROR_OVERFLOW: Overflow."; |
| 693 |
break; |
| 694 |
case LIBUSB_ERROR_PIPE : |
| 695 |
msg = "LIBUSB_ERROR_PIPE: Pipe error."; |
| 696 |
break; |
| 697 |
case LIBUSB_ERROR_INTERRUPTED : |
| 698 |
msg = "LIBUSB_ERROR_INTERRUPTED: System call interrupted (perhaps due to signal)"; |
| 699 |
break; |
| 700 |
case LIBUSB_ERROR_NO_MEM : |
| 701 |
msg = "LIBUSB_ERROR_NO_MEM: Insufficient memory."; |
| 702 |
break; |
| 703 |
case LIBUSB_ERROR_NOT_SUPPORTED : |
| 704 |
msg = "LIBUSB_ERROR_NOT_SUPPORTED: Operation not supported or unimplemented on this platform."; |
| 705 |
break; |
| 706 |
case LIBUSB_ERROR_OTHER : |
| 707 |
msg = "LIBUSB_ERROR_OTHER: Other error."; |
| 708 |
break; |
| 709 |
|
| 710 |
default: |
| 711 |
msg = "Unknown error"; |
| 712 |
break; |
| 713 |
} |
| 714 |
DEBUG( "%s ERR: %s 0x%08x (%d)\n", function, msg, result, result ); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
#ifdef malloc |
| 719 |
#undef malloc |
| 720 |
void* rpl_malloc( size_t n ) { |
| 721 |
if( 0 == n ) { |
| 722 |
n = 1; |
| 723 |
} |
| 724 |
|
| 725 |
return malloc( n ); |
| 726 |
} |
| 727 |
#endif |