From: Lars-Peter Clausen Date: Mon, 2 May 2016 11:24:04 +0000 (+0200) Subject: Remove unnecessary driver context checks X-Git-Tag: libsigrok-0.5.0~462 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=efa9840222f2925c187715377e3646679fd8496c Remove unnecessary driver context checks Some drivers check in some of their driver callbacks if the driver has been initialized and return an error if it has not. For the scan() callback the sigrok core checks if the driver has been initialized and if not returns an error. So it is not possible that the scan() callback gets called if the driver is not initialized. Without the scan() callback succeeding it is not possible to get a reference to a device which is associated with the driver, so it is not possible that any of the device specific callbacks is called without the driver first being initialized either. In conclusion these checks are not necessary since they never evaluate to true and can be dropped. If they should ever become necessary they should be done in the sigrok core so all drivers and all callbacks are equally handled. Signed-off-by: Lars-Peter Clausen --- diff --git a/src/hardware/ikalogic-scanalogic2/api.c b/src/hardware/ikalogic-scanalogic2/api.c index dd2e4211..1ce3b91c 100644 --- a/src/hardware/ikalogic-scanalogic2/api.c +++ b/src/hardware/ikalogic-scanalogic2/api.c @@ -183,17 +183,12 @@ static int dev_clear(const struct sr_dev_driver *di) static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; - struct drv_context *drvc; + struct drv_context *drvc = di->context; struct dev_context *devc; struct sr_usb_dev_inst *usb; uint8_t buffer[PACKET_LENGTH]; int ret; - if (!(drvc = di->context)) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; devc = sdi->priv; @@ -254,14 +249,8 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) diff --git a/src/hardware/kecheng-kc-330b/api.c b/src/hardware/kecheng-kc-330b/api.c index 15c43b1b..86ac30b1 100644 --- a/src/hardware/kecheng-kc-330b/api.c +++ b/src/hardware/kecheng-kc-330b/api.c @@ -163,15 +163,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; - struct drv_context *drvc; + struct drv_context *drvc = di->context; struct sr_usb_dev_inst *usb; int ret; - if (!(drvc = di->context)) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) @@ -193,15 +188,9 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) @@ -274,7 +263,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; uint64_t p, q; unsigned int i; @@ -286,11 +274,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; ret = SR_OK; switch (key) { diff --git a/src/hardware/lascar-el-usb/api.c b/src/hardware/lascar-el-usb/api.c index fb9d1930..3b78b198 100644 --- a/src/hardware/lascar-el-usb/api.c +++ b/src/hardware/lascar-el-usb/api.c @@ -92,15 +92,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; - struct drv_context *drvc; + struct drv_context *drvc = di->context;; struct sr_usb_dev_inst *usb; int ret; - if (!(drvc = di->context)) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) @@ -117,14 +112,8 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) @@ -178,7 +167,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; int ret; @@ -187,11 +175,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; ret = SR_OK; switch (key) { @@ -321,11 +304,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - drvc = di->context; devc = sdi->priv; usb = sdi->conn; @@ -427,13 +405,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) SR_PRIV int dev_acquisition_stop(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't stop acquisition."); return SR_ERR; diff --git a/src/hardware/lecroy-logicstudio/api.c b/src/hardware/lecroy-logicstudio/api.c index 9cf7018e..b6c51160 100644 --- a/src/hardware/lecroy-logicstudio/api.c +++ b/src/hardware/lecroy-logicstudio/api.c @@ -263,19 +263,12 @@ static int open_device(struct sr_dev_inst *sdi) static int dev_open(struct sr_dev_inst *sdi) { - struct drv_context *drvc; struct dev_context *devc; int64_t timediff_us, timediff_ms; int ret; - drvc = sdi->driver->context; devc = sdi->priv; - if (!drvc) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - /* * If we didn't need to upload FX2 firmware in scan(), open the device * right away. Otherwise, wait up to MAX_RENUM_DELAY_MS ms for the diff --git a/src/hardware/link-mso19/api.c b/src/hardware/link-mso19/api.c index faee455c..c35fc718 100644 --- a/src/hardware/link-mso19/api.c +++ b/src/hardware/link-mso19/api.c @@ -55,15 +55,12 @@ SR_PRIV struct sr_dev_driver link_mso19_driver_info; /* TODO: Use sr_dev_inst to store connection handle & use std_dev_clear(). */ static int dev_clear(const struct sr_dev_driver *di) { + struct drv_context *drvc = di->context; GSList *l; struct sr_dev_inst *sdi; - struct drv_context *drvc; struct dev_context *devc; int ret = SR_OK; - if (!(drvc = di->context)) - return SR_OK; - /* Properly close and free all devices. */ for (l = drvc->instances; l; l = l->next) { if (!(sdi = l->data)) { diff --git a/src/hardware/sysclk-lwla/api.c b/src/hardware/sysclk-lwla/api.c index 59d12633..0a614f9d 100644 --- a/src/hardware/sysclk-lwla/api.c +++ b/src/hardware/sysclk-lwla/api.c @@ -282,10 +282,6 @@ static int dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (!drvc) { - sr_err("Driver was not initialized."); - return SR_ERR; - } if (sdi->status != SR_ST_INACTIVE) { sr_err("Device already open."); return SR_ERR; @@ -349,19 +345,13 @@ static int dev_open(struct sr_dev_inst *sdi) */ static int dev_close(struct sr_dev_inst *sdi) { - struct drv_context *drvc; struct dev_context *devc; struct sr_usb_dev_inst *usb; int ret; - drvc = sdi->driver->context; devc = sdi->priv; usb = sdi->conn; - if (!drvc) { - sr_err("Driver was not initialized."); - return SR_ERR; - } if (sdi->status == SR_ST_INACTIVE) { sr_dbg("Device already closed."); return SR_OK; diff --git a/src/hardware/testo/api.c b/src/hardware/testo/api.c index d0e60237..5d5ccb90 100644 --- a/src/hardware/testo/api.c +++ b/src/hardware/testo/api.c @@ -155,11 +155,6 @@ static int dev_open(struct sr_dev_inst *sdi) int ret, i; char connection_id[64]; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { @@ -199,14 +194,8 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) /* Nothing to do. */ @@ -256,11 +245,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; ret = SR_OK; switch (key) { @@ -434,10 +418,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } devc = sdi->priv; usb = sdi->conn; @@ -474,13 +454,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; diff --git a/src/hardware/uni-t-ut32x/api.c b/src/hardware/uni-t-ut32x/api.c index f19dabef..7ed3f88c 100644 --- a/src/hardware/uni-t-ut32x/api.c +++ b/src/hardware/uni-t-ut32x/api.c @@ -101,15 +101,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; - struct drv_context *drvc; + struct drv_context *drvc = di->context; struct sr_usb_dev_inst *usb; int ret; - if (!(drvc = di->context)) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) @@ -145,14 +140,8 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) /* Nothing to do. */ @@ -194,7 +183,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; const char *tmp_str; @@ -203,11 +191,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; switch (key) { diff --git a/src/hardware/victor-dmm/api.c b/src/hardware/victor-dmm/api.c index 9323e700..8f20f599 100644 --- a/src/hardware/victor-dmm/api.c +++ b/src/hardware/victor-dmm/api.c @@ -110,11 +110,6 @@ static int dev_open(struct sr_dev_inst *sdi) int ret, i; char connection_id[64]; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); @@ -156,14 +151,8 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; if (!usb->devhdl) @@ -204,7 +193,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; gint64 now; @@ -213,11 +201,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; switch (key) { @@ -351,11 +334,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; std_session_send_df_header(sdi, LOG_PREFIX); @@ -384,13 +362,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - if (sdi->status != SR_ST_ACTIVE) { sr_err("Device not active, can't stop acquisition."); return SR_ERR;