| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* ALSA sequencer System services Client |
| 4 |
* Copyright (c) 1998-1999 by Frank van de Pol <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/init.h> |
| 8 |
#include <linux/export.h> |
| 9 |
#include <linux/slab.h> |
| 10 |
#include <sound/core.h> |
| 11 |
#include "seq_system.h" |
| 12 |
#include "seq_timer.h" |
| 13 |
#include "seq_queue.h" |
| 14 |
|
| 15 |
/* internal client that provide system services, access to timer etc. */ |
| 16 |
|
| 17 |
/* |
| 18 |
* Port "Timer" |
| 19 |
* - send tempo /start/stop etc. events to this port to manipulate the |
| 20 |
* queue's timer. The queue address is specified in |
| 21 |
* data.queue.queue. |
| 22 |
* - this port supports subscription. The received timer events are |
| 23 |
* broadcasted to all subscribed clients. The modified tempo |
| 24 |
* value is stored on data.queue.value. |
| 25 |
* The modifier client/port is not send. |
| 26 |
* |
| 27 |
* Port "Announce" |
| 28 |
* - does not receive message |
| 29 |
* - supports supscription. For each client or port attaching to or |
| 30 |
* detaching from the system an announcement is send to the subscribed |
| 31 |
* clients. |
| 32 |
* |
| 33 |
* Idea: the subscription mechanism might also work handy for distributing |
| 34 |
* synchronisation and timing information. In this case we would ideally have |
| 35 |
* a list of subscribers for each type of sync (time, tick), for each timing |
| 36 |
* queue. |
| 37 |
* |
| 38 |
* NOTE: the queue to be started, stopped, etc. must be specified |
| 39 |
* in data.queue.addr.queue field. queue is used only for |
| 40 |
* scheduling, and no longer referred as affected queue. |
| 41 |
* They are used only for timer broadcast (see above). |
| 42 |
* -- iwai |
| 43 |
*/ |
| 44 |
|
| 45 |
|
| 46 |
/* client id of our system client */ |
| 47 |
static int sysclient = -1; |
| 48 |
|
| 49 |
/* port id numbers for this client */ |
| 50 |
static int announce_port = -1; |
| 51 |
|
| 52 |
/* number of subscriptions to announce port */ |
| 53 |
static int announce_subscribed; |
| 54 |
|
| 55 |
|
| 56 |
/* fill standard header data, source port & channel are filled in */ |
| 57 |
static int setheader(struct snd_seq_event * ev, int client, int port) |
| 58 |
{ |
| 59 |
if (announce_port < 0 || !announce_subscribed) |
| 60 |
return -ENODEV; |
| 61 |
|
| 62 |
memset(ev, 0, sizeof(struct snd_seq_event)); |
| 63 |
|
| 64 |
ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK; |
| 65 |
ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED; |
| 66 |
|
| 67 |
ev->source.client = sysclient; |
| 68 |
ev->source.port = announce_port; |
| 69 |
ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS; |
| 70 |
|
| 71 |
/* fill data */ |
| 72 |
/*ev->data.addr.queue = SNDRV_SEQ_ADDRESS_UNKNOWN;*/ |
| 73 |
ev->data.addr.client = client; |
| 74 |
ev->data.addr.port = port; |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/* entry points for broadcasting system events */ |
| 81 |
void snd_seq_system_broadcast(int client, int port, int type, bool atomic) |
| 82 |
{ |
| 83 |
struct snd_seq_event ev; |
| 84 |
|
| 85 |
if (setheader(&ev, client, port) < 0) |
| 86 |
return; |
| 87 |
ev.type = type; |
| 88 |
snd_seq_kernel_client_dispatch(sysclient, &ev, atomic, 0); |
| 89 |
} |
| 90 |
EXPORT_SYMBOL_GPL(snd_seq_system_broadcast); |
| 91 |
|
| 92 |
/* entry points for broadcasting system events */ |
| 93 |
int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev, |
| 94 |
bool atomic) |
| 95 |
{ |
| 96 |
ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED; |
| 97 |
ev->source.client = sysclient; |
| 98 |
ev->source.port = announce_port; |
| 99 |
ev->dest.client = client; |
| 100 |
ev->dest.port = port; |
| 101 |
return snd_seq_kernel_client_dispatch(sysclient, ev, atomic, 0); |
| 102 |
} |
| 103 |
|
| 104 |
/* call-back handler for timer events */ |
| 105 |
static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop) |
| 106 |
{ |
| 107 |
return snd_seq_control_queue(ev, atomic, hop); |
| 108 |
} |
| 109 |
|
| 110 |
static int sys_announce_subscribe(void *private_data, |
| 111 |
struct snd_seq_port_subscribe *info) |
| 112 |
{ |
| 113 |
announce_subscribed++; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
static int sys_announce_unsubscribe(void *private_data, |
| 118 |
struct snd_seq_port_subscribe *info) |
| 119 |
{ |
| 120 |
if (snd_BUG_ON(!announce_subscribed)) |
| 121 |
return 0; |
| 122 |
announce_subscribed--; |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
/* register our internal client */ |
| 127 |
int __init snd_seq_system_client_init(void) |
| 128 |
{ |
| 129 |
struct snd_seq_port_callback pcallbacks; |
| 130 |
struct snd_seq_port_info *port; |
| 131 |
int err; |
| 132 |
|
| 133 |
port = kzalloc_obj(*port); |
| 134 |
if (!port) |
| 135 |
return -ENOMEM; |
| 136 |
|
| 137 |
memset(&pcallbacks, 0, sizeof(pcallbacks)); |
| 138 |
pcallbacks.owner = THIS_MODULE; |
| 139 |
pcallbacks.event_input = event_input_timer; |
| 140 |
|
| 141 |
/* register client */ |
| 142 |
sysclient = snd_seq_create_kernel_client(NULL, 0, "System"); |
| 143 |
if (sysclient < 0) { |
| 144 |
kfree(port); |
| 145 |
return sysclient; |
| 146 |
} |
| 147 |
|
| 148 |
/* register timer */ |
| 149 |
strscpy(port->name, "Timer"); |
| 150 |
port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */ |
| 151 |
port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */ |
| 152 |
port->kernel = &pcallbacks; |
| 153 |
port->type = 0; |
| 154 |
port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; |
| 155 |
port->addr.client = sysclient; |
| 156 |
port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER; |
| 157 |
err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, |
| 158 |
port); |
| 159 |
if (err < 0) |
| 160 |
goto error_port; |
| 161 |
|
| 162 |
/* register announcement port */ |
| 163 |
strscpy(port->name, "Announce"); |
| 164 |
port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */ |
| 165 |
pcallbacks.event_input = NULL; |
| 166 |
pcallbacks.subscribe = sys_announce_subscribe; |
| 167 |
pcallbacks.unsubscribe = sys_announce_unsubscribe; |
| 168 |
port->kernel = &pcallbacks; |
| 169 |
port->type = 0; |
| 170 |
port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT; |
| 171 |
port->addr.client = sysclient; |
| 172 |
port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE; |
| 173 |
err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, |
| 174 |
port); |
| 175 |
if (err < 0) |
| 176 |
goto error_port; |
| 177 |
announce_port = port->addr.port; |
| 178 |
|
| 179 |
kfree(port); |
| 180 |
return 0; |
| 181 |
|
| 182 |
error_port: |
| 183 |
snd_seq_system_client_done(); |
| 184 |
kfree(port); |
| 185 |
return err; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
/* unregister our internal client */ |
| 190 |
void snd_seq_system_client_done(void) |
| 191 |
{ |
| 192 |
int oldsysclient = sysclient; |
| 193 |
|
| 194 |
if (oldsysclient >= 0) { |
| 195 |
sysclient = -1; |
| 196 |
announce_port = -1; |
| 197 |
snd_seq_delete_kernel_client(oldsysclient); |
| 198 |
} |
| 199 |
} |