dmrconfig/dfu-libusb.c

446 lines
12 KiB
C
Raw Normal View History

2018-08-25 19:31:02 -07:00
/*
2018-09-07 21:12:21 -07:00
* DFU routines for Linux and MacOS, via libusb-1.0.
2018-08-25 19:31:02 -07:00
*
* Copyright (C) 2018 Serge Vakulenko, KK6ABQ
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>
2018-08-27 17:50:32 -07:00
#include "util.h"
2018-08-25 19:31:02 -07:00
//
2018-08-25 20:20:51 -07:00
// USB request types.
2018-08-25 19:31:02 -07:00
//
#define REQUEST_TYPE_TO_HOST 0xA1
#define REQUEST_TYPE_TO_DEVICE 0x21
enum {
REQUEST_DETACH = 0,
REQUEST_DNLOAD = 1,
REQUEST_UPLOAD = 2,
REQUEST_GETSTATUS = 3,
REQUEST_CLRSTATUS = 4,
REQUEST_GETSTATE = 5,
REQUEST_ABORT = 6,
};
enum {
appIDLE = 0,
appDETACH = 1,
dfuIDLE = 2,
dfuDNLOAD_SYNC = 3,
dfuDNBUSY = 4,
dfuDNLOAD_IDLE = 5,
dfuMANIFEST_SYNC = 6,
dfuMANIFEST = 7,
dfuMANIFEST_WAIT_RESET = 8,
dfuUPLOAD_IDLE = 9,
dfuERROR = 10,
};
typedef struct {
unsigned status : 8;
unsigned poll_timeout : 24;
unsigned state : 8;
unsigned string_index : 8;
} status_t;
static libusb_context *ctx = NULL;
static libusb_device_handle *dev;
static status_t status;
static int detach(int timeout)
{
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send DETACH\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DETACH, timeout, 0, NULL, 0, 0);
return error;
}
static int get_status()
{
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send GETSTATUS [6]\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_HOST,
REQUEST_GETSTATUS, 0, 0, (unsigned char*)&status, 6, 0);
2018-09-04 23:37:42 -07:00
if (trace_flag && error >= 0) {
2018-08-27 17:50:32 -07:00
printf("--- Recv ");
print_hex((unsigned char*)&status, 6);
printf("\n");
}
2018-08-25 19:31:02 -07:00
return error;
}
static int clear_status()
{
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send CLRSTATUS\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_CLRSTATUS, 0, 0, NULL, 0, 0);
return error;
}
2018-08-27 17:50:32 -07:00
static int get_state(int *pstate)
2018-08-25 19:31:02 -07:00
{
2018-08-27 17:50:32 -07:00
unsigned char state;
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send GETSTATE [1]\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_HOST,
2018-08-27 17:50:32 -07:00
REQUEST_GETSTATE, 0, 0, &state, 1, 0);
*pstate = state;
2018-09-04 23:37:42 -07:00
if (trace_flag && error >= 0) {
2018-08-27 17:50:32 -07:00
printf("--- Recv ");
print_hex(&state, 1);
printf("\n");
}
2018-08-25 19:31:02 -07:00
return error;
}
static int dfu_abort()
{
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send ABORT\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_ABORT, 0, 0, NULL, 0, 0);
return error;
}
static void wait_dfu_idle()
{
int state, error;
for (;;) {
error = get_state(&state);
if (error < 0) {
fprintf(stderr, "%s: cannot get state: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
switch (state) {
case dfuIDLE:
return;
case appIDLE:
error = detach(1000);
break;
case dfuERROR:
error = clear_status();
break;
case appDETACH:
case dfuDNBUSY:
case dfuMANIFEST_WAIT_RESET:
usleep(100000);
continue;
default:
error = dfu_abort();
break;
}
if (error < 0) {
fprintf(stderr, "%s: unexpected usb error in state=%d: %d: %s\n",
__func__, state, error, libusb_strerror(error));
exit(-1);
}
}
}
static void md380_command(uint8_t a, uint8_t b)
{
unsigned char cmd[2] = { a, b };
2018-08-27 17:50:32 -07:00
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send DNLOAD [2] ");
print_hex(cmd, 2);
printf("\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DNLOAD, 0, 0, cmd, 2, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot send command: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
get_status();
usleep(100000);
wait_dfu_idle();
}
static void set_address(uint32_t address)
{
unsigned char cmd[5] = { 0x21,
(uint8_t)address,
(uint8_t)(address >> 8),
(uint8_t)(address >> 16),
(uint8_t)(address >> 24),
};
2018-08-27 17:50:32 -07:00
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send DNLOAD [5] ");
print_hex(cmd, 5);
printf("\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DNLOAD, 0, 0, cmd, 5, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot send command: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
get_status();
wait_dfu_idle();
}
static void erase_block(uint32_t address, int progress_flag)
2018-08-25 19:31:02 -07:00
{
unsigned char cmd[5] = { 0x41,
(uint8_t)address,
(uint8_t)(address >> 8),
(uint8_t)(address >> 16),
(uint8_t)(address >> 24),
};
2018-08-27 17:50:32 -07:00
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send DNLOAD [5] ");
print_hex(cmd, 5);
printf("\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DNLOAD, 0, 0, cmd, 5, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot send command: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
get_status();
wait_dfu_idle();
if (progress_flag) {
fprintf(stderr, "#");
fflush(stderr);
}
2018-08-25 19:31:02 -07:00
}
2018-08-25 20:20:51 -07:00
static const char *identify()
{
static uint8_t data[64];
md380_command(0xa2, 0x01);
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send UPLOAD [64]\n");
}
2018-08-25 20:20:51 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_HOST,
REQUEST_UPLOAD, 0, 0, data, 64, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot read data: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
2018-09-04 23:37:42 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Recv ");
print_hex(data, 64);
printf("\n");
}
2018-08-25 20:20:51 -07:00
get_status();
return (const char*) data;
}
2018-08-27 17:50:32 -07:00
const char *dfu_init(unsigned vid, unsigned pid)
2018-08-25 19:31:02 -07:00
{
int error = libusb_init(&ctx);
if (error < 0) {
fprintf(stderr, "libusb init failed: %d: %s\n",
error, libusb_strerror(error));
exit(-1);
}
dev = libusb_open_device_with_vid_pid(ctx, vid, pid);
if (!dev) {
fprintf(stderr, "Cannot find USB device %04x:%04x\n",
vid, pid);
libusb_exit(ctx);
ctx = 0;
exit(-1);
}
if (libusb_kernel_driver_active(dev, 0)) {
libusb_detach_kernel_driver(dev, 0);
}
error = libusb_claim_interface(dev, 0);
if (error < 0) {
fprintf(stderr, "Failed to claim USB interface: %d: %s\n",
error, libusb_strerror(error));
libusb_close(dev);
libusb_exit(ctx);
ctx = 0;
exit(-1);
}
// Enter Programming Mode.
2018-09-07 21:12:21 -07:00
wait_dfu_idle();
2018-08-25 19:31:02 -07:00
md380_command(0x91, 0x01);
2018-08-25 20:20:51 -07:00
// Get device identifier in a static buffer.
const char *ident = identify();
2018-08-25 19:31:02 -07:00
// Zero address.
set_address(0x00000000);
2018-08-25 20:20:51 -07:00
return ident;
2018-08-25 19:31:02 -07:00
}
void dfu_close()
{
if (ctx) {
libusb_release_interface(dev, 0);
libusb_close(dev);
libusb_exit(ctx);
ctx = 0;
}
}
void dfu_erase(unsigned start, unsigned finish)
2018-08-25 20:20:51 -07:00
{
2018-08-25 20:52:04 -07:00
// Enter Programming Mode.
get_status();
wait_dfu_idle();
2018-08-25 20:52:04 -07:00
md380_command(0x91, 0x01);
usleep(100000);
if (start == 0) {
// Erase 256kbytes of configuration memory.
erase_block(0x00000000, 1);
erase_block(0x00010000, 1);
erase_block(0x00020000, 1);
erase_block(0x00030000, 1);
if (finish > 256*1024) {
// Erase 768kbytes of extended configuration memory.
erase_block(0x00110000, 1);
erase_block(0x00120000, 1);
erase_block(0x00130000, 1);
erase_block(0x00140000, 1);
erase_block(0x00150000, 1);
erase_block(0x00160000, 1);
erase_block(0x00170000, 1);
erase_block(0x00180000, 1);
erase_block(0x00190000, 1);
erase_block(0x001a0000, 1);
erase_block(0x001b0000, 1);
erase_block(0x001c0000, 1);
erase_block(0x001d0000, 1);
}
} else {
// Erase callsign database.
int addr;
for (addr=start; addr<finish; addr+=0x00010000) {
erase_block(addr, (addr & 0x00070000) == 0x00070000);
}
2018-08-25 20:20:51 -07:00
}
2018-08-25 20:52:04 -07:00
// Zero address.
set_address(0x00000000);
2018-08-25 20:20:51 -07:00
}
void dfu_read_block(int bno, uint8_t *data, int nbytes)
2018-08-25 19:31:02 -07:00
{
if (bno >= 256 && bno < 2048)
2018-08-25 19:31:02 -07:00
bno += 832;
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send UPLOAD [%d]\n", nbytes);
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_HOST,
REQUEST_UPLOAD, bno+2, 0, data, nbytes, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot read block %d, nbytes = %d: %d: %s\n",
__func__, bno, nbytes, error, libusb_strerror(error));
exit(-1);
}
2018-09-04 23:37:42 -07:00
if (trace_flag > 1) {
2018-08-27 17:50:32 -07:00
printf("--- Recv ");
print_hex(data, nbytes);
printf("\n");
}
2018-08-25 19:31:02 -07:00
get_status();
}
2018-08-25 20:20:51 -07:00
void dfu_write_block(int bno, uint8_t *data, int nbytes)
2018-08-25 19:31:02 -07:00
{
if (bno >= 256 && bno < 2048)
2018-08-25 19:31:02 -07:00
bno += 832;
2018-08-31 11:56:21 -07:00
if (trace_flag) {
2018-08-27 17:50:32 -07:00
printf("--- Send DNLOAD [%d] ", nbytes);
2018-09-04 23:37:42 -07:00
if (trace_flag > 1)
print_hex(data, nbytes);
2018-08-27 17:50:32 -07:00
printf("\n");
}
2018-08-25 19:31:02 -07:00
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DNLOAD, bno+2, 0, data, nbytes, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot write block %d, nbytes = %d: %d: %s\n",
__func__, bno, nbytes, error, libusb_strerror(error));
exit(-1);
}
get_status();
2018-08-25 20:52:04 -07:00
wait_dfu_idle();
2018-08-25 19:31:02 -07:00
}
2018-09-04 23:37:42 -07:00
void dfu_reboot()
{
unsigned char cmd[2] = { 0x91, 0x05 };
if (trace_flag) {
printf("--- Send DNLOAD [2] ");
print_hex(cmd, 2);
printf("\n");
}
wait_dfu_idle();
int error = libusb_control_transfer(dev, REQUEST_TYPE_TO_DEVICE,
REQUEST_DNLOAD, 0, 0, cmd, 2, 0);
if (error < 0) {
fprintf(stderr, "%s: cannot send command: %d: %s\n",
__func__, error, libusb_strerror(error));
exit(-1);
}
get_status();
}