From patchwork Sun Jan 25 11:34:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Sakamoto X-Patchwork-Id: 5701071 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 7E4039F1D6 for ; Sun, 25 Jan 2015 11:36:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2F48E201FB for ; Sun, 25 Jan 2015 11:36:42 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 9332A2017E for ; Sun, 25 Jan 2015 11:36:40 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id C136F2605A3; Sun, 25 Jan 2015 12:36:39 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NO_DNS_FOR_FROM, UNPARSEABLE_RELAY autolearn=no version=3.3.1 Received: from alsa0.perex.cz (localhost [IPv6:::1]) by alsa0.perex.cz (Postfix) with ESMTP id A5C7B26047A; Sun, 25 Jan 2015 12:35:16 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 1D5E526047C; Sun, 25 Jan 2015 12:35:15 +0100 (CET) Received: from smtp302.phy.lolipop.jp (smtp302.phy.lolipop.jp [210.157.22.85]) by alsa0.perex.cz (Postfix) with ESMTP id BA4AF260435 for ; Sun, 25 Jan 2015 12:35:01 +0100 (CET) Received: from smtp302.phy.lolipop.lan (HELO smtp302.phy.lolipop.jp) (172.17.1.85) (smtp-auth username m12129643-o-takashi, mechanism plain) by smtp302.phy.lolipop.jp (qpsmtpd/0.82) with ESMTPA; Sun, 25 Jan 2015 20:34:59 +0900 Received: from 127.0.0.1 (127.0.0.1) by smtp302.phy.lolipop.jp (LOLIPOP-Fsecure); Sun, 25 Jan 2015 20:34:35 +0900 (JST) X-Virus-Status: clean(LOLIPOP-Fsecure) From: Takashi Sakamoto To: clemens@ladisch.de, tiwai@suse.de, perex@perex.cz Date: Sun, 25 Jan 2015 20:34:25 +0900 Message-Id: <1422185674-16431-5-git-send-email-o-takashi@sakamocchi.jp> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1422185674-16431-1-git-send-email-o-takashi@sakamocchi.jp> References: <1422185674-16431-1-git-send-email-o-takashi@sakamocchi.jp> Cc: alsa-devel@alsa-project.org, linux1394-devel@lists.sourceforge.net, ffado-devel@lists.sf.net Subject: [alsa-devel] [PATCH 04/13] libhinawa: add 'fw_unit' object as a listener for FireWire unit X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP Linux FireWire subsystem gives a way to listen to any units on IEEE 1394 bus. This subsystem adds FireWire character devices and applications can get any events by reading the devices. This commit adds HINAWA_TYPE_FW_UNIT object to perform this. This object is written with GObject way. When constructing this object, applications gets file descriptor by open() with a path of FireWire character devices. When calling listen(), the object starts listening. Then the 'generation' property of this object is valid and 'bus-update' signal is generated at IEEE 1394 bus-reset event. When destructing the object, the file descriptor is released. I note that src/hinawa_sigs_marshal.* are newly generated by glib tools to detect the signature of GObject signal handler. These files should be regenerated when new signals are added or existed signals are changes, thus don't forget to 'make clean' when applying following patches. Signed-off-by: Takashi Sakamoto --- libhinawa/doc/reference/hinawa-docs.sgml | 1 + libhinawa/src/Makefile.am | 27 ++- libhinawa/src/fw_unit.c | 335 +++++++++++++++++++++++++++++++ libhinawa/src/fw_unit.h | 52 +++++ libhinawa/src/internal.h | 13 ++ 5 files changed, 425 insertions(+), 3 deletions(-) create mode 100644 libhinawa/src/fw_unit.c create mode 100644 libhinawa/src/fw_unit.h create mode 100644 libhinawa/src/internal.h diff --git a/libhinawa/doc/reference/hinawa-docs.sgml b/libhinawa/doc/reference/hinawa-docs.sgml index 43c5c4e..662d8c5 100644 --- a/libhinawa/doc/reference/hinawa-docs.sgml +++ b/libhinawa/doc/reference/hinawa-docs.sgml @@ -30,6 +30,7 @@ functionality in ALSA firewire stack, the others are abstractions of the transaction functionality in Linux firewire stack. + diff --git a/libhinawa/src/Makefile.am b/libhinawa/src/Makefile.am index 5673255..9eda8f4 100644 --- a/libhinawa/src/Makefile.am +++ b/libhinawa/src/Makefile.am @@ -1,5 +1,7 @@ # Remove auto-generated files when cleaning -CLEANFILES = +CLEANFILES = \ + hinawa_sigs_marshal.* + AM_CPPFLAGS = \ -I$(top_builddir) \ @@ -19,6 +21,25 @@ libhinawa_la_LIBADD = \ $(GLIB_LIBS) libhinawa_la_SOURCES = \ - hinawa_context.c + hinawa_context.c \ + hinawa_sigs_marshal.h \ + hinawa_sigs_marshal.c \ + fw_unit.h \ + fw_unit.c + +pkginclude_HEADERS = \ + hinawa_sigs_marshal.h \ + fw_unit.h + +hinawa_sigs_marshal.list: + $(AM_V_GEN)( find | grep \.c$$ | xargs cat | \ + sed -n -e 's/.*hinawa_sigs_marshal_\([A-Z]*__[A-Z_]*\).*/\1/p' |\ + sed -e 's/__/:/' -e 'y/_/,/' ) | sort -u > $@ + +hinawa_sigs_marshal.h: hinawa_sigs_marshal.list + $(AM_V_GEN)( glib-genmarshal --header \ + --prefix=hinawa_sigs_marshal $< ) > $@ -pkginclude_HEADERS = +hinawa_sigs_marshal.c: hinawa_sigs_marshal.list hinawa_sigs_marshal.h + $(AM_V_GEN)(echo '#include "hinawa_sigs_marshal.h"'; \ + glib-genmarshal --body --prefix=hinawa_sigs_marshal $< ) > $@ diff --git a/libhinawa/src/fw_unit.c b/libhinawa/src/fw_unit.c new file mode 100644 index 0000000..b948ef8 --- /dev/null +++ b/libhinawa/src/fw_unit.c @@ -0,0 +1,335 @@ +#include +#include +#include +#include +#include + +#include "hinawa_context.h" +#include "fw_unit.h" +#include "internal.h" + +#ifdef HAVE_CONFIG_H +# include +#endif + +/** + * SECTION:fw_unit + * @Title: HinawaFwUnit + * @Short_description: An event listener for FireWire unit + * + * A #HinawaFwUnit is an event listener for a certain FireWire unit. + * This class is an application of Linux FireWire subsystem. + * All of operations utilize ioctl(2) with subsystem specific request commands. + */ +typedef struct { + GSource src; + HinawaFwUnit *unit; + gpointer tag; +} FwUnitSource; + +struct _HinawaFwUnitPrivate { + int fd; + guint64 generation; + + unsigned int len; + void *buf; + FwUnitSource *src; +}; +G_DEFINE_TYPE_WITH_PRIVATE(HinawaFwUnit, hinawa_fw_unit, G_TYPE_OBJECT) +#define FW_UNIT_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), \ + HINAWA_TYPE_FW_UNIT, HinawaFwUnitPrivate)) + +/* This object has properties. */ +enum fw_unit_prop_type { + FW_UNIT_PROP_TYPE_GENERATION = 1, + FW_UNIT_PROP_TYPE_COUNT, +}; +static GParamSpec *fw_unit_props[FW_UNIT_PROP_TYPE_COUNT] = { NULL, }; + +/* This object has one signal. */ +enum fw_unit_sig_type { + FW_UNIT_SIG_TYPE_BUS_UPDATE = 0, + FW_UNIT_SIG_TYPE_COUNT, +}; +static guint fw_unit_sigs[FW_UNIT_SIG_TYPE_COUNT] = { 0 }; + +static void fw_unit_get_property(GObject *obj, guint id, + GValue *val, GParamSpec *spec) +{ + HinawaFwUnit *self = HINAWA_FW_UNIT(obj); + HinawaFwUnitPrivate *priv = FW_UNIT_GET_PRIVATE(self); + + switch (id) { + case FW_UNIT_PROP_TYPE_GENERATION: + g_value_set_uint64(val, priv->generation); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec); + break; + } +} + +static void fw_unit_set_property(GObject *obj, guint id, + const GValue *val, GParamSpec *spec) +{ + G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec); +} + +static void fw_unit_dispose(GObject *obj) +{ + HinawaFwUnit *self = HINAWA_FW_UNIT(obj); + HinawaFwUnitPrivate *priv = FW_UNIT_GET_PRIVATE(self); + + hinawa_fw_unit_unlisten(self); + + close(priv->fd); + + G_OBJECT_CLASS(hinawa_fw_unit_parent_class)->dispose(obj); +} + +static void fw_unit_finalize(GObject *gobject) +{ + G_OBJECT_CLASS(hinawa_fw_unit_parent_class)->finalize(gobject); +} + +static void hinawa_fw_unit_class_init(HinawaFwUnitClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->get_property = fw_unit_get_property; + gobject_class->set_property = fw_unit_set_property; + gobject_class->dispose = fw_unit_dispose; + gobject_class->finalize = fw_unit_finalize; + + fw_unit_props[FW_UNIT_PROP_TYPE_GENERATION] = + g_param_spec_uint64("generation", "generation", + "current level of generation on this bus.", + 0, ULONG_MAX, 0, + G_PARAM_READABLE); + + g_object_class_install_properties(gobject_class, + FW_UNIT_PROP_TYPE_COUNT, + fw_unit_props); + + /** + * HinawaFwUnit::bus-update: + * @self: A #HinawaFwUnit + * + * When IEEE 1394 bus is updated, the ::bus-update signal is generated. + * Handlers can read current generation in the bus via 'generation' + * property. + */ + fw_unit_sigs[FW_UNIT_SIG_TYPE_BUS_UPDATE] = + g_signal_new("bus-update", + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_LAST, + 0, + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0, G_TYPE_NONE); +} + +static void hinawa_fw_unit_init(HinawaFwUnit *self) +{ + self->priv = hinawa_fw_unit_get_instance_private(self); +} + +/** + * hinawa_fw_unit_open: + * @self: A #HinawaFwUnit + * @path: A path to Linux FireWire character device + * @exception: A #GError + */ +void hinawa_fw_unit_open(HinawaFwUnit *self, gchar *path, GError **exception) +{ + HinawaFwUnitPrivate *priv; + int fd; + struct fw_cdev_get_info info = {0}; + struct fw_cdev_event_bus_reset br = {0}; + + g_return_if_fail(HINAWA_IS_FW_UNIT(self)); + priv = FW_UNIT_GET_PRIVATE(self); + + fd = open(path, O_RDONLY); + if (fd < 0) { + g_set_error(exception, g_quark_from_static_string(__func__), + errno, "%s", strerror(errno)); + return; + } + + info.version = 4; + info.bus_reset = (guint64)&br; + info.bus_reset_closure = (guint64)self; + if (ioctl(fd, FW_CDEV_IOC_GET_INFO, &info) < 0) { + g_set_error(exception, g_quark_from_static_string(__func__), + errno, "%s", strerror(errno)); + close(fd); + return; + } + + priv->fd = fd; + priv->generation = br.generation; +} + +/* Internal use only. */ +void hinawa_fw_unit_ioctl(HinawaFwUnit *self, int req, void *args, int *err) +{ + HinawaFwUnitPrivate *priv; + + g_return_if_fail(HINAWA_IS_FW_UNIT(self)); + priv = FW_UNIT_GET_PRIVATE(self); + + *err = 0; + if (ioctl(priv->fd, req, args) < 0) + *err = errno; +} + +static void handle_update(HinawaFwUnit *self, + struct fw_cdev_event_bus_reset *event) +{ + HinawaFwUnitPrivate *priv; + + g_return_if_fail(HINAWA_IS_FW_UNIT(self)); + priv = FW_UNIT_GET_PRIVATE(self); + + priv->generation = event->generation; + + g_signal_emit(self, fw_unit_sigs[FW_UNIT_SIG_TYPE_BUS_UPDATE], 0, + NULL); +} + +static gboolean prepare_src(GSource *src, gint *timeout) +{ + /* Set 2msec for poll(2) timeout. */ + *timeout = 2; + + /* This source is not ready, let's poll(2) */ + return FALSE; +} + +static gboolean check_src(GSource *gsrc) +{ + FwUnitSource *src = (FwUnitSource *)gsrc; + HinawaFwUnit *unit = src->unit; + HinawaFwUnitPrivate *priv = FW_UNIT_GET_PRIVATE(unit); + struct fw_cdev_event_common *common; + int len; + GIOCondition condition; + + if (unit == NULL) + goto end; + + /* Let's process this source if any inputs are available. */ + condition = g_source_query_unix_fd((GSource *)src, src->tag); + if (!(condition & G_IO_IN)) + goto end; + + len = read(priv->fd, priv->buf, priv->len); + if (len <= 0) + goto end; + + common = (struct fw_cdev_event_common *)priv->buf; + + if (HINAWA_IS_FW_UNIT(common->closure) && + common->type == FW_CDEV_EVENT_BUS_RESET) + handle_update(HINAWA_FW_UNIT(common->closure), + (struct fw_cdev_event_bus_reset *)common); +end: + /* Don't go to dispatch, then continue to process this source. */ + return FALSE; +} + +static gboolean dispatch_src(GSource *src, GSourceFunc callback, + gpointer user_data) +{ + /* Just be sure to continue to process this source. */ + return TRUE; +} + +/** + * hinawa_fw_unit_listen: + * @self: A #HinawaFwUnit + * @exception: A #GError + * + * Start to listen to any events from the unit. + */ +void hinawa_fw_unit_listen(HinawaFwUnit *self, GError **exception) +{ + static GSourceFuncs funcs = { + .prepare = prepare_src, + .check = check_src, + .dispatch = dispatch_src, + .finalize = NULL, + }; + HinawaFwUnitPrivate *priv; + void *buf; + GSource *src; + + g_return_if_fail(HINAWA_IS_FW_UNIT(self)); + priv = FW_UNIT_GET_PRIVATE(self); + + /* + * MEMO: allocate one page because we cannot assume the size of + * transaction frame. + */ + buf = g_malloc0(getpagesize()); + if (buf == NULL) { + g_set_error(exception, g_quark_from_static_string(__func__), + ENOMEM, "%s", strerror(ENOMEM)); + return; + } + + src = g_source_new(&funcs, sizeof(FwUnitSource)); + if (src == NULL) { + g_set_error(exception, g_quark_from_static_string(__func__), + ENOMEM, "%s", strerror(ENOMEM)); + g_free(buf); + return; + } + + g_source_set_name(src, "HinawaFwUnit"); + g_source_set_priority(src, G_PRIORITY_HIGH_IDLE); + g_source_set_can_recurse(src, TRUE); + + ((FwUnitSource *)src)->unit = self; + priv->src = (FwUnitSource *)src; + priv->buf = buf; + priv->len = getpagesize(); + + ((FwUnitSource *)src)->tag = + hinawa_context_add_src(src, priv->fd, G_IO_IN, exception); + if (*exception != NULL) { + g_free(buf); + g_source_destroy(src); + priv->buf = NULL; + priv->len = 0; + priv->src = NULL; + return; + } +} + +/** + * hinawa_fw_unit_unlisten: + * @self: A #HinawaFwUnit + * + * Stop to listen to any events from the unit. + */ +void hinawa_fw_unit_unlisten(HinawaFwUnit *self) +{ + HinawaFwUnitPrivate *priv; + + g_return_if_fail(HINAWA_IS_FW_UNIT(self)); + priv = FW_UNIT_GET_PRIVATE(self); + + if (priv->src == NULL) + return; + + g_source_destroy((GSource *)priv->src); + g_free(priv->src); + priv->src = NULL; + + g_free(priv->buf); + priv->buf = NULL; + priv->len = 0; +} diff --git a/libhinawa/src/fw_unit.h b/libhinawa/src/fw_unit.h new file mode 100644 index 0000000..f8f38a4 --- /dev/null +++ b/libhinawa/src/fw_unit.h @@ -0,0 +1,52 @@ +#ifndef __ALSA_TOOLS_HINAWA_FW_UNIT_H__ +#define __ALSA_TOOLS_HINAWA_FW_UNIT_H__ + +#include +#include + +G_BEGIN_DECLS + +#define HINAWA_TYPE_FW_UNIT (hinawa_fw_unit_get_type()) + +#define HINAWA_FW_UNIT(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + HINAWA_TYPE_FW_UNIT, \ + HinawaFwUnit)) +#define HINAWA_IS_FW_UNIT(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + HINAWA_TYPE_FW_UNIT)) + +#define HINAWA_FW_UNIT_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + HINAWA_TYPE_FW_UNIT, \ + HinawaFwUnitClass)) +#define HINAWA_IS_FW_UNIT_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + HINAWA_TYPE_FW_UNIT)) +#define HINAWA_FW_UNIT_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + HINAWA_TYPE_FW_UNIT, \ + HinawaFwUnitClass)) + +typedef struct _HinawaFwUnit HinawaFwUnit; +typedef struct _HinawaFwUnitClass HinawaFwUnitClass; +typedef struct _HinawaFwUnitPrivate HinawaFwUnitPrivate; + +struct _HinawaFwUnit { + GObject parent_instance; + + HinawaFwUnitPrivate *priv; +}; + +struct _HinawaFwUnitClass { + GObjectClass parent_class; +}; + +GType hinawa_fw_unit_get_type(void) G_GNUC_CONST; + +void hinawa_fw_unit_open(HinawaFwUnit *self, gchar *path, GError **exception); + +void hinawa_fw_unit_listen(HinawaFwUnit *self, GError **exception); +void hinawa_fw_unit_unlisten(HinawaFwUnit *self); + +#endif diff --git a/libhinawa/src/internal.h b/libhinawa/src/internal.h new file mode 100644 index 0000000..83f920b --- /dev/null +++ b/libhinawa/src/internal.h @@ -0,0 +1,13 @@ +#ifndef __ALSA_TOOLS_HINAWA_INTERNAL_H__ +#define __ALSA_TOOLS_HINAWA_INTERNAL_H__ + +#include +#include + +#include +#include + +#include "fw_unit.h" + +void hinawa_fw_unit_ioctl(HinawaFwUnit *self, int req, void *args, int *err); +#endif