diff options
author | Takashi Sakamoto <o-takashi@sakamocchi.jp> | 2022-05-05 11:37:24 +0900 |
---|---|---|
committer | 坂本 貴史 <o-takashi@sakamocchi.jp> | 2022-05-05 16:09:50 +0900 |
commit | 2a0d205f9ff61fd6ab8d045ca3d5480fc5b1e83b (patch) | |
tree | 1cde84fd7c894c25dc3099105a38e5829f8b7fd8 | |
parent | c211df6e8197db800a5d470870a62669bcced7b8 (diff) | |
download | libhinoko-2a0d205f9ff61fd6ab8d045ca3d5480fc5b1e83b.tar.gz |
fw_iso_ctx: code refactoring to split map function
It's planned to make Hinoko.FwIsoCtx as interface. It's convenient
for derived object to use private helper function instead of instance
method.
This commit splits current implementaion of Hinoko.FwIsoCtx.map()
into internal API part and private implementation.
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
-rw-r--r-- | src/fw_iso_ctx.c | 39 | ||||
-rw-r--r-- | src/fw_iso_ctx_private.c | 57 | ||||
-rw-r--r-- | src/fw_iso_ctx_private.h | 4 |
3 files changed, 61 insertions, 39 deletions
diff --git a/src/fw_iso_ctx.c b/src/fw_iso_ctx.c index b153d69..a6d8e9f 100644 --- a/src/fw_iso_ctx.c +++ b/src/fw_iso_ctx.c @@ -219,48 +219,11 @@ void hinoko_fw_iso_ctx_map_buffer(HinokoFwIsoCtx *self, guint bytes_per_chunk, guint chunks_per_buffer, GError **error) { HinokoFwIsoCtxPrivate *priv; - unsigned int datum_size; - int prot; g_return_if_fail(HINOKO_IS_FW_ISO_CTX(self)); - g_return_if_fail(bytes_per_chunk > 0); - g_return_if_fail(chunks_per_buffer > 0); - g_return_if_fail(error == NULL || *error == NULL); priv = hinoko_fw_iso_ctx_get_instance_private(self); - if (priv->fd < 0) { - generate_local_error(error, HINOKO_FW_ISO_CTX_ERROR_NOT_ALLOCATED); - return; - } - - if (priv->addr != NULL) { - generate_local_error(error, HINOKO_FW_ISO_CTX_ERROR_MAPPED); - return; - } - - datum_size = sizeof(struct fw_cdev_iso_packet); - if (priv->mode == HINOKO_FW_ISO_CTX_MODE_TX) - datum_size += priv->header_size; - - priv->data = g_malloc_n(chunks_per_buffer, datum_size); - - priv->alloc_data_length = chunks_per_buffer * datum_size; - - prot = PROT_READ; - if (priv->mode == HINOKO_FW_ISO_CTX_MODE_TX) - prot |= PROT_WRITE; - - // Align to size of page. - priv->addr = mmap(NULL, bytes_per_chunk * chunks_per_buffer, prot, - MAP_SHARED, priv->fd, 0); - if (priv->addr == MAP_FAILED) { - generate_syscall_error(error, errno, - "mmap(%d)", bytes_per_chunk * chunks_per_buffer); - return; - } - - priv->bytes_per_chunk = bytes_per_chunk; - priv->chunks_per_buffer = chunks_per_buffer; + (void)fw_iso_ctx_state_map_buffer(priv, bytes_per_chunk, chunks_per_buffer, error); } /** diff --git a/src/fw_iso_ctx_private.c b/src/fw_iso_ctx_private.c index e622b00..c20b3e7 100644 --- a/src/fw_iso_ctx_private.c +++ b/src/fw_iso_ctx_private.c @@ -4,6 +4,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <sys/mman.h> /** * fw_iso_ctx_state_allocate: @@ -112,3 +113,59 @@ void fw_iso_ctx_state_release(struct fw_iso_ctx_state *state) state->fd = -1; } + +/** + * fw_iso_ctx_state_map_buffer: + * @state: A [struct@FwIsoCtxState]. + * @bytes_per_chunk: The number of bytes per chunk in buffer going to be allocated. + * @chunks_per_buffer: The number of chunks in buffer going to be allocated. + * @error: A [struct@GLib.Error]. + * + * Map intermediate buffer to share payload of isochronous context with 1394 OHCI controller. + */ +gboolean fw_iso_ctx_state_map_buffer(struct fw_iso_ctx_state *state, guint bytes_per_chunk, + guint chunks_per_buffer, GError **error) +{ + unsigned int datum_size; + int prot; + + g_return_val_if_fail(bytes_per_chunk > 0, FALSE); + g_return_val_if_fail(chunks_per_buffer > 0, FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + + if (state->fd < 0) { + generate_local_error(error, HINOKO_FW_ISO_CTX_ERROR_NOT_ALLOCATED); + return FALSE; + } + + if (state->addr != NULL) { + generate_local_error(error, HINOKO_FW_ISO_CTX_ERROR_MAPPED); + return FALSE; + } + + datum_size = sizeof(struct fw_cdev_iso_packet); + if (state->mode == HINOKO_FW_ISO_CTX_MODE_TX) + datum_size += state->header_size; + + state->data = g_malloc_n(chunks_per_buffer, datum_size); + + state->alloc_data_length = chunks_per_buffer * datum_size; + + prot = PROT_READ; + if (state->mode == HINOKO_FW_ISO_CTX_MODE_TX) + prot |= PROT_WRITE; + + // Align to size of page. + state->addr = mmap(NULL, bytes_per_chunk * chunks_per_buffer, prot, + MAP_SHARED, state->fd, 0); + if (state->addr == MAP_FAILED) { + generate_syscall_error(error, errno, + "mmap(%d)", bytes_per_chunk * chunks_per_buffer); + return FALSE; + } + + state->bytes_per_chunk = bytes_per_chunk; + state->chunks_per_buffer = chunks_per_buffer; + + return TRUE; +} diff --git a/src/fw_iso_ctx_private.h b/src/fw_iso_ctx_private.h index f902ed3..73622c8 100644 --- a/src/fw_iso_ctx_private.h +++ b/src/fw_iso_ctx_private.h @@ -41,13 +41,15 @@ struct fw_iso_ctx_state { gboolean running; }; +#define STOPPED_SIGNAL_NEME "stopped" gboolean fw_iso_ctx_state_allocate(struct fw_iso_ctx_state *state, const char *path, HinokoFwIsoCtxMode mode, HinokoFwScode scode, guint channel, guint header_size, GError **error); void fw_iso_ctx_state_release(struct fw_iso_ctx_state *state); -#define STOPPED_SIGNAL_NEME "stopped" +gboolean fw_iso_ctx_state_map_buffer(struct fw_iso_ctx_state *state, guint bytes_per_chunk, + guint chunks_per_buffer, GError **error); void hinoko_fw_iso_ctx_allocate(HinokoFwIsoCtx *self, const char *path, HinokoFwIsoCtxMode mode, HinokoFwScode scode, |