Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / sound / core / seq / seq_system.c
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 <sound/driver.h>
23 #include <linux/init.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 */
61 static int sysclient = -1;
62
63 /* port id numbers for this client */
64 static int announce_port = -1;
65
66
67
68 /* fill standard header data, source port & channel are filled in */
69 static int setheader(snd_seq_event_t * ev, int client, int port)
70 {
71         if (announce_port < 0)
72                 return -ENODEV;
73
74         memset(ev, 0, sizeof(snd_seq_event_t));
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 */
93 void snd_seq_system_broadcast(int client, int port, int type)
94 {
95         snd_seq_event_t 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 */
104 int snd_seq_system_notify(int client, int port, snd_seq_event_t *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 */
115 static int event_input_timer(snd_seq_event_t * 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 */
121 int __init snd_seq_system_client_init(void)
122 {
123
124         snd_seq_client_callback_t callbacks;
125         snd_seq_port_callback_t pcallbacks;
126         snd_seq_client_info_t *inf;
127         snd_seq_port_info_t *port;
128
129         inf = kcalloc(1, sizeof(*inf), GFP_KERNEL);
130         port = kcalloc(1, sizeof(*port), GFP_KERNEL);
131         if (! inf || ! port) {
132                 kfree(inf);
133                 kfree(port);
134                 return -ENOMEM;
135         }
136
137         memset(&callbacks, 0, sizeof(callbacks));
138         memset(&pcallbacks, 0, sizeof(pcallbacks));
139         pcallbacks.owner = THIS_MODULE;
140         pcallbacks.event_input = event_input_timer;
141
142         /* register client */
143         callbacks.allow_input = callbacks.allow_output = 1;
144         sysclient = snd_seq_create_kernel_client(NULL, 0, &callbacks);
145
146         /* set our name */
147         inf->client = 0;
148         inf->type = KERNEL_CLIENT;
149         strcpy(inf->name, "System");
150         snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, inf);
151
152         /* register timer */
153         strcpy(port->name, "Timer");
154         port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
155         port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
156         port->kernel = &pcallbacks;
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_TIMER;
161         snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
162
163         /* register announcement port */
164         strcpy(port->name, "Announce");
165         port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
166         port->kernel = NULL;
167         port->type = 0;
168         port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
169         port->addr.client = sysclient;
170         port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
171         snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
172         announce_port = port->addr.port;
173
174         kfree(inf);
175         kfree(port);
176         return 0;
177 }
178
179
180 /* unregister our internal client */
181 void __exit snd_seq_system_client_done(void)
182 {
183         int oldsysclient = sysclient;
184
185         if (oldsysclient >= 0) {
186                 sysclient = -1;
187                 announce_port = -1;
188                 snd_seq_delete_kernel_client(oldsysclient);
189         }
190 }