1 // SPDX-License-Identifier: GPL-2.0
3 * Broadcom BM2835 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 <linux/raspberrypi/vchiq.h>
27 #include <media/videobuf2-vmalloc.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 /* vmalloc page to receive scratch bulk xfers into */
174 struct idr context_map;
175 /* protect accesses to context_map */
176 struct mutex context_map_lock;
178 struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
180 /* ordered workqueue to process all bulk operations */
181 struct workqueue_struct *bulk_wq;
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 witin 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->service_handle);
296 ret = vchiq_bulk_receive(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->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 sepecific 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->service_handle);
441 ret = vchiq_queue_kernel_message(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->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 enum vchiq_status service_callback(enum vchiq_reason reason,
552 struct vchiq_header *header,
553 unsigned int handle, void *bulk_ctx)
555 struct vchiq_mmal_instance *instance = vchiq_get_service_userdata(handle);
557 struct mmal_msg *msg;
558 struct mmal_msg_context *msg_context;
561 pr_err("Message callback passed NULL instance\n");
562 return VCHIQ_SUCCESS;
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(handle, header);
578 case MMAL_MSG_TYPE_EVENT_TO_HOST:
579 event_to_host_cb(instance, msg, msg_len);
580 vchiq_release_message(handle, header);
584 case MMAL_MSG_TYPE_BUFFER_TO_HOST:
585 buffer_to_host_cb(instance, msg, msg_len);
586 vchiq_release_message(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(handle, header);
597 msg_context = lookup_msg_context(instance,
600 pr_err("received invalid message context %u!\n",
602 vchiq_release_message(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);
646 return VCHIQ_SUCCESS;
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->service_handle);
683 ret = vchiq_queue_kernel_message(instance->service_handle, msg,
684 sizeof(struct mmal_msg_header) +
687 vchiq_release_service(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.num,
748 port->es.video.frame_rate.den,
749 port->es.video.par.num, port->es.video.par.den);
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->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)
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->service_handle, rmsg_handle);
927 /* create comonent 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 strncpy(m.u.component_create.name, name,
941 sizeof(m.u.component_create.name));
943 ret = send_synchronous_mmal_msg(instance, &m,
944 sizeof(m.u.component_create),
945 &rmsg, &rmsg_handle);
949 if (rmsg->h.type != m.h.type) {
950 /* got an unexpected message type in reply */
955 ret = -rmsg->u.component_create_reply.status;
956 if (ret != MMAL_MSG_STATUS_SUCCESS)
959 /* a valid component response received */
960 component->handle = rmsg->u.component_create_reply.component_handle;
961 component->inputs = rmsg->u.component_create_reply.input_num;
962 component->outputs = rmsg->u.component_create_reply.output_num;
963 component->clocks = rmsg->u.component_create_reply.clock_num;
965 pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
967 component->inputs, component->outputs, component->clocks);
970 vchiq_release_message(instance->service_handle, rmsg_handle);
975 /* destroys a component on vc */
976 static int destroy_component(struct vchiq_mmal_instance *instance,
977 struct vchiq_mmal_component *component)
981 struct mmal_msg *rmsg;
982 struct vchiq_header *rmsg_handle;
984 m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
985 m.u.component_destroy.component_handle = component->handle;
987 ret = send_synchronous_mmal_msg(instance, &m,
988 sizeof(m.u.component_destroy),
989 &rmsg, &rmsg_handle);
993 if (rmsg->h.type != m.h.type) {
994 /* got an unexpected message type in reply */
999 ret = -rmsg->u.component_destroy_reply.status;
1003 vchiq_release_message(instance->service_handle, rmsg_handle);
1008 /* enable a component on vc */
1009 static int enable_component(struct vchiq_mmal_instance *instance,
1010 struct vchiq_mmal_component *component)
1014 struct mmal_msg *rmsg;
1015 struct vchiq_header *rmsg_handle;
1017 m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
1018 m.u.component_enable.component_handle = component->handle;
1020 ret = send_synchronous_mmal_msg(instance, &m,
1021 sizeof(m.u.component_enable),
1022 &rmsg, &rmsg_handle);
1026 if (rmsg->h.type != m.h.type) {
1027 /* got an unexpected message type in reply */
1032 ret = -rmsg->u.component_enable_reply.status;
1035 vchiq_release_message(instance->service_handle, rmsg_handle);
1040 /* disable a component on vc */
1041 static int disable_component(struct vchiq_mmal_instance *instance,
1042 struct vchiq_mmal_component *component)
1046 struct mmal_msg *rmsg;
1047 struct vchiq_header *rmsg_handle;
1049 m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
1050 m.u.component_disable.component_handle = component->handle;
1052 ret = send_synchronous_mmal_msg(instance, &m,
1053 sizeof(m.u.component_disable),
1054 &rmsg, &rmsg_handle);
1058 if (rmsg->h.type != m.h.type) {
1059 /* got an unexpected message type in reply */
1064 ret = -rmsg->u.component_disable_reply.status;
1068 vchiq_release_message(instance->service_handle, rmsg_handle);
1073 /* get version of mmal implementation */
1074 static int get_version(struct vchiq_mmal_instance *instance,
1075 u32 *major_out, u32 *minor_out)
1079 struct mmal_msg *rmsg;
1080 struct vchiq_header *rmsg_handle;
1082 m.h.type = MMAL_MSG_TYPE_GET_VERSION;
1084 ret = send_synchronous_mmal_msg(instance, &m,
1085 sizeof(m.u.version),
1086 &rmsg, &rmsg_handle);
1090 if (rmsg->h.type != m.h.type) {
1091 /* got an unexpected message type in reply */
1096 *major_out = rmsg->u.version.major;
1097 *minor_out = rmsg->u.version.minor;
1100 vchiq_release_message(instance->service_handle, rmsg_handle);
1105 /* do a port action with a port as a parameter */
1106 static int port_action_port(struct vchiq_mmal_instance *instance,
1107 struct vchiq_mmal_port *port,
1108 enum mmal_msg_port_action_type action_type)
1112 struct mmal_msg *rmsg;
1113 struct vchiq_header *rmsg_handle;
1115 m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
1116 m.u.port_action_port.component_handle = port->component->handle;
1117 m.u.port_action_port.port_handle = port->handle;
1118 m.u.port_action_port.action = action_type;
1120 port_to_mmal_msg(port, &m.u.port_action_port.port);
1122 ret = send_synchronous_mmal_msg(instance, &m,
1123 sizeof(m.u.port_action_port),
1124 &rmsg, &rmsg_handle);
1128 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
1129 /* got an unexpected message type in reply */
1134 ret = -rmsg->u.port_action_reply.status;
1136 pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
1138 ret, port->component->handle, port->handle,
1139 port_action_type_names[action_type], action_type);
1142 vchiq_release_message(instance->service_handle, rmsg_handle);
1147 /* do a port action with handles as parameters */
1148 static int port_action_handle(struct vchiq_mmal_instance *instance,
1149 struct vchiq_mmal_port *port,
1150 enum mmal_msg_port_action_type action_type,
1151 u32 connect_component_handle,
1152 u32 connect_port_handle)
1156 struct mmal_msg *rmsg;
1157 struct vchiq_header *rmsg_handle;
1159 m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
1161 m.u.port_action_handle.component_handle = port->component->handle;
1162 m.u.port_action_handle.port_handle = port->handle;
1163 m.u.port_action_handle.action = action_type;
1165 m.u.port_action_handle.connect_component_handle =
1166 connect_component_handle;
1167 m.u.port_action_handle.connect_port_handle = connect_port_handle;
1169 ret = send_synchronous_mmal_msg(instance, &m,
1170 sizeof(m.u.port_action_handle),
1171 &rmsg, &rmsg_handle);
1175 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
1176 /* got an unexpected message type in reply */
1181 ret = -rmsg->u.port_action_reply.status;
1183 pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d) connect component:0x%x connect port:%d\n",
1185 ret, port->component->handle, port->handle,
1186 port_action_type_names[action_type],
1187 action_type, connect_component_handle, connect_port_handle);
1190 vchiq_release_message(instance->service_handle, rmsg_handle);
1195 static int port_parameter_set(struct vchiq_mmal_instance *instance,
1196 struct vchiq_mmal_port *port,
1197 u32 parameter_id, void *value, u32 value_size)
1201 struct mmal_msg *rmsg;
1202 struct vchiq_header *rmsg_handle;
1204 m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
1206 m.u.port_parameter_set.component_handle = port->component->handle;
1207 m.u.port_parameter_set.port_handle = port->handle;
1208 m.u.port_parameter_set.id = parameter_id;
1209 m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
1210 memcpy(&m.u.port_parameter_set.value, value, value_size);
1212 ret = send_synchronous_mmal_msg(instance, &m,
1213 (4 * sizeof(u32)) + value_size,
1214 &rmsg, &rmsg_handle);
1218 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
1219 /* got an unexpected message type in reply */
1224 ret = -rmsg->u.port_parameter_set_reply.status;
1226 pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
1228 ret, port->component->handle, port->handle, parameter_id);
1231 vchiq_release_message(instance->service_handle, rmsg_handle);
1236 static int port_parameter_get(struct vchiq_mmal_instance *instance,
1237 struct vchiq_mmal_port *port,
1238 u32 parameter_id, void *value, u32 *value_size)
1242 struct mmal_msg *rmsg;
1243 struct vchiq_header *rmsg_handle;
1245 m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
1247 m.u.port_parameter_get.component_handle = port->component->handle;
1248 m.u.port_parameter_get.port_handle = port->handle;
1249 m.u.port_parameter_get.id = parameter_id;
1250 m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
1252 ret = send_synchronous_mmal_msg(instance, &m,
1254 mmal_msg_port_parameter_get),
1255 &rmsg, &rmsg_handle);
1259 if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
1260 /* got an unexpected message type in reply */
1261 pr_err("Incorrect reply type %d\n", rmsg->h.type);
1266 ret = rmsg->u.port_parameter_get_reply.status;
1268 /* port_parameter_get_reply.size includes the header,
1269 * whilst *value_size doesn't.
1271 rmsg->u.port_parameter_get_reply.size -= (2 * sizeof(u32));
1273 if (ret || rmsg->u.port_parameter_get_reply.size > *value_size) {
1274 /* Copy only as much as we have space for
1275 * but report true size of parameter
1277 memcpy(value, &rmsg->u.port_parameter_get_reply.value,
1280 memcpy(value, &rmsg->u.port_parameter_get_reply.value,
1281 rmsg->u.port_parameter_get_reply.size);
1283 /* Always report the size of the returned parameter to the caller */
1284 *value_size = rmsg->u.port_parameter_get_reply.size;
1286 pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
1287 ret, port->component->handle, port->handle, parameter_id);
1290 vchiq_release_message(instance->service_handle, rmsg_handle);
1295 /* disables a port and drains buffers from it */
1296 static int port_disable(struct vchiq_mmal_instance *instance,
1297 struct vchiq_mmal_port *port)
1300 struct list_head *q, *buf_head;
1301 unsigned long flags = 0;
1308 ret = port_action_port(instance, port,
1309 MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
1312 * Drain all queued buffers on port. This should only
1313 * apply to buffers that have been queued before the port
1314 * has been enabled. If the port has been enabled and buffers
1315 * passed, then the buffers should have been removed from this
1316 * list, and we should get the relevant callbacks via VCHIQ
1317 * to release the buffers.
1319 spin_lock_irqsave(&port->slock, flags);
1321 list_for_each_safe(buf_head, q, &port->buffers) {
1322 struct mmal_buffer *mmalbuf;
1324 mmalbuf = list_entry(buf_head, struct mmal_buffer,
1327 if (port->buffer_cb) {
1328 mmalbuf->length = 0;
1329 mmalbuf->mmal_flags = 0;
1330 mmalbuf->dts = MMAL_TIME_UNKNOWN;
1331 mmalbuf->pts = MMAL_TIME_UNKNOWN;
1332 port->buffer_cb(instance,
1337 spin_unlock_irqrestore(&port->slock, flags);
1339 ret = port_info_get(instance, port);
1346 static int port_enable(struct vchiq_mmal_instance *instance,
1347 struct vchiq_mmal_port *port)
1349 unsigned int hdr_count;
1350 struct list_head *q, *buf_head;
1356 ret = port_action_port(instance, port,
1357 MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
1363 if (port->buffer_cb) {
1364 /* send buffer headers to videocore */
1366 list_for_each_safe(buf_head, q, &port->buffers) {
1367 struct mmal_buffer *mmalbuf;
1369 mmalbuf = list_entry(buf_head, struct mmal_buffer,
1371 ret = buffer_from_host(instance, port, mmalbuf);
1377 if (hdr_count > port->current_buffer.num)
1382 ret = port_info_get(instance, port);
1388 /* ------------------------------------------------------------------
1390 *------------------------------------------------------------------
1393 int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
1394 struct vchiq_mmal_port *port)
1398 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1401 ret = port_info_set(instance, port);
1403 goto release_unlock;
1405 /* read what has actually been set */
1406 ret = port_info_get(instance, port);
1409 mutex_unlock(&instance->vchiq_mutex);
1413 EXPORT_SYMBOL_GPL(vchiq_mmal_port_set_format);
1415 int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
1416 struct vchiq_mmal_port *port,
1417 u32 parameter, void *value, u32 value_size)
1421 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1424 ret = port_parameter_set(instance, port, parameter, value, value_size);
1426 mutex_unlock(&instance->vchiq_mutex);
1430 EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set);
1432 int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
1433 struct vchiq_mmal_port *port,
1434 u32 parameter, void *value, u32 *value_size)
1438 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1441 ret = port_parameter_get(instance, port, parameter, value, value_size);
1443 mutex_unlock(&instance->vchiq_mutex);
1447 EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_get);
1451 * enables a port and queues buffers for satisfying callbacks if we
1452 * provide a callback handler
1454 int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
1455 struct vchiq_mmal_port *port,
1456 vchiq_mmal_buffer_cb buffer_cb)
1460 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1463 /* already enabled - noop */
1464 if (port->enabled) {
1469 port->buffer_cb = buffer_cb;
1471 ret = port_enable(instance, port);
1474 mutex_unlock(&instance->vchiq_mutex);
1478 EXPORT_SYMBOL_GPL(vchiq_mmal_port_enable);
1480 int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
1481 struct vchiq_mmal_port *port)
1485 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1488 if (!port->enabled) {
1489 mutex_unlock(&instance->vchiq_mutex);
1493 ret = port_disable(instance, port);
1495 mutex_unlock(&instance->vchiq_mutex);
1499 EXPORT_SYMBOL_GPL(vchiq_mmal_port_disable);
1501 /* ports will be connected in a tunneled manner so data buffers
1502 * are not handled by client.
1504 int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
1505 struct vchiq_mmal_port *src,
1506 struct vchiq_mmal_port *dst)
1510 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1513 /* disconnect ports if connected */
1514 if (src->connected) {
1515 ret = port_disable(instance, src);
1517 pr_err("failed disabling src port(%d)\n", ret);
1518 goto release_unlock;
1521 /* do not need to disable the destination port as they
1522 * are connected and it is done automatically
1525 ret = port_action_handle(instance, src,
1526 MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
1527 src->connected->component->handle,
1528 src->connected->handle);
1530 pr_err("failed disconnecting src port\n");
1531 goto release_unlock;
1533 src->connected->enabled = 0;
1534 src->connected = NULL;
1538 /* do not make new connection */
1540 pr_debug("not making new connection\n");
1541 goto release_unlock;
1544 /* copy src port format to dst */
1545 dst->format.encoding = src->format.encoding;
1546 dst->es.video.width = src->es.video.width;
1547 dst->es.video.height = src->es.video.height;
1548 dst->es.video.crop.x = src->es.video.crop.x;
1549 dst->es.video.crop.y = src->es.video.crop.y;
1550 dst->es.video.crop.width = src->es.video.crop.width;
1551 dst->es.video.crop.height = src->es.video.crop.height;
1552 dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
1553 dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
1555 /* set new format */
1556 ret = port_info_set(instance, dst);
1558 pr_debug("setting port info failed\n");
1559 goto release_unlock;
1562 /* read what has actually been set */
1563 ret = port_info_get(instance, dst);
1565 pr_debug("read back port info failed\n");
1566 goto release_unlock;
1569 /* connect two ports together */
1570 ret = port_action_handle(instance, src,
1571 MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
1572 dst->component->handle, dst->handle);
1574 pr_debug("connecting port %d:%d to %d:%d failed\n",
1575 src->component->handle, src->handle,
1576 dst->component->handle, dst->handle);
1577 goto release_unlock;
1579 src->connected = dst;
1583 mutex_unlock(&instance->vchiq_mutex);
1587 EXPORT_SYMBOL_GPL(vchiq_mmal_port_connect_tunnel);
1589 int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
1590 struct vchiq_mmal_port *port,
1591 struct mmal_buffer *buffer)
1593 unsigned long flags = 0;
1596 ret = buffer_from_host(instance, port, buffer);
1597 if (ret == -EINVAL) {
1598 /* Port is disabled. Queue for when it is enabled. */
1599 spin_lock_irqsave(&port->slock, flags);
1600 list_add_tail(&buffer->list, &port->buffers);
1601 spin_unlock_irqrestore(&port->slock, flags);
1606 EXPORT_SYMBOL_GPL(vchiq_mmal_submit_buffer);
1608 int mmal_vchi_buffer_init(struct vchiq_mmal_instance *instance,
1609 struct mmal_buffer *buf)
1611 struct mmal_msg_context *msg_context = get_msg_context(instance);
1613 if (IS_ERR(msg_context))
1614 return (PTR_ERR(msg_context));
1616 buf->msg_context = msg_context;
1619 EXPORT_SYMBOL_GPL(mmal_vchi_buffer_init);
1621 int mmal_vchi_buffer_cleanup(struct mmal_buffer *buf)
1623 struct mmal_msg_context *msg_context = buf->msg_context;
1626 release_msg_context(msg_context);
1627 buf->msg_context = NULL;
1631 EXPORT_SYMBOL_GPL(mmal_vchi_buffer_cleanup);
1633 /* Initialise a mmal component and its ports
1636 int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
1638 struct vchiq_mmal_component **component_out)
1641 int idx; /* port index */
1642 struct vchiq_mmal_component *component = NULL;
1644 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1647 for (idx = 0; idx < VCHIQ_MMAL_MAX_COMPONENTS; idx++) {
1648 if (!instance->component[idx].in_use) {
1649 component = &instance->component[idx];
1650 component->in_use = 1;
1656 ret = -EINVAL; /* todo is this correct error? */
1660 /* We need a handle to reference back to our component structure.
1661 * Use the array index in instance->component rather than rolling
1664 component->client_component = idx;
1666 ret = create_component(instance, component, name);
1668 pr_err("%s: failed to create component %d (Not enough GPU mem?)\n",
1673 /* ports info needs gathering */
1674 component->control.type = MMAL_PORT_TYPE_CONTROL;
1675 component->control.index = 0;
1676 component->control.component = component;
1677 spin_lock_init(&component->control.slock);
1678 INIT_LIST_HEAD(&component->control.buffers);
1679 ret = port_info_get(instance, &component->control);
1681 goto release_component;
1683 for (idx = 0; idx < component->inputs; idx++) {
1684 component->input[idx].type = MMAL_PORT_TYPE_INPUT;
1685 component->input[idx].index = idx;
1686 component->input[idx].component = component;
1687 spin_lock_init(&component->input[idx].slock);
1688 INIT_LIST_HEAD(&component->input[idx].buffers);
1689 ret = port_info_get(instance, &component->input[idx]);
1691 goto release_component;
1694 for (idx = 0; idx < component->outputs; idx++) {
1695 component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
1696 component->output[idx].index = idx;
1697 component->output[idx].component = component;
1698 spin_lock_init(&component->output[idx].slock);
1699 INIT_LIST_HEAD(&component->output[idx].buffers);
1700 ret = port_info_get(instance, &component->output[idx]);
1702 goto release_component;
1705 for (idx = 0; idx < component->clocks; idx++) {
1706 component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
1707 component->clock[idx].index = idx;
1708 component->clock[idx].component = component;
1709 spin_lock_init(&component->clock[idx].slock);
1710 INIT_LIST_HEAD(&component->clock[idx].buffers);
1711 ret = port_info_get(instance, &component->clock[idx]);
1713 goto release_component;
1716 *component_out = component;
1718 mutex_unlock(&instance->vchiq_mutex);
1723 destroy_component(instance, component);
1726 component->in_use = 0;
1727 mutex_unlock(&instance->vchiq_mutex);
1731 EXPORT_SYMBOL_GPL(vchiq_mmal_component_init);
1734 * cause a mmal component to be destroyed
1736 int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
1737 struct vchiq_mmal_component *component)
1741 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1744 if (component->enabled)
1745 ret = disable_component(instance, component);
1747 ret = destroy_component(instance, component);
1749 component->in_use = 0;
1751 mutex_unlock(&instance->vchiq_mutex);
1755 EXPORT_SYMBOL_GPL(vchiq_mmal_component_finalise);
1758 * cause a mmal component to be enabled
1760 int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
1761 struct vchiq_mmal_component *component)
1765 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1768 if (component->enabled) {
1769 mutex_unlock(&instance->vchiq_mutex);
1773 ret = enable_component(instance, component);
1775 component->enabled = true;
1777 mutex_unlock(&instance->vchiq_mutex);
1781 EXPORT_SYMBOL_GPL(vchiq_mmal_component_enable);
1784 * cause a mmal component to be enabled
1786 int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
1787 struct vchiq_mmal_component *component)
1791 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1794 if (!component->enabled) {
1795 mutex_unlock(&instance->vchiq_mutex);
1799 ret = disable_component(instance, component);
1801 component->enabled = 0;
1803 mutex_unlock(&instance->vchiq_mutex);
1807 EXPORT_SYMBOL_GPL(vchiq_mmal_component_disable);
1809 int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
1810 u32 *major_out, u32 *minor_out)
1814 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1817 ret = get_version(instance, major_out, minor_out);
1819 mutex_unlock(&instance->vchiq_mutex);
1823 EXPORT_SYMBOL_GPL(vchiq_mmal_version);
1825 int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
1832 if (mutex_lock_interruptible(&instance->vchiq_mutex))
1835 vchiq_use_service(instance->service_handle);
1837 status = vchiq_close_service(instance->service_handle);
1839 pr_err("mmal-vchiq: VCHIQ close failed\n");
1841 mutex_unlock(&instance->vchiq_mutex);
1843 flush_workqueue(instance->bulk_wq);
1844 destroy_workqueue(instance->bulk_wq);
1846 vfree(instance->bulk_scratch);
1848 idr_destroy(&instance->context_map);
1854 EXPORT_SYMBOL_GPL(vchiq_mmal_finalise);
1856 int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
1859 struct vchiq_mmal_instance *instance;
1860 static struct vchiq_instance *vchiq_instance;
1861 struct vchiq_service_params 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);
1896 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1901 mutex_init(&instance->vchiq_mutex);
1903 instance->bulk_scratch = vmalloc(PAGE_SIZE);
1905 mutex_init(&instance->context_map_lock);
1906 idr_init_base(&instance->context_map, 1);
1908 params.userdata = instance;
1910 instance->bulk_wq = alloc_ordered_workqueue("mmal-vchiq",
1912 if (!instance->bulk_wq)
1915 status = vchiq_open_service(vchiq_instance, ¶ms,
1916 &instance->service_handle);
1918 pr_err("Failed to open VCHI service connection (status=%d)\n",
1920 goto err_close_services;
1923 vchiq_release_service(instance->service_handle);
1925 *out_instance = instance;
1930 vchiq_close_service(instance->service_handle);
1931 destroy_workqueue(instance->bulk_wq);
1933 vfree(instance->bulk_scratch);
1937 EXPORT_SYMBOL_GPL(vchiq_mmal_init);
1939 MODULE_DESCRIPTION("BCM2835 MMAL VCHIQ interface");
1940 MODULE_AUTHOR("Dave Stevenson, <dave.stevenson@raspberrypi.org>");
1941 MODULE_LICENSE("GPL");