ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / seq / oss / seq_oss_writeq.c
1 /*
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
30
31 /*
32  * create a write queue record
33  */
34 seq_oss_writeq_t *
35 snd_seq_oss_writeq_new(seq_oss_devinfo_t *dp, int maxlen)
36 {
37         seq_oss_writeq_t *q;
38         snd_seq_client_pool_t pool;
39
40         if ((q = snd_kcalloc(sizeof(*q), GFP_KERNEL)) == NULL)
41                 return NULL;
42         q->dp = dp;
43         q->maxlen = maxlen;
44         spin_lock_init(&q->sync_lock);
45         q->sync_event_put = 0;
46         q->sync_time = 0;
47         init_waitqueue_head(&q->sync_sleep);
48
49         memset(&pool, 0, sizeof(pool));
50         pool.client = dp->cseq;
51         pool.output_pool = maxlen;
52         pool.output_room = maxlen / 2;
53
54         snd_seq_oss_control(dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
55
56         return q;
57 }
58
59 /*
60  * delete the write queue
61  */
62 void
63 snd_seq_oss_writeq_delete(seq_oss_writeq_t *q)
64 {
65         snd_seq_oss_writeq_clear(q);    /* to be sure */
66         kfree(q);
67 }
68
69
70 /*
71  * reset the write queue
72  */
73 void
74 snd_seq_oss_writeq_clear(seq_oss_writeq_t *q)
75 {
76         snd_seq_remove_events_t reset;
77
78         memset(&reset, 0, sizeof(reset));
79         reset.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT; /* remove all */
80         snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, &reset);
81
82         /* wake up sleepers if any */
83         snd_seq_oss_writeq_wakeup(q, 0);
84 }
85
86 /*
87  * wait until the write buffer has enough room
88  */
89 int
90 snd_seq_oss_writeq_sync(seq_oss_writeq_t *q)
91 {
92         seq_oss_devinfo_t *dp = q->dp;
93         abstime_t time;
94         unsigned long flags;
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         spin_lock_irqsave(&q->sync_lock, flags);
119         if (! q->sync_event_put) { /* echoback event has been received */
120                 spin_unlock_irqrestore(&q->sync_lock, flags);
121                 return 0;
122         }
123                 
124         /* wait for echo event */
125         spin_unlock(&q->sync_lock);
126         interruptible_sleep_on_timeout(&q->sync_sleep, HZ);
127         spin_lock(&q->sync_lock);
128         if (signal_pending(current)) {
129                 /* interrupted - return 0 to finish sync */
130                 q->sync_event_put = 0;
131                 spin_unlock_irqrestore(&q->sync_lock, flags);
132                 return 0;
133         }
134         spin_unlock_irqrestore(&q->sync_lock, flags);
135         if (q->sync_time >= time)
136                 return 0;
137         else
138                 return 1;
139 }
140
141 /*
142  * wake up sync - echo event was catched
143  */
144 void
145 snd_seq_oss_writeq_wakeup(seq_oss_writeq_t *q, abstime_t time)
146 {
147         unsigned long flags;
148
149         spin_lock_irqsave(&q->sync_lock, flags);
150         q->sync_time = time;
151         q->sync_event_put = 0;
152         if (waitqueue_active(&q->sync_sleep)) {
153                 wake_up(&q->sync_sleep);
154         }
155         spin_unlock_irqrestore(&q->sync_lock, flags);
156 }
157
158
159 /*
160  * return the unused pool size
161  */
162 int
163 snd_seq_oss_writeq_get_free_size(seq_oss_writeq_t *q)
164 {
165         snd_seq_client_pool_t pool;
166         pool.client = q->dp->cseq;
167         snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
168         return pool.output_free;
169 }
170
171
172 /*
173  * set output threshold size from ioctl
174  */
175 void
176 snd_seq_oss_writeq_set_output(seq_oss_writeq_t *q, int val)
177 {
178         snd_seq_client_pool_t pool;
179         pool.client = q->dp->cseq;
180         snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
181         pool.output_room = val;
182         snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
183 }
184