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.
This commit is contained in:
Uwe Hermann 2013-01-28 19:36:16 +01:00
parent bd36d826d4
commit c0eea11c45
8 changed files with 34 additions and 10 deletions

View File

@ -426,6 +426,7 @@ static int hw_init(struct sr_context *sr_ctx)
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;

View File

@ -78,7 +78,7 @@ static int hw_init(struct sr_context *sr_ctx)
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;

View File

@ -154,6 +154,7 @@ static int hw_init(struct sr_context *sr_ctx)
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;

View File

@ -63,6 +63,7 @@ static int hw_init(struct sr_context *sr_ctx)
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;

View File

@ -153,13 +153,13 @@ static int clear_instances(void)
static int hw_init(struct sr_context *sr_ctx) static int hw_init(struct sr_context *sr_ctx)
{ {
struct drv_context *drvc; struct drv_context *drvc;
(void)sr_ctx;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;
return SR_OK; return SR_OK;

View File

@ -80,6 +80,7 @@ static int hw_init(struct sr_context *sr_ctx)
sr_err("Driver context malloc failed."); sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;

View File

@ -292,9 +292,10 @@ static int hw_init(struct sr_context *sr_ctx)
struct drv_context *drvc; struct drv_context *drvc;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { 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; return SR_ERR_MALLOC;
} }
drvc->sr_ctx = sr_ctx; drvc->sr_ctx = sr_ctx;
di->priv = drvc; di->priv = drvc;

View File

@ -245,19 +245,38 @@ SR_API struct sr_dev_driver **sr_driver_list(void)
/** /**
* Initialize a hardware driver. * Initialize a hardware driver.
* *
* @param ctx A libsigrok context object allocated by a previous call to * This usually involves memory allocations and variable initializations
* sr_init(). * within the driver, but _not_ scanning for attached devices.
* @param driver The driver to initialize. * The API call sr_driver_scan() is used for that.
* *
* @return SR_OK if all went well, or an error code otherwise. * @param ctx A libsigrok context object allocated by a previous call to
* 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 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) SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
{ {
int ret;
if (driver->init) if (!ctx) {
return driver->init(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;
} }
/** /**