seq_oss_readq.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * OSS compatible sequencer driver
  3. *
  4. * seq_oss_readq.c - MIDI input queue
  5. *
  6. * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "seq_oss_readq.h"
  23. #include "seq_oss_event.h"
  24. #include <sound/seq_oss_legacy.h>
  25. #include "../seq_lock.h"
  26. #include <linux/wait.h>
  27. #include <linux/slab.h>
  28. /*
  29. * constants
  30. */
  31. //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1)
  32. #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
  33. /*
  34. * prototypes
  35. */
  36. /*
  37. * create a read queue
  38. */
  39. struct seq_oss_readq *
  40. snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen)
  41. {
  42. struct seq_oss_readq *q;
  43. q = kzalloc(sizeof(*q), GFP_KERNEL);
  44. if (!q)
  45. return NULL;
  46. q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL);
  47. if (!q->q) {
  48. kfree(q);
  49. return NULL;
  50. }
  51. q->maxlen = maxlen;
  52. q->qlen = 0;
  53. q->head = q->tail = 0;
  54. init_waitqueue_head(&q->midi_sleep);
  55. spin_lock_init(&q->lock);
  56. q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT;
  57. q->input_time = (unsigned long)-1;
  58. return q;
  59. }
  60. /*
  61. * delete the read queue
  62. */
  63. void
  64. snd_seq_oss_readq_delete(struct seq_oss_readq *q)
  65. {
  66. if (q) {
  67. kfree(q->q);
  68. kfree(q);
  69. }
  70. }
  71. /*
  72. * reset the read queue
  73. */
  74. void
  75. snd_seq_oss_readq_clear(struct seq_oss_readq *q)
  76. {
  77. if (q->qlen) {
  78. q->qlen = 0;
  79. q->head = q->tail = 0;
  80. }
  81. /* if someone sleeping, wake'em up */
  82. wake_up(&q->midi_sleep);
  83. q->input_time = (unsigned long)-1;
  84. }
  85. /*
  86. * put a midi byte
  87. */
  88. int
  89. snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
  90. {
  91. union evrec rec;
  92. int result;
  93. memset(&rec, 0, sizeof(rec));
  94. rec.c[0] = SEQ_MIDIPUTC;
  95. rec.c[2] = dev;
  96. while (len-- > 0) {
  97. rec.c[1] = *data++;
  98. result = snd_seq_oss_readq_put_event(q, &rec);
  99. if (result < 0)
  100. return result;
  101. }
  102. return 0;
  103. }
  104. /*
  105. * put MIDI sysex bytes; the event buffer may be chained, thus it has
  106. * to be expanded via snd_seq_dump_var_event().
  107. */
  108. struct readq_sysex_ctx {
  109. struct seq_oss_readq *readq;
  110. int dev;
  111. };
  112. static int readq_dump_sysex(void *ptr, void *buf, int count)
  113. {
  114. struct readq_sysex_ctx *ctx = ptr;
  115. return snd_seq_oss_readq_puts(ctx->readq, ctx->dev, buf, count);
  116. }
  117. int snd_seq_oss_readq_sysex(struct seq_oss_readq *q, int dev,
  118. struct snd_seq_event *ev)
  119. {
  120. struct readq_sysex_ctx ctx = {
  121. .readq = q,
  122. .dev = dev
  123. };
  124. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  125. return 0;
  126. return snd_seq_dump_var_event(ev, readq_dump_sysex, &ctx);
  127. }
  128. /*
  129. * copy an event to input queue:
  130. * return zero if enqueued
  131. */
  132. int
  133. snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
  134. {
  135. unsigned long flags;
  136. spin_lock_irqsave(&q->lock, flags);
  137. if (q->qlen >= q->maxlen - 1) {
  138. spin_unlock_irqrestore(&q->lock, flags);
  139. return -ENOMEM;
  140. }
  141. memcpy(&q->q[q->tail], ev, sizeof(*ev));
  142. q->tail = (q->tail + 1) % q->maxlen;
  143. q->qlen++;
  144. /* wake up sleeper */
  145. wake_up(&q->midi_sleep);
  146. spin_unlock_irqrestore(&q->lock, flags);
  147. return 0;
  148. }
  149. /*
  150. * pop queue
  151. * caller must hold lock
  152. */
  153. int
  154. snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
  155. {
  156. if (q->qlen == 0)
  157. return -EAGAIN;
  158. memcpy(rec, &q->q[q->head], sizeof(*rec));
  159. return 0;
  160. }
  161. /*
  162. * sleep until ready
  163. */
  164. void
  165. snd_seq_oss_readq_wait(struct seq_oss_readq *q)
  166. {
  167. wait_event_interruptible_timeout(q->midi_sleep,
  168. (q->qlen > 0 || q->head == q->tail),
  169. q->pre_event_timeout);
  170. }
  171. /*
  172. * drain one record
  173. * caller must hold lock
  174. */
  175. void
  176. snd_seq_oss_readq_free(struct seq_oss_readq *q)
  177. {
  178. if (q->qlen > 0) {
  179. q->head = (q->head + 1) % q->maxlen;
  180. q->qlen--;
  181. }
  182. }
  183. /*
  184. * polling/select:
  185. * return non-zero if readq is not empty.
  186. */
  187. unsigned int
  188. snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
  189. {
  190. poll_wait(file, &q->midi_sleep, wait);
  191. return q->qlen;
  192. }
  193. /*
  194. * put a timestamp
  195. */
  196. int
  197. snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
  198. {
  199. if (curt != q->input_time) {
  200. union evrec rec;
  201. memset(&rec, 0, sizeof(rec));
  202. switch (seq_mode) {
  203. case SNDRV_SEQ_OSS_MODE_SYNTH:
  204. rec.echo = (curt << 8) | SEQ_WAIT;
  205. snd_seq_oss_readq_put_event(q, &rec);
  206. break;
  207. case SNDRV_SEQ_OSS_MODE_MUSIC:
  208. rec.t.code = EV_TIMING;
  209. rec.t.cmd = TMR_WAIT_ABS;
  210. rec.t.time = curt;
  211. snd_seq_oss_readq_put_event(q, &rec);
  212. break;
  213. }
  214. q->input_time = curt;
  215. }
  216. return 0;
  217. }
  218. #ifdef CONFIG_SND_PROC_FS
  219. /*
  220. * proc interface
  221. */
  222. void
  223. snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
  224. {
  225. snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
  226. (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
  227. q->qlen, q->input_time);
  228. }
  229. #endif /* CONFIG_SND_PROC_FS */