2
0
forked from Ivasoft/DSView

Code refactoring 5

This commit is contained in:
dreamsourcelabTAI
2022-07-28 16:57:15 +08:00
parent 478ec6b06e
commit a027a19448
19 changed files with 326 additions and 304 deletions

View File

@@ -28,6 +28,8 @@ SR_PRIV int command_ctl_wr(libusb_device_handle *devhdl, struct ctl_wr_cmd cmd)
{
int ret;
assert(devhdl);
/* Send the control command. */
ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_ENDPOINT_OUT, CMD_CTL_WR, 0x0000, 0x0000,
@@ -46,6 +48,8 @@ SR_PRIV int command_ctl_rd(libusb_device_handle *devhdl, struct ctl_rd_cmd cmd)
{
int ret;
assert(devhdl);
/* Send the control message. */
ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_ENDPOINT_OUT, CMD_CTL_RD_PRE, 0x0000, 0x0000,

View File

@@ -198,12 +198,18 @@ static GSList *scan(GSList *options)
GSList *l, *devices, *conn_devices;
struct libusb_device_descriptor des;
libusb_device **devlist;
libusb_device *device_handle = NULL;
int devcnt, ret, i, j;
const char *conn;
enum libusb_speed usb_speed;
drvc = di->priv;
if (options != NULL)
sr_info("%s", "Scan DSCope device with options.");
else
sr_info("%s", "Scan DSCope device.");
conn = NULL;
for (l = options; l; l = l->next) {
src = l->data;
@@ -213,21 +219,27 @@ static GSList *scan(GSList *options)
break;
}
}
if (conn)
if (conn){
sr_info("%s", "Find usb device with connect config.");
conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
}
else
conn_devices = NULL;
/* Find all DSCope compatible devices and upload firmware to them. */
devices = NULL;
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
for (i = 0; devlist[i]; i++) {
for (i = 0; devlist[i]; i++)
{
device_handle = devlist[i];
if (conn) {
usb = NULL;
for (l = conn_devices; l; l = l->next) {
usb = l->data;
if (usb->bus == libusb_get_bus_number(devlist[i])
&& usb->address == libusb_get_device_address(devlist[i]))
if (usb->bus == libusb_get_bus_number(device_handle)
&& usb->address == libusb_get_device_address(device_handle))
break;
}
if (!l)
@@ -236,13 +248,13 @@ static GSList *scan(GSList *options)
continue;
}
if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
if ((ret = libusb_get_device_descriptor( device_handle, &des)) != 0) {
sr_warn("Failed to get device descriptor: %s.",
libusb_error_name(ret));
continue;
}
usb_speed = libusb_get_device_speed( devlist[i]);
usb_speed = libusb_get_device_speed(device_handle);
if ((usb_speed != LIBUSB_SPEED_HIGH) &&
(usb_speed != LIBUSB_SPEED_SUPER))
continue;
@@ -253,6 +265,7 @@ static GSList *scan(GSList *options)
des.idProduct == supported_DSCope[j].pid &&
usb_speed == supported_DSCope[j].usb_speed) {
prof = &supported_DSCope[j];
break;
}
}
@@ -260,10 +273,13 @@ static GSList *scan(GSList *options)
if (!prof)
continue;
sr_info("Got a device handle: %p", device_handle);
devcnt = g_slist_length(drvc->instances);
devc = DSCope_dev_new(prof);
if (!devc)
return NULL;
sdi = sr_dev_inst_new(channel_modes[devc->ch_mode].mode, devcnt, SR_ST_INITIALIZING,
prof->vendor, prof->model, prof->model_version);
if (!sdi) {
@@ -272,24 +288,25 @@ static GSList *scan(GSList *options)
}
sdi->priv = devc;
sdi->driver = di;
sdi->dev_type = DEV_TYPE_HARDWARE;
drvc->instances = g_slist_append(drvc->instances, sdi);
//devices = g_slist_append(devices, sdi);
/* Fill in probelist according to this device's profile. */
if (dsl_setup_probes(sdi, channel_modes[devc->ch_mode].num) != SR_OK)
return NULL;
if (dsl_check_conf_profile(devlist[i])) {
if (dsl_check_conf_profile(device_handle)) {
/* Already has the firmware, so fix the new address. */
sr_info("Found an DSCope device.");
sr_info("Found a DSCope device, name: \"%s\"", prof->model);
sdi->status = SR_ST_INACTIVE;
sdi->inst_type = SR_INST_USB;
sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
libusb_get_device_address(devlist[i]), NULL);
sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(device_handle),
libusb_get_device_address(device_handle), NULL);
/* only report device after firmware is ready */
devices = g_slist_append(devices, sdi);
} else {
}
else {
char *firmware;
char *res_path = sr_get_firmware_res_path();
if (!(firmware = g_try_malloc(strlen(res_path)+strlen(prof->firmware)+1))) {
@@ -298,7 +315,7 @@ static GSList *scan(GSList *options)
}
strcpy(firmware, res_path);
strcat(firmware, prof->firmware);
if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
if (ezusb_upload_firmware(device_handle, USB_CONFIGURATION,
firmware) == SR_OK)
/* Store when this device's FW was updated. */
devc->fw_updated = g_get_monotonic_time();
@@ -307,10 +324,11 @@ static GSList *scan(GSList *options)
"device %d.", devcnt);
g_free(firmware);
sdi->inst_type = SR_INST_USB;
sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(device_handle),
0xff, NULL);
}
}
libusb_free_device_list(devlist, 1);
g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
@@ -1813,6 +1831,12 @@ static int dev_close(struct sr_dev_inst *sdi)
return ret;
}
static int dev_destroy(struct sr_dev_inst *sdi)
{
dsl_destroy_device(sdi);
}
static int cleanup(void)
{
int ret;
@@ -2077,6 +2101,7 @@ SR_PRIV struct sr_dev_driver DSCope_driver_info = {
.config_list = config_list,
.dev_open = dev_open,
.dev_close = dev_close,
.dev_destroy = dev_destroy,
.dev_status_get = dev_status_get,
.dev_acquisition_start = dev_acquisition_start,
.dev_acquisition_stop = dev_acquisition_stop,

View File

@@ -28,7 +28,7 @@
#include <sys/stat.h>
#undef LOG_PREFIX
#define LOG_PREFIX "DSL Hardware: "
#define LOG_PREFIX "dsl: "
extern struct ds_trigger *trigger;
@@ -290,6 +290,7 @@ SR_PRIV gboolean dsl_check_conf_profile(libusb_device *dev)
static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi)
{
libusb_device **devlist;
libusb_device *dev_handel=NULL;
struct sr_usb_dev_inst *usb;
struct libusb_device_descriptor des;
struct DSL_context *devc;
@@ -303,8 +304,13 @@ static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi)
devc = sdi->priv;
usb = sdi->conn;
sr_info("%s", "Try to open device instance.");
assert(usb->devhdl == NULL);
if (sdi->status == SR_ST_ACTIVE) {
/* Device is already in use. */
sr_info("%s", "Device is actived, can't to open.");
return SR_ERR;
}
@@ -316,42 +322,55 @@ static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi)
return SR_ERR;
}
for (i = 0; i < device_count; i++) {
if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
for (i = 0; i < device_count; i++)
{
dev_handel = devlist[i];
if ((ret = libusb_get_device_descriptor(dev_handel, &des))) {
sr_err("Failed to get device descriptor: %s.",
libusb_error_name(ret));
continue;
}
if (des.idVendor != devc->profile->vid
|| des.idProduct != devc->profile->pid)
|| des.idProduct != devc->profile->pid){
continue;
}
if (sdi->status == SR_ST_INITIALIZING) {
sr_info("%s", "The device instance is still boosting.");
if (skip != sdi->index) {
/* Skip devices of this type that aren't the one we want. */
sr_info("%s", "Skip devices of this type that aren't the one we want.");
skip += 1;
continue;
}
} else if (sdi->status == SR_ST_INACTIVE) {
}
else if (sdi->status == SR_ST_INACTIVE) {
/*
* This device is fully enumerated, so we need to find
* this device by vendor, product, bus and address.
*/
if (libusb_get_bus_number(devlist[i]) != usb->bus
|| libusb_get_device_address(devlist[i]) != usb->address)
sr_info("%s", "The device instance is live, but not use.");
if (libusb_get_bus_number(dev_handel) != usb->bus
|| libusb_get_device_address(dev_handel) != usb->address){
/* This is not the one. */
continue;
sr_info("%s", "Font a device, but is not the one.");
continue;
}
}
if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
sr_info("Open device instance, handle: %p", dev_handel);
if (!(ret = libusb_open(dev_handel, &usb->devhdl))) {
if (usb->address == 0xff)
/*
* First time we touch this device after FW
* upload, so we don't know the address yet.
*/
usb->address = libusb_get_device_address(devlist[i]);
} else {
usb->address = libusb_get_device_address(dev_handel);
}
else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
break;
@@ -1831,15 +1850,17 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo
devc = sdi->priv;
usb = sdi->conn;
/*
* If the firmware was recently uploaded, no dev_open operation should be called.
* Just wait for renumerate -> detach -> attach
*/
ret = SR_ERR;
if (devc->fw_updated > 0) {
sr_info("%s: Firmware upload have done.");
return SR_ERR;
} else {
}
else {
sr_info("%s: Firmware upload was not needed.", __func__);
ret = hw_dev_open(di, sdi);
}
@@ -1848,6 +1869,8 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo
sr_err("%s: Unable to open device.", __func__);
return SR_ERR;
}
assert(usb->devhdl);
ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
if (ret != 0) {
@@ -1886,6 +1909,7 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo
rd_cmd.header.size = 1;
hw_info = 0;
rd_cmd.data = &hw_info;
if ((ret = command_ctl_rd(usb->devhdl, rd_cmd)) != SR_OK) {
sr_err("Failed to get hardware infos.");
return SR_ERR;
@@ -1938,11 +1962,14 @@ SR_PRIV int dsl_dev_close(struct sr_dev_inst *sdi)
struct sr_usb_dev_inst *usb;
usb = sdi->conn;
if (usb->devhdl == NULL)
if (usb->devhdl == NULL){
sr_info("%s", "dsl_dev_close(),Device handle is null.");
return SR_ERR;
}
sr_info("%s: Closing device %d on %d.%d interface %d.",
sdi->driver->name, sdi->index, usb->bus, usb->address, USB_INTERFACE);
libusb_release_interface(usb->devhdl, USB_INTERFACE);
libusb_close(usb->devhdl);
usb->devhdl = NULL;
@@ -2480,3 +2507,25 @@ SR_PRIV int dsl_start_transfers(const struct sr_dev_inst *sdi)
return SR_OK;
}
SR_PRIV int dsl_destroy_device(const struct sr_dev_inst *sdi)
{
assert(sdi);
struct sr_dev_driver *driver;
driver = sdi->driver;
if (driver->dev_close){
driver->dev_close(sdi);
}
if (sdi->conn) {
if (sdi->inst_type == SR_INST_USB)
sr_usb_dev_inst_free(sdi->conn);
else if (sdi->inst_type == SR_INST_SERIAL)
sr_serial_dev_inst_free(sdi->conn);
}
sr_dev_inst_free(sdi);
}

View File

@@ -175,7 +175,7 @@ struct DSL_profile {
enum libusb_speed usb_speed;
const char *vendor;
const char *model;
const char *model; //product name
const char *model_version;
const char *firmware;
@@ -1346,4 +1346,6 @@ SR_PRIV unsigned int dsl_get_timeout(const struct sr_dev_inst *sdi);
SR_PRIV int dsl_start_transfers(const struct sr_dev_inst *sdi);
SR_PRIV int dsl_header_size(const struct DSL_context *devc);
SR_PRIV int dsl_destroy_device(const struct sr_dev_inst *sdi);
#endif

View File

@@ -268,11 +268,17 @@ static GSList *scan(GSList *options)
GSList *l, *devices, *conn_devices;
struct libusb_device_descriptor des;
libusb_device **devlist;
libusb_device *device_handle = NULL;
int devcnt, ret, i, j;
const char *conn;
enum libusb_speed usb_speed;
drvc = di->priv;
if (options != NULL)
sr_info("%s", "Scan DSLogic device with options.");
else
sr_info("%s", "Scan DSLogic device.");
conn = NULL;
for (l = options; l; l = l->next) {
@@ -283,21 +289,27 @@ static GSList *scan(GSList *options)
break;
}
}
if (conn)
if (conn){
sr_info("%s", "Find usb device with connect config.");
conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
}
else
conn_devices = NULL;
/* Find all DSLogic compatible devices and upload firmware to them. */
devices = NULL;
libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
for (i = 0; devlist[i]; i++) {
for (i = 0; devlist[i]; i++)
{
device_handle = devlist[i];
if (conn) {
usb = NULL;
for (l = conn_devices; l; l = l->next) {
usb = l->data;
if (usb->bus == libusb_get_bus_number(devlist[i])
&& usb->address == libusb_get_device_address(devlist[i]))
if (usb->bus == libusb_get_bus_number(device_handle)
&& usb->address == libusb_get_device_address(device_handle))
break;
}
if (!l)
@@ -306,60 +318,66 @@ static GSList *scan(GSList *options)
continue;
}
if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
if ((ret = libusb_get_device_descriptor(device_handle, &des)) != 0) {
sr_warn("Failed to get device descriptor: %s.",
libusb_error_name(ret));
continue;
}
usb_speed = libusb_get_device_speed( devlist[i]);
if ((usb_speed != LIBUSB_SPEED_HIGH) &&
(usb_speed != LIBUSB_SPEED_SUPER))
usb_speed = libusb_get_device_speed(device_handle);
if ((usb_speed != LIBUSB_SPEED_HIGH) && (usb_speed != LIBUSB_SPEED_SUPER)){
continue;
}
/* Check manufactory id and product id, and speed type. */
prof = NULL;
for (j = 0; supported_DSLogic[j].vid; j++) {
if (des.idVendor == supported_DSLogic[j].vid &&
des.idProduct == supported_DSLogic[j].pid &&
usb_speed == supported_DSLogic[j].usb_speed) {
prof = &supported_DSLogic[j];
break;
}
}
/* Skip if the device was not found. */
if (!prof)
continue;
sr_info("Got a device handle: %p", device_handle);
devcnt = g_slist_length(drvc->instances);
devc = DSLogic_dev_new(prof);
if (!devc)
return NULL;
sdi = sr_dev_inst_new(channel_modes[devc->ch_mode].mode, devcnt, SR_ST_INITIALIZING,
prof->vendor, prof->model, prof->model_version);
prof->vendor, prof->model, prof->model_version);
if (!sdi) {
g_free(devc);
return NULL;
}
sdi->priv = devc;
sdi->driver = di;
sdi->dev_type = DEV_TYPE_HARDWARE;
drvc->instances = g_slist_append(drvc->instances, sdi);
//devices = g_slist_append(devices, sdi);
/* Fill in probelist according to this device's profile. */
if (dsl_setup_probes(sdi, channel_modes[devc->ch_mode].num) != SR_OK)
return NULL;
if (dsl_check_conf_profile(devlist[i])) {
if (dsl_check_conf_profile(device_handle)) {
/* Already has the firmware, so fix the new address. */
sr_info("Found an DSLogic device.");
sr_info("Found a DSLogic device,name: \"%s\"", prof->model);
sdi->status = SR_ST_INACTIVE;
sdi->inst_type = SR_INST_USB;
sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
libusb_get_device_address(devlist[i]), NULL);
sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(device_handle),
libusb_get_device_address(device_handle), NULL);
/* only report device after firmware is ready */
devices = g_slist_append(devices, sdi);
} else {
}
else {
char *firmware;
char *res_path = sr_get_firmware_res_path();
if (!(firmware = g_try_malloc(strlen(res_path)+strlen(prof->firmware)+1))) {
@@ -368,7 +386,7 @@ static GSList *scan(GSList *options)
}
strcpy(firmware, res_path);
strcat(firmware, prof->firmware);
if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
if (ezusb_upload_firmware(device_handle, USB_CONFIGURATION,
firmware) == SR_OK)
/* Store when this device's FW was updated. */
devc->fw_updated = g_get_monotonic_time();
@@ -377,10 +395,11 @@ static GSList *scan(GSList *options)
"device %d.", devcnt);
g_free(firmware);
sdi->inst_type = SR_INST_USB;
sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(device_handle),
0xff, NULL);
}
}
libusb_free_device_list(devlist, 1);
g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
@@ -1175,6 +1194,11 @@ static int dev_close(struct sr_dev_inst *sdi)
return ret;
}
static int dev_destroy(struct sr_dev_inst *sdi)
{
dsl_destroy_device(sdi);
}
static int cleanup(void)
{
int ret;
@@ -1416,6 +1440,7 @@ SR_PRIV struct sr_dev_driver DSLogic_driver_info = {
.config_list = config_list,
.dev_open = dev_open,
.dev_close = dev_close,
.dev_destroy = dev_destroy,
.dev_status_get = dev_status_get,
.dev_acquisition_start = dev_acquisition_start,
.dev_acquisition_stop = dev_acquisition_stop,