From: Uwe Hermann Date: Mon, 28 Jan 2013 18:36:16 +0000 (+0100) Subject: sr_driver_init(): Improve checks. X-Git-Tag: dsupstream~293 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c0eea11c4535e071c72b357fa7e2d1288104c134;p=libsigrok.git sr_driver_init(): Improve checks. Check the relevant arguments for != NULL before calling the actual driver-specific function, so that the driver can safely assume those arguments are non-NULL. This removes the need to duplicate these checks in every driver. Also, change one SR_ERR to the more correct SR_ERR_MALLOC, and assign sr_ctx in the rigol-ds1xx2's hw_init() function, like all the other drivers do. --- diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index 667df3a3..ec50a2b2 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -426,6 +426,7 @@ static int hw_init(struct sr_context *sr_ctx) sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index 048fdd91..fd6112d8 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -78,7 +78,7 @@ static int hw_init(struct sr_context *sr_ctx) if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { sr_err("Driver context malloc failed."); - return SR_ERR; + return SR_ERR_MALLOC; } drvc->sr_ctx = sr_ctx; diff --git a/hardware/demo/demo.c b/hardware/demo/demo.c index df6a2543..b20239a1 100644 --- a/hardware/demo/demo.c +++ b/hardware/demo/demo.c @@ -154,6 +154,7 @@ static int hw_init(struct sr_context *sr_ctx) sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; diff --git a/hardware/openbench-logic-sniffer/api.c b/hardware/openbench-logic-sniffer/api.c index 52b9d975..c7e1e687 100644 --- a/hardware/openbench-logic-sniffer/api.c +++ b/hardware/openbench-logic-sniffer/api.c @@ -63,6 +63,7 @@ static int hw_init(struct sr_context *sr_ctx) sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 5d2e36b6..7cb57912 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -153,13 +153,13 @@ static int clear_instances(void) static int hw_init(struct sr_context *sr_ctx) { struct drv_context *drvc; - (void)sr_ctx; if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; return SR_OK; diff --git a/hardware/victor-dmm/api.c b/hardware/victor-dmm/api.c index 53178b2a..d6bc5a34 100644 --- a/hardware/victor-dmm/api.c +++ b/hardware/victor-dmm/api.c @@ -80,6 +80,7 @@ static int hw_init(struct sr_context *sr_ctx) sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 7a72e7c4..f8cf81f8 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -292,9 +292,10 @@ static int hw_init(struct sr_context *sr_ctx) struct drv_context *drvc; if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { - sr_err("zeroplus: driver context malloc failed."); + sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } + drvc->sr_ctx = sr_ctx; di->priv = drvc; diff --git a/hwdriver.c b/hwdriver.c index def770e9..364d97bd 100644 --- a/hwdriver.c +++ b/hwdriver.c @@ -245,19 +245,38 @@ SR_API struct sr_dev_driver **sr_driver_list(void) /** * Initialize a hardware driver. * + * This usually involves memory allocations and variable initializations + * within the driver, but _not_ scanning for attached devices. + * The API call sr_driver_scan() is used for that. + * * @param ctx A libsigrok context object allocated by a previous call to - * sr_init(). - * @param driver The driver to initialize. + * sr_init(). Must not be NULL. + * @param driver The driver to initialize. This must be a pointer to one of + * the entries returned by sr_driver_list(). Must not be NULL. * - * @return SR_OK if all went well, or an error code otherwise. + * @return SR_OK upon success, SR_ERR_ARG upon invalid parameters, + * SR_ERR_BUG upon internal errors, or another negative error code + * upon other errors. */ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) { + int ret; - if (driver->init) - return driver->init(ctx); + if (!ctx) { + sr_err("Invalid libsigrok context, can't initialize."); + return SR_ERR_ARG; + } - return SR_OK; + if (!driver) { + sr_err("Invalid driver, can't initialize."); + return SR_ERR_ARG; + } + + sr_spew("Initializing driver '%s'.", driver->name); + if ((ret = driver->init(ctx)) < 0) + sr_err("Failed to initialize the driver: %d.", ret); + + return ret; } /**