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