Loading...
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 */
47static int sysclient = -1;
48
49/* port id numbers for this client */
50static int announce_port = -1;
51
52
53
54/* fill standard header data, source port & channel are filled in */
55static 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 */
79void 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
89/* entry points for broadcasting system events */
90int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev)
91{
92 ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
93 ev->source.client = sysclient;
94 ev->source.port = announce_port;
95 ev->dest.client = client;
96 ev->dest.port = port;
97 return snd_seq_kernel_client_dispatch(sysclient, ev, 0, 0);
98}
99
100/* call-back handler for timer events */
101static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop)
102{
103 return snd_seq_control_queue(ev, atomic, hop);
104}
105
106/* register our internal client */
107int __init snd_seq_system_client_init(void)
108{
109 struct snd_seq_port_callback pcallbacks;
110 struct snd_seq_port_info *port;
111 int err;
112
113 port = kzalloc(sizeof(*port), GFP_KERNEL);
114 if (!port)
115 return -ENOMEM;
116
117 memset(&pcallbacks, 0, sizeof(pcallbacks));
118 pcallbacks.owner = THIS_MODULE;
119 pcallbacks.event_input = event_input_timer;
120
121 /* register client */
122 sysclient = snd_seq_create_kernel_client(NULL, 0, "System");
123 if (sysclient < 0) {
124 kfree(port);
125 return sysclient;
126 }
127
128 /* register timer */
129 strcpy(port->name, "Timer");
130 port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
131 port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
132 port->kernel = &pcallbacks;
133 port->type = 0;
134 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
135 port->addr.client = sysclient;
136 port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
137 err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
138 port);
139 if (err < 0)
140 goto error_port;
141
142 /* register announcement port */
143 strcpy(port->name, "Announce");
144 port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
145 port->kernel = NULL;
146 port->type = 0;
147 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
148 port->addr.client = sysclient;
149 port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
150 err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
151 port);
152 if (err < 0)
153 goto error_port;
154 announce_port = port->addr.port;
155
156 kfree(port);
157 return 0;
158
159 error_port:
160 snd_seq_system_client_done();
161 kfree(port);
162 return err;
163}
164
165
166/* unregister our internal client */
167void snd_seq_system_client_done(void)
168{
169 int oldsysclient = sysclient;
170
171 if (oldsysclient >= 0) {
172 sysclient = -1;
173 announce_port = -1;
174 snd_seq_delete_kernel_client(oldsysclient);
175 }
176}
1/*
2 * ALSA sequencer System services Client
3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <sound/core.h>
25#include "seq_system.h"
26#include "seq_timer.h"
27#include "seq_queue.h"
28
29/* internal client that provide system services, access to timer etc. */
30
31/*
32 * Port "Timer"
33 * - send tempo /start/stop etc. events to this port to manipulate the
34 * queue's timer. The queue address is specified in
35 * data.queue.queue.
36 * - this port supports subscription. The received timer events are
37 * broadcasted to all subscribed clients. The modified tempo
38 * value is stored on data.queue.value.
39 * The modifier client/port is not send.
40 *
41 * Port "Announce"
42 * - does not receive message
43 * - supports supscription. For each client or port attaching to or
44 * detaching from the system an announcement is send to the subscribed
45 * clients.
46 *
47 * Idea: the subscription mechanism might also work handy for distributing
48 * synchronisation and timing information. In this case we would ideally have
49 * a list of subscribers for each type of sync (time, tick), for each timing
50 * queue.
51 *
52 * NOTE: the queue to be started, stopped, etc. must be specified
53 * in data.queue.addr.queue field. queue is used only for
54 * scheduling, and no longer referred as affected queue.
55 * They are used only for timer broadcast (see above).
56 * -- iwai
57 */
58
59
60/* client id of our system client */
61static int sysclient = -1;
62
63/* port id numbers for this client */
64static int announce_port = -1;
65
66
67
68/* fill standard header data, source port & channel are filled in */
69static int setheader(struct snd_seq_event * ev, int client, int port)
70{
71 if (announce_port < 0)
72 return -ENODEV;
73
74 memset(ev, 0, sizeof(struct snd_seq_event));
75
76 ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
77 ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
78
79 ev->source.client = sysclient;
80 ev->source.port = announce_port;
81 ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
82
83 /* fill data */
84 /*ev->data.addr.queue = SNDRV_SEQ_ADDRESS_UNKNOWN;*/
85 ev->data.addr.client = client;
86 ev->data.addr.port = port;
87
88 return 0;
89}
90
91
92/* entry points for broadcasting system events */
93void snd_seq_system_broadcast(int client, int port, int type)
94{
95 struct snd_seq_event ev;
96
97 if (setheader(&ev, client, port) < 0)
98 return;
99 ev.type = type;
100 snd_seq_kernel_client_dispatch(sysclient, &ev, 0, 0);
101}
102
103/* entry points for broadcasting system events */
104int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev)
105{
106 ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
107 ev->source.client = sysclient;
108 ev->source.port = announce_port;
109 ev->dest.client = client;
110 ev->dest.port = port;
111 return snd_seq_kernel_client_dispatch(sysclient, ev, 0, 0);
112}
113
114/* call-back handler for timer events */
115static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop)
116{
117 return snd_seq_control_queue(ev, atomic, hop);
118}
119
120/* register our internal client */
121int __init snd_seq_system_client_init(void)
122{
123 struct snd_seq_port_callback pcallbacks;
124 struct snd_seq_port_info *port;
125
126 port = kzalloc(sizeof(*port), GFP_KERNEL);
127 if (!port)
128 return -ENOMEM;
129
130 memset(&pcallbacks, 0, sizeof(pcallbacks));
131 pcallbacks.owner = THIS_MODULE;
132 pcallbacks.event_input = event_input_timer;
133
134 /* register client */
135 sysclient = snd_seq_create_kernel_client(NULL, 0, "System");
136
137 /* register timer */
138 strcpy(port->name, "Timer");
139 port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
140 port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
141 port->kernel = &pcallbacks;
142 port->type = 0;
143 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
144 port->addr.client = sysclient;
145 port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
146 snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
147
148 /* register announcement port */
149 strcpy(port->name, "Announce");
150 port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
151 port->kernel = NULL;
152 port->type = 0;
153 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
154 port->addr.client = sysclient;
155 port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
156 snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
157 announce_port = port->addr.port;
158
159 kfree(port);
160 return 0;
161}
162
163
164/* unregister our internal client */
165void __exit snd_seq_system_client_done(void)
166{
167 int oldsysclient = sysclient;
168
169 if (oldsysclient >= 0) {
170 sysclient = -1;
171 announce_port = -1;
172 snd_seq_delete_kernel_client(oldsysclient);
173 }
174}