xref: /linux/sound/core/seq/seq_system.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 <fvdpol@coil.demon.nl>
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 
53 
54 /* fill standard header data, source port & channel are filled in */
55 static int setheader(struct snd_seq_event * ev, int client, int port)
56 {
57 	if (announce_port < 0)
58 		return -ENODEV;
59 
60 	memset(ev, 0, sizeof(struct snd_seq_event));
61 
62 	ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
63 	ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
64 
65 	ev->source.client = sysclient;
66 	ev->source.port = announce_port;
67 	ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
68 
69 	/* fill data */
70 	/*ev->data.addr.queue = SNDRV_SEQ_ADDRESS_UNKNOWN;*/
71 	ev->data.addr.client = client;
72 	ev->data.addr.port = port;
73 
74 	return 0;
75 }
76 
77 
78 /* entry points for broadcasting system events */
79 void snd_seq_system_broadcast(int client, int port, int type)
80 {
81 	struct snd_seq_event ev;
82 
83 	if (setheader(&ev, client, port) < 0)
84 		return;
85 	ev.type = type;
86 	snd_seq_kernel_client_dispatch(sysclient, &ev, 0, 0);
87 }
88 EXPORT_SYMBOL_GPL(snd_seq_system_broadcast);
89 
90 /* entry points for broadcasting system events */
91 int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev)
92 {
93 	ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
94 	ev->source.client = sysclient;
95 	ev->source.port = announce_port;
96 	ev->dest.client = client;
97 	ev->dest.port = port;
98 	return snd_seq_kernel_client_dispatch(sysclient, ev, 0, 0);
99 }
100 
101 /* call-back handler for timer events */
102 static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop)
103 {
104 	return snd_seq_control_queue(ev, atomic, hop);
105 }
106 
107 /* register our internal client */
108 int __init snd_seq_system_client_init(void)
109 {
110 	struct snd_seq_port_callback pcallbacks;
111 	struct snd_seq_port_info *port;
112 	int err;
113 
114 	port = kzalloc(sizeof(*port), GFP_KERNEL);
115 	if (!port)
116 		return -ENOMEM;
117 
118 	memset(&pcallbacks, 0, sizeof(pcallbacks));
119 	pcallbacks.owner = THIS_MODULE;
120 	pcallbacks.event_input = event_input_timer;
121 
122 	/* register client */
123 	sysclient = snd_seq_create_kernel_client(NULL, 0, "System");
124 	if (sysclient < 0) {
125 		kfree(port);
126 		return sysclient;
127 	}
128 
129 	/* register timer */
130 	strcpy(port->name, "Timer");
131 	port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
132 	port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
133 	port->kernel = &pcallbacks;
134 	port->type = 0;
135 	port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
136 	port->addr.client = sysclient;
137 	port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
138 	err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
139 					port);
140 	if (err < 0)
141 		goto error_port;
142 
143 	/* register announcement port */
144 	strcpy(port->name, "Announce");
145 	port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
146 	port->kernel = NULL;
147 	port->type = 0;
148 	port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
149 	port->addr.client = sysclient;
150 	port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
151 	err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
152 					port);
153 	if (err < 0)
154 		goto error_port;
155 	announce_port = port->addr.port;
156 
157 	kfree(port);
158 	return 0;
159 
160  error_port:
161 	snd_seq_system_client_done();
162 	kfree(port);
163 	return err;
164 }
165 
166 
167 /* unregister our internal client */
168 void snd_seq_system_client_done(void)
169 {
170 	int oldsysclient = sysclient;
171 
172 	if (oldsysclient >= 0) {
173 		sysclient = -1;
174 		announce_port = -1;
175 		snd_seq_delete_kernel_client(oldsysclient);
176 	}
177 }
178