seq_system.c 5.3 KB

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