2 * OSS compatible sequencer driver
4 * seq_oss_writeq.c - write queue and sync
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_writeq.h"
24 #include "seq_oss_event.h"
25 #include "seq_oss_timer.h"
26 #include <sound/seq_oss_legacy.h>
27 #include "../seq_lock.h"
28 #include "../seq_clientmgr.h"
29 #include <linux/wait.h>
30 #include <linux/slab.h>
31 #include <linux/sched/signal.h>
35 * create a write queue record
37 struct seq_oss_writeq
*
38 snd_seq_oss_writeq_new(struct seq_oss_devinfo
*dp
, int maxlen
)
40 struct seq_oss_writeq
*q
;
41 struct snd_seq_client_pool pool
;
43 if ((q
= kzalloc(sizeof(*q
), GFP_KERNEL
)) == NULL
)
47 spin_lock_init(&q
->sync_lock
);
48 q
->sync_event_put
= 0;
50 init_waitqueue_head(&q
->sync_sleep
);
52 memset(&pool
, 0, sizeof(pool
));
53 pool
.client
= dp
->cseq
;
54 pool
.output_pool
= maxlen
;
55 pool
.output_room
= maxlen
/ 2;
57 snd_seq_oss_control(dp
, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL
, &pool
);
63 * delete the write queue
66 snd_seq_oss_writeq_delete(struct seq_oss_writeq
*q
)
69 snd_seq_oss_writeq_clear(q
); /* to be sure */
76 * reset the write queue
79 snd_seq_oss_writeq_clear(struct seq_oss_writeq
*q
)
81 struct snd_seq_remove_events reset
;
83 memset(&reset
, 0, sizeof(reset
));
84 reset
.remove_mode
= SNDRV_SEQ_REMOVE_OUTPUT
; /* remove all */
85 snd_seq_oss_control(q
->dp
, SNDRV_SEQ_IOCTL_REMOVE_EVENTS
, &reset
);
87 /* wake up sleepers if any */
88 snd_seq_oss_writeq_wakeup(q
, 0);
92 * wait until the write buffer has enough room
95 snd_seq_oss_writeq_sync(struct seq_oss_writeq
*q
)
97 struct seq_oss_devinfo
*dp
= q
->dp
;
100 time
= snd_seq_oss_timer_cur_tick(dp
->timer
);
101 if (q
->sync_time
>= time
)
102 return 0; /* already finished */
104 if (! q
->sync_event_put
) {
105 struct snd_seq_event ev
;
108 /* put echoback event */
109 memset(&ev
, 0, sizeof(ev
));
111 ev
.type
= SNDRV_SEQ_EVENT_ECHO
;
113 /* echo back to itself */
114 snd_seq_oss_fill_addr(dp
, &ev
, dp
->addr
.client
, dp
->addr
.port
);
115 rec
= (union evrec
*)&ev
.data
;
116 rec
->t
.code
= SEQ_SYNCTIMER
;
118 q
->sync_event_put
= 1;
119 snd_seq_kernel_client_enqueue_blocking(dp
->cseq
, &ev
, NULL
, 0, 0);
122 wait_event_interruptible_timeout(q
->sync_sleep
, ! q
->sync_event_put
, HZ
);
123 if (signal_pending(current
))
124 /* interrupted - return 0 to finish sync */
125 q
->sync_event_put
= 0;
126 if (! q
->sync_event_put
|| q
->sync_time
>= time
)
132 * wake up sync - echo event was catched
135 snd_seq_oss_writeq_wakeup(struct seq_oss_writeq
*q
, abstime_t time
)
139 spin_lock_irqsave(&q
->sync_lock
, flags
);
141 q
->sync_event_put
= 0;
142 wake_up(&q
->sync_sleep
);
143 spin_unlock_irqrestore(&q
->sync_lock
, flags
);
148 * return the unused pool size
151 snd_seq_oss_writeq_get_free_size(struct seq_oss_writeq
*q
)
153 struct snd_seq_client_pool pool
;
154 pool
.client
= q
->dp
->cseq
;
155 snd_seq_oss_control(q
->dp
, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL
, &pool
);
156 return pool
.output_free
;
161 * set output threshold size from ioctl
164 snd_seq_oss_writeq_set_output(struct seq_oss_writeq
*q
, int val
)
166 struct snd_seq_client_pool pool
;
167 pool
.client
= q
->dp
->cseq
;
168 snd_seq_oss_control(q
->dp
, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL
, &pool
);
169 pool
.output_room
= val
;
170 snd_seq_oss_control(q
->dp
, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL
, &pool
);