seq_system.c 4.9 KB

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