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 23#include "seq_oss_readq.h" 24#include "seq_oss_event.h" 25#include <sound/seq_oss_legacy.h> 26#include "../seq_lock.h" 27#include <linux/wait.h> 28#include <linux/slab.h> 29 30/* 31 * constants 32 */ 33//#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1) 34#define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600) 35 36 37/* 38 * prototypes 39 */ 40 41 42/* 43 * create a read queue 44 */ 45struct seq_oss_readq * 46snd_seq_oss_readq_new(struct seq_oss_devinfo *dp, int maxlen) 47{ 48 struct seq_oss_readq *q; 49 50 q = kzalloc(sizeof(*q), GFP_KERNEL); 51 if (!q) 52 return NULL; 53 54 q->q = kcalloc(maxlen, sizeof(union evrec), GFP_KERNEL); 55 if (!q->q) { 56 kfree(q); 57 return NULL; 58 } 59 60 q->maxlen = maxlen; 61 q->qlen = 0; 62 q->head = q->tail = 0; 63 init_waitqueue_head(&q->midi_sleep); 64 spin_lock_init(&q->lock); 65 q->pre_event_timeout = SNDRV_SEQ_OSS_MAX_TIMEOUT; 66 q->input_time = (unsigned long)-1; 67 68 return q; 69} 70 71/* 72 * delete the read queue 73 */ 74void 75snd_seq_oss_readq_delete(struct seq_oss_readq *q) 76{ 77 if (q) { 78 kfree(q->q); 79 kfree(q); 80 } 81} 82 83/* 84 * reset the read queue 85 */ 86void 87snd_seq_oss_readq_clear(struct seq_oss_readq *q) 88{ 89 if (q->qlen) { 90 q->qlen = 0; 91 q->head = q->tail = 0; 92 } 93 /* if someone sleeping, wake'em up */ 94 wake_up(&q->midi_sleep); 95 q->input_time = (unsigned long)-1; 96} 97 98/* 99 * put a midi byte 100 */ 101int 102snd_seq_oss_readq_puts(struct seq_oss_readq *q, int dev, unsigned char *data, int len) 103{ 104 union evrec rec; 105 int result; 106 107 memset(&rec, 0, sizeof(rec)); 108 rec.c[0] = SEQ_MIDIPUTC; 109 rec.c[2] = dev; 110 111 while (len-- > 0) { 112 rec.c[1] = *data++; 113 result = snd_seq_oss_readq_put_event(q, &rec); 114 if (result < 0) 115 return result; 116 } 117 return 0; 118} 119 120/* 121 * copy an event to input queue: 122 * return zero if enqueued 123 */ 124int 125snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev) 126{ 127 unsigned long flags; 128 129 spin_lock_irqsave(&q->lock, flags); 130 if (q->qlen >= q->maxlen - 1) { 131 spin_unlock_irqrestore(&q->lock, flags); 132 return -ENOMEM; 133 } 134 135 memcpy(&q->q[q->tail], ev, sizeof(*ev)); 136 q->tail = (q->tail + 1) % q->maxlen; 137 q->qlen++; 138 139 /* wake up sleeper */ 140 wake_up(&q->midi_sleep); 141 142 spin_unlock_irqrestore(&q->lock, flags); 143 144 return 0; 145} 146 147 148/* 149 * pop queue 150 * caller must hold lock 151 */ 152int 153snd_seq_oss_readq_pick(struct seq_oss_readq *q, union evrec *rec) 154{ 155 if (q->qlen == 0) 156 return -EAGAIN; 157 memcpy(rec, &q->q[q->head], sizeof(*rec)); 158 return 0; 159} 160 161/* 162 * sleep until ready 163 */ 164void 165snd_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/* 173 * drain one record 174 * caller must hold lock 175 */ 176void 177snd_seq_oss_readq_free(struct seq_oss_readq *q) 178{ 179 if (q->qlen > 0) { 180 q->head = (q->head + 1) % q->maxlen; 181 q->qlen--; 182 } 183} 184 185/* 186 * polling/select: 187 * return non-zero if readq is not empty. 188 */ 189unsigned int 190snd_seq_oss_readq_poll(struct seq_oss_readq *q, struct file *file, poll_table *wait) 191{ 192 poll_wait(file, &q->midi_sleep, wait); 193 return q->qlen; 194} 195 196/* 197 * put a timestamp 198 */ 199int 200snd_seq_oss_readq_put_timestamp(struct seq_oss_readq *q, unsigned long curt, int seq_mode) 201{ 202 if (curt != q->input_time) { 203 union evrec rec; 204 memset(&rec, 0, sizeof(rec)); 205 switch (seq_mode) { 206 case SNDRV_SEQ_OSS_MODE_SYNTH: 207 rec.echo = (curt << 8) | SEQ_WAIT; 208 snd_seq_oss_readq_put_event(q, &rec); 209 break; 210 case SNDRV_SEQ_OSS_MODE_MUSIC: 211 rec.t.code = EV_TIMING; 212 rec.t.cmd = TMR_WAIT_ABS; 213 rec.t.time = curt; 214 snd_seq_oss_readq_put_event(q, &rec); 215 break; 216 } 217 q->input_time = curt; 218 } 219 return 0; 220} 221 222 223#ifdef CONFIG_SND_PROC_FS 224/* 225 * proc interface 226 */ 227void 228snd_seq_oss_readq_info_read(struct seq_oss_readq *q, struct snd_info_buffer *buf) 229{ 230 snd_iprintf(buf, " read queue [%s] length = %d : tick = %ld\n", 231 (waitqueue_active(&q->midi_sleep) ? "sleeping":"running"), 232 q->qlen, q->input_time); 233} 234#endif /* CONFIG_SND_PROC_FS */ 235