#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/vmalloc.h>
+#include <linux/btree.h>
#include <asm/cacheflush.h>
#include <media/videobuf2-vmalloc.h>
#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
#endif
+struct vchiq_mmal_instance;
+
/* normal message context */
struct mmal_msg_context {
+ struct vchiq_mmal_instance *instance;
+ u32 handle;
+
union {
struct {
/* work struct for defered callback - must come first */
};
+struct vchiq_mmal_context_map {
+ /* ensure serialized access to the btree(contention should be low) */
+ struct mutex lock;
+ struct btree_head32 btree_head;
+ u32 last_handle;
+};
+
struct vchiq_mmal_instance {
VCHI_SERVICE_HANDLE_T handle;
/* vmalloc page to receive scratch bulk xfers into */
void *bulk_scratch;
+ /* mapping table between context handles and mmal_msg_contexts */
+ struct vchiq_mmal_context_map context_map;
+
/* component to use next */
int component_idx;
struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
};
+static int __must_check
+mmal_context_map_init(struct vchiq_mmal_context_map *context_map)
+{
+ mutex_init(&context_map->lock);
+ context_map->last_handle = 0;
+ return btree_init32(&context_map->btree_head);
+}
+
+static void mmal_context_map_destroy(struct vchiq_mmal_context_map *context_map)
+{
+ mutex_lock(&context_map->lock);
+ btree_destroy32(&context_map->btree_head);
+ mutex_unlock(&context_map->lock);
+}
+
+static u32
+mmal_context_map_create_handle(struct vchiq_mmal_context_map *context_map,
+ struct mmal_msg_context *msg_context,
+ gfp_t gfp)
+{
+ u32 handle;
+
+ mutex_lock(&context_map->lock);
+
+ while (1) {
+ /* just use a simple count for handles, but do not use 0 */
+ context_map->last_handle++;
+ if (!context_map->last_handle)
+ context_map->last_handle++;
+
+ handle = context_map->last_handle;
+
+ /* check if the handle is already in use */
+ if (!btree_lookup32(&context_map->btree_head, handle))
+ break;
+ }
+
+ if (btree_insert32(&context_map->btree_head, handle,
+ msg_context, gfp)) {
+ /* probably out of memory */
+ mutex_unlock(&context_map->lock);
+ return 0;
+ }
+
+ mutex_unlock(&context_map->lock);
+ return handle;
+}
+
+static struct mmal_msg_context *
+mmal_context_map_lookup_handle(struct vchiq_mmal_context_map *context_map,
+ u32 handle)
+{
+ struct mmal_msg_context *msg_context;
+
+ if (!handle)
+ return NULL;
+
+ mutex_lock(&context_map->lock);
+
+ msg_context = btree_lookup32(&context_map->btree_head, handle);
+
+ mutex_unlock(&context_map->lock);
+ return msg_context;
+}
+
+static void
+mmal_context_map_destroy_handle(struct vchiq_mmal_context_map *context_map,
+ u32 handle)
+{
+ mutex_lock(&context_map->lock);
+ btree_remove32(&context_map->btree_head, handle);
+ mutex_unlock(&context_map->lock);
+}
+
static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
*instance)
{
struct mmal_msg_context *msg_context;
- /* todo: should this be allocated from a pool to avoid kmalloc */
- msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
- memset(msg_context, 0, sizeof(*msg_context));
+ /* todo: should this be allocated from a pool to avoid kzalloc */
+ msg_context = kzalloc(sizeof(*msg_context), GFP_KERNEL);
+
+ if (!msg_context)
+ return ERR_PTR(-ENOMEM);
+
+ msg_context->instance = instance;
+ msg_context->handle =
+ mmal_context_map_create_handle(&instance->context_map,
+ msg_context,
+ GFP_KERNEL);
+
+ if (!msg_context->handle) {
+ kfree(msg_context);
+ return ERR_PTR(-ENOMEM);
+ }
return msg_context;
}
+static struct mmal_msg_context *
+lookup_msg_context(struct vchiq_mmal_instance *instance, u32 handle)
+{
+ return mmal_context_map_lookup_handle(&instance->context_map,
+ handle);
+}
+
static void release_msg_context(struct mmal_msg_context *msg_context)
{
+ mmal_context_map_destroy_handle(&msg_context->instance->context_map,
+ msg_context->handle);
kfree(msg_context);
}
struct mmal_msg *msg, u32 msg_len)
{
pr_debug("unhandled event\n");
- pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
+ pr_debug("component:%u port type:%d num:%d cmd:0x%x length:%d\n",
msg->u.event_to_host.client_component,
msg->u.event_to_host.port_type,
msg->u.event_to_host.port_num,
*/
static void buffer_work_cb(struct work_struct *work)
{
- struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
+ struct mmal_msg_context *msg_context =
+ container_of(work, struct mmal_msg_context, u.bulk.work);
msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
msg_context->u.bulk.port,
/* get context */
msg_context = get_msg_context(instance);
- if (msg_context == NULL)
- return -ENOMEM;
+ if (IS_ERR(msg_context)) {
+ ret = PTR_ERR(msg_context);
+ goto unlock;
+ }
/* store bulk message context for when data arrives */
msg_context->u.bulk.instance = instance;
m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
m.h.magic = MMAL_MAGIC;
- m.h.context = msg_context;
+ m.h.context = msg_context->handle;
m.h.status = 0;
/* drvbuf is our private data passed back */
m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
m.u.buffer_from_host.drvbuf.port_handle = port->handle;
- m.u.buffer_from_host.drvbuf.client_context = msg_context;
+ m.u.buffer_from_host.drvbuf.client_context = msg_context->handle;
/* buffer header */
m.u.buffer_from_host.buffer_header.cmd = 0;
- m.u.buffer_from_host.buffer_header.data = buf->buffer;
+ m.u.buffer_from_host.buffer_header.data =
+ (u32)(unsigned long)buf->buffer;
m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
vchi_service_release(instance->handle);
+unlock:
mutex_unlock(&instance->bulk_mutex);
return ret;
struct mmal_msg *msg, u32 msg_len)
{
struct mmal_msg_context *msg_context;
+ u32 handle;
pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
instance, msg, msg_len);
if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
- msg_context = msg->u.buffer_from_host.drvbuf.client_context;
+ handle = msg->u.buffer_from_host.drvbuf.client_context;
+ msg_context = lookup_msg_context(instance, handle);
+
+ if (!msg_context) {
+ pr_err("drvbuf.client_context(%u) is invalid\n",
+ handle);
+ return;
+ }
} else {
pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
return;
u32 msg_len;
struct mmal_msg *msg;
VCHI_HELD_MSG_T msg_handle;
+ struct mmal_msg_context *msg_context;
if (!instance) {
pr_err("Message callback passed NULL instance\n");
* should be verified the address lies in the kernel
* address space.
*/
- if (msg->h.context == NULL) {
+ if (!msg->h.context) {
pr_err("received message context was null!\n");
vchi_held_msg_release(&msg_handle);
break;
}
+ msg_context = lookup_msg_context(instance,
+ msg->h.context);
+ if (!msg_context) {
+ pr_err("received invalid message context %u!\n",
+ msg->h.context);
+ vchi_held_msg_release(&msg_handle);
+ break;
+ }
+
/* fill in context values */
- msg->h.context->u.sync.msg_handle = msg_handle;
- msg->h.context->u.sync.msg = msg;
- msg->h.context->u.sync.msg_len = msg_len;
+ msg_context->u.sync.msg_handle = msg_handle;
+ msg_context->u.sync.msg = msg;
+ msg_context->u.sync.msg_len = msg_len;
/* todo: should this check (completion_done()
* == 1) for no one waiting? or do we need a
*/
/* complete message so caller knows it happened */
- complete(&msg->h.context->u.sync.cmplt);
+ complete(&msg_context->u.sync.cmplt);
break;
}
struct mmal_msg **msg_out,
VCHI_HELD_MSG_T *msg_handle_out)
{
- struct mmal_msg_context msg_context;
+ struct mmal_msg_context *msg_context;
int ret;
/* payload size must not cause message to exceed max size */
return -EINVAL;
}
- init_completion(&msg_context.u.sync.cmplt);
+ msg_context = get_msg_context(instance);
+ if (IS_ERR(msg_context))
+ return PTR_ERR(msg_context);
+
+ init_completion(&msg_context->u.sync.cmplt);
msg->h.magic = MMAL_MAGIC;
- msg->h.context = &msg_context;
+ msg->h.context = msg_context->handle;
msg->h.status = 0;
DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
return ret;
}
- ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
+ ret = wait_for_completion_timeout(&msg_context->u.sync.cmplt, 3*HZ);
if (ret <= 0) {
pr_err("error %d waiting for sync completion\n", ret);
if (ret == 0)
ret = -ETIME;
/* todo: what happens if the message arrives after aborting */
+ release_msg_context(msg_context);
return ret;
}
- *msg_out = msg_context.u.sync.msg;
- *msg_handle_out = msg_context.u.sync.msg_handle;
+ *msg_out = msg_context->u.sync.msg;
+ *msg_handle_out = msg_context->u.sync.msg_handle;
+ release_msg_context(msg_context);
return 0;
}
/* only three writable fields in a port */
p->buffer_num = port->current_buffer.num;
p->buffer_size = port->current_buffer.size;
- p->userdata = port;
+ p->userdata = (u32)(unsigned long)port;
}
static int port_info_set(struct vchiq_mmal_instance *instance,
/* build component create message */
m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
- m.u.component_create.client_component = component;
+ m.u.component_create.client_component = (u32)(unsigned long)component;
strncpy(m.u.component_create.name, name,
sizeof(m.u.component_create.name));
vfree(instance->bulk_scratch);
+ mmal_context_map_destroy(&instance->context_map);
+
kfree(instance);
return status;
* directly (de)serialised from memory.
*/
-#ifdef CONFIG_ARM
/* ensure the header structure has packed to the correct size */
BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
/* mmal port struct is correct size */
BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
-#endif
/* create a vchi instance */
status = vchi_initialise(&vchi_instance);
return -EIO;
}
- instance = kmalloc(sizeof(*instance), GFP_KERNEL);
- memset(instance, 0, sizeof(*instance));
+ instance = kzalloc(sizeof(*instance), GFP_KERNEL);
+
+ if (!instance)
+ return -ENOMEM;
mutex_init(&instance->vchiq_mutex);
mutex_init(&instance->bulk_mutex);
instance->bulk_scratch = vmalloc(PAGE_SIZE);
+ status = mmal_context_map_init(&instance->context_map);
+ if (status) {
+ pr_err("Failed to init context map (status=%d)\n", status);
+ kfree(instance);
+ return status;
+ }
+
params.callback_param = instance;
status = vchi_service_open(vchi_instance, ¶ms, &instance->handle);