1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
4 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/sched/signal.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/cdev.h>
14 #include <linux/device.h>
16 #include <linux/highmem.h>
17 #include <linux/pagemap.h>
18 #include <linux/bug.h>
19 #include <linux/completion.h>
20 #include <linux/list.h>
22 #include <linux/platform_device.h>
23 #include <linux/compat.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/rcupdate.h>
26 #include <soc/bcm2835/raspberrypi-firmware.h>
28 #include "vchiq_core.h"
29 #include "vchiq_ioctl.h"
30 #include "vchiq_arm.h"
31 #include "vchiq_debugfs.h"
33 #define DEVICE_NAME "vchiq"
35 /* Override the default prefix, which would be vchiq_arm (from the filename) */
36 #undef MODULE_PARAM_PREFIX
37 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
39 /* Some per-instance constants */
40 #define MAX_COMPLETIONS 128
41 #define MAX_SERVICES 64
42 #define MAX_ELEMENTS 8
43 #define MSG_QUEUE_SIZE 128
45 #define KEEPALIVE_VER 1
46 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
48 /* Run time control of log level, based on KERN_XXX level. */
49 int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
50 int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
53 struct vchiq_service *service;
55 struct vchiq_instance *instance;
59 int message_available_pos;
62 struct completion insert_event;
63 struct completion remove_event;
64 struct completion close_event;
65 struct vchiq_header *msg_queue[MSG_QUEUE_SIZE];
68 struct bulk_waiter_node {
69 struct bulk_waiter bulk_waiter;
71 struct list_head list;
74 struct vchiq_instance {
75 struct vchiq_state *state;
76 struct vchiq_completion_data completions[MAX_COMPLETIONS];
77 int completion_insert;
78 int completion_remove;
79 struct completion insert_event;
80 struct completion remove_event;
81 struct mutex completion_mutex;
87 int use_close_delivered;
90 struct list_head bulk_waiter_list;
91 struct mutex bulk_waiter_list_mutex;
93 struct vchiq_debugfs_node debugfs_node;
103 static struct cdev vchiq_cdev;
104 static dev_t vchiq_devid;
105 static struct vchiq_state g_state;
106 static struct class *vchiq_class;
107 static DEFINE_SPINLOCK(msg_queue_spinlock);
108 static struct platform_device *bcm2835_camera;
109 static struct platform_device *bcm2835_audio;
111 static struct vchiq_drvdata bcm2835_drvdata = {
112 .cache_line_size = 32,
115 static struct vchiq_drvdata bcm2836_drvdata = {
116 .cache_line_size = 64,
119 static const char *const ioctl_names[] = {
125 "QUEUE_BULK_TRANSMIT",
126 "QUEUE_BULK_RECEIVE",
134 "SET_SERVICE_OPTION",
140 vchiq_static_assert(ARRAY_SIZE(ioctl_names) ==
141 (VCHIQ_IOC_MAX + 1));
143 static enum vchiq_status
144 vchiq_blocking_bulk_transfer(unsigned int handle, void *data,
145 unsigned int size, enum vchiq_bulk_dir dir);
147 #define VCHIQ_INIT_RETRIES 10
148 enum vchiq_status vchiq_initialise(struct vchiq_instance **instance_out)
150 enum vchiq_status status = VCHIQ_ERROR;
151 struct vchiq_state *state;
152 struct vchiq_instance *instance = NULL;
155 vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
157 /* VideoCore may not be ready due to boot up timing.
158 * It may never be ready if kernel and firmware are mismatched,so don't
161 for (i = 0; i < VCHIQ_INIT_RETRIES; i++) {
162 state = vchiq_get_state();
165 usleep_range(500, 600);
167 if (i == VCHIQ_INIT_RETRIES) {
168 vchiq_log_error(vchiq_core_log_level,
169 "%s: videocore not initialized\n", __func__);
172 vchiq_log_warning(vchiq_core_log_level,
173 "%s: videocore initialized after %d retries\n",
177 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
179 vchiq_log_error(vchiq_core_log_level,
180 "%s: error allocating vchiq instance\n", __func__);
184 instance->connected = 0;
185 instance->state = state;
186 mutex_init(&instance->bulk_waiter_list_mutex);
187 INIT_LIST_HEAD(&instance->bulk_waiter_list);
189 *instance_out = instance;
191 status = VCHIQ_SUCCESS;
194 vchiq_log_trace(vchiq_core_log_level,
195 "%s(%p): returning %d", __func__, instance, status);
199 EXPORT_SYMBOL(vchiq_initialise);
201 enum vchiq_status vchiq_shutdown(struct vchiq_instance *instance)
203 enum vchiq_status status;
204 struct vchiq_state *state = instance->state;
206 vchiq_log_trace(vchiq_core_log_level,
207 "%s(%p) called", __func__, instance);
209 if (mutex_lock_killable(&state->mutex))
212 /* Remove all services */
213 status = vchiq_shutdown_internal(state, instance);
215 mutex_unlock(&state->mutex);
217 vchiq_log_trace(vchiq_core_log_level,
218 "%s(%p): returning %d", __func__, instance, status);
220 if (status == VCHIQ_SUCCESS) {
221 struct bulk_waiter_node *waiter, *next;
223 list_for_each_entry_safe(waiter, next,
224 &instance->bulk_waiter_list, list) {
225 list_del(&waiter->list);
226 vchiq_log_info(vchiq_arm_log_level,
227 "bulk_waiter - cleaned up %pK for pid %d",
228 waiter, waiter->pid);
236 EXPORT_SYMBOL(vchiq_shutdown);
238 static int vchiq_is_connected(struct vchiq_instance *instance)
240 return instance->connected;
243 enum vchiq_status vchiq_connect(struct vchiq_instance *instance)
245 enum vchiq_status status;
246 struct vchiq_state *state = instance->state;
248 vchiq_log_trace(vchiq_core_log_level,
249 "%s(%p) called", __func__, instance);
251 if (mutex_lock_killable(&state->mutex)) {
252 vchiq_log_trace(vchiq_core_log_level,
253 "%s: call to mutex_lock failed", __func__);
254 status = VCHIQ_RETRY;
257 status = vchiq_connect_internal(state, instance);
259 if (status == VCHIQ_SUCCESS)
260 instance->connected = 1;
262 mutex_unlock(&state->mutex);
265 vchiq_log_trace(vchiq_core_log_level,
266 "%s(%p): returning %d", __func__, instance, status);
270 EXPORT_SYMBOL(vchiq_connect);
272 enum vchiq_status vchiq_add_service(
273 struct vchiq_instance *instance,
274 const struct vchiq_service_params *params,
275 unsigned int *phandle)
277 enum vchiq_status status;
278 struct vchiq_state *state = instance->state;
279 struct vchiq_service *service = NULL;
282 vchiq_log_trace(vchiq_core_log_level,
283 "%s(%p) called", __func__, instance);
285 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
287 srvstate = vchiq_is_connected(instance)
288 ? VCHIQ_SRVSTATE_LISTENING
289 : VCHIQ_SRVSTATE_HIDDEN;
291 service = vchiq_add_service_internal(
299 *phandle = service->handle;
300 status = VCHIQ_SUCCESS;
302 status = VCHIQ_ERROR;
304 vchiq_log_trace(vchiq_core_log_level,
305 "%s(%p): returning %d", __func__, instance, status);
309 EXPORT_SYMBOL(vchiq_add_service);
311 enum vchiq_status vchiq_open_service(
312 struct vchiq_instance *instance,
313 const struct vchiq_service_params *params,
314 unsigned int *phandle)
316 enum vchiq_status status = VCHIQ_ERROR;
317 struct vchiq_state *state = instance->state;
318 struct vchiq_service *service = NULL;
320 vchiq_log_trace(vchiq_core_log_level,
321 "%s(%p) called", __func__, instance);
323 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
325 if (!vchiq_is_connected(instance))
328 service = vchiq_add_service_internal(state,
330 VCHIQ_SRVSTATE_OPENING,
335 *phandle = service->handle;
336 status = vchiq_open_service_internal(service, current->pid);
337 if (status != VCHIQ_SUCCESS) {
338 vchiq_remove_service(service->handle);
339 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
344 vchiq_log_trace(vchiq_core_log_level,
345 "%s(%p): returning %d", __func__, instance, status);
349 EXPORT_SYMBOL(vchiq_open_service);
352 vchiq_bulk_transmit(unsigned int handle, const void *data,
353 unsigned int size, void *userdata, enum vchiq_bulk_mode mode)
355 enum vchiq_status status;
358 case VCHIQ_BULK_MODE_NOCALLBACK:
359 case VCHIQ_BULK_MODE_CALLBACK:
360 status = vchiq_bulk_transfer(handle, (void *)data, size,
362 VCHIQ_BULK_TRANSMIT);
364 case VCHIQ_BULK_MODE_BLOCKING:
365 status = vchiq_blocking_bulk_transfer(handle,
366 (void *)data, size, VCHIQ_BULK_TRANSMIT);
374 EXPORT_SYMBOL(vchiq_bulk_transmit);
377 vchiq_bulk_receive(unsigned int handle, void *data,
378 unsigned int size, void *userdata, enum vchiq_bulk_mode mode)
380 enum vchiq_status status;
383 case VCHIQ_BULK_MODE_NOCALLBACK:
384 case VCHIQ_BULK_MODE_CALLBACK:
385 status = vchiq_bulk_transfer(handle, data, size, userdata,
386 mode, VCHIQ_BULK_RECEIVE);
388 case VCHIQ_BULK_MODE_BLOCKING:
389 status = vchiq_blocking_bulk_transfer(handle,
390 (void *)data, size, VCHIQ_BULK_RECEIVE);
398 EXPORT_SYMBOL(vchiq_bulk_receive);
400 static enum vchiq_status
401 vchiq_blocking_bulk_transfer(unsigned int handle, void *data,
402 unsigned int size, enum vchiq_bulk_dir dir)
404 struct vchiq_instance *instance;
405 struct vchiq_service *service;
406 enum vchiq_status status;
407 struct bulk_waiter_node *waiter = NULL;
409 service = find_service_by_handle(handle);
413 instance = service->instance;
415 unlock_service(service);
417 mutex_lock(&instance->bulk_waiter_list_mutex);
418 list_for_each_entry(waiter, &instance->bulk_waiter_list, list) {
419 if (waiter->pid == current->pid) {
420 list_del(&waiter->list);
424 mutex_unlock(&instance->bulk_waiter_list_mutex);
427 struct vchiq_bulk *bulk = waiter->bulk_waiter.bulk;
430 /* This thread has an outstanding bulk transfer. */
431 if ((bulk->data != data) ||
432 (bulk->size != size)) {
433 /* This is not a retry of the previous one.
434 * Cancel the signal when the transfer
437 spin_lock(&bulk_waiter_spinlock);
438 bulk->userdata = NULL;
439 spin_unlock(&bulk_waiter_spinlock);
445 waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
447 vchiq_log_error(vchiq_core_log_level,
448 "%s - out of memory", __func__);
453 status = vchiq_bulk_transfer(handle, data, size, &waiter->bulk_waiter,
454 VCHIQ_BULK_MODE_BLOCKING, dir);
455 if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
456 !waiter->bulk_waiter.bulk) {
457 struct vchiq_bulk *bulk = waiter->bulk_waiter.bulk;
460 /* Cancel the signal when the transfer
463 spin_lock(&bulk_waiter_spinlock);
464 bulk->userdata = NULL;
465 spin_unlock(&bulk_waiter_spinlock);
469 waiter->pid = current->pid;
470 mutex_lock(&instance->bulk_waiter_list_mutex);
471 list_add(&waiter->list, &instance->bulk_waiter_list);
472 mutex_unlock(&instance->bulk_waiter_list_mutex);
473 vchiq_log_info(vchiq_arm_log_level,
474 "saved bulk_waiter %pK for pid %d",
475 waiter, current->pid);
480 /****************************************************************************
484 ***************************************************************************/
486 static enum vchiq_status
487 add_completion(struct vchiq_instance *instance, enum vchiq_reason reason,
488 struct vchiq_header *header, struct user_service *user_service,
491 struct vchiq_completion_data *completion;
494 DEBUG_INITIALISE(g_state.local)
496 insert = instance->completion_insert;
497 while ((insert - instance->completion_remove) >= MAX_COMPLETIONS) {
498 /* Out of space - wait for the client */
499 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
500 vchiq_log_trace(vchiq_arm_log_level,
501 "%s - completion queue full", __func__);
502 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
503 if (wait_for_completion_interruptible(
504 &instance->remove_event)) {
505 vchiq_log_info(vchiq_arm_log_level,
506 "service_callback interrupted");
508 } else if (instance->closing) {
509 vchiq_log_info(vchiq_arm_log_level,
510 "service_callback closing");
511 return VCHIQ_SUCCESS;
513 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
516 completion = &instance->completions[insert & (MAX_COMPLETIONS - 1)];
518 completion->header = header;
519 completion->reason = reason;
520 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
521 completion->service_userdata = user_service->service;
522 completion->bulk_userdata = bulk_userdata;
524 if (reason == VCHIQ_SERVICE_CLOSED) {
525 /* Take an extra reference, to be held until
526 this CLOSED notification is delivered. */
527 lock_service(user_service->service);
528 if (instance->use_close_delivered)
529 user_service->close_pending = 1;
532 /* A write barrier is needed here to ensure that the entire completion
533 record is written out before the insert point. */
536 if (reason == VCHIQ_MESSAGE_AVAILABLE)
537 user_service->message_available_pos = insert;
540 instance->completion_insert = insert;
542 complete(&instance->insert_event);
544 return VCHIQ_SUCCESS;
547 /****************************************************************************
551 ***************************************************************************/
553 static enum vchiq_status
554 service_callback(enum vchiq_reason reason, struct vchiq_header *header,
555 unsigned int handle, void *bulk_userdata)
557 /* How do we ensure the callback goes to the right client?
558 ** The service_user data points to a user_service record
559 ** containing the original callback and the user state structure, which
560 ** contains a circular buffer for completion records.
562 struct user_service *user_service;
563 struct vchiq_service *service;
564 struct vchiq_instance *instance;
565 bool skip_completion = false;
567 DEBUG_INITIALISE(g_state.local)
569 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
571 service = handle_to_service(handle);
573 user_service = (struct user_service *)service->base.userdata;
574 instance = user_service->instance;
576 if (!instance || instance->closing)
577 return VCHIQ_SUCCESS;
579 vchiq_log_trace(vchiq_arm_log_level,
580 "%s - service %lx(%d,%p), reason %d, header %lx, "
581 "instance %lx, bulk_userdata %lx",
582 __func__, (unsigned long)user_service,
583 service->localport, user_service->userdata,
584 reason, (unsigned long)header,
585 (unsigned long)instance, (unsigned long)bulk_userdata);
587 if (header && user_service->is_vchi) {
588 spin_lock(&msg_queue_spinlock);
589 while (user_service->msg_insert ==
590 (user_service->msg_remove + MSG_QUEUE_SIZE)) {
591 spin_unlock(&msg_queue_spinlock);
592 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
593 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
594 vchiq_log_trace(vchiq_arm_log_level,
595 "service_callback - msg queue full");
596 /* If there is no MESSAGE_AVAILABLE in the completion
599 if ((user_service->message_available_pos -
600 instance->completion_remove) < 0) {
601 enum vchiq_status status;
603 vchiq_log_info(vchiq_arm_log_level,
604 "Inserting extra MESSAGE_AVAILABLE");
605 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
606 status = add_completion(instance, reason,
607 NULL, user_service, bulk_userdata);
608 if (status != VCHIQ_SUCCESS) {
609 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
614 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
615 if (wait_for_completion_interruptible(
616 &user_service->remove_event)) {
617 vchiq_log_info(vchiq_arm_log_level,
618 "%s interrupted", __func__);
619 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
621 } else if (instance->closing) {
622 vchiq_log_info(vchiq_arm_log_level,
623 "%s closing", __func__);
624 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
627 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
628 spin_lock(&msg_queue_spinlock);
631 user_service->msg_queue[user_service->msg_insert &
632 (MSG_QUEUE_SIZE - 1)] = header;
633 user_service->msg_insert++;
635 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
636 ** there is a MESSAGE_AVAILABLE in the completion queue then
637 ** bypass the completion queue.
639 if (((user_service->message_available_pos -
640 instance->completion_remove) >= 0) ||
641 user_service->dequeue_pending) {
642 user_service->dequeue_pending = 0;
643 skip_completion = true;
646 spin_unlock(&msg_queue_spinlock);
647 complete(&user_service->insert_event);
651 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
654 return VCHIQ_SUCCESS;
656 return add_completion(instance, reason, header, user_service,
660 /****************************************************************************
664 ***************************************************************************/
666 user_service_free(void *userdata)
671 /****************************************************************************
675 ***************************************************************************/
676 static void close_delivered(struct user_service *user_service)
678 vchiq_log_info(vchiq_arm_log_level,
680 __func__, user_service->service->handle);
682 if (user_service->close_pending) {
683 /* Allow the underlying service to be culled */
684 unlock_service(user_service->service);
686 /* Wake the user-thread blocked in close_ or remove_service */
687 complete(&user_service->close_event);
689 user_service->close_pending = 0;
693 struct vchiq_io_copy_callback_context {
694 struct vchiq_element *element;
695 size_t element_offset;
696 unsigned long elements_to_go;
699 static ssize_t vchiq_ioc_copy_element_data(void *context, void *dest,
700 size_t offset, size_t maxsize)
702 struct vchiq_io_copy_callback_context *cc = context;
703 size_t total_bytes_copied = 0;
704 size_t bytes_this_round;
706 while (total_bytes_copied < maxsize) {
707 if (!cc->elements_to_go)
708 return total_bytes_copied;
710 if (!cc->element->size) {
711 cc->elements_to_go--;
713 cc->element_offset = 0;
717 bytes_this_round = min(cc->element->size - cc->element_offset,
718 maxsize - total_bytes_copied);
720 if (copy_from_user(dest + total_bytes_copied,
721 cc->element->data + cc->element_offset,
725 cc->element_offset += bytes_this_round;
726 total_bytes_copied += bytes_this_round;
728 if (cc->element_offset == cc->element->size) {
729 cc->elements_to_go--;
731 cc->element_offset = 0;
738 /**************************************************************************
740 * vchiq_ioc_queue_message
742 **************************************************************************/
743 static enum vchiq_status
744 vchiq_ioc_queue_message(unsigned int handle,
745 struct vchiq_element *elements,
748 struct vchiq_io_copy_callback_context context;
750 size_t total_size = 0;
752 context.element = elements;
753 context.element_offset = 0;
754 context.elements_to_go = count;
756 for (i = 0; i < count; i++) {
757 if (!elements[i].data && elements[i].size != 0)
760 total_size += elements[i].size;
763 return vchiq_queue_message(handle, vchiq_ioc_copy_element_data,
764 &context, total_size);
767 /****************************************************************************
771 ***************************************************************************/
773 vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
775 struct vchiq_instance *instance = file->private_data;
776 enum vchiq_status status = VCHIQ_SUCCESS;
777 struct vchiq_service *service = NULL;
781 DEBUG_INITIALISE(g_state.local)
783 vchiq_log_trace(vchiq_arm_log_level,
784 "%s - instance %pK, cmd %s, arg %lx",
786 ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
787 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
788 ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
791 case VCHIQ_IOC_SHUTDOWN:
792 if (!instance->connected)
795 /* Remove all services */
797 while ((service = next_service_by_instance(instance->state,
799 status = vchiq_remove_service(service->handle);
800 unlock_service(service);
801 if (status != VCHIQ_SUCCESS)
806 if (status == VCHIQ_SUCCESS) {
807 /* Wake the completion thread and ask it to exit */
808 instance->closing = 1;
809 complete(&instance->insert_event);
814 case VCHIQ_IOC_CONNECT:
815 if (instance->connected) {
819 rc = mutex_lock_killable(&instance->state->mutex);
821 vchiq_log_error(vchiq_arm_log_level,
822 "vchiq: connect: could not lock mutex for "
824 instance->state->id, rc);
828 status = vchiq_connect_internal(instance->state, instance);
829 mutex_unlock(&instance->state->mutex);
831 if (status == VCHIQ_SUCCESS)
832 instance->connected = 1;
834 vchiq_log_error(vchiq_arm_log_level,
835 "vchiq: could not connect: %d", status);
838 case VCHIQ_IOC_CREATE_SERVICE: {
839 struct vchiq_create_service args;
840 struct user_service *user_service = NULL;
844 if (copy_from_user(&args, (const void __user *)arg,
850 user_service = kmalloc(sizeof(*user_service), GFP_KERNEL);
857 if (!instance->connected) {
862 srvstate = VCHIQ_SRVSTATE_OPENING;
865 instance->connected ?
866 VCHIQ_SRVSTATE_LISTENING :
867 VCHIQ_SRVSTATE_HIDDEN;
870 userdata = args.params.userdata;
871 args.params.callback = service_callback;
872 args.params.userdata = user_service;
873 service = vchiq_add_service_internal(
875 &args.params, srvstate,
876 instance, user_service_free);
879 user_service->service = service;
880 user_service->userdata = userdata;
881 user_service->instance = instance;
882 user_service->is_vchi = (args.is_vchi != 0);
883 user_service->dequeue_pending = 0;
884 user_service->close_pending = 0;
885 user_service->message_available_pos =
886 instance->completion_remove - 1;
887 user_service->msg_insert = 0;
888 user_service->msg_remove = 0;
889 init_completion(&user_service->insert_event);
890 init_completion(&user_service->remove_event);
891 init_completion(&user_service->close_event);
894 status = vchiq_open_service_internal
895 (service, instance->pid);
896 if (status != VCHIQ_SUCCESS) {
897 vchiq_remove_service(service->handle);
899 ret = (status == VCHIQ_RETRY) ?
905 if (copy_to_user((void __user *)
906 &(((struct vchiq_create_service __user *)
908 (const void *)&service->handle,
909 sizeof(service->handle))) {
911 vchiq_remove_service(service->handle);
921 case VCHIQ_IOC_CLOSE_SERVICE:
922 case VCHIQ_IOC_REMOVE_SERVICE: {
923 unsigned int handle = (unsigned int)arg;
924 struct user_service *user_service;
926 service = find_service_for_instance(instance, handle);
932 user_service = service->base.userdata;
934 /* close_pending is false on first entry, and when the
935 wait in vchiq_close_service has been interrupted. */
936 if (!user_service->close_pending) {
937 status = (cmd == VCHIQ_IOC_CLOSE_SERVICE) ?
938 vchiq_close_service(service->handle) :
939 vchiq_remove_service(service->handle);
940 if (status != VCHIQ_SUCCESS)
944 /* close_pending is true once the underlying service
945 has been closed until the client library calls the
946 CLOSE_DELIVERED ioctl, signalling close_event. */
947 if (user_service->close_pending &&
948 wait_for_completion_interruptible(
949 &user_service->close_event))
950 status = VCHIQ_RETRY;
954 case VCHIQ_IOC_USE_SERVICE:
955 case VCHIQ_IOC_RELEASE_SERVICE: {
956 unsigned int handle = (unsigned int)arg;
958 service = find_service_for_instance(instance, handle);
960 status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
961 vchiq_use_service_internal(service) :
962 vchiq_release_service_internal(service);
963 if (status != VCHIQ_SUCCESS) {
964 vchiq_log_error(vchiq_susp_log_level,
965 "%s: cmd %s returned error %d for "
966 "service %c%c%c%c:%03d",
968 (cmd == VCHIQ_IOC_USE_SERVICE) ?
969 "VCHIQ_IOC_USE_SERVICE" :
970 "VCHIQ_IOC_RELEASE_SERVICE",
972 VCHIQ_FOURCC_AS_4CHARS(
973 service->base.fourcc),
981 case VCHIQ_IOC_QUEUE_MESSAGE: {
982 struct vchiq_queue_message args;
984 if (copy_from_user(&args, (const void __user *)arg,
990 service = find_service_for_instance(instance, args.handle);
992 if (service && (args.count <= MAX_ELEMENTS)) {
993 /* Copy elements into kernel space */
994 struct vchiq_element elements[MAX_ELEMENTS];
996 if (copy_from_user(elements, args.elements,
997 args.count * sizeof(struct vchiq_element)) == 0)
998 status = vchiq_ioc_queue_message
1000 elements, args.count);
1008 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
1009 case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
1010 struct vchiq_queue_bulk_transfer args;
1011 struct bulk_waiter_node *waiter = NULL;
1013 enum vchiq_bulk_dir dir =
1014 (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
1015 VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
1017 if (copy_from_user(&args, (const void __user *)arg,
1023 service = find_service_for_instance(instance, args.handle);
1029 if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
1030 waiter = kzalloc(sizeof(struct bulk_waiter_node),
1037 args.userdata = &waiter->bulk_waiter;
1038 } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
1039 mutex_lock(&instance->bulk_waiter_list_mutex);
1040 list_for_each_entry(waiter, &instance->bulk_waiter_list,
1042 if (waiter->pid == current->pid) {
1043 list_del(&waiter->list);
1047 mutex_unlock(&instance->bulk_waiter_list_mutex);
1049 vchiq_log_error(vchiq_arm_log_level,
1050 "no bulk_waiter found for pid %d",
1055 vchiq_log_info(vchiq_arm_log_level,
1056 "found bulk_waiter %pK for pid %d", waiter,
1058 args.userdata = &waiter->bulk_waiter;
1061 status = vchiq_bulk_transfer(args.handle, args.data, args.size,
1062 args.userdata, args.mode, dir);
1067 if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
1068 !waiter->bulk_waiter.bulk) {
1069 if (waiter->bulk_waiter.bulk) {
1070 /* Cancel the signal when the transfer
1072 spin_lock(&bulk_waiter_spinlock);
1073 waiter->bulk_waiter.bulk->userdata = NULL;
1074 spin_unlock(&bulk_waiter_spinlock);
1078 const enum vchiq_bulk_mode mode_waiting =
1079 VCHIQ_BULK_MODE_WAITING;
1080 waiter->pid = current->pid;
1081 mutex_lock(&instance->bulk_waiter_list_mutex);
1082 list_add(&waiter->list, &instance->bulk_waiter_list);
1083 mutex_unlock(&instance->bulk_waiter_list_mutex);
1084 vchiq_log_info(vchiq_arm_log_level,
1085 "saved bulk_waiter %pK for pid %d",
1086 waiter, current->pid);
1088 if (copy_to_user((void __user *)
1089 &(((struct vchiq_queue_bulk_transfer __user *)
1091 (const void *)&mode_waiting,
1092 sizeof(mode_waiting)))
1097 case VCHIQ_IOC_AWAIT_COMPLETION: {
1098 struct vchiq_await_completion args;
1100 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1101 if (!instance->connected) {
1106 if (copy_from_user(&args, (const void __user *)arg,
1112 mutex_lock(&instance->completion_mutex);
1114 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1115 while ((instance->completion_remove ==
1116 instance->completion_insert)
1117 && !instance->closing) {
1120 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1121 mutex_unlock(&instance->completion_mutex);
1122 rc = wait_for_completion_interruptible(
1123 &instance->insert_event);
1124 mutex_lock(&instance->completion_mutex);
1126 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1127 vchiq_log_info(vchiq_arm_log_level,
1128 "AWAIT_COMPLETION interrupted");
1133 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1136 int msgbufcount = args.msgbufcount;
1137 int remove = instance->completion_remove;
1139 for (ret = 0; ret < args.count; ret++) {
1140 struct vchiq_completion_data *completion;
1141 struct vchiq_service *service;
1142 struct user_service *user_service;
1143 struct vchiq_header *header;
1145 if (remove == instance->completion_insert)
1148 completion = &instance->completions[
1149 remove & (MAX_COMPLETIONS - 1)];
1152 * A read memory barrier is needed to stop
1153 * prefetch of a stale completion record
1157 service = completion->service_userdata;
1158 user_service = service->base.userdata;
1159 completion->service_userdata =
1160 user_service->userdata;
1162 header = completion->header;
1164 void __user *msgbuf;
1167 msglen = header->size +
1168 sizeof(struct vchiq_header);
1169 /* This must be a VCHIQ-style service */
1170 if (args.msgbufsize < msglen) {
1172 vchiq_arm_log_level,
1173 "header %pK: msgbufsize %x < msglen %x",
1174 header, args.msgbufsize,
1176 WARN(1, "invalid message "
1182 if (msgbufcount <= 0)
1183 /* Stall here for lack of a
1184 ** buffer for the message. */
1186 /* Get the pointer from user space */
1188 if (copy_from_user(&msgbuf,
1189 (const void __user *)
1190 &args.msgbufs[msgbufcount],
1197 /* Copy the message to user space */
1198 if (copy_to_user(msgbuf, header,
1205 /* Now it has been copied, the message
1206 ** can be released. */
1207 vchiq_release_message(service->handle,
1210 /* The completion must point to the
1212 completion->header = msgbuf;
1215 if ((completion->reason ==
1216 VCHIQ_SERVICE_CLOSED) &&
1217 !instance->use_close_delivered)
1218 unlock_service(service);
1220 if (copy_to_user((void __user *)(
1221 (size_t)args.buf + ret *
1222 sizeof(struct vchiq_completion_data)),
1224 sizeof(struct vchiq_completion_data))) {
1231 * Ensure that the above copy has completed
1232 * before advancing the remove pointer.
1236 instance->completion_remove = remove;
1239 if (msgbufcount != args.msgbufcount) {
1240 if (copy_to_user((void __user *)
1241 &((struct vchiq_await_completion *)arg)
1244 sizeof(msgbufcount))) {
1251 complete(&instance->remove_event);
1252 mutex_unlock(&instance->completion_mutex);
1253 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1256 case VCHIQ_IOC_DEQUEUE_MESSAGE: {
1257 struct vchiq_dequeue_message args;
1258 struct user_service *user_service;
1259 struct vchiq_header *header;
1261 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1262 if (copy_from_user(&args, (const void __user *)arg,
1267 service = find_service_for_instance(instance, args.handle);
1272 user_service = (struct user_service *)service->base.userdata;
1273 if (user_service->is_vchi == 0) {
1278 spin_lock(&msg_queue_spinlock);
1279 if (user_service->msg_remove == user_service->msg_insert) {
1280 if (!args.blocking) {
1281 spin_unlock(&msg_queue_spinlock);
1282 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1286 user_service->dequeue_pending = 1;
1288 spin_unlock(&msg_queue_spinlock);
1289 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1290 if (wait_for_completion_interruptible(
1291 &user_service->insert_event)) {
1292 vchiq_log_info(vchiq_arm_log_level,
1293 "DEQUEUE_MESSAGE interrupted");
1297 spin_lock(&msg_queue_spinlock);
1298 } while (user_service->msg_remove ==
1299 user_service->msg_insert);
1305 BUG_ON((int)(user_service->msg_insert -
1306 user_service->msg_remove) < 0);
1308 header = user_service->msg_queue[user_service->msg_remove &
1309 (MSG_QUEUE_SIZE - 1)];
1310 user_service->msg_remove++;
1311 spin_unlock(&msg_queue_spinlock);
1313 complete(&user_service->remove_event);
1316 else if (header->size <= args.bufsize) {
1317 /* Copy to user space if msgbuf is not NULL */
1319 (copy_to_user((void __user *)args.buf,
1321 header->size) == 0)) {
1323 vchiq_release_message(
1329 vchiq_log_error(vchiq_arm_log_level,
1330 "header %pK: bufsize %x < size %x",
1331 header, args.bufsize, header->size);
1332 WARN(1, "invalid size\n");
1335 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1338 case VCHIQ_IOC_GET_CLIENT_ID: {
1339 unsigned int handle = (unsigned int)arg;
1341 ret = vchiq_get_client_id(handle);
1344 case VCHIQ_IOC_GET_CONFIG: {
1345 struct vchiq_get_config args;
1346 struct vchiq_config config;
1348 if (copy_from_user(&args, (const void __user *)arg,
1353 if (args.config_size > sizeof(config)) {
1358 vchiq_get_config(&config);
1359 if (copy_to_user(args.pconfig, &config, args.config_size)) {
1365 case VCHIQ_IOC_SET_SERVICE_OPTION: {
1366 struct vchiq_set_service_option args;
1368 if (copy_from_user(&args, (const void __user *)arg,
1374 service = find_service_for_instance(instance, args.handle);
1380 status = vchiq_set_service_option(
1381 args.handle, args.option, args.value);
1384 case VCHIQ_IOC_LIB_VERSION: {
1385 unsigned int lib_version = (unsigned int)arg;
1387 if (lib_version < VCHIQ_VERSION_MIN)
1389 else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
1390 instance->use_close_delivered = 1;
1393 case VCHIQ_IOC_CLOSE_DELIVERED: {
1394 unsigned int handle = (unsigned int)arg;
1396 service = find_closed_service_for_instance(instance, handle);
1398 struct user_service *user_service =
1399 (struct user_service *)service->base.userdata;
1400 close_delivered(user_service);
1411 unlock_service(service);
1414 if (status == VCHIQ_ERROR)
1416 else if (status == VCHIQ_RETRY)
1420 if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
1421 (ret != -EWOULDBLOCK))
1422 vchiq_log_info(vchiq_arm_log_level,
1423 " ioctl instance %pK, cmd %s -> status %d, %ld",
1425 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1426 ioctl_names[_IOC_NR(cmd)] :
1430 vchiq_log_trace(vchiq_arm_log_level,
1431 " ioctl instance %pK, cmd %s -> status %d, %ld",
1433 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1434 ioctl_names[_IOC_NR(cmd)] :
1441 #if defined(CONFIG_COMPAT)
1443 struct vchiq_service_params32 {
1445 compat_uptr_t callback;
1446 compat_uptr_t userdata;
1447 short version; /* Increment for non-trivial changes */
1448 short version_min; /* Update for incompatible changes */
1451 struct vchiq_create_service32 {
1452 struct vchiq_service_params32 params;
1455 unsigned int handle; /* OUT */
1458 #define VCHIQ_IOC_CREATE_SERVICE32 \
1459 _IOWR(VCHIQ_IOC_MAGIC, 2, struct vchiq_create_service32)
1462 vchiq_compat_ioctl_create_service(
1467 struct vchiq_create_service __user *args;
1468 struct vchiq_create_service32 __user *ptrargs32 =
1469 (struct vchiq_create_service32 __user *)arg;
1470 struct vchiq_create_service32 args32;
1473 args = compat_alloc_user_space(sizeof(*args));
1477 if (copy_from_user(&args32, ptrargs32, sizeof(args32)))
1480 if (put_user(args32.params.fourcc, &args->params.fourcc) ||
1481 put_user(compat_ptr(args32.params.callback),
1482 &args->params.callback) ||
1483 put_user(compat_ptr(args32.params.userdata),
1484 &args->params.userdata) ||
1485 put_user(args32.params.version, &args->params.version) ||
1486 put_user(args32.params.version_min,
1487 &args->params.version_min) ||
1488 put_user(args32.is_open, &args->is_open) ||
1489 put_user(args32.is_vchi, &args->is_vchi) ||
1490 put_user(args32.handle, &args->handle))
1493 ret = vchiq_ioctl(file, VCHIQ_IOC_CREATE_SERVICE, (unsigned long)args);
1498 if (get_user(args32.handle, &args->handle))
1501 if (copy_to_user(&ptrargs32->handle,
1503 sizeof(args32.handle)))
1509 struct vchiq_element32 {
1514 struct vchiq_queue_message32 {
1515 unsigned int handle;
1517 compat_uptr_t elements;
1520 #define VCHIQ_IOC_QUEUE_MESSAGE32 \
1521 _IOW(VCHIQ_IOC_MAGIC, 4, struct vchiq_queue_message32)
1524 vchiq_compat_ioctl_queue_message(struct file *file,
1528 struct vchiq_queue_message __user *args;
1529 struct vchiq_element __user *elements;
1530 struct vchiq_queue_message32 args32;
1533 if (copy_from_user(&args32,
1534 (struct vchiq_queue_message32 __user *)arg,
1538 args = compat_alloc_user_space(sizeof(*args) +
1539 (sizeof(*elements) * MAX_ELEMENTS));
1544 if (put_user(args32.handle, &args->handle) ||
1545 put_user(args32.count, &args->count) ||
1546 put_user(compat_ptr(args32.elements), &args->elements))
1549 if (args32.count > MAX_ELEMENTS)
1552 if (args32.elements && args32.count) {
1553 struct vchiq_element32 tempelement32[MAX_ELEMENTS];
1555 elements = (struct vchiq_element __user *)(args + 1);
1557 if (copy_from_user(&tempelement32,
1558 compat_ptr(args32.elements),
1559 sizeof(tempelement32)))
1562 for (count = 0; count < args32.count; count++) {
1563 if (put_user(compat_ptr(tempelement32[count].data),
1564 &elements[count].data) ||
1565 put_user(tempelement32[count].size,
1566 &elements[count].size))
1570 if (put_user(elements, &args->elements))
1574 return vchiq_ioctl(file, VCHIQ_IOC_QUEUE_MESSAGE, (unsigned long)args);
1577 struct vchiq_queue_bulk_transfer32 {
1578 unsigned int handle;
1581 compat_uptr_t userdata;
1582 enum vchiq_bulk_mode mode;
1585 #define VCHIQ_IOC_QUEUE_BULK_TRANSMIT32 \
1586 _IOWR(VCHIQ_IOC_MAGIC, 5, struct vchiq_queue_bulk_transfer32)
1587 #define VCHIQ_IOC_QUEUE_BULK_RECEIVE32 \
1588 _IOWR(VCHIQ_IOC_MAGIC, 6, struct vchiq_queue_bulk_transfer32)
1591 vchiq_compat_ioctl_queue_bulk(struct file *file,
1595 struct vchiq_queue_bulk_transfer __user *args;
1596 struct vchiq_queue_bulk_transfer32 args32;
1597 struct vchiq_queue_bulk_transfer32 __user *ptrargs32 =
1598 (struct vchiq_queue_bulk_transfer32 __user *)arg;
1601 args = compat_alloc_user_space(sizeof(*args));
1605 if (copy_from_user(&args32, ptrargs32, sizeof(args32)))
1608 if (put_user(args32.handle, &args->handle) ||
1609 put_user(compat_ptr(args32.data), &args->data) ||
1610 put_user(args32.size, &args->size) ||
1611 put_user(compat_ptr(args32.userdata), &args->userdata) ||
1612 put_user(args32.mode, &args->mode))
1615 if (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT32)
1616 cmd = VCHIQ_IOC_QUEUE_BULK_TRANSMIT;
1618 cmd = VCHIQ_IOC_QUEUE_BULK_RECEIVE;
1620 ret = vchiq_ioctl(file, cmd, (unsigned long)args);
1625 if (get_user(args32.mode, &args->mode))
1628 if (copy_to_user(&ptrargs32->mode,
1630 sizeof(args32.mode)))
1636 struct vchiq_completion_data32 {
1637 enum vchiq_reason reason;
1638 compat_uptr_t header;
1639 compat_uptr_t service_userdata;
1640 compat_uptr_t bulk_userdata;
1643 struct vchiq_await_completion32 {
1646 unsigned int msgbufsize;
1647 unsigned int msgbufcount; /* IN/OUT */
1648 compat_uptr_t msgbufs;
1651 #define VCHIQ_IOC_AWAIT_COMPLETION32 \
1652 _IOWR(VCHIQ_IOC_MAGIC, 7, struct vchiq_await_completion32)
1655 vchiq_compat_ioctl_await_completion(struct file *file,
1659 struct vchiq_await_completion __user *args;
1660 struct vchiq_completion_data __user *completion;
1661 struct vchiq_completion_data completiontemp;
1662 struct vchiq_await_completion32 args32;
1663 struct vchiq_completion_data32 completion32;
1664 unsigned int __user *msgbufcount32;
1665 unsigned int msgbufcount_native;
1666 compat_uptr_t msgbuf32;
1667 void __user *msgbuf;
1668 void * __user *msgbufptr;
1671 args = compat_alloc_user_space(sizeof(*args) +
1672 sizeof(*completion) +
1673 sizeof(*msgbufptr));
1677 completion = (struct vchiq_completion_data __user *)(args + 1);
1678 msgbufptr = (void * __user *)(completion + 1);
1680 if (copy_from_user(&args32,
1681 (struct vchiq_completion_data32 __user *)arg,
1685 if (put_user(args32.count, &args->count) ||
1686 put_user(compat_ptr(args32.buf), &args->buf) ||
1687 put_user(args32.msgbufsize, &args->msgbufsize) ||
1688 put_user(args32.msgbufcount, &args->msgbufcount) ||
1689 put_user(compat_ptr(args32.msgbufs), &args->msgbufs))
1692 /* These are simple cases, so just fall into the native handler */
1693 if (!args32.count || !args32.buf || !args32.msgbufcount)
1694 return vchiq_ioctl(file,
1695 VCHIQ_IOC_AWAIT_COMPLETION,
1696 (unsigned long)args);
1699 * These are the more complex cases. Typical applications of this
1700 * ioctl will use a very large count, with a very large msgbufcount.
1701 * Since the native ioctl can asynchronously fill in the returned
1702 * buffers and the application can in theory begin processing messages
1703 * even before the ioctl returns, a bit of a trick is used here.
1705 * By forcing both count and msgbufcount to be 1, it forces the native
1706 * ioctl to only claim at most 1 message is available. This tricks
1707 * the calling application into thinking only 1 message was actually
1708 * available in the queue so like all good applications it will retry
1709 * waiting until all the required messages are received.
1711 * This trick has been tested and proven to work with vchiq_test,
1712 * Minecraft_PI, the "hello pi" examples, and various other
1713 * applications that are included in Raspbian.
1716 if (copy_from_user(&msgbuf32,
1717 compat_ptr(args32.msgbufs) +
1718 (sizeof(compat_uptr_t) *
1719 (args32.msgbufcount - 1)),
1723 msgbuf = compat_ptr(msgbuf32);
1725 if (copy_to_user(msgbufptr,
1730 if (copy_to_user(&args->msgbufs,
1735 if (put_user(1U, &args->count) ||
1736 put_user(completion, &args->buf) ||
1737 put_user(1U, &args->msgbufcount))
1740 ret = vchiq_ioctl(file,
1741 VCHIQ_IOC_AWAIT_COMPLETION,
1742 (unsigned long)args);
1745 * An return value of 0 here means that no messages where available
1746 * in the message queue. In this case the native ioctl does not
1747 * return any data to the application at all. Not even to update
1748 * msgbufcount. This functionality needs to be kept here for
1751 * Of course, < 0 means that an error occurred and no data is being
1754 * Since count and msgbufcount was forced to 1, that means
1755 * the only other possible return value is 1. Meaning that 1 message
1756 * was available, so that multiple message case does not need to be
1762 if (copy_from_user(&completiontemp, completion, sizeof(*completion)))
1765 completion32.reason = completiontemp.reason;
1766 completion32.header = ptr_to_compat(completiontemp.header);
1767 completion32.service_userdata =
1768 ptr_to_compat(completiontemp.service_userdata);
1769 completion32.bulk_userdata =
1770 ptr_to_compat(completiontemp.bulk_userdata);
1772 if (copy_to_user(compat_ptr(args32.buf),
1774 sizeof(completion32)))
1777 if (get_user(msgbufcount_native, &args->msgbufcount))
1780 if (!msgbufcount_native)
1781 args32.msgbufcount--;
1784 &((struct vchiq_await_completion32 __user *)arg)->msgbufcount;
1786 if (copy_to_user(msgbufcount32,
1787 &args32.msgbufcount,
1788 sizeof(args32.msgbufcount)))
1794 struct vchiq_dequeue_message32 {
1795 unsigned int handle;
1797 unsigned int bufsize;
1801 #define VCHIQ_IOC_DEQUEUE_MESSAGE32 \
1802 _IOWR(VCHIQ_IOC_MAGIC, 8, struct vchiq_dequeue_message32)
1805 vchiq_compat_ioctl_dequeue_message(struct file *file,
1809 struct vchiq_dequeue_message __user *args;
1810 struct vchiq_dequeue_message32 args32;
1812 args = compat_alloc_user_space(sizeof(*args));
1816 if (copy_from_user(&args32,
1817 (struct vchiq_dequeue_message32 __user *)arg,
1821 if (put_user(args32.handle, &args->handle) ||
1822 put_user(args32.blocking, &args->blocking) ||
1823 put_user(args32.bufsize, &args->bufsize) ||
1824 put_user(compat_ptr(args32.buf), &args->buf))
1827 return vchiq_ioctl(file, VCHIQ_IOC_DEQUEUE_MESSAGE,
1828 (unsigned long)args);
1831 struct vchiq_get_config32 {
1832 unsigned int config_size;
1833 compat_uptr_t pconfig;
1836 #define VCHIQ_IOC_GET_CONFIG32 \
1837 _IOWR(VCHIQ_IOC_MAGIC, 10, struct vchiq_get_config32)
1840 vchiq_compat_ioctl_get_config(struct file *file,
1844 struct vchiq_get_config __user *args;
1845 struct vchiq_get_config32 args32;
1847 args = compat_alloc_user_space(sizeof(*args));
1851 if (copy_from_user(&args32,
1852 (struct vchiq_get_config32 __user *)arg,
1856 if (put_user(args32.config_size, &args->config_size) ||
1857 put_user(compat_ptr(args32.pconfig), &args->pconfig))
1860 return vchiq_ioctl(file, VCHIQ_IOC_GET_CONFIG, (unsigned long)args);
1864 vchiq_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1867 case VCHIQ_IOC_CREATE_SERVICE32:
1868 return vchiq_compat_ioctl_create_service(file, cmd, arg);
1869 case VCHIQ_IOC_QUEUE_MESSAGE32:
1870 return vchiq_compat_ioctl_queue_message(file, cmd, arg);
1871 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT32:
1872 case VCHIQ_IOC_QUEUE_BULK_RECEIVE32:
1873 return vchiq_compat_ioctl_queue_bulk(file, cmd, arg);
1874 case VCHIQ_IOC_AWAIT_COMPLETION32:
1875 return vchiq_compat_ioctl_await_completion(file, cmd, arg);
1876 case VCHIQ_IOC_DEQUEUE_MESSAGE32:
1877 return vchiq_compat_ioctl_dequeue_message(file, cmd, arg);
1878 case VCHIQ_IOC_GET_CONFIG32:
1879 return vchiq_compat_ioctl_get_config(file, cmd, arg);
1881 return vchiq_ioctl(file, cmd, arg);
1887 static int vchiq_open(struct inode *inode, struct file *file)
1889 struct vchiq_state *state = vchiq_get_state();
1890 struct vchiq_instance *instance;
1892 vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
1895 vchiq_log_error(vchiq_arm_log_level,
1896 "vchiq has no connection to VideoCore");
1900 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1904 instance->state = state;
1905 instance->pid = current->tgid;
1907 vchiq_debugfs_add_instance(instance);
1909 init_completion(&instance->insert_event);
1910 init_completion(&instance->remove_event);
1911 mutex_init(&instance->completion_mutex);
1912 mutex_init(&instance->bulk_waiter_list_mutex);
1913 INIT_LIST_HEAD(&instance->bulk_waiter_list);
1915 file->private_data = instance;
1920 static int vchiq_release(struct inode *inode, struct file *file)
1922 struct vchiq_instance *instance = file->private_data;
1923 struct vchiq_state *state = vchiq_get_state();
1924 struct vchiq_service *service;
1928 vchiq_log_info(vchiq_arm_log_level, "%s: instance=%lx", __func__,
1929 (unsigned long)instance);
1936 /* Ensure videocore is awake to allow termination. */
1937 vchiq_use_internal(instance->state, NULL, USE_TYPE_VCHIQ);
1939 mutex_lock(&instance->completion_mutex);
1941 /* Wake the completion thread and ask it to exit */
1942 instance->closing = 1;
1943 complete(&instance->insert_event);
1945 mutex_unlock(&instance->completion_mutex);
1947 /* Wake the slot handler if the completion queue is full. */
1948 complete(&instance->remove_event);
1950 /* Mark all services for termination... */
1952 while ((service = next_service_by_instance(state, instance, &i))) {
1953 struct user_service *user_service = service->base.userdata;
1955 /* Wake the slot handler if the msg queue is full. */
1956 complete(&user_service->remove_event);
1958 vchiq_terminate_service_internal(service);
1959 unlock_service(service);
1962 /* ...and wait for them to die */
1964 while ((service = next_service_by_instance(state, instance, &i))) {
1965 struct user_service *user_service = service->base.userdata;
1967 wait_for_completion(&service->remove_event);
1969 BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
1971 spin_lock(&msg_queue_spinlock);
1973 while (user_service->msg_remove != user_service->msg_insert) {
1974 struct vchiq_header *header;
1975 int m = user_service->msg_remove & (MSG_QUEUE_SIZE - 1);
1977 header = user_service->msg_queue[m];
1978 user_service->msg_remove++;
1979 spin_unlock(&msg_queue_spinlock);
1982 vchiq_release_message(service->handle, header);
1983 spin_lock(&msg_queue_spinlock);
1986 spin_unlock(&msg_queue_spinlock);
1988 unlock_service(service);
1991 /* Release any closed services */
1992 while (instance->completion_remove !=
1993 instance->completion_insert) {
1994 struct vchiq_completion_data *completion;
1995 struct vchiq_service *service;
1997 completion = &instance->completions[
1998 instance->completion_remove & (MAX_COMPLETIONS - 1)];
1999 service = completion->service_userdata;
2000 if (completion->reason == VCHIQ_SERVICE_CLOSED) {
2001 struct user_service *user_service =
2002 service->base.userdata;
2004 /* Wake any blocked user-thread */
2005 if (instance->use_close_delivered)
2006 complete(&user_service->close_event);
2007 unlock_service(service);
2009 instance->completion_remove++;
2012 /* Release the PEER service count. */
2013 vchiq_release_internal(instance->state, NULL);
2016 struct bulk_waiter_node *waiter, *next;
2018 list_for_each_entry_safe(waiter, next,
2019 &instance->bulk_waiter_list, list) {
2020 list_del(&waiter->list);
2021 vchiq_log_info(vchiq_arm_log_level,
2022 "bulk_waiter - cleaned up %pK for pid %d",
2023 waiter, waiter->pid);
2028 vchiq_debugfs_remove_instance(instance);
2031 file->private_data = NULL;
2037 /****************************************************************************
2041 ***************************************************************************/
2043 int vchiq_dump(void *dump_context, const char *str, int len)
2045 struct dump_context *context = (struct dump_context *)dump_context;
2048 if (context->actual >= context->space)
2051 if (context->offset > 0) {
2052 int skip_bytes = min_t(int, len, context->offset);
2056 context->offset -= skip_bytes;
2057 if (context->offset > 0)
2060 copy_bytes = min_t(int, len, context->space - context->actual);
2061 if (copy_bytes == 0)
2063 if (copy_to_user(context->buf + context->actual, str,
2066 context->actual += copy_bytes;
2070 * If the terminating NUL is included in the length, then it
2071 * marks the end of a line and should be replaced with a
2074 if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
2077 if (copy_to_user(context->buf + context->actual - 1,
2084 /****************************************************************************
2086 * vchiq_dump_platform_instance_state
2088 ***************************************************************************/
2090 int vchiq_dump_platform_instances(void *dump_context)
2092 struct vchiq_state *state = vchiq_get_state();
2097 /* There is no list of instances, so instead scan all services,
2098 marking those that have been dumped. */
2101 for (i = 0; i < state->unused_service; i++) {
2102 struct vchiq_service *service;
2103 struct vchiq_instance *instance;
2105 service = rcu_dereference(state->services[i]);
2106 if (!service || service->base.callback != service_callback)
2109 instance = service->instance;
2115 for (i = 0; i < state->unused_service; i++) {
2116 struct vchiq_service *service;
2117 struct vchiq_instance *instance;
2121 service = rcu_dereference(state->services[i]);
2122 if (!service || service->base.callback != service_callback) {
2127 instance = service->instance;
2128 if (!instance || instance->mark) {
2134 len = snprintf(buf, sizeof(buf),
2135 "Instance %pK: pid %d,%s completions %d/%d",
2136 instance, instance->pid,
2137 instance->connected ? " connected, " :
2139 instance->completion_insert -
2140 instance->completion_remove,
2142 err = vchiq_dump(dump_context, buf, len + 1);
2150 /****************************************************************************
2152 * vchiq_dump_platform_service_state
2154 ***************************************************************************/
2156 int vchiq_dump_platform_service_state(void *dump_context,
2157 struct vchiq_service *service)
2159 struct user_service *user_service =
2160 (struct user_service *)service->base.userdata;
2164 len = scnprintf(buf, sizeof(buf), " instance %pK", service->instance);
2166 if ((service->base.callback == service_callback) &&
2167 user_service->is_vchi) {
2168 len += scnprintf(buf + len, sizeof(buf) - len,
2170 user_service->msg_insert - user_service->msg_remove,
2173 if (user_service->dequeue_pending)
2174 len += scnprintf(buf + len, sizeof(buf) - len,
2175 " (dequeue pending)");
2178 return vchiq_dump(dump_context, buf, len + 1);
2181 /****************************************************************************
2185 ***************************************************************************/
2188 vchiq_read(struct file *file, char __user *buf,
2189 size_t count, loff_t *ppos)
2191 struct dump_context context;
2196 context.space = count;
2197 context.offset = *ppos;
2199 err = vchiq_dump_state(&context, &g_state);
2203 *ppos += context.actual;
2205 return context.actual;
2208 struct vchiq_state *
2209 vchiq_get_state(void)
2212 if (!g_state.remote)
2213 printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
2214 else if (g_state.remote->initialised != 1)
2215 printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
2216 __func__, g_state.remote->initialised);
2218 return (g_state.remote &&
2219 (g_state.remote->initialised == 1)) ? &g_state : NULL;
2222 static const struct file_operations
2224 .owner = THIS_MODULE,
2225 .unlocked_ioctl = vchiq_ioctl,
2226 #if defined(CONFIG_COMPAT)
2227 .compat_ioctl = vchiq_compat_ioctl,
2230 .release = vchiq_release,
2235 * Autosuspend related functionality
2238 static enum vchiq_status
2239 vchiq_keepalive_vchiq_callback(enum vchiq_reason reason,
2240 struct vchiq_header *header,
2241 unsigned int service_user,
2244 vchiq_log_error(vchiq_susp_log_level,
2245 "%s callback reason %d", __func__, reason);
2250 vchiq_keepalive_thread_func(void *v)
2252 struct vchiq_state *state = (struct vchiq_state *)v;
2253 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2255 enum vchiq_status status;
2256 struct vchiq_instance *instance;
2257 unsigned int ka_handle;
2259 struct vchiq_service_params params = {
2260 .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
2261 .callback = vchiq_keepalive_vchiq_callback,
2262 .version = KEEPALIVE_VER,
2263 .version_min = KEEPALIVE_VER_MIN
2266 status = vchiq_initialise(&instance);
2267 if (status != VCHIQ_SUCCESS) {
2268 vchiq_log_error(vchiq_susp_log_level,
2269 "%s vchiq_initialise failed %d", __func__, status);
2273 status = vchiq_connect(instance);
2274 if (status != VCHIQ_SUCCESS) {
2275 vchiq_log_error(vchiq_susp_log_level,
2276 "%s vchiq_connect failed %d", __func__, status);
2280 status = vchiq_add_service(instance, ¶ms, &ka_handle);
2281 if (status != VCHIQ_SUCCESS) {
2282 vchiq_log_error(vchiq_susp_log_level,
2283 "%s vchiq_open_service failed %d", __func__, status);
2288 long rc = 0, uc = 0;
2290 if (wait_for_completion_interruptible(&arm_state->ka_evt)) {
2291 vchiq_log_error(vchiq_susp_log_level,
2292 "%s interrupted", __func__);
2293 flush_signals(current);
2297 /* read and clear counters. Do release_count then use_count to
2298 * prevent getting more releases than uses */
2299 rc = atomic_xchg(&arm_state->ka_release_count, 0);
2300 uc = atomic_xchg(&arm_state->ka_use_count, 0);
2302 /* Call use/release service the requisite number of times.
2303 * Process use before release so use counts don't go negative */
2305 atomic_inc(&arm_state->ka_use_ack_count);
2306 status = vchiq_use_service(ka_handle);
2307 if (status != VCHIQ_SUCCESS) {
2308 vchiq_log_error(vchiq_susp_log_level,
2309 "%s vchiq_use_service error %d",
2314 status = vchiq_release_service(ka_handle);
2315 if (status != VCHIQ_SUCCESS) {
2316 vchiq_log_error(vchiq_susp_log_level,
2317 "%s vchiq_release_service error %d",
2324 vchiq_shutdown(instance);
2330 vchiq_arm_init_state(struct vchiq_state *state,
2331 struct vchiq_arm_state *arm_state)
2334 rwlock_init(&arm_state->susp_res_lock);
2336 init_completion(&arm_state->ka_evt);
2337 atomic_set(&arm_state->ka_use_count, 0);
2338 atomic_set(&arm_state->ka_use_ack_count, 0);
2339 atomic_set(&arm_state->ka_release_count, 0);
2341 arm_state->state = state;
2342 arm_state->first_connect = 0;
2345 return VCHIQ_SUCCESS;
2349 vchiq_use_internal(struct vchiq_state *state, struct vchiq_service *service,
2350 enum USE_TYPE_E use_type)
2352 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2353 enum vchiq_status ret = VCHIQ_SUCCESS;
2356 int local_uc, local_entity_uc;
2361 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2363 if (use_type == USE_TYPE_VCHIQ) {
2364 sprintf(entity, "VCHIQ: ");
2365 entity_uc = &arm_state->peer_use_count;
2366 } else if (service) {
2367 sprintf(entity, "%c%c%c%c:%03d",
2368 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2369 service->client_id);
2370 entity_uc = &service->service_use_count;
2372 vchiq_log_error(vchiq_susp_log_level, "%s null service "
2378 write_lock_bh(&arm_state->susp_res_lock);
2379 local_uc = ++arm_state->videocore_use_count;
2380 local_entity_uc = ++(*entity_uc);
2382 vchiq_log_trace(vchiq_susp_log_level,
2383 "%s %s count %d, state count %d",
2384 __func__, entity, *entity_uc, local_uc);
2386 write_unlock_bh(&arm_state->susp_res_lock);
2388 if (ret == VCHIQ_SUCCESS) {
2389 enum vchiq_status status = VCHIQ_SUCCESS;
2390 long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
2392 while (ack_cnt && (status == VCHIQ_SUCCESS)) {
2393 /* Send the use notify to videocore */
2394 status = vchiq_send_remote_use_active(state);
2395 if (status == VCHIQ_SUCCESS)
2399 &arm_state->ka_use_ack_count);
2404 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2409 vchiq_release_internal(struct vchiq_state *state, struct vchiq_service *service)
2411 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2412 enum vchiq_status ret = VCHIQ_SUCCESS;
2419 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2422 sprintf(entity, "%c%c%c%c:%03d",
2423 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2424 service->client_id);
2425 entity_uc = &service->service_use_count;
2427 sprintf(entity, "PEER: ");
2428 entity_uc = &arm_state->peer_use_count;
2431 write_lock_bh(&arm_state->susp_res_lock);
2432 if (!arm_state->videocore_use_count || !(*entity_uc)) {
2433 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2434 WARN_ON(!arm_state->videocore_use_count);
2435 WARN_ON(!(*entity_uc));
2439 --arm_state->videocore_use_count;
2442 vchiq_log_trace(vchiq_susp_log_level,
2443 "%s %s count %d, state count %d",
2444 __func__, entity, *entity_uc,
2445 arm_state->videocore_use_count);
2448 write_unlock_bh(&arm_state->susp_res_lock);
2451 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2456 vchiq_on_remote_use(struct vchiq_state *state)
2458 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2460 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2461 atomic_inc(&arm_state->ka_use_count);
2462 complete(&arm_state->ka_evt);
2466 vchiq_on_remote_release(struct vchiq_state *state)
2468 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2470 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2471 atomic_inc(&arm_state->ka_release_count);
2472 complete(&arm_state->ka_evt);
2476 vchiq_use_service_internal(struct vchiq_service *service)
2478 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
2482 vchiq_release_service_internal(struct vchiq_service *service)
2484 return vchiq_release_internal(service->state, service);
2487 struct vchiq_debugfs_node *
2488 vchiq_instance_get_debugfs_node(struct vchiq_instance *instance)
2490 return &instance->debugfs_node;
2494 vchiq_instance_get_use_count(struct vchiq_instance *instance)
2496 struct vchiq_service *service;
2497 int use_count = 0, i;
2501 while ((service = __next_service_by_instance(instance->state,
2503 use_count += service->service_use_count;
2509 vchiq_instance_get_pid(struct vchiq_instance *instance)
2511 return instance->pid;
2515 vchiq_instance_get_trace(struct vchiq_instance *instance)
2517 return instance->trace;
2521 vchiq_instance_set_trace(struct vchiq_instance *instance, int trace)
2523 struct vchiq_service *service;
2528 while ((service = __next_service_by_instance(instance->state,
2530 service->trace = trace;
2532 instance->trace = (trace != 0);
2536 vchiq_use_service(unsigned int handle)
2538 enum vchiq_status ret = VCHIQ_ERROR;
2539 struct vchiq_service *service = find_service_by_handle(handle);
2542 ret = vchiq_use_internal(service->state, service,
2544 unlock_service(service);
2550 vchiq_release_service(unsigned int handle)
2552 enum vchiq_status ret = VCHIQ_ERROR;
2553 struct vchiq_service *service = find_service_by_handle(handle);
2556 ret = vchiq_release_internal(service->state, service);
2557 unlock_service(service);
2562 struct service_data_struct {
2569 vchiq_dump_service_use_state(struct vchiq_state *state)
2571 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2572 struct service_data_struct *service_data;
2574 /* If there's more than 64 services, only dump ones with
2575 * non-zero counts */
2576 int only_nonzero = 0;
2577 static const char *nz = "<-- preventing suspend";
2581 int active_services;
2586 service_data = kmalloc_array(MAX_SERVICES, sizeof(*service_data),
2591 read_lock_bh(&arm_state->susp_res_lock);
2592 peer_count = arm_state->peer_use_count;
2593 vc_use_count = arm_state->videocore_use_count;
2594 active_services = state->unused_service;
2595 if (active_services > MAX_SERVICES)
2599 for (i = 0; i < active_services; i++) {
2600 struct vchiq_service *service_ptr =
2601 rcu_dereference(state->services[i]);
2606 if (only_nonzero && !service_ptr->service_use_count)
2609 if (service_ptr->srvstate == VCHIQ_SRVSTATE_FREE)
2612 service_data[found].fourcc = service_ptr->base.fourcc;
2613 service_data[found].clientid = service_ptr->client_id;
2614 service_data[found].use_count = service_ptr->service_use_count;
2616 if (found >= MAX_SERVICES)
2621 read_unlock_bh(&arm_state->susp_res_lock);
2624 vchiq_log_warning(vchiq_susp_log_level, "Too many active "
2625 "services (%d). Only dumping up to first %d services "
2626 "with non-zero use-count", active_services, found);
2628 for (i = 0; i < found; i++) {
2629 vchiq_log_warning(vchiq_susp_log_level,
2630 "----- %c%c%c%c:%d service count %d %s",
2631 VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
2632 service_data[i].clientid,
2633 service_data[i].use_count,
2634 service_data[i].use_count ? nz : "");
2636 vchiq_log_warning(vchiq_susp_log_level,
2637 "----- VCHIQ use count count %d", peer_count);
2638 vchiq_log_warning(vchiq_susp_log_level,
2639 "--- Overall vchiq instance use count %d", vc_use_count);
2641 kfree(service_data);
2645 vchiq_check_service(struct vchiq_service *service)
2647 struct vchiq_arm_state *arm_state;
2648 enum vchiq_status ret = VCHIQ_ERROR;
2650 if (!service || !service->state)
2653 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2655 arm_state = vchiq_platform_get_arm_state(service->state);
2657 read_lock_bh(&arm_state->susp_res_lock);
2658 if (service->service_use_count)
2659 ret = VCHIQ_SUCCESS;
2660 read_unlock_bh(&arm_state->susp_res_lock);
2662 if (ret == VCHIQ_ERROR) {
2663 vchiq_log_error(vchiq_susp_log_level,
2664 "%s ERROR - %c%c%c%c:%d service count %d, "
2665 "state count %d", __func__,
2666 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2667 service->client_id, service->service_use_count,
2668 arm_state->videocore_use_count);
2669 vchiq_dump_service_use_state(service->state);
2675 void vchiq_platform_conn_state_changed(struct vchiq_state *state,
2676 enum vchiq_connstate oldstate,
2677 enum vchiq_connstate newstate)
2679 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
2680 char threadname[16];
2682 vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
2683 get_conn_state_name(oldstate), get_conn_state_name(newstate));
2684 if (state->conn_state != VCHIQ_CONNSTATE_CONNECTED)
2687 write_lock_bh(&arm_state->susp_res_lock);
2688 if (arm_state->first_connect) {
2689 write_unlock_bh(&arm_state->susp_res_lock);
2693 arm_state->first_connect = 1;
2694 write_unlock_bh(&arm_state->susp_res_lock);
2695 snprintf(threadname, sizeof(threadname), "vchiq-keep/%d",
2697 arm_state->ka_thread = kthread_create(&vchiq_keepalive_thread_func,
2700 if (IS_ERR(arm_state->ka_thread)) {
2701 vchiq_log_error(vchiq_susp_log_level,
2702 "vchiq: FATAL: couldn't create thread %s",
2705 wake_up_process(arm_state->ka_thread);
2709 static const struct of_device_id vchiq_of_match[] = {
2710 { .compatible = "brcm,bcm2835-vchiq", .data = &bcm2835_drvdata },
2711 { .compatible = "brcm,bcm2836-vchiq", .data = &bcm2836_drvdata },
2714 MODULE_DEVICE_TABLE(of, vchiq_of_match);
2716 static struct platform_device *
2717 vchiq_register_child(struct platform_device *pdev, const char *name)
2719 struct platform_device_info pdevinfo;
2720 struct platform_device *child;
2722 memset(&pdevinfo, 0, sizeof(pdevinfo));
2724 pdevinfo.parent = &pdev->dev;
2725 pdevinfo.name = name;
2726 pdevinfo.id = PLATFORM_DEVID_NONE;
2727 pdevinfo.dma_mask = DMA_BIT_MASK(32);
2729 child = platform_device_register_full(&pdevinfo);
2730 if (IS_ERR(child)) {
2731 dev_warn(&pdev->dev, "%s not registered\n", name);
2738 static int vchiq_probe(struct platform_device *pdev)
2740 struct device_node *fw_node;
2741 const struct of_device_id *of_id;
2742 struct vchiq_drvdata *drvdata;
2743 struct device *vchiq_dev;
2746 of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
2747 drvdata = (struct vchiq_drvdata *)of_id->data;
2751 fw_node = of_find_compatible_node(NULL, NULL,
2752 "raspberrypi,bcm2835-firmware");
2754 dev_err(&pdev->dev, "Missing firmware node\n");
2758 drvdata->fw = rpi_firmware_get(fw_node);
2759 of_node_put(fw_node);
2761 return -EPROBE_DEFER;
2763 platform_set_drvdata(pdev, drvdata);
2765 err = vchiq_platform_init(pdev, &g_state);
2767 goto failed_platform_init;
2769 cdev_init(&vchiq_cdev, &vchiq_fops);
2770 vchiq_cdev.owner = THIS_MODULE;
2771 err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
2773 vchiq_log_error(vchiq_arm_log_level,
2774 "Unable to register device");
2775 goto failed_platform_init;
2778 vchiq_dev = device_create(vchiq_class, &pdev->dev, vchiq_devid, NULL,
2780 if (IS_ERR(vchiq_dev)) {
2781 err = PTR_ERR(vchiq_dev);
2782 goto failed_device_create;
2785 vchiq_debugfs_init();
2787 vchiq_log_info(vchiq_arm_log_level,
2788 "vchiq: initialised - version %d (min %d), device %d.%d",
2789 VCHIQ_VERSION, VCHIQ_VERSION_MIN,
2790 MAJOR(vchiq_devid), MINOR(vchiq_devid));
2792 bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
2793 bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
2797 failed_device_create:
2798 cdev_del(&vchiq_cdev);
2799 failed_platform_init:
2800 vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
2804 static int vchiq_remove(struct platform_device *pdev)
2806 platform_device_unregister(bcm2835_camera);
2807 vchiq_debugfs_deinit();
2808 device_destroy(vchiq_class, vchiq_devid);
2809 cdev_del(&vchiq_cdev);
2814 static struct platform_driver vchiq_driver = {
2816 .name = "bcm2835_vchiq",
2817 .of_match_table = vchiq_of_match,
2819 .probe = vchiq_probe,
2820 .remove = vchiq_remove,
2823 static int __init vchiq_driver_init(void)
2827 vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
2828 if (IS_ERR(vchiq_class)) {
2829 pr_err("Failed to create vchiq class\n");
2830 return PTR_ERR(vchiq_class);
2833 ret = alloc_chrdev_region(&vchiq_devid, 0, 1, DEVICE_NAME);
2835 pr_err("Failed to allocate vchiq's chrdev region\n");
2839 ret = platform_driver_register(&vchiq_driver);
2841 pr_err("Failed to register vchiq driver\n");
2842 goto region_unregister;
2848 unregister_chrdev_region(vchiq_devid, 1);
2851 class_destroy(vchiq_class);
2855 module_init(vchiq_driver_init);
2857 static void __exit vchiq_driver_exit(void)
2859 platform_driver_unregister(&vchiq_driver);
2860 unregister_chrdev_region(vchiq_devid, 1);
2861 class_destroy(vchiq_class);
2863 module_exit(vchiq_driver_exit);
2865 MODULE_LICENSE("Dual BSD/GPL");
2866 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2867 MODULE_AUTHOR("Broadcom Corporation");