2 * OSS compatible sequencer driver
4 * seq_oss_readq.c - MIDI input queue
6 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
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.
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.
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
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>
33 //#define SNDRV_SEQ_OSS_MAX_TIMEOUT (unsigned long)(-1)
34 #define SNDRV_SEQ_OSS_MAX_TIMEOUT (HZ * 3600)
45 struct seq_oss_readq
*
46 snd_seq_oss_readq_new(struct seq_oss_devinfo
*dp
, int maxlen
)
48 struct seq_oss_readq
*q
;
50 if ((q
= kzalloc(sizeof(*q
), GFP_KERNEL
)) == NULL
) {
51 snd_printk(KERN_ERR
"can't malloc read queue\n");
55 if ((q
->q
= kcalloc(maxlen
, sizeof(union evrec
), GFP_KERNEL
)) == NULL
) {
56 snd_printk(KERN_ERR
"can't malloc read queue buffer\n");
63 q
->head
= q
->tail
= 0;
64 init_waitqueue_head(&q
->midi_sleep
);
65 spin_lock_init(&q
->lock
);
66 q
->pre_event_timeout
= SNDRV_SEQ_OSS_MAX_TIMEOUT
;
67 q
->input_time
= (unsigned long)-1;
73 * delete the read queue
76 snd_seq_oss_readq_delete(struct seq_oss_readq
*q
)
85 * reset the read queue
88 snd_seq_oss_readq_clear(struct seq_oss_readq
*q
)
92 q
->head
= q
->tail
= 0;
94 /* if someone sleeping, wake'em up */
95 if (waitqueue_active(&q
->midi_sleep
))
96 wake_up(&q
->midi_sleep
);
97 q
->input_time
= (unsigned long)-1;
104 snd_seq_oss_readq_puts(struct seq_oss_readq
*q
, int dev
, unsigned char *data
, int len
)
109 memset(&rec
, 0, sizeof(rec
));
110 rec
.c
[0] = SEQ_MIDIPUTC
;
115 result
= snd_seq_oss_readq_put_event(q
, &rec
);
123 * copy an event to input queue:
124 * return zero if enqueued
127 snd_seq_oss_readq_put_event(struct seq_oss_readq
*q
, union evrec
*ev
)
131 spin_lock_irqsave(&q
->lock
, flags
);
132 if (q
->qlen
>= q
->maxlen
- 1) {
133 spin_unlock_irqrestore(&q
->lock
, flags
);
137 memcpy(&q
->q
[q
->tail
], ev
, sizeof(*ev
));
138 q
->tail
= (q
->tail
+ 1) % q
->maxlen
;
141 /* wake up sleeper */
142 if (waitqueue_active(&q
->midi_sleep
))
143 wake_up(&q
->midi_sleep
);
145 spin_unlock_irqrestore(&q
->lock
, flags
);
153 * caller must hold lock
156 snd_seq_oss_readq_pick(struct seq_oss_readq
*q
, union evrec
*rec
)
160 memcpy(rec
, &q
->q
[q
->head
], sizeof(*rec
));
168 snd_seq_oss_readq_wait(struct seq_oss_readq
*q
)
170 wait_event_interruptible_timeout(q
->midi_sleep
,
171 (q
->qlen
> 0 || q
->head
== q
->tail
),
172 q
->pre_event_timeout
);
177 * caller must hold lock
180 snd_seq_oss_readq_free(struct seq_oss_readq
*q
)
183 q
->head
= (q
->head
+ 1) % q
->maxlen
;
190 * return non-zero if readq is not empty.
193 snd_seq_oss_readq_poll(struct seq_oss_readq
*q
, struct file
*file
, poll_table
*wait
)
195 poll_wait(file
, &q
->midi_sleep
, wait
);
203 snd_seq_oss_readq_put_timestamp(struct seq_oss_readq
*q
, unsigned long curt
, int seq_mode
)
205 if (curt
!= q
->input_time
) {
207 memset(&rec
, 0, sizeof(rec
));
209 case SNDRV_SEQ_OSS_MODE_SYNTH
:
210 rec
.echo
= (curt
<< 8) | SEQ_WAIT
;
211 snd_seq_oss_readq_put_event(q
, &rec
);
213 case SNDRV_SEQ_OSS_MODE_MUSIC
:
214 rec
.t
.code
= EV_TIMING
;
215 rec
.t
.cmd
= TMR_WAIT_ABS
;
217 snd_seq_oss_readq_put_event(q
, &rec
);
220 q
->input_time
= curt
;
226 #ifdef CONFIG_PROC_FS
231 snd_seq_oss_readq_info_read(struct seq_oss_readq
*q
, struct snd_info_buffer
*buf
)
233 snd_iprintf(buf
, " read queue [%s] length = %d : tick = %ld\n",
234 (waitqueue_active(&q
->midi_sleep
) ? "sleeping":"running"),
235 q
->qlen
, q
->input_time
);
237 #endif /* CONFIG_PROC_FS */