1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BCM2835 V4L2 driver
5 * Copyright © 2013 Raspberry Pi (Trading) Ltd.
7 * Authors: Vincent Sanders @ Collabora
8 * Dave Stevenson @ Broadcom
9 * (now dave.stevenson@raspberrypi.org)
10 * Simon Mellor @ Broadcom
11 * Luke Diamand @ Broadcom
13 * V4L2 driver MMAL vchiq interface code
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/completion.h>
25 #include <linux/vmalloc.h>
26 #include <media/videobuf2-vmalloc.h>
28 #include "../include/linux/raspberrypi/vchiq.h"
29 #include "mmal-common.h"
30 #include "mmal-vchiq.h"
34 * maximum number of components supported.
35 * This matches the maximum permitted by default on the VPU
37 #define VCHIQ_MMAL_MAX_COMPONENTS 64
40 * Timeout for synchronous msg responses in seconds.
41 * Helpful to increase this if stopping in the VPU debugger.
43 #define SYNC_MSG_TIMEOUT 3
45 /*#define FULL_MSG_DUMP 1*/
48 static const char *const msg_type_names[] = {
66 "GET_CORE_STATS_FOR_PORT",
70 "OPAQUE_ALLOCATOR_DESC",
73 "BUFFER_FROM_HOST_ZEROLEN",
79 static const char *const port_action_type_names[] = {
90 #if defined(FULL_MSG_DUMP)
91 #define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
93 pr_debug(TITLE" type:%s(%d) length:%d\n", \
94 msg_type_names[(MSG)->h.type], \
95 (MSG)->h.type, (MSG_LEN)); \
96 print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
98 sizeof(struct mmal_msg_header), 1); \
99 print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
101 ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
102 (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
105 #define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
107 pr_debug(TITLE" type:%s(%d) length:%d\n", \
108 msg_type_names[(MSG)->h.type], \
109 (MSG)->h.type, (MSG_LEN)); \
113 #define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
116 struct vchiq_mmal_instance;
118 /* normal message context */
119 struct mmal_msg_context {
120 struct vchiq_mmal_instance *instance;
122 /* Index in the context_map idr so that we can find the
123 * mmal_msg_context again when servicing the VCHI reply.
129 /* work struct for buffer_cb callback */
130 struct work_struct work;
131 /* work struct for deferred callback */
132 struct work_struct buffer_to_host_work;
134 struct vchiq_mmal_instance *instance;
136 struct vchiq_mmal_port *port;
137 /* actual buffer used to store bulk reply */
138 struct mmal_buffer *buffer;
139 /* amount of buffer used */
140 unsigned long buffer_used;
141 /* MMAL buffer flags */
143 /* Presentation and Decode timestamps */
147 int status; /* context status */
149 } bulk; /* bulk data */
152 /* message handle to release */
153 struct vchiq_header *msg_handle;
154 /* pointer to received message */
155 struct mmal_msg *msg;
156 /* received message length */
158 /* completion upon reply */
159 struct completion cmplt;
160 } sync; /* synchronous response */
165 struct vchiq_mmal_instance {
166 unsigned int service_handle;
168 /* ensure serialised access to service */
169 struct mutex vchiq_mutex;
171 struct idr context_map;
172 /* protect accesses to context_map */
173 struct mutex context_map_lock;
175 struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
177 /* ordered workqueue to process all bulk operations */
178 struct workqueue_struct *bulk_wq;
180 /* handle for a vchiq instance */
181 struct vchiq_instance *vchiq_instance;
184 static struct mmal_msg_context *
185 get_msg_context(struct vchiq_mmal_instance *instance)
187 struct mmal_msg_context *msg_context;
190 /* todo: should this be allocated from a pool to avoid kzalloc */
191 msg_context = kzalloc(sizeof(*msg_context), GFP_KERNEL);
194 return ERR_PTR(-ENOMEM);
196 /* Create an ID that will be passed along with our message so
197 * that when we service the VCHI reply, we can look up what
198 * message is being replied to.
200 mutex_lock(&instance->context_map_lock);
201 handle = idr_alloc(&instance->context_map, msg_context,
203 mutex_unlock(&instance->context_map_lock);
207 return ERR_PTR(handle);
210 msg_context->instance = instance;
211 msg_context->handle = handle;
216 static struct mmal_msg_context *
217 lookup_msg_context(struct vchiq_mmal_instance *instance, int handle)
219 return idr_find(&instance->context_map, handle);
223 release_msg_context(struct mmal_msg_context *msg_context)
225 struct vchiq_mmal_instance *instance = msg_context->instance;
227 mutex_lock(&instance->context_map_lock);
228 idr_remove(&instance->context_map, msg_context->handle);
229 mutex_unlock(&instance->context_map_lock);
233 /* deals with receipt of event to host message */
234 static void event_to_host_cb(struct vchiq_mmal_instance *instance,
235 struct mmal_msg *msg, u32 msg_len)
237 pr_debug("unhandled event\n");
238 pr_debug("component:%u port type:%d num:%d cmd:0x%x length:%d\n",
239 msg->u.event_to_host.client_component,
240 msg->u.event_to_host.port_type,
241 msg->u.event_to_host.port_num,
242 msg->u.event_to_host.cmd, msg->u.event_to_host.length);
245 /* workqueue scheduled callback
247 * we do this because it is important we do not call any other vchiq
248 * sync calls from within the message delivery thread
250 static void buffer_work_cb(struct work_struct *work)
252 struct mmal_msg_context *msg_context =
253 container_of(work, struct mmal_msg_context, u.bulk.work);
254 struct mmal_buffer *buffer = msg_context->u.bulk.buffer;
257 pr_err("%s: ctx: %p, No mmal buffer to pass details\n",
258 __func__, msg_context);
262 buffer->length = msg_context->u.bulk.buffer_used;
263 buffer->mmal_flags = msg_context->u.bulk.mmal_flags;
264 buffer->dts = msg_context->u.bulk.dts;
265 buffer->pts = msg_context->u.bulk.pts;
267 atomic_dec(&msg_context->u.bulk.port->buffers_with_vpu);
269 msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
270 msg_context->u.bulk.port,
271 msg_context->u.bulk.status,
272 msg_context->u.bulk.buffer);
275 /* workqueue scheduled callback to handle receiving buffers
277 * VCHI will allow up to 4 bulk receives to be scheduled before blocking.
278 * If we block in the service_callback context then we can't process the
279 * VCHI_CALLBACK_BULK_RECEIVED message that would otherwise allow the blocked
280 * vchiq_bulk_receive() call to complete.
282 static void buffer_to_host_work_cb(struct work_struct *work)
284 struct mmal_msg_context *msg_context =
285 container_of(work, struct mmal_msg_context,
286 u.bulk.buffer_to_host_work);
287 struct vchiq_mmal_instance *instance = msg_context->instance;
288 unsigned long len = msg_context->u.bulk.buffer_used;
292 /* Dummy receive to ensure the buffers remain in order */
294 /* queue the bulk submission */
295 vchiq_use_service(instance->vchiq_instance, instance->service_handle);
296 ret = vchiq_bulk_receive(instance->vchiq_instance, instance->service_handle,
297 msg_context->u.bulk.buffer->buffer,
298 /* Actual receive needs to be a multiple
303 VCHIQ_BULK_MODE_CALLBACK);
305 vchiq_release_service(instance->vchiq_instance, instance->service_handle);
308 pr_err("%s: ctx: %p, vchiq_bulk_receive failed %d\n",
309 __func__, msg_context, ret);
312 /* enqueue a bulk receive for a given message context */
313 static int bulk_receive(struct vchiq_mmal_instance *instance,
314 struct mmal_msg *msg,
315 struct mmal_msg_context *msg_context)
317 unsigned long rd_len;
319 rd_len = msg->u.buffer_from_host.buffer_header.length;
321 if (!msg_context->u.bulk.buffer) {
322 pr_err("bulk.buffer not configured - error in buffer_from_host\n");
324 /* todo: this is a serious error, we should never have
325 * committed a buffer_to_host operation to the mmal
326 * port without the buffer to back it up (underflow
327 * handling) and there is no obvious way to deal with
328 * this - how is the mmal servie going to react when
329 * we fail to do the xfer and reschedule a buffer when
330 * it arrives? perhaps a starved flag to indicate a
331 * waiting bulk receive?
337 /* ensure we do not overrun the available buffer */
338 if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
339 rd_len = msg_context->u.bulk.buffer->buffer_size;
340 pr_warn("short read as not enough receive buffer space\n");
341 /* todo: is this the correct response, what happens to
342 * the rest of the message data?
347 msg_context->u.bulk.buffer_used = rd_len;
348 msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
349 msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
351 queue_work(msg_context->instance->bulk_wq,
352 &msg_context->u.bulk.buffer_to_host_work);
357 /* data in message, memcpy from packet into output buffer */
358 static int inline_receive(struct vchiq_mmal_instance *instance,
359 struct mmal_msg *msg,
360 struct mmal_msg_context *msg_context)
362 memcpy(msg_context->u.bulk.buffer->buffer,
363 msg->u.buffer_from_host.short_data,
364 msg->u.buffer_from_host.payload_in_message);
366 msg_context->u.bulk.buffer_used =
367 msg->u.buffer_from_host.payload_in_message;
372 /* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
374 buffer_from_host(struct vchiq_mmal_instance *instance,
375 struct vchiq_mmal_port *port, struct mmal_buffer *buf)
377 struct mmal_msg_context *msg_context;
384 pr_debug("instance:%u buffer:%p\n", instance->service_handle, buf);
387 if (!buf->msg_context) {
388 pr_err("%s: msg_context not allocated, buf %p\n", __func__,
392 msg_context = buf->msg_context;
394 /* store bulk message context for when data arrives */
395 msg_context->u.bulk.instance = instance;
396 msg_context->u.bulk.port = port;
397 msg_context->u.bulk.buffer = buf;
398 msg_context->u.bulk.buffer_used = 0;
400 /* initialise work structure ready to schedule callback */
401 INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
402 INIT_WORK(&msg_context->u.bulk.buffer_to_host_work,
403 buffer_to_host_work_cb);
405 atomic_inc(&port->buffers_with_vpu);
407 /* prep the buffer from host message */
408 memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
410 m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
411 m.h.magic = MMAL_MAGIC;
412 m.h.context = msg_context->handle;
415 /* drvbuf is our private data passed back */
416 m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
417 m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
418 m.u.buffer_from_host.drvbuf.port_handle = port->handle;
419 m.u.buffer_from_host.drvbuf.client_context = msg_context->handle;
422 m.u.buffer_from_host.buffer_header.cmd = 0;
423 m.u.buffer_from_host.buffer_header.data =
424 (u32)(unsigned long)buf->buffer;
425 m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
426 m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
427 m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
428 m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
429 m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
430 m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
432 /* clear buffer type specific data */
433 memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
434 sizeof(m.u.buffer_from_host.buffer_header_type_specific));
436 /* no payload in message */
437 m.u.buffer_from_host.payload_in_message = 0;
439 vchiq_use_service(instance->vchiq_instance, instance->service_handle);
441 ret = vchiq_queue_kernel_message(instance->vchiq_instance, instance->service_handle, &m,
442 sizeof(struct mmal_msg_header) +
443 sizeof(m.u.buffer_from_host));
445 atomic_dec(&port->buffers_with_vpu);
447 vchiq_release_service(instance->vchiq_instance, instance->service_handle);
452 /* deals with receipt of buffer to host message */
453 static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
454 struct mmal_msg *msg, u32 msg_len)
456 struct mmal_msg_context *msg_context;
459 pr_debug("%s: instance:%p msg:%p msg_len:%d\n",
460 __func__, instance, msg, msg_len);
462 if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
463 handle = msg->u.buffer_from_host.drvbuf.client_context;
464 msg_context = lookup_msg_context(instance, handle);
467 pr_err("drvbuf.client_context(%u) is invalid\n",
472 pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
476 msg_context->u.bulk.mmal_flags =
477 msg->u.buffer_from_host.buffer_header.flags;
479 if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
480 /* message reception had an error */
481 pr_warn("error %d in reply\n", msg->h.status);
483 msg_context->u.bulk.status = msg->h.status;
485 } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
487 if (msg->u.buffer_from_host.buffer_header.flags &
488 MMAL_BUFFER_HEADER_FLAG_EOS) {
489 msg_context->u.bulk.status =
490 bulk_receive(instance, msg, msg_context);
491 if (msg_context->u.bulk.status == 0)
492 return; /* successful bulk submission, bulk
493 * completion will trigger callback
496 /* do callback with empty buffer - not EOS though */
497 msg_context->u.bulk.status = 0;
498 msg_context->u.bulk.buffer_used = 0;
500 } else if (msg->u.buffer_from_host.payload_in_message == 0) {
501 /* data is not in message, queue a bulk receive */
502 msg_context->u.bulk.status =
503 bulk_receive(instance, msg, msg_context);
504 if (msg_context->u.bulk.status == 0)
505 return; /* successful bulk submission, bulk
506 * completion will trigger callback
509 /* failed to submit buffer, this will end badly */
510 pr_err("error %d on bulk submission\n",
511 msg_context->u.bulk.status);
513 } else if (msg->u.buffer_from_host.payload_in_message <=
514 MMAL_VC_SHORT_DATA) {
515 /* data payload within message */
516 msg_context->u.bulk.status = inline_receive(instance, msg,
519 pr_err("message with invalid short payload\n");
522 msg_context->u.bulk.status = -EINVAL;
523 msg_context->u.bulk.buffer_used =
524 msg->u.buffer_from_host.payload_in_message;
527 /* schedule the port callback */
528 schedule_work(&msg_context->u.bulk.work);
531 static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
532 struct mmal_msg_context *msg_context)
534 msg_context->u.bulk.status = 0;
536 /* schedule the port callback */
537 schedule_work(&msg_context->u.bulk.work);
540 static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
541 struct mmal_msg_context *msg_context)
543 pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
545 msg_context->u.bulk.status = -EINTR;
547 schedule_work(&msg_context->u.bulk.work);
550 /* incoming event service callback */
551 static int service_callback(struct vchiq_instance *vchiq_instance,
552 enum vchiq_reason reason, struct vchiq_header *header,
553 unsigned int handle, void *bulk_ctx)
555 struct vchiq_mmal_instance *instance = vchiq_get_service_userdata(vchiq_instance, handle);
557 struct mmal_msg *msg;
558 struct mmal_msg_context *msg_context;
561 pr_err("Message callback passed NULL instance\n");
566 case VCHIQ_MESSAGE_AVAILABLE:
567 msg = (void *)header->data;
568 msg_len = header->size;
570 DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
572 /* handling is different for buffer messages */
573 switch (msg->h.type) {
574 case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
575 vchiq_release_message(vchiq_instance, handle, header);
578 case MMAL_MSG_TYPE_EVENT_TO_HOST:
579 event_to_host_cb(instance, msg, msg_len);
580 vchiq_release_message(vchiq_instance, handle, header);
584 case MMAL_MSG_TYPE_BUFFER_TO_HOST:
585 buffer_to_host_cb(instance, msg, msg_len);
586 vchiq_release_message(vchiq_instance, handle, header);
590 /* messages dependent on header context to complete */
591 if (!msg->h.context) {
592 pr_err("received message context was null!\n");
593 vchiq_release_message(vchiq_instance, handle, header);
597 msg_context = lookup_msg_context(instance,
600 pr_err("received invalid message context %u!\n",
602 vchiq_release_message(vchiq_instance, handle, header);
606 /* fill in context values */
607 msg_context->u.sync.msg_handle = header;
608 msg_context->u.sync.msg = msg;
609 msg_context->u.sync.msg_len = msg_len;
611 /* todo: should this check (completion_done()
612 * == 1) for no one waiting? or do we need a
613 * flag to tell us the completion has been
614 * interrupted so we can free the message and
615 * its context. This probably also solves the
616 * message arriving after interruption todo
620 /* complete message so caller knows it happened */
621 complete(&msg_context->u.sync.cmplt);
627 case VCHIQ_BULK_RECEIVE_DONE:
628 bulk_receive_cb(instance, bulk_ctx);
631 case VCHIQ_BULK_RECEIVE_ABORTED:
632 bulk_abort_cb(instance, bulk_ctx);
635 case VCHIQ_SERVICE_CLOSED:
636 /* TODO: consider if this requires action if received when
637 * driver is not explicitly closing the service
642 pr_err("Received unhandled message reason %d\n", reason);
649 static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
650 struct mmal_msg *msg,
651 unsigned int payload_len,
652 struct mmal_msg **msg_out,
653 struct vchiq_header **msg_handle)
655 struct mmal_msg_context *msg_context;
657 unsigned long timeout;
659 /* payload size must not cause message to exceed max size */
661 (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
662 pr_err("payload length %d exceeds max:%d\n", payload_len,
663 (int)(MMAL_MSG_MAX_SIZE -
664 sizeof(struct mmal_msg_header)));
668 msg_context = get_msg_context(instance);
669 if (IS_ERR(msg_context))
670 return PTR_ERR(msg_context);
672 init_completion(&msg_context->u.sync.cmplt);
674 msg->h.magic = MMAL_MAGIC;
675 msg->h.context = msg_context->handle;
678 DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
681 vchiq_use_service(instance->vchiq_instance, instance->service_handle);
683 ret = vchiq_queue_kernel_message(instance->vchiq_instance, instance->service_handle, msg,
684 sizeof(struct mmal_msg_header) +
687 vchiq_release_service(instance->vchiq_instance, instance->service_handle);
690 pr_err("error %d queuing message\n", ret);
691 release_msg_context(msg_context);
695 timeout = wait_for_completion_timeout(&msg_context->u.sync.cmplt,
696 SYNC_MSG_TIMEOUT * HZ);
698 pr_err("timed out waiting for sync completion\n");
700 /* todo: what happens if the message arrives after aborting */
701 release_msg_context(msg_context);
705 *msg_out = msg_context->u.sync.msg;
706 *msg_handle = msg_context->u.sync.msg_handle;
707 release_msg_context(msg_context);
712 static void dump_port_info(struct vchiq_mmal_port *port)
714 pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
716 pr_debug("buffer minimum num:%d size:%d align:%d\n",
717 port->minimum_buffer.num,
718 port->minimum_buffer.size, port->minimum_buffer.alignment);
720 pr_debug("buffer recommended num:%d size:%d align:%d\n",
721 port->recommended_buffer.num,
722 port->recommended_buffer.size,
723 port->recommended_buffer.alignment);
725 pr_debug("buffer current values num:%d size:%d align:%d\n",
726 port->current_buffer.num,
727 port->current_buffer.size, port->current_buffer.alignment);
729 pr_debug("elementary stream: type:%d encoding:0x%x variant:0x%x\n",
731 port->format.encoding, port->format.encoding_variant);
733 pr_debug(" bitrate:%d flags:0x%x\n",
734 port->format.bitrate, port->format.flags);
736 if (port->format.type == MMAL_ES_TYPE_VIDEO) {
738 ("es video format: width:%d height:%d colourspace:0x%x\n",
739 port->es.video.width, port->es.video.height,
740 port->es.video.color_space);
742 pr_debug(" : crop xywh %d,%d,%d,%d\n",
743 port->es.video.crop.x,
744 port->es.video.crop.y,
745 port->es.video.crop.width, port->es.video.crop.height);
746 pr_debug(" : framerate %d/%d aspect %d/%d\n",
747 port->es.video.frame_rate.numerator,
748 port->es.video.frame_rate.denominator,
749 port->es.video.par.numerator, port->es.video.par.denominator);
753 static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
755 /* todo do readonly fields need setting at all? */
756 p->type = port->type;
757 p->index = port->index;
759 p->is_enabled = port->enabled;
760 p->buffer_num_min = port->minimum_buffer.num;
761 p->buffer_size_min = port->minimum_buffer.size;
762 p->buffer_alignment_min = port->minimum_buffer.alignment;
763 p->buffer_num_recommended = port->recommended_buffer.num;
764 p->buffer_size_recommended = port->recommended_buffer.size;
766 /* only three writable fields in a port */
767 p->buffer_num = port->current_buffer.num;
768 p->buffer_size = port->current_buffer.size;
769 p->userdata = (u32)(unsigned long)port;
772 static int port_info_set(struct vchiq_mmal_instance *instance,
773 struct vchiq_mmal_port *port)
777 struct mmal_msg *rmsg;
778 struct vchiq_header *rmsg_handle;
780 pr_debug("setting port info port %p\n", port);
783 dump_port_info(port);
785 m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
787 m.u.port_info_set.component_handle = port->component->handle;
788 m.u.port_info_set.port_type = port->type;
789 m.u.port_info_set.port_index = port->index;
791 port_to_mmal_msg(port, &m.u.port_info_set.port);
793 /* elementary stream format setup */
794 m.u.port_info_set.format.type = port->format.type;
795 m.u.port_info_set.format.encoding = port->format.encoding;
796 m.u.port_info_set.format.encoding_variant =
797 port->format.encoding_variant;
798 m.u.port_info_set.format.bitrate = port->format.bitrate;
799 m.u.port_info_set.format.flags = port->format.flags;
801 memcpy(&m.u.port_info_set.es, &port->es,
802 sizeof(union mmal_es_specific_format));
804 m.u.port_info_set.format.extradata_size = port->format.extradata_size;
805 memcpy(&m.u.port_info_set.extradata, port->format.extradata,
806 port->format.extradata_size);
808 ret = send_synchronous_mmal_msg(instance, &m,
809 sizeof(m.u.port_info_set),
810 &rmsg, &rmsg_handle);
814 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
815 /* got an unexpected message type in reply */
820 /* return operation status */
821 ret = -rmsg->u.port_info_get_reply.status;
823 pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
824 port->component->handle, port->handle);
827 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
832 /* use port info get message to retrieve port information */
833 static int port_info_get(struct vchiq_mmal_instance *instance,
834 struct vchiq_mmal_port *port)
838 struct mmal_msg *rmsg;
839 struct vchiq_header *rmsg_handle;
842 m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
843 m.u.port_info_get.component_handle = port->component->handle;
844 m.u.port_info_get.port_type = port->type;
845 m.u.port_info_get.index = port->index;
847 ret = send_synchronous_mmal_msg(instance, &m,
848 sizeof(m.u.port_info_get),
849 &rmsg, &rmsg_handle);
853 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
854 /* got an unexpected message type in reply */
859 /* return operation status */
860 ret = -rmsg->u.port_info_get_reply.status;
861 if (ret != MMAL_MSG_STATUS_SUCCESS)
864 if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
865 port->enabled = false;
867 port->enabled = true;
869 /* copy the values out of the message */
870 port->handle = rmsg->u.port_info_get_reply.port_handle;
872 /* port type and index cached to use on port info set because
873 * it does not use a port handle
875 port->type = rmsg->u.port_info_get_reply.port_type;
876 port->index = rmsg->u.port_info_get_reply.port_index;
878 port->minimum_buffer.num =
879 rmsg->u.port_info_get_reply.port.buffer_num_min;
880 port->minimum_buffer.size =
881 rmsg->u.port_info_get_reply.port.buffer_size_min;
882 port->minimum_buffer.alignment =
883 rmsg->u.port_info_get_reply.port.buffer_alignment_min;
885 port->recommended_buffer.alignment =
886 rmsg->u.port_info_get_reply.port.buffer_alignment_min;
887 port->recommended_buffer.num =
888 rmsg->u.port_info_get_reply.port.buffer_num_recommended;
890 port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
891 port->current_buffer.size =
892 rmsg->u.port_info_get_reply.port.buffer_size;
895 port->format.type = rmsg->u.port_info_get_reply.format.type;
896 port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
897 port->format.encoding_variant =
898 rmsg->u.port_info_get_reply.format.encoding_variant;
899 port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
900 port->format.flags = rmsg->u.port_info_get_reply.format.flags;
902 /* elementary stream format */
904 &rmsg->u.port_info_get_reply.es,
905 sizeof(union mmal_es_specific_format));
906 port->format.es = &port->es;
908 port->format.extradata_size =
909 rmsg->u.port_info_get_reply.format.extradata_size;
910 memcpy(port->format.extradata,
911 rmsg->u.port_info_get_reply.extradata,
912 port->format.extradata_size);
914 pr_debug("received port info\n");
915 dump_port_info(port);
919 pr_debug("%s:result:%d component:0x%x port:%d\n",
920 __func__, ret, port->component->handle, port->handle);
922 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
927 /* create component on vc */
928 static int create_component(struct vchiq_mmal_instance *instance,
929 struct vchiq_mmal_component *component,
934 struct mmal_msg *rmsg;
935 struct vchiq_header *rmsg_handle;
937 /* build component create message */
938 m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
939 m.u.component_create.client_component = component->client_component;
940 strscpy_pad(m.u.component_create.name, name,
941 sizeof(m.u.component_create.name));
942 m.u.component_create.pid = 0;
944 ret = send_synchronous_mmal_msg(instance, &m,
945 sizeof(m.u.component_create),
946 &rmsg, &rmsg_handle);
950 if (rmsg->h.type != m.h.type) {
951 /* got an unexpected message type in reply */
956 ret = -rmsg->u.component_create_reply.status;
957 if (ret != MMAL_MSG_STATUS_SUCCESS)
960 /* a valid component response received */
961 component->handle = rmsg->u.component_create_reply.component_handle;
962 component->inputs = rmsg->u.component_create_reply.input_num;
963 component->outputs = rmsg->u.component_create_reply.output_num;
964 component->clocks = rmsg->u.component_create_reply.clock_num;
966 pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
968 component->inputs, component->outputs, component->clocks);
971 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
976 /* destroys a component on vc */
977 static int destroy_component(struct vchiq_mmal_instance *instance,
978 struct vchiq_mmal_component *component)
982 struct mmal_msg *rmsg;
983 struct vchiq_header *rmsg_handle;
985 m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
986 m.u.component_destroy.component_handle = component->handle;
988 ret = send_synchronous_mmal_msg(instance, &m,
989 sizeof(m.u.component_destroy),
990 &rmsg, &rmsg_handle);
994 if (rmsg->h.type != m.h.type) {
995 /* got an unexpected message type in reply */
1000 ret = -rmsg->u.component_destroy_reply.status;
1004 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1009 /* enable a component on vc */
1010 static int enable_component(struct vchiq_mmal_instance *instance,
1011 struct vchiq_mmal_component *component)
1015 struct mmal_msg *rmsg;
1016 struct vchiq_header *rmsg_handle;
1018 m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
1019 m.u.component_enable.component_handle = component->handle;
1021 ret = send_synchronous_mmal_msg(instance, &m,
1022 sizeof(m.u.component_enable),
1023 &rmsg, &rmsg_handle);
1027 if (rmsg->h.type != m.h.type) {
1028 /* got an unexpected message type in reply */
1033 ret = -rmsg->u.component_enable_reply.status;
1036 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1041 /* disable a component on vc */
1042 static int disable_component(struct vchiq_mmal_instance *instance,
1043 struct vchiq_mmal_component *component)
1047 struct mmal_msg *rmsg;
1048 struct vchiq_header *rmsg_handle;
1050 m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
1051 m.u.component_disable.component_handle = component->handle;
1053 ret = send_synchronous_mmal_msg(instance, &m,
1054 sizeof(m.u.component_disable),
1055 &rmsg, &rmsg_handle);
1059 if (rmsg->h.type != m.h.type) {
1060 /* got an unexpected message type in reply */
1065 ret = -rmsg->u.component_disable_reply.status;
1069 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1074 /* get version of mmal implementation */
1075 static int get_version(struct vchiq_mmal_instance *instance,
1076 u32 *major_out, u32 *minor_out)
1080 struct mmal_msg *rmsg;
1081 struct vchiq_header *rmsg_handle;
1083 m.h.type = MMAL_MSG_TYPE_GET_VERSION;
1085 ret = send_synchronous_mmal_msg(instance, &m,
1086 sizeof(m.u.version),
1087 &rmsg, &rmsg_handle);
1091 if (rmsg->h.type != m.h.type) {
1092 /* got an unexpected message type in reply */
1097 *major_out = rmsg->u.version.major;
1098 *minor_out = rmsg->u.version.minor;
1101 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1106 /* do a port action with a port as a parameter */
1107 static int port_action_port(struct vchiq_mmal_instance *instance,
1108 struct vchiq_mmal_port *port,
1109 enum mmal_msg_port_action_type action_type)
1113 struct mmal_msg *rmsg;
1114 struct vchiq_header *rmsg_handle;
1116 m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
1117 m.u.port_action_port.component_handle = port->component->handle;
1118 m.u.port_action_port.port_handle = port->handle;
1119 m.u.port_action_port.action = action_type;
1121 port_to_mmal_msg(port, &m.u.port_action_port.port);
1123 ret = send_synchronous_mmal_msg(instance, &m,
1124 sizeof(m.u.port_action_port),
1125 &rmsg, &rmsg_handle);
1129 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
1130 /* got an unexpected message type in reply */
1135 ret = -rmsg->u.port_action_reply.status;
1137 pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
1139 ret, port->component->handle, port->handle,
1140 port_action_type_names[action_type], action_type);
1143 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1148 /* do a port action with handles as parameters */
1149 static int port_action_handle(struct vchiq_mmal_instance *instance,
1150 struct vchiq_mmal_port *port,
1151 enum mmal_msg_port_action_type action_type,
1152 u32 connect_component_handle,
1153 u32 connect_port_handle)
1157 struct mmal_msg *rmsg;
1158 struct vchiq_header *rmsg_handle;
1160 m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
1162 m.u.port_action_handle.component_handle = port->component->handle;
1163 m.u.port_action_handle.port_handle = port->handle;
1164 m.u.port_action_handle.action = action_type;
1166 m.u.port_action_handle.connect_component_handle =
1167 connect_component_handle;
1168 m.u.port_action_handle.connect_port_handle = connect_port_handle;
1170 ret = send_synchronous_mmal_msg(instance, &m,
1171 sizeof(m.u.port_action_handle),
1172 &rmsg, &rmsg_handle);
1176 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
1177 /* got an unexpected message type in reply */
1182 ret = -rmsg->u.port_action_reply.status;
1184 pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d) connect component:0x%x connect port:%d\n",
1186 ret, port->component->handle, port->handle,
1187 port_action_type_names[action_type],
1188 action_type, connect_component_handle, connect_port_handle);
1191 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1196 static int port_parameter_set(struct vchiq_mmal_instance *instance,
1197 struct vchiq_mmal_port *port,
1198 u32 parameter_id, void *value, u32 value_size)
1202 struct mmal_msg *rmsg;
1203 struct vchiq_header *rmsg_handle;
1205 m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
1207 m.u.port_parameter_set.component_handle = port->component->handle;
1208 m.u.port_parameter_set.port_handle = port->handle;
1209 m.u.port_parameter_set.id = parameter_id;
1210 m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
1211 memcpy(&m.u.port_parameter_set.value, value, value_size);
1213 ret = send_synchronous_mmal_msg(instance, &m,
1214 (4 * sizeof(u32)) + value_size,
1215 &rmsg, &rmsg_handle);
1219 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
1220 /* got an unexpected message type in reply */
1225 ret = -rmsg->u.port_parameter_set_reply.status;
1227 pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
1229 ret, port->component->handle, port->handle, parameter_id);
1232 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1237 static int port_parameter_get(struct vchiq_mmal_instance *instance,
1238 struct vchiq_mmal_port *port,
1239 u32 parameter_id, void *value, u32 *value_size)
1243 struct mmal_msg *rmsg;
1244 struct vchiq_header *rmsg_handle;
1246 m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
1248 m.u.port_parameter_get.component_handle = port->component->handle;
1249 m.u.port_parameter_get.port_handle = port->handle;
1250 m.u.port_parameter_get.id = parameter_id;
1251 m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
1253 ret = send_synchronous_mmal_msg(instance, &m,
1255 mmal_msg_port_parameter_get),
1256 &rmsg, &rmsg_handle);
1260 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
1261 /* got an unexpected message type in reply */
1262 pr_err("Incorrect reply type %d\n", rmsg->h.type);
1267 ret = rmsg->u.port_parameter_get_reply.status;
1269 /* port_parameter_get_reply.size includes the header,
1270 * whilst *value_size doesn't.
1272 rmsg->u.port_parameter_get_reply.size -= (2 * sizeof(u32));
1274 if (ret || rmsg->u.port_parameter_get_reply.size > *value_size) {
1275 /* Copy only as much as we have space for
1276 * but report true size of parameter
1278 memcpy(value, &rmsg->u.port_parameter_get_reply.value,
1281 memcpy(value, &rmsg->u.port_parameter_get_reply.value,
1282 rmsg->u.port_parameter_get_reply.size);
1284 /* Always report the size of the returned parameter to the caller */
1285 *value_size = rmsg->u.port_parameter_get_reply.size;
1287 pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
1288 ret, port->component->handle, port->handle, parameter_id);
1291 vchiq_release_message(instance->vchiq_instance, instance->service_handle, rmsg_handle);
1296 /* disables a port and drains buffers from it */
1297 static int port_disable(struct vchiq_mmal_instance *instance,
1298 struct vchiq_mmal_port *port)
1301 struct list_head *q, *buf_head;
1302 unsigned long flags = 0;
1307 port->enabled = false;
1309 ret = port_action_port(instance, port,
1310 MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
1313 * Drain all queued buffers on port. This should only
1314 * apply to buffers that have been queued before the port
1315 * has been enabled. If the port has been enabled and buffers
1316 * passed, then the buffers should have been removed from this
1317 * list, and we should get the relevant callbacks via VCHIQ
1318 * to release the buffers.
1320 spin_lock_irqsave(&port->slock, flags);
1322 list_for_each_safe(buf_head, q, &port->buffers) {
1323 struct mmal_buffer *mmalbuf;
1325 mmalbuf = list_entry(buf_head, struct mmal_buffer,
1328 if (port->buffer_cb) {
1329 mmalbuf->length = 0;
1330 mmalbuf->mmal_flags = 0;
1331 mmalbuf->dts = MMAL_TIME_UNKNOWN;
1332 mmalbuf->pts = MMAL_TIME_UNKNOWN;
1333 port->buffer_cb(instance,
1338 spin_unlock_irqrestore(&port->slock, flags);
1340 ret = port_info_get(instance, port);
1347 static int port_enable(struct vchiq_mmal_instance *instance,
1348 struct vchiq_mmal_port *port)
1350 unsigned int hdr_count;
1351 struct list_head *q, *buf_head;
1357 ret = port_action_port(instance, port,
1358 MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
1362 port->enabled = true;
1364 if (port->buffer_cb) {
1365 /* send buffer headers to videocore */
1367 list_for_each_safe(buf_head, q, &port->buffers) {
1368 struct mmal_buffer *mmalbuf;
1370 mmalbuf = list_entry(buf_head, struct mmal_buffer,
1372 ret = buffer_from_host(instance, port, mmalbuf);
1378 if (hdr_count > port->current_buffer.num)
1383 ret = port_info_get(instance, port);
1389 /* ------------------------------------------------------------------
1391 *------------------------------------------------------------------
1394 int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
1395 struct vchiq_mmal_port *port)
1399 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1402 ret = port_info_set(instance, port);
1404 goto release_unlock;
1406 /* read what has actually been set */
1407 ret = port_info_get(instance, port);
1410 mutex_unlock(&instance->vchiq_mutex);
1414 EXPORT_SYMBOL_GPL(vchiq_mmal_port_set_format);
1416 int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
1417 struct vchiq_mmal_port *port,
1418 u32 parameter, void *value, u32 value_size)
1422 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1425 ret = port_parameter_set(instance, port, parameter, value, value_size);
1427 mutex_unlock(&instance->vchiq_mutex);
1431 EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set);
1433 int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
1434 struct vchiq_mmal_port *port,
1435 u32 parameter, void *value, u32 *value_size)
1439 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1442 ret = port_parameter_get(instance, port, parameter, value, value_size);
1444 mutex_unlock(&instance->vchiq_mutex);
1448 EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_get);
1452 * enables a port and queues buffers for satisfying callbacks if we
1453 * provide a callback handler
1455 int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
1456 struct vchiq_mmal_port *port,
1457 vchiq_mmal_buffer_cb buffer_cb)
1461 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1464 /* already enabled - noop */
1465 if (port->enabled) {
1470 port->buffer_cb = buffer_cb;
1472 ret = port_enable(instance, port);
1475 mutex_unlock(&instance->vchiq_mutex);
1479 EXPORT_SYMBOL_GPL(vchiq_mmal_port_enable);
1481 int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
1482 struct vchiq_mmal_port *port)
1486 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1489 if (!port->enabled) {
1490 mutex_unlock(&instance->vchiq_mutex);
1494 ret = port_disable(instance, port);
1496 mutex_unlock(&instance->vchiq_mutex);
1500 EXPORT_SYMBOL_GPL(vchiq_mmal_port_disable);
1502 /* ports will be connected in a tunneled manner so data buffers
1503 * are not handled by client.
1505 int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
1506 struct vchiq_mmal_port *src,
1507 struct vchiq_mmal_port *dst)
1511 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1514 /* disconnect ports if connected */
1515 if (src->connected) {
1516 ret = port_disable(instance, src);
1518 pr_err("failed disabling src port(%d)\n", ret);
1519 goto release_unlock;
1522 /* do not need to disable the destination port as they
1523 * are connected and it is done automatically
1526 ret = port_action_handle(instance, src,
1527 MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
1528 src->connected->component->handle,
1529 src->connected->handle);
1531 pr_err("failed disconnecting src port\n");
1532 goto release_unlock;
1534 src->connected->enabled = false;
1535 src->connected = NULL;
1539 /* do not make new connection */
1541 pr_debug("not making new connection\n");
1542 goto release_unlock;
1545 /* copy src port format to dst */
1546 dst->format.encoding = src->format.encoding;
1547 dst->es.video.width = src->es.video.width;
1548 dst->es.video.height = src->es.video.height;
1549 dst->es.video.crop.x = src->es.video.crop.x;
1550 dst->es.video.crop.y = src->es.video.crop.y;
1551 dst->es.video.crop.width = src->es.video.crop.width;
1552 dst->es.video.crop.height = src->es.video.crop.height;
1553 dst->es.video.frame_rate.numerator = src->es.video.frame_rate.numerator;
1554 dst->es.video.frame_rate.denominator = src->es.video.frame_rate.denominator;
1556 /* set new format */
1557 ret = port_info_set(instance, dst);
1559 pr_debug("setting port info failed\n");
1560 goto release_unlock;
1563 /* read what has actually been set */
1564 ret = port_info_get(instance, dst);
1566 pr_debug("read back port info failed\n");
1567 goto release_unlock;
1570 /* connect two ports together */
1571 ret = port_action_handle(instance, src,
1572 MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
1573 dst->component->handle, dst->handle);
1575 pr_debug("connecting port %d:%d to %d:%d failed\n",
1576 src->component->handle, src->handle,
1577 dst->component->handle, dst->handle);
1578 goto release_unlock;
1580 src->connected = dst;
1584 mutex_unlock(&instance->vchiq_mutex);
1588 EXPORT_SYMBOL_GPL(vchiq_mmal_port_connect_tunnel);
1590 int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
1591 struct vchiq_mmal_port *port,
1592 struct mmal_buffer *buffer)
1594 unsigned long flags = 0;
1597 ret = buffer_from_host(instance, port, buffer);
1598 if (ret == -EINVAL) {
1599 /* Port is disabled. Queue for when it is enabled. */
1600 spin_lock_irqsave(&port->slock, flags);
1601 list_add_tail(&buffer->list, &port->buffers);
1602 spin_unlock_irqrestore(&port->slock, flags);
1607 EXPORT_SYMBOL_GPL(vchiq_mmal_submit_buffer);
1609 int mmal_vchi_buffer_init(struct vchiq_mmal_instance *instance,
1610 struct mmal_buffer *buf)
1612 struct mmal_msg_context *msg_context = get_msg_context(instance);
1614 if (IS_ERR(msg_context))
1615 return (PTR_ERR(msg_context));
1617 buf->msg_context = msg_context;
1620 EXPORT_SYMBOL_GPL(mmal_vchi_buffer_init);
1622 int mmal_vchi_buffer_cleanup(struct mmal_buffer *buf)
1624 struct mmal_msg_context *msg_context = buf->msg_context;
1627 release_msg_context(msg_context);
1628 buf->msg_context = NULL;
1632 EXPORT_SYMBOL_GPL(mmal_vchi_buffer_cleanup);
1634 /* Initialise a mmal component and its ports
1637 int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
1639 struct vchiq_mmal_component **component_out)
1642 int idx; /* port index */
1643 struct vchiq_mmal_component *component = NULL;
1645 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1648 for (idx = 0; idx < VCHIQ_MMAL_MAX_COMPONENTS; idx++) {
1649 if (!instance->component[idx].in_use) {
1650 component = &instance->component[idx];
1651 component->in_use = true;
1657 ret = -EINVAL; /* todo is this correct error? */
1661 /* We need a handle to reference back to our component structure.
1662 * Use the array index in instance->component rather than rolling
1665 component->client_component = idx;
1667 ret = create_component(instance, component, name);
1669 pr_err("%s: failed to create component %d (Not enough GPU mem?)\n",
1674 /* ports info needs gathering */
1675 component->control.type = MMAL_PORT_TYPE_CONTROL;
1676 component->control.index = 0;
1677 component->control.component = component;
1678 spin_lock_init(&component->control.slock);
1679 INIT_LIST_HEAD(&component->control.buffers);
1680 ret = port_info_get(instance, &component->control);
1682 goto release_component;
1684 for (idx = 0; idx < component->inputs; idx++) {
1685 component->input[idx].type = MMAL_PORT_TYPE_INPUT;
1686 component->input[idx].index = idx;
1687 component->input[idx].component = component;
1688 spin_lock_init(&component->input[idx].slock);
1689 INIT_LIST_HEAD(&component->input[idx].buffers);
1690 ret = port_info_get(instance, &component->input[idx]);
1692 goto release_component;
1695 for (idx = 0; idx < component->outputs; idx++) {
1696 component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
1697 component->output[idx].index = idx;
1698 component->output[idx].component = component;
1699 spin_lock_init(&component->output[idx].slock);
1700 INIT_LIST_HEAD(&component->output[idx].buffers);
1701 ret = port_info_get(instance, &component->output[idx]);
1703 goto release_component;
1706 for (idx = 0; idx < component->clocks; idx++) {
1707 component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
1708 component->clock[idx].index = idx;
1709 component->clock[idx].component = component;
1710 spin_lock_init(&component->clock[idx].slock);
1711 INIT_LIST_HEAD(&component->clock[idx].buffers);
1712 ret = port_info_get(instance, &component->clock[idx]);
1714 goto release_component;
1717 *component_out = component;
1719 mutex_unlock(&instance->vchiq_mutex);
1724 destroy_component(instance, component);
1727 component->in_use = false;
1728 mutex_unlock(&instance->vchiq_mutex);
1732 EXPORT_SYMBOL_GPL(vchiq_mmal_component_init);
1735 * cause a mmal component to be destroyed
1737 int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
1738 struct vchiq_mmal_component *component)
1742 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1745 if (component->enabled)
1746 ret = disable_component(instance, component);
1748 ret = destroy_component(instance, component);
1750 component->in_use = false;
1752 mutex_unlock(&instance->vchiq_mutex);
1756 EXPORT_SYMBOL_GPL(vchiq_mmal_component_finalise);
1759 * cause a mmal component to be enabled
1761 int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
1762 struct vchiq_mmal_component *component)
1766 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1769 if (component->enabled) {
1770 mutex_unlock(&instance->vchiq_mutex);
1774 ret = enable_component(instance, component);
1776 component->enabled = true;
1778 mutex_unlock(&instance->vchiq_mutex);
1782 EXPORT_SYMBOL_GPL(vchiq_mmal_component_enable);
1785 * cause a mmal component to be enabled
1787 int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
1788 struct vchiq_mmal_component *component)
1792 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1795 if (!component->enabled) {
1796 mutex_unlock(&instance->vchiq_mutex);
1800 ret = disable_component(instance, component);
1802 component->enabled = false;
1804 mutex_unlock(&instance->vchiq_mutex);
1808 EXPORT_SYMBOL_GPL(vchiq_mmal_component_disable);
1810 int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
1811 u32 *major_out, u32 *minor_out)
1815 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1818 ret = get_version(instance, major_out, minor_out);
1820 mutex_unlock(&instance->vchiq_mutex);
1824 EXPORT_SYMBOL_GPL(vchiq_mmal_version);
1826 int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
1833 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1836 vchiq_use_service(instance->vchiq_instance, instance->service_handle);
1838 status = vchiq_close_service(instance->vchiq_instance, instance->service_handle);
1840 pr_err("mmal-vchiq: VCHIQ close failed\n");
1842 mutex_unlock(&instance->vchiq_mutex);
1844 vchiq_shutdown(instance->vchiq_instance);
1845 destroy_workqueue(instance->bulk_wq);
1847 idr_destroy(&instance->context_map);
1853 EXPORT_SYMBOL_GPL(vchiq_mmal_finalise);
1855 int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
1859 struct vchiq_mmal_instance *instance;
1860 struct vchiq_instance *vchiq_instance;
1861 struct vchiq_service_params_kernel params = {
1862 .version = VC_MMAL_VER,
1863 .version_min = VC_MMAL_MIN_VER,
1864 .fourcc = VCHIQ_MAKE_FOURCC('m', 'm', 'a', 'l'),
1865 .callback = service_callback,
1869 /* compile time checks to ensure structure size as they are
1870 * directly (de)serialised from memory.
1873 /* ensure the header structure has packed to the correct size */
1874 BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
1876 /* ensure message structure does not exceed maximum length */
1877 BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
1879 /* mmal port struct is correct size */
1880 BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
1882 /* create a vchi instance */
1883 status = vchiq_initialise(&vchiq_instance);
1885 pr_err("Failed to initialise VCHI instance (status=%d)\n",
1890 status = vchiq_connect(vchiq_instance);
1892 pr_err("Failed to connect VCHI instance (status=%d)\n", status);
1894 goto err_shutdown_vchiq;
1897 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1901 goto err_shutdown_vchiq;
1904 mutex_init(&instance->vchiq_mutex);
1906 instance->vchiq_instance = vchiq_instance;
1908 mutex_init(&instance->context_map_lock);
1909 idr_init_base(&instance->context_map, 1);
1911 params.userdata = instance;
1913 instance->bulk_wq = alloc_ordered_workqueue("mmal-vchiq",
1915 if (!instance->bulk_wq)
1918 status = vchiq_open_service(vchiq_instance, ¶ms,
1919 &instance->service_handle);
1921 pr_err("Failed to open VCHI service connection (status=%d)\n",
1923 goto err_close_services;
1926 vchiq_release_service(instance->vchiq_instance, instance->service_handle);
1928 *out_instance = instance;
1933 vchiq_close_service(instance->vchiq_instance, instance->service_handle);
1934 destroy_workqueue(instance->bulk_wq);
1938 vchiq_shutdown(vchiq_instance);
1941 EXPORT_SYMBOL_GPL(vchiq_mmal_init);
1943 MODULE_DESCRIPTION("BCM2835 MMAL VCHIQ interface");
1944 MODULE_AUTHOR("Dave Stevenson, <dave.stevenson@raspberrypi.org>");
1945 MODULE_LICENSE("GPL");