Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / oss / emu10k1 / passthrough.c
blob4094be55f3be335338fd120224a0fba22c6ce711
1 /*
2 **********************************************************************
3 * passthrough.c -- Emu10k1 digital passthrough
4 * Copyright (C) 2001 Juha Yrjölä <jyrjola@cc.hut.fi>
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * May 15, 2001 Juha Yrjölä base code release
12 **********************************************************************
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.
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.
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.
29 **********************************************************************
32 #include <linux/module.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/bitops.h>
36 #include <asm/io.h>
37 #include <linux/sched.h>
38 #include <linux/smp_lock.h>
40 #include "hwaccess.h"
41 #include "cardwo.h"
42 #include "cardwi.h"
43 #include "recmgr.h"
44 #include "irqmgr.h"
45 #include "audio.h"
46 #include "8010.h"
48 static void pt_putsamples(struct pt_data *pt, u16 *ptr, u16 left, u16 right)
50 unsigned int idx;
52 ptr[pt->copyptr] = left;
53 idx = pt->copyptr + PT_SAMPLES/2;
54 idx %= PT_SAMPLES;
55 ptr[idx] = right;
58 static inline int pt_can_write(struct pt_data *pt)
60 return pt->blocks_copied < pt->blocks_played + 8;
63 static int pt_wait_for_write(struct emu10k1_wavedevice *wavedev, int nonblock)
65 struct emu10k1_card *card = wavedev->card;
66 struct pt_data *pt = &card->pt;
68 if (nonblock && !pt_can_write(pt))
69 return -EAGAIN;
70 while (!pt_can_write(pt) && pt->state != PT_STATE_INACTIVE) {
71 interruptible_sleep_on(&pt->wait);
72 if (signal_pending(current))
73 return -ERESTARTSYS;
75 if (pt->state == PT_STATE_INACTIVE)
76 return -EAGAIN;
78 return 0;
81 static int pt_putblock(struct emu10k1_wavedevice *wave_dev, u16 *block, int nonblock)
83 struct woinst *woinst = wave_dev->woinst;
84 struct emu10k1_card *card = wave_dev->card;
85 struct pt_data *pt = &card->pt;
86 u16 *ptr = (u16 *) card->tankmem.addr;
87 int i = 0, r;
88 unsigned long flags;
90 r = pt_wait_for_write(wave_dev, nonblock);
91 if (r < 0)
92 return r;
93 spin_lock_irqsave(&card->pt.lock, flags);
94 while (i < PT_BLOCKSAMPLES) {
95 pt_putsamples(pt, ptr, block[2*i], block[2*i+1]);
96 if (pt->copyptr == 0)
97 pt->copyptr = PT_SAMPLES;
98 pt->copyptr--;
99 i++;
101 woinst->total_copied += PT_BLOCKSIZE;
102 pt->blocks_copied++;
103 if (pt->blocks_copied >= 4 && pt->state != PT_STATE_PLAYING) {
104 DPF(2, "activating digital pass-through playback\n");
105 sblive_writeptr(card, GPR_BASE + pt->enable_gpr, 0, 1);
106 pt->state = PT_STATE_PLAYING;
108 spin_unlock_irqrestore(&card->pt.lock, flags);
109 return 0;
112 int emu10k1_pt_setup(struct emu10k1_wavedevice *wave_dev)
114 u32 bits;
115 struct emu10k1_card *card = wave_dev->card;
116 struct pt_data *pt = &card->pt;
117 int i;
119 for (i = 0; i < 3; i++) {
120 pt->old_spcs[i] = sblive_readptr(card, SPCS0 + i, 0);
121 if (pt->spcs_to_use & (1 << i)) {
122 DPD(2, "using S/PDIF port %d\n", i);
123 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
124 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
125 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT;
126 if (pt->ac3data)
127 bits |= SPCS_NOTAUDIODATA;
128 sblive_writeptr(card, SPCS0 + i, 0, bits);
131 return 0;
134 ssize_t emu10k1_pt_write(struct file *file, const char __user *buffer, size_t count)
136 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
137 struct emu10k1_card *card = wave_dev->card;
138 struct pt_data *pt = &card->pt;
139 int nonblock, i, r, blocks, blocks_copied, bytes_copied = 0;
141 DPD(3, "emu10k1_pt_write(): %d bytes\n", count);
143 nonblock = file->f_flags & O_NONBLOCK;
145 if (card->tankmem.size < PT_SAMPLES*2)
146 return -EFAULT;
147 if (pt->state == PT_STATE_INACTIVE) {
148 DPF(2, "bufptr init\n");
149 pt->playptr = PT_SAMPLES-1;
150 pt->copyptr = PT_INITPTR;
151 pt->blocks_played = pt->blocks_copied = 0;
152 memset(card->tankmem.addr, 0, card->tankmem.size);
153 pt->state = PT_STATE_ACTIVATED;
154 pt->buf = kmalloc(PT_BLOCKSIZE, GFP_KERNEL);
155 pt->prepend_size = 0;
156 if (pt->buf == NULL)
157 return -ENOMEM;
158 emu10k1_pt_setup(wave_dev);
160 if (pt->prepend_size) {
161 int needed = PT_BLOCKSIZE - pt->prepend_size;
163 DPD(3, "prepend size %d, prepending %d bytes\n", pt->prepend_size, needed);
164 if (count < needed) {
165 copy_from_user(pt->buf + pt->prepend_size, buffer, count);
166 pt->prepend_size += count;
167 DPD(3, "prepend size now %d\n", pt->prepend_size);
168 return count;
170 copy_from_user(pt->buf + pt->prepend_size, buffer, needed);
171 r = pt_putblock(wave_dev, (u16 *) pt->buf, nonblock);
172 if (r)
173 return r;
174 bytes_copied += needed;
175 pt->prepend_size = 0;
177 blocks = (count-bytes_copied)/PT_BLOCKSIZE;
178 blocks_copied = 0;
179 while (blocks > 0) {
180 u16 __user *bufptr = (u16 __user *) buffer + (bytes_copied/2);
181 copy_from_user(pt->buf, bufptr, PT_BLOCKSIZE);
182 r = pt_putblock(wave_dev, (u16 *)pt->buf, nonblock);
183 if (r) {
184 if (bytes_copied)
185 return bytes_copied;
186 else
187 return r;
189 bytes_copied += PT_BLOCKSIZE;
190 blocks--;
191 blocks_copied++;
193 i = count - bytes_copied;
194 if (i) {
195 pt->prepend_size = i;
196 copy_from_user(pt->buf, buffer + bytes_copied, i);
197 bytes_copied += i;
198 DPD(3, "filling prepend buffer with %d bytes", i);
200 return bytes_copied;
203 void emu10k1_pt_stop(struct emu10k1_card *card)
205 struct pt_data *pt = &card->pt;
206 int i;
208 if (pt->state != PT_STATE_INACTIVE) {
209 DPF(2, "digital pass-through stopped\n");
210 sblive_writeptr(card, (card->is_audigy ? A_GPR_BASE : GPR_BASE) + pt->enable_gpr, 0, 0);
211 for (i = 0; i < 3; i++) {
212 if (pt->spcs_to_use & (1 << i))
213 sblive_writeptr(card, SPCS0 + i, 0, pt->old_spcs[i]);
215 pt->state = PT_STATE_INACTIVE;
216 if(pt->buf)
217 kfree(pt->buf);
221 void emu10k1_pt_waveout_update(struct emu10k1_wavedevice *wave_dev)
223 struct woinst *woinst = wave_dev->woinst;
224 struct pt_data *pt = &wave_dev->card->pt;
225 u32 pos;
227 if (pt->state == PT_STATE_PLAYING && pt->pos_gpr >= 0) {
228 pos = sblive_readptr(wave_dev->card, GPR_BASE + pt->pos_gpr, 0);
229 if (pos > PT_BLOCKSAMPLES)
230 pos = PT_BLOCKSAMPLES;
231 pos = 4 * (PT_BLOCKSAMPLES - pos);
232 } else
233 pos = 0;
234 woinst->total_played = pt->blocks_played * woinst->buffer.fragment_size + pos;
235 woinst->buffer.hw_pos = pos;