blob: 87f85f7ee81461f7463c1a4bd4575f89ca027843 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * OSS compatible sequencer driver
3 *
4 * seq_oss_writeq.c - write queue and sync
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_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
31
32/*
33 * create a write queue record
34 */
35seq_oss_writeq_t *
36snd_seq_oss_writeq_new(seq_oss_devinfo_t *dp, int maxlen)
37{
38 seq_oss_writeq_t *q;
39 snd_seq_client_pool_t pool;
40
41 if ((q = kcalloc(1, sizeof(*q), GFP_KERNEL)) == NULL)
42 return NULL;
43 q->dp = dp;
44 q->maxlen = maxlen;
45 spin_lock_init(&q->sync_lock);
46 q->sync_event_put = 0;
47 q->sync_time = 0;
48 init_waitqueue_head(&q->sync_sleep);
49
50 memset(&pool, 0, sizeof(pool));
51 pool.client = dp->cseq;
52 pool.output_pool = maxlen;
53 pool.output_room = maxlen / 2;
54
55 snd_seq_oss_control(dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
56
57 return q;
58}
59
60/*
61 * delete the write queue
62 */
63void
64snd_seq_oss_writeq_delete(seq_oss_writeq_t *q)
65{
66 snd_seq_oss_writeq_clear(q); /* to be sure */
67 kfree(q);
68}
69
70
71/*
72 * reset the write queue
73 */
74void
75snd_seq_oss_writeq_clear(seq_oss_writeq_t *q)
76{
77 snd_seq_remove_events_t reset;
78
79 memset(&reset, 0, sizeof(reset));
80 reset.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT; /* remove all */
81 snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, &reset);
82
83 /* wake up sleepers if any */
84 snd_seq_oss_writeq_wakeup(q, 0);
85}
86
87/*
88 * wait until the write buffer has enough room
89 */
90int
91snd_seq_oss_writeq_sync(seq_oss_writeq_t *q)
92{
93 seq_oss_devinfo_t *dp = q->dp;
94 abstime_t time;
95
96 time = snd_seq_oss_timer_cur_tick(dp->timer);
97 if (q->sync_time >= time)
98 return 0; /* already finished */
99
100 if (! q->sync_event_put) {
101 snd_seq_event_t ev;
102 evrec_t *rec;
103
104 /* put echoback event */
105 memset(&ev, 0, sizeof(ev));
106 ev.flags = 0;
107 ev.type = SNDRV_SEQ_EVENT_ECHO;
108 ev.time.tick = time;
109 /* echo back to itself */
110 snd_seq_oss_fill_addr(dp, &ev, dp->addr.client, dp->addr.port);
111 rec = (evrec_t*)&ev.data;
112 rec->t.code = SEQ_SYNCTIMER;
113 rec->t.time = time;
114 q->sync_event_put = 1;
115 snd_seq_kernel_client_enqueue_blocking(dp->cseq, &ev, NULL, 0, 0);
116 }
117
118 wait_event_interruptible_timeout(q->sync_sleep, ! q->sync_event_put, HZ);
119 if (signal_pending(current))
120 /* interrupted - return 0 to finish sync */
121 q->sync_event_put = 0;
122 if (! q->sync_event_put || q->sync_time >= time)
123 return 0;
124 return 1;
125}
126
127/*
128 * wake up sync - echo event was catched
129 */
130void
131snd_seq_oss_writeq_wakeup(seq_oss_writeq_t *q, abstime_t time)
132{
133 unsigned long flags;
134
135 spin_lock_irqsave(&q->sync_lock, flags);
136 q->sync_time = time;
137 q->sync_event_put = 0;
138 if (waitqueue_active(&q->sync_sleep)) {
139 wake_up(&q->sync_sleep);
140 }
141 spin_unlock_irqrestore(&q->sync_lock, flags);
142}
143
144
145/*
146 * return the unused pool size
147 */
148int
149snd_seq_oss_writeq_get_free_size(seq_oss_writeq_t *q)
150{
151 snd_seq_client_pool_t pool;
152 pool.client = q->dp->cseq;
153 snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
154 return pool.output_free;
155}
156
157
158/*
159 * set output threshold size from ioctl
160 */
161void
162snd_seq_oss_writeq_set_output(seq_oss_writeq_t *q, int val)
163{
164 snd_seq_client_pool_t pool;
165 pool.client = q->dp->cseq;
166 snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
167 pool.output_room = val;
168 snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
169}
170