Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   ALSA sequencer System services Client
0004  *   Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/export.h>
0009 #include <linux/slab.h>
0010 #include <sound/core.h>
0011 #include "seq_system.h"
0012 #include "seq_timer.h"
0013 #include "seq_queue.h"
0014 
0015 /* internal client that provide system services, access to timer etc. */
0016 
0017 /*
0018  * Port "Timer"
0019  *      - send tempo /start/stop etc. events to this port to manipulate the 
0020  *        queue's timer. The queue address is specified in
0021  *    data.queue.queue.
0022  *      - this port supports subscription. The received timer events are 
0023  *        broadcasted to all subscribed clients. The modified tempo
0024  *    value is stored on data.queue.value.
0025  *    The modifier client/port is not send.
0026  *
0027  * Port "Announce"
0028  *      - does not receive message
0029  *      - supports supscription. For each client or port attaching to or 
0030  *        detaching from the system an announcement is send to the subscribed
0031  *        clients.
0032  *
0033  * Idea: the subscription mechanism might also work handy for distributing 
0034  * synchronisation and timing information. In this case we would ideally have
0035  * a list of subscribers for each type of sync (time, tick), for each timing
0036  * queue.
0037  *
0038  * NOTE: the queue to be started, stopped, etc. must be specified
0039  *   in data.queue.addr.queue field.  queue is used only for
0040  *   scheduling, and no longer referred as affected queue.
0041  *   They are used only for timer broadcast (see above).
0042  *                          -- iwai
0043  */
0044 
0045 
0046 /* client id of our system client */
0047 static int sysclient = -1;
0048 
0049 /* port id numbers for this client */
0050 static int announce_port = -1;
0051 
0052 
0053 
0054 /* fill standard header data, source port & channel are filled in */
0055 static int setheader(struct snd_seq_event * ev, int client, int port)
0056 {
0057     if (announce_port < 0)
0058         return -ENODEV;
0059 
0060     memset(ev, 0, sizeof(struct snd_seq_event));
0061 
0062     ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
0063     ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
0064 
0065     ev->source.client = sysclient;
0066     ev->source.port = announce_port;
0067     ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
0068 
0069     /* fill data */
0070     /*ev->data.addr.queue = SNDRV_SEQ_ADDRESS_UNKNOWN;*/
0071     ev->data.addr.client = client;
0072     ev->data.addr.port = port;
0073 
0074     return 0;
0075 }
0076 
0077 
0078 /* entry points for broadcasting system events */
0079 void snd_seq_system_broadcast(int client, int port, int type)
0080 {
0081     struct snd_seq_event ev;
0082     
0083     if (setheader(&ev, client, port) < 0)
0084         return;
0085     ev.type = type;
0086     snd_seq_kernel_client_dispatch(sysclient, &ev, 0, 0);
0087 }
0088 
0089 /* entry points for broadcasting system events */
0090 int snd_seq_system_notify(int client, int port, struct snd_seq_event *ev)
0091 {
0092     ev->flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
0093     ev->source.client = sysclient;
0094     ev->source.port = announce_port;
0095     ev->dest.client = client;
0096     ev->dest.port = port;
0097     return snd_seq_kernel_client_dispatch(sysclient, ev, 0, 0);
0098 }
0099 
0100 /* call-back handler for timer events */
0101 static int event_input_timer(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop)
0102 {
0103     return snd_seq_control_queue(ev, atomic, hop);
0104 }
0105 
0106 /* register our internal client */
0107 int __init snd_seq_system_client_init(void)
0108 {
0109     struct snd_seq_port_callback pcallbacks;
0110     struct snd_seq_port_info *port;
0111     int err;
0112 
0113     port = kzalloc(sizeof(*port), GFP_KERNEL);
0114     if (!port)
0115         return -ENOMEM;
0116 
0117     memset(&pcallbacks, 0, sizeof(pcallbacks));
0118     pcallbacks.owner = THIS_MODULE;
0119     pcallbacks.event_input = event_input_timer;
0120 
0121     /* register client */
0122     sysclient = snd_seq_create_kernel_client(NULL, 0, "System");
0123     if (sysclient < 0) {
0124         kfree(port);
0125         return sysclient;
0126     }
0127 
0128     /* register timer */
0129     strcpy(port->name, "Timer");
0130     port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
0131     port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
0132     port->kernel = &pcallbacks;
0133     port->type = 0;
0134     port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
0135     port->addr.client = sysclient;
0136     port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
0137     err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
0138                     port);
0139     if (err < 0)
0140         goto error_port;
0141 
0142     /* register announcement port */
0143     strcpy(port->name, "Announce");
0144     port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
0145     port->kernel = NULL;
0146     port->type = 0;
0147     port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
0148     port->addr.client = sysclient;
0149     port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
0150     err = snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT,
0151                     port);
0152     if (err < 0)
0153         goto error_port;
0154     announce_port = port->addr.port;
0155 
0156     kfree(port);
0157     return 0;
0158 
0159  error_port:
0160     snd_seq_system_client_done();
0161     kfree(port);
0162     return err;
0163 }
0164 
0165 
0166 /* unregister our internal client */
0167 void snd_seq_system_client_done(void)
0168 {
0169     int oldsysclient = sysclient;
0170 
0171     if (oldsysclient >= 0) {
0172         sysclient = -1;
0173         announce_port = -1;
0174         snd_seq_delete_kernel_client(oldsysclient);
0175     }
0176 }