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.
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("Driver context malloc failed.");
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;
}
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
static int hw_init(struct sr_context *sr_ctx)
{
struct drv_context *drvc;
static int hw_init(struct sr_context *sr_ctx)
{
struct drv_context *drvc;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
di->priv = drvc;
return SR_OK;
di->priv = drvc;
return SR_OK;
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
sr_err("Driver context malloc failed.");
return SR_ERR_MALLOC;
}
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
struct drv_context *drvc;
if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
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.");
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
drvc->sr_ctx = sr_ctx;
di->priv = drvc;
/**
* Initialize a hardware driver.
*
/**
* 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
* @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)
{
*/
SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
{
- if (driver->init)
- return driver->init(ctx);
+ if (!ctx) {
+ sr_err("Invalid libsigrok context, can't initialize.");
+ return SR_ERR_ARG;
+ }
+ 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;