File: /usr/src/linux/drivers/sound/emu10k1/passthrough.c
1 /*
2 **********************************************************************
3 * passthrough.c -- Emu10k1 digital passthrough
4 * Copyright (C) 2001 Juha Yrjölä <jyrjola@cc.hut.fi>
5 *
6 **********************************************************************
7 *
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * May 15, 2001 Juha Yrjölä base code release
11 *
12 **********************************************************************
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the Free
26 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
27 * USA.
28 *
29 **********************************************************************
30 */
31
32 #define __NO_VERSION__
33 #include <linux/module.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/version.h>
37 #include <linux/bitops.h>
38 #include <asm/io.h>
39 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
41 #include <linux/wrapper.h>
42
43 #include "hwaccess.h"
44 #include "cardwo.h"
45 #include "cardwi.h"
46 #include "recmgr.h"
47 #include "irqmgr.h"
48 #include "audio.h"
49 #include "8010.h"
50 #include "passthrough.h"
51
52 static void pt_putsamples(struct pt_data *pt, u16 *ptr, u16 left, u16 right)
53 {
54 unsigned int idx;
55
56 ptr[pt->copyptr] = left;
57 idx = pt->copyptr + PT_SAMPLES/2;
58 idx %= PT_SAMPLES;
59 ptr[idx] = right;
60 }
61
62 static inline int pt_can_write(struct pt_data *pt)
63 {
64 return pt->blocks_copied < pt->blocks_played + 8;
65 }
66
67 static int pt_wait_for_write(struct emu10k1_wavedevice *wavedev, int nonblock)
68 {
69 struct emu10k1_card *card = wavedev->card;
70 struct pt_data *pt = &card->pt;
71
72 if (nonblock && !pt_can_write(pt))
73 return -EAGAIN;
74 while (!pt_can_write(pt) && pt->state != PT_STATE_INACTIVE) {
75 interruptible_sleep_on(&pt->wait);
76 if (signal_pending(current))
77 return -ERESTARTSYS;
78 }
79 if (pt->state == PT_STATE_INACTIVE)
80 return -EAGAIN;
81
82 return 0;
83 }
84
85 static int pt_putblock(struct emu10k1_wavedevice *wave_dev, u16 *block, int nonblock)
86 {
87 struct woinst *woinst = wave_dev->woinst;
88 struct emu10k1_card *card = wave_dev->card;
89 struct pt_data *pt = &card->pt;
90 u16 *ptr = (u16 *) card->tankmem.addr;
91 int i = 0, r;
92 unsigned long flags;
93
94 r = pt_wait_for_write(wave_dev, nonblock);
95 if (r < 0)
96 return r;
97 spin_lock_irqsave(&card->pt.lock, flags);
98 while (i < PT_BLOCKSAMPLES) {
99 pt_putsamples(pt, ptr, block[2*i], block[2*i+1]);
100 if (pt->copyptr == 0)
101 pt->copyptr = PT_SAMPLES;
102 pt->copyptr--;
103 i++;
104 }
105 woinst->total_copied += PT_BLOCKSIZE;
106 pt->blocks_copied++;
107 if (pt->blocks_copied >= 4 && pt->state != PT_STATE_PLAYING) {
108 DPF(2, "activating digital pass-through playback\n");
109 sblive_writeptr(card, GPR_BASE + pt->enable_gpr, 0, 1);
110 pt->state = PT_STATE_PLAYING;
111 }
112 spin_unlock_irqrestore(&card->pt.lock, flags);
113 return 0;
114 }
115
116 static int pt_setup(struct emu10k1_wavedevice *wave_dev)
117 {
118 u32 bits;
119 struct emu10k1_card *card = wave_dev->card;
120 struct pt_data *pt = &card->pt;
121 int i;
122
123 for (i = 0; i < 3; i++) {
124 pt->old_spcs[i] = sblive_readptr(card, SPCS0 + i, 0);
125 if (pt->spcs_to_use & (1 << i)) {
126 DPD(2, "using S/PDIF port %d\n", i);
127 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
128 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
129 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
130 if (pt->ac3data)
131 bits |= SPCS_NOTAUDIODATA;
132 sblive_writeptr(card, SPCS0 + i, 0, bits);
133 }
134 }
135 return 0;
136 }
137
138 ssize_t emu10k1_pt_write(struct file *file, const char *buffer, size_t count)
139 {
140 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
141 struct emu10k1_card *card = wave_dev->card;
142 struct pt_data *pt = &card->pt;
143 int nonblock, i, r, blocks, blocks_copied, bytes_copied = 0;
144
145 DPD(3, "emu10k1_pt_write(): %d bytes\n", count);
146
147 nonblock = file->f_flags & O_NONBLOCK;
148
149 if (card->tankmem.size < PT_SAMPLES*2)
150 return -EFAULT;
151 if (pt->state == PT_STATE_INACTIVE) {
152 DPF(2, "bufptr init\n");
153 pt->playptr = PT_SAMPLES-1;
154 pt->copyptr = PT_INITPTR;
155 pt->blocks_played = pt->blocks_copied = 0;
156 memset(card->tankmem.addr, 0, card->tankmem.size);
157 pt->state = PT_STATE_ACTIVATED;
158 pt->buf = kmalloc(PT_BLOCKSIZE, GFP_KERNEL);
159 pt->prepend_size = 0;
160 if (pt->buf == NULL)
161 return -ENOMEM;
162 pt_setup(wave_dev);
163 }
164 if (pt->prepend_size) {
165 int needed = PT_BLOCKSIZE - pt->prepend_size;
166
167 DPD(3, "prepend size %d, prepending %d bytes\n", pt->prepend_size, needed);
168 if (count < needed) {
169 copy_from_user(pt->buf + pt->prepend_size, buffer, count);
170 pt->prepend_size += count;
171 DPD(3, "prepend size now %d\n", pt->prepend_size);
172 return count;
173 }
174 copy_from_user(pt->buf + pt->prepend_size, buffer, needed);
175 r = pt_putblock(wave_dev, (u16 *) pt->buf, nonblock);
176 if (r)
177 return r;
178 bytes_copied += needed;
179 pt->prepend_size = 0;
180 }
181 blocks = (count-bytes_copied)/PT_BLOCKSIZE;
182 blocks_copied = 0;
183 while (blocks > 0) {
184 u16 *bufptr = (u16 *) buffer + (bytes_copied/2);
185 copy_from_user(pt->buf, bufptr, PT_BLOCKSIZE);
186 bufptr = (u16 *) pt->buf;
187 r = pt_putblock(wave_dev, bufptr, nonblock);
188 if (r) {
189 if (bytes_copied)
190 return bytes_copied;
191 else
192 return r;
193 }
194 bytes_copied += PT_BLOCKSIZE;
195 blocks--;
196 blocks_copied++;
197 }
198 i = count - bytes_copied;
199 if (i) {
200 pt->prepend_size = i;
201 copy_from_user(pt->buf, buffer + bytes_copied, i);
202 bytes_copied += i;
203 DPD(3, "filling prepend buffer with %d bytes", i);
204 }
205 return bytes_copied;
206 }
207
208 void emu10k1_pt_stop(struct emu10k1_card *card)
209 {
210 struct pt_data *pt = &card->pt;
211 int i;
212 unsigned long flags;
213
214 spin_lock_irqsave(&card->pt.lock, flags);
215 if (pt->state != PT_STATE_INACTIVE) {
216 DPF(2, "digital pass-through stopped\n");
217 sblive_writeptr(card, GPR_BASE + pt->enable_gpr, 0, 0);
218 for (i = 0; i < 3; i++) {
219 if (pt->spcs_to_use & (1 << i))
220 sblive_writeptr(card, SPCS0 + i, 0, pt->old_spcs[i]);
221 }
222 pt->state = PT_STATE_INACTIVE;
223 kfree(pt->buf);
224 }
225 spin_unlock_irqrestore(&card->pt.lock, flags);
226 }
227
228 void emu10k1_pt_waveout_update(struct emu10k1_wavedevice *wave_dev)
229 {
230 struct woinst *woinst = wave_dev->woinst;
231 struct pt_data *pt = &wave_dev->card->pt;
232 u32 pos;
233
234 if (pt->state == PT_STATE_PLAYING && pt->pos_gpr >= 0) {
235 pos = sblive_readptr(wave_dev->card, GPR_BASE + pt->pos_gpr, 0);
236 if (pos > PT_BLOCKSAMPLES)
237 pos = PT_BLOCKSAMPLES;
238 pos = 4 * (PT_BLOCKSAMPLES - pos);
239 } else
240 pos = 0;
241 woinst->total_played = pt->blocks_played * woinst->buffer.fragment_size + pos;
242 woinst->buffer.hw_pos = pos;
243 }
244