seq_system.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(snd_seq_event_t * ev, int client, int port)
  61. {
  62. if (announce_port < 0)
  63. return -ENODEV;
  64. memset(ev, 0, sizeof(snd_seq_event_t));
  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. snd_seq_event_t 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, snd_seq_event_t *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(snd_seq_event_t * 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. snd_seq_client_callback_t callbacks;
  104. snd_seq_port_callback_t pcallbacks;
  105. snd_seq_client_info_t *inf;
  106. snd_seq_port_info_t *port;
  107. inf = kcalloc(1, sizeof(*inf), GFP_KERNEL);
  108. port = kcalloc(1, sizeof(*port), GFP_KERNEL);
  109. if (! inf || ! port) {
  110. kfree(inf);
  111. kfree(port);
  112. return -ENOMEM;
  113. }
  114. memset(&callbacks, 0, sizeof(callbacks));
  115. memset(&pcallbacks, 0, sizeof(pcallbacks));
  116. pcallbacks.owner = THIS_MODULE;
  117. pcallbacks.event_input = event_input_timer;
  118. /* register client */
  119. callbacks.allow_input = callbacks.allow_output = 1;
  120. sysclient = snd_seq_create_kernel_client(NULL, 0, &callbacks);
  121. /* set our name */
  122. inf->client = 0;
  123. inf->type = KERNEL_CLIENT;
  124. strcpy(inf->name, "System");
  125. snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, inf);
  126. /* register timer */
  127. strcpy(port->name, "Timer");
  128. port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* accept queue control */
  129. port->capability |= SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast */
  130. port->kernel = &pcallbacks;
  131. port->type = 0;
  132. port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
  133. port->addr.client = sysclient;
  134. port->addr.port = SNDRV_SEQ_PORT_SYSTEM_TIMER;
  135. snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
  136. /* register announcement port */
  137. strcpy(port->name, "Announce");
  138. port->capability = SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ; /* for broadcast only */
  139. port->kernel = NULL;
  140. port->type = 0;
  141. port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
  142. port->addr.client = sysclient;
  143. port->addr.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
  144. snd_seq_kernel_client_ctl(sysclient, SNDRV_SEQ_IOCTL_CREATE_PORT, port);
  145. announce_port = port->addr.port;
  146. kfree(inf);
  147. kfree(port);
  148. return 0;
  149. }
  150. /* unregister our internal client */
  151. void __exit snd_seq_system_client_done(void)
  152. {
  153. int oldsysclient = sysclient;
  154. if (oldsysclient >= 0) {
  155. sysclient = -1;
  156. announce_port = -1;
  157. snd_seq_delete_kernel_client(oldsysclient);
  158. }
  159. }