.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * VMware VMCI Driver |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2012 VMware, Inc. All rights reserved. |
---|
5 | | - * |
---|
6 | | - * This program is free software; you can redistribute it and/or modify it |
---|
7 | | - * under the terms of the GNU General Public License as published by the |
---|
8 | | - * Free Software Foundation version 2 and no later version. |
---|
9 | | - * |
---|
10 | | - * This program is distributed in the hope that it will be useful, but |
---|
11 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
12 | | - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
13 | | - * for more details. |
---|
14 | 6 | */ |
---|
15 | 7 | |
---|
16 | 8 | #include <linux/vmw_vmci_defs.h> |
---|
.. | .. |
---|
36 | 28 | static bool vmci_guest_personality_initialized; |
---|
37 | 29 | static bool vmci_host_personality_initialized; |
---|
38 | 30 | |
---|
| 31 | +static DEFINE_MUTEX(vmci_vsock_mutex); /* protects vmci_vsock_transport_cb */ |
---|
| 32 | +static vmci_vsock_cb vmci_vsock_transport_cb; |
---|
| 33 | +static bool vmci_vsock_cb_host_called; |
---|
| 34 | + |
---|
39 | 35 | /* |
---|
40 | 36 | * vmci_get_context_id() - Gets the current context ID. |
---|
41 | 37 | * |
---|
.. | .. |
---|
53 | 49 | } |
---|
54 | 50 | EXPORT_SYMBOL_GPL(vmci_get_context_id); |
---|
55 | 51 | |
---|
| 52 | +/* |
---|
| 53 | + * vmci_register_vsock_callback() - Register the VSOCK vmci_transport callback. |
---|
| 54 | + * |
---|
| 55 | + * The callback will be called when the first host or guest becomes active, |
---|
| 56 | + * or if they are already active when this function is called. |
---|
| 57 | + * To unregister the callback, call this function with NULL parameter. |
---|
| 58 | + * |
---|
| 59 | + * Returns 0 on success. -EBUSY if a callback is already registered. |
---|
| 60 | + */ |
---|
| 61 | +int vmci_register_vsock_callback(vmci_vsock_cb callback) |
---|
| 62 | +{ |
---|
| 63 | + int err = 0; |
---|
| 64 | + |
---|
| 65 | + mutex_lock(&vmci_vsock_mutex); |
---|
| 66 | + |
---|
| 67 | + if (vmci_vsock_transport_cb && callback) { |
---|
| 68 | + err = -EBUSY; |
---|
| 69 | + goto out; |
---|
| 70 | + } |
---|
| 71 | + |
---|
| 72 | + vmci_vsock_transport_cb = callback; |
---|
| 73 | + |
---|
| 74 | + if (!vmci_vsock_transport_cb) { |
---|
| 75 | + vmci_vsock_cb_host_called = false; |
---|
| 76 | + goto out; |
---|
| 77 | + } |
---|
| 78 | + |
---|
| 79 | + if (vmci_guest_code_active()) |
---|
| 80 | + vmci_vsock_transport_cb(false); |
---|
| 81 | + |
---|
| 82 | + if (vmci_host_users() > 0) { |
---|
| 83 | + vmci_vsock_cb_host_called = true; |
---|
| 84 | + vmci_vsock_transport_cb(true); |
---|
| 85 | + } |
---|
| 86 | + |
---|
| 87 | +out: |
---|
| 88 | + mutex_unlock(&vmci_vsock_mutex); |
---|
| 89 | + return err; |
---|
| 90 | +} |
---|
| 91 | +EXPORT_SYMBOL_GPL(vmci_register_vsock_callback); |
---|
| 92 | + |
---|
| 93 | +void vmci_call_vsock_callback(bool is_host) |
---|
| 94 | +{ |
---|
| 95 | + mutex_lock(&vmci_vsock_mutex); |
---|
| 96 | + |
---|
| 97 | + if (!vmci_vsock_transport_cb) |
---|
| 98 | + goto out; |
---|
| 99 | + |
---|
| 100 | + /* In the host, this function could be called multiple times, |
---|
| 101 | + * but we want to register it only once. |
---|
| 102 | + */ |
---|
| 103 | + if (is_host) { |
---|
| 104 | + if (vmci_vsock_cb_host_called) |
---|
| 105 | + goto out; |
---|
| 106 | + |
---|
| 107 | + vmci_vsock_cb_host_called = true; |
---|
| 108 | + } |
---|
| 109 | + |
---|
| 110 | + vmci_vsock_transport_cb(is_host); |
---|
| 111 | +out: |
---|
| 112 | + mutex_unlock(&vmci_vsock_mutex); |
---|
| 113 | +} |
---|
| 114 | + |
---|
56 | 115 | static int __init vmci_drv_init(void) |
---|
57 | 116 | { |
---|
58 | 117 | int vmci_err; |
---|