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