From: Uwe Hermann Date: Tue, 29 Jan 2013 11:55:00 +0000 (+0100) Subject: Factor out common hw_init() driver code. X-Git-Tag: dsupstream~291 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=063e7aef6d41d4c44591ff93672079998bf9622f Factor out common hw_init() driver code. Most drivers do pretty much the same things in their hw_init() right now, so factor out that code to std_hw_init() in std.c. --- diff --git a/Makefile.am b/Makefile.am index 503233b7..4e2da389 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,7 +37,8 @@ libsigrok_la_SOURCES = \ strutil.c \ log.c \ version.c \ - error.c + error.c \ + std.c libsigrok_la_LIBADD = \ $(LIBOBJS) \ diff --git a/hardware/agilent-dmm/api.c b/hardware/agilent-dmm/api.c index 8c792a35..26213938 100644 --- a/hardware/agilent-dmm/api.c +++ b/hardware/agilent-dmm/api.c @@ -91,17 +91,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/alsa/api.c b/hardware/alsa/api.c index 67773f60..d9dd83ef 100644 --- a/hardware/alsa/api.c +++ b/hardware/alsa/api.c @@ -52,17 +52,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index d40c911b..59a1ec42 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -420,17 +420,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/chronovu-la8/api.c b/hardware/chronovu-la8/api.c index 2a97440f..b1f88919 100644 --- a/hardware/chronovu-la8/api.c +++ b/hardware/chronovu-la8/api.c @@ -70,17 +70,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index 39e557a6..5532b1a1 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -74,17 +74,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/demo/demo.c b/hardware/demo/demo.c index 923d20a9..60a511f2 100644 --- a/hardware/demo/demo.c +++ b/hardware/demo/demo.c @@ -148,17 +148,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/fluke-dmm/api.c b/hardware/fluke-dmm/api.c index 331b4afd..0d73fb63 100644 --- a/hardware/fluke-dmm/api.c +++ b/hardware/fluke-dmm/api.c @@ -89,17 +89,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *fluke_scan(const char *conn, const char *serialcomm) diff --git a/hardware/fx2lafw/fx2lafw.c b/hardware/fx2lafw/fx2lafw.c index b0f94c59..a0955371 100644 --- a/hardware/fx2lafw/fx2lafw.c +++ b/hardware/fx2lafw/fx2lafw.c @@ -384,17 +384,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/hantek-dso/api.c b/hardware/hantek-dso/api.c index acb6d815..97742ffe 100644 --- a/hardware/hantek-dso/api.c +++ b/hardware/hantek-dso/api.c @@ -259,17 +259,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/lascar-el-usb/api.c b/hardware/lascar-el-usb/api.c index 1804f2d5..1ab2a68f 100644 --- a/hardware/lascar-el-usb/api.c +++ b/hardware/lascar-el-usb/api.c @@ -70,17 +70,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/link-mso19/api.c b/hardware/link-mso19/api.c index 11ae8888..9fcb9cf8 100644 --- a/hardware/link-mso19/api.c +++ b/hardware/link-mso19/api.c @@ -54,17 +54,7 @@ static struct sr_dev_driver *di = &link_mso19_driver_info; 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/nexus-osciprime/api.c b/hardware/nexus-osciprime/api.c index 1b8d8083..bb3936b8 100644 --- a/hardware/nexus-osciprime/api.c +++ b/hardware/nexus-osciprime/api.c @@ -139,17 +139,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/openbench-logic-sniffer/api.c b/hardware/openbench-logic-sniffer/api.c index 6da0eab2..f69ca511 100644 --- a/hardware/openbench-logic-sniffer/api.c +++ b/hardware/openbench-logic-sniffer/api.c @@ -57,17 +57,7 @@ static struct sr_dev_driver *di = &ols_driver_info; 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 894f5afc..8107fb8a 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -152,17 +152,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/serial-dmm/api.c b/hardware/serial-dmm/api.c index b3b0a8ef..987fafbf 100644 --- a/hardware/serial-dmm/api.c +++ b/hardware/serial-dmm/api.c @@ -192,19 +192,9 @@ static int clear_instances(int dmm) static int hw_init(struct sr_context *sr_ctx, int dmm) { - struct drv_context *drvc; - - if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { - sr_err("Driver context malloc failed."); - return SR_ERR_MALLOC; - } - sr_dbg("Selected '%s' subdriver.", dmms[dmm].di->name); - drvc->sr_ctx = sr_ctx; - dmms[dmm].di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, dmms[dmm].di, DRIVER_LOG_DOMAIN); } static GSList *scan(const char *conn, const char *serialcomm, int dmm) diff --git a/hardware/tondaj-sl-814/api.c b/hardware/tondaj-sl-814/api.c index c621eeb8..da88bd07 100644 --- a/hardware/tondaj-sl-814/api.c +++ b/hardware/tondaj-sl-814/api.c @@ -70,17 +70,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/uni-t-dmm/api.c b/hardware/uni-t-dmm/api.c index 26b3e16e..fcc8c0f6 100644 --- a/hardware/uni-t-dmm/api.c +++ b/hardware/uni-t-dmm/api.c @@ -57,23 +57,13 @@ static int clear_instances(void) static int hw_init(struct sr_context *sr_ctx, int dmm) { - struct drv_context *drvc; - - if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { - sr_err("Driver context malloc failed."); - return SR_ERR_MALLOC; - } - if (dmm == UNI_T_UT61D) di = di_ut61d; else if (dmm == VOLTCRAFT_VC820) di = di_vc820; sr_dbg("Selected '%s' subdriver.", di->name); - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static int hw_init_ut61d(struct sr_context *sr_ctx) diff --git a/hardware/victor-dmm/api.c b/hardware/victor-dmm/api.c index 0edd79e4..75c893d1 100644 --- a/hardware/victor-dmm/api.c +++ b/hardware/victor-dmm/api.c @@ -74,17 +74,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); } static GSList *hw_scan(GSList *options) diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 8ccd40f1..9d9e227d 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -289,17 +289,7 @@ static int clear_instances(void) 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; - } - - drvc->sr_ctx = sr_ctx; - di->priv = drvc; - - return SR_OK; + return std_hw_init(sr_ctx, di, "zeroplus: "); } static GSList *hw_scan(GSList *options) diff --git a/libsigrok-internal.h b/libsigrok-internal.h index 59709e63..41144cbd 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -123,6 +123,11 @@ SR_PRIV int sr_source_add(int fd, int events, int timeout, SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet); +/*--- std.c -----------------------------------------------------------------*/ + +SR_PRIV int std_hw_init(struct sr_context *sr_ctx, struct sr_dev_driver *di, + const char *prefix); + /*--- hardware/common/serial.c ----------------------------------------------*/ enum { diff --git a/std.c b/std.c new file mode 100644 index 00000000..a9cbec0e --- /dev/null +++ b/std.c @@ -0,0 +1,59 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2013 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "libsigrok.h" +#include "libsigrok-internal.h" + +/** + * Standard sr_driver_init() API helper. + * + * This function can be used to simplify most driver's hw_init() API callback. + * + * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and + * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed. + * + * @param sr_ctx The libsigrok context to assign. + * @param di The driver instance to use. + * @param prefix A driver-specific prefix string used for log messages. + * + * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or + * SR_ERR_MALLOC upon memory allocation errors. + */ +SR_PRIV int std_hw_init(struct sr_context *sr_ctx, struct sr_dev_driver *di, + const char *prefix) +{ + struct drv_context *drvc; + + if (!di) { + sr_err("%sInvalid driver, cannot initialize.", prefix); + return SR_ERR_ARG; + } + + if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { + sr_err("%sDriver context malloc failed.", prefix); + return SR_ERR_MALLOC; + } + + drvc->sr_ctx = sr_ctx; + di->priv = drvc; + + return SR_OK; +}