seq_system.c 5.3 KB

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