seq_oss_readq.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (waitqueue_active(&q->midi_sleep))
  83. wake_up(&q->midi_sleep);
  84. q->input_time = (unsigned long)-1;
  85. }
  86. /*
  87. * put a midi byte
  88. */
  89. int
  90. snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len)
  91. {
  92. union evrec rec;
  93. int result;
  94. memset(&rec, 0, sizeof(rec));
  95. rec.c[0] = SEQ_MIDIPUTC;
  96. rec.c[2] = dev;
  97. while (len-- > 0) {
  98. rec.c[1] = *data++;
  99. result = snd_seq_oss_readq_put_event(q, &rec);
  100. if (result < 0)
  101. return result;
  102. }
  103. return 0;
  104. }
  105. /*
  106. * copy an event to input queue:
  107. * return zero if enqueued
  108. */
  109. int
  110. snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
  111. {
  112. unsigned long flags;
  113. spin_lock_irqsave(&q->lock, flags);
  114. if (q->qlen >= q->maxlen - 1) {
  115. spin_unlock_irqrestore(&q->lock, flags);
  116. return -ENOMEM;
  117. }
  118. memcpy(&q->q[q->tail], ev, sizeof(*ev));
  119. q->tail = (q->tail + 1) % q->maxlen;
  120. q->qlen++;
  121. /* wake up sleeper */
  122. if (waitqueue_active(&q->midi_sleep))
  123. wake_up(&q->midi_sleep);
  124. spin_unlock_irqrestore(&q->lock, flags);
  125. return 0;
  126. }
  127. /*
  128. * pop queue
  129. * caller must hold lock
  130. */
  131. int
  132. snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec)
  133. {
  134. if (q->qlen == 0)
  135. return -EAGAIN;
  136. memcpy(rec, &q->q[q->head], sizeof(*rec));
  137. return 0;
  138. }
  139. /*
  140. * sleep until ready
  141. */
  142. void
  143. snd_seq_oss_readq_wait(struct seq_oss_readq *q)
  144. {
  145. wait_event_interruptible_timeout(q->midi_sleep,
  146. (q->qlen > 0 || q->head == q->tail),
  147. q->pre_event_timeout);
  148. }
  149. /*
  150. * drain one record
  151. * caller must hold lock
  152. */
  153. void
  154. snd_seq_oss_readq_free(struct seq_oss_readq *q)
  155. {
  156. if (q->qlen > 0) {
  157. q->head = (q->head + 1) % q->maxlen;
  158. q->qlen--;
  159. }
  160. }
  161. /*
  162. * polling/select:
  163. * return non-zero if readq is not empty.
  164. */
  165. unsigned int
  166. snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait)
  167. {
  168. poll_wait(file, &q->midi_sleep, wait);
  169. return q->qlen;
  170. }
  171. /*
  172. * put a timestamp
  173. */
  174. int
  175. snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode)
  176. {
  177. if (curt != q->input_time) {
  178. union evrec rec;
  179. memset(&rec, 0, sizeof(rec));
  180. switch (seq_mode) {
  181. case SNDRV_SEQ_OSS_MODE_SYNTH:
  182. rec.echo = (curt << 8) | SEQ_WAIT;
  183. snd_seq_oss_readq_put_event(q, &rec);
  184. break;
  185. case SNDRV_SEQ_OSS_MODE_MUSIC:
  186. rec.t.code = EV_TIMING;
  187. rec.t.cmd = TMR_WAIT_ABS;
  188. rec.t.time = curt;
  189. snd_seq_oss_readq_put_event(q, &rec);
  190. break;
  191. }
  192. q->input_time = curt;
  193. }
  194. return 0;
  195. }
  196. #ifdef CONFIG_SND_PROC_FS
  197. /*
  198. * proc interface
  199. */
  200. void
  201. snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf)
  202. {
  203. snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n",
  204. (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"),
  205. q->qlen, q->input_time);
  206. }
  207. #endif /* CONFIG_SND_PROC_FS */