File: /usr/src/linux/drivers/sound/emu10k1/efxmgr.c

1     /*     
2      **********************************************************************
3      *     efxmgr.c
4      *     Copyright 1999, 2000 Creative Labs, Inc. 
5      * 
6      ********************************************************************** 
7      * 
8      *     Date                 Author          Summary of changes 
9      *     ----                 ------          ------------------ 
10      *     October 20, 1999     Bertrand Lee    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     #include <linux/bitops.h>
33     #include "hwaccess.h"
34     #include "efxmgr.h"
35     
36     int emu10k1_find_control_gpr(struct patch_manager *mgr, const char *patch_name, const char *gpr_name)
37     {
38             struct dsp_patch *patch;
39     	struct dsp_rpatch *rpatch;
40     	char s[PATCH_NAME_SIZE + 4];
41     	u32 *gpr_used;
42     	int i;
43     
44     	DPD(2, "emu10k1_find_control_gpr(): %s %s\n", patch_name, gpr_name);
45     
46     	rpatch = &mgr->rpatch;
47     	if (!strcmp(rpatch->name, patch_name)) {
48     		gpr_used = rpatch->gpr_used;
49     		goto match;
50     	}
51     
52     	for(i = 0; i < mgr->current_pages * PATCHES_PER_PAGE; i++) {
53     		patch = PATCH(mgr, i);
54     			sprintf(s,"%s", patch->name);
55     
56     		if (!strcmp(s, patch_name)) {
57     			gpr_used = patch->gpr_used;
58     			goto match;
59     		}
60     	}
61     
62     	return -1;
63     
64       match:
65     	for (i = 0; i < NUM_GPRS; i++)
66     		if (mgr->gpr[i].type == GPR_TYPE_CONTROL &&
67     		    test_bit(i, gpr_used) &&
68     		    !strcmp(mgr->gpr[i].name, gpr_name))
69     			return i;
70     
71     	return -1;
72     }
73     
74     void emu10k1_set_control_gpr(struct emu10k1_card *card, int addr, s32 val, int flag)
75     {
76     	struct patch_manager *mgr = &card->mgr;
77     
78     	DPD(2, "emu10k1_set_control_gpr(): %d %x\n", addr, val);
79     
80     	if (addr < 0 || addr >= NUM_GPRS)
81     		return;
82     
83     	if (flag)
84     		val += sblive_readptr(card, GPR_BASE + addr, 0);
85     
86     	if (val > mgr->gpr[addr].max)
87     		val = mgr->gpr[addr].max;
88     	else if (val < mgr->gpr[addr].min)
89     		val = mgr->gpr[addr].min;
90     
91     	sblive_writeptr(card, GPR_BASE + addr, 0, val);
92     }
93     
94     //TODO: make this configurable:
95     #define VOLCTRL_CHANNEL SOUND_MIXER_VOLUME
96     #define VOLCTRL_STEP_SIZE        5
97     
98     //An internal function for setting OSS mixer controls.
99     void emu10k1_set_oss_vol(struct emu10k1_card *card, int oss_mixer,
100     			 unsigned int left, unsigned int right){
101     
102     	extern struct oss_scaling volume_params[SOUND_MIXER_NRDEVICES];
103     
104     	card->ac97.mixer_state[oss_mixer] = (right << 8) | left;
105     
106     	if (!card->isaps)
107     		card->ac97.write_mixer(&card->ac97, oss_mixer, left, right);
108     
109     	
110     	emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][0], left,
111     			       volume_params[oss_mixer].scale,
112     			       volume_params[oss_mixer].muting);
113     	emu10k1_set_volume_gpr(card, card->mgr.ctrl_gpr[oss_mixer][1], right,
114     			       volume_params[oss_mixer].scale,
115     			       volume_params[oss_mixer].muting);
116     
117     }
118     
119     
120     //FIXME: mute should unmute when pressed a second time
121     void emu10k1_mute_irqhandler(struct emu10k1_card *card)
122     {
123     	struct patch_manager *mgr = &card->mgr;
124     	unsigned long  flags;
125     
126     	spin_lock_irqsave(&mgr->lock, flags);
127     	emu10k1_set_oss_vol(card,VOLCTRL_CHANNEL,0,0);
128     	spin_unlock_irqrestore(&mgr->lock, flags);
129     }
130     
131     void emu10k1_volincr_irqhandler(struct emu10k1_card *card)
132     {
133     	struct patch_manager *mgr = &card->mgr;
134     	unsigned long  flags;
135     	unsigned int oss_channel=VOLCTRL_CHANNEL, left=0,right=0;
136     
137     	spin_lock_irqsave(&mgr->lock, flags);
138     	left = card->ac97.mixer_state[oss_channel] & 0xff;
139     	right = (card->ac97.mixer_state[oss_channel] >> 8) & 0xff;
140     
141     	if((left+=VOLCTRL_STEP_SIZE )>100)
142     		left=100;
143     	if((right+=VOLCTRL_STEP_SIZE )>100)
144     		right=100;
145     	emu10k1_set_oss_vol(card,oss_channel,left,right);
146     	spin_unlock_irqrestore(&mgr->lock, flags);	
147     }
148     void emu10k1_voldecr_irqhandler(struct emu10k1_card *card)
149     {
150     	struct patch_manager *mgr = &card->mgr;
151     	unsigned long  flags;
152     	int oss_channel=VOLCTRL_CHANNEL, left=0,right=0;
153     
154     	spin_lock_irqsave(&mgr->lock, flags);
155     	left = card->ac97.mixer_state[oss_channel] & 0xff;
156     	right = (card->ac97.mixer_state[oss_channel] >> 8) & 0xff;
157     
158     	if((left-=VOLCTRL_STEP_SIZE )<0)
159     		left=0;
160     	if((right-=VOLCTRL_STEP_SIZE )<0)
161     		right=0;
162     	emu10k1_set_oss_vol(card,oss_channel,left,right);
163     	spin_unlock_irqrestore(&mgr->lock, flags);
164     }
165     
166     
167     void emu10k1_set_volume_gpr(struct emu10k1_card *card, int addr, s32 vol, int scale, int muting)
168     {
169     	struct patch_manager *mgr = &card->mgr;
170     	unsigned long flags;
171     
172     	const s32 log2lin[5] ={                  //  attenuation (dB)
173     		0x7fffffff,                      //       0.0         
174     		0x7fffffff * 0.840896415253715 , //       1.5          
175     		0x7fffffff * 0.707106781186548,  //       3.0
176     		0x7fffffff * 0.594603557501361 , //       4.5
177     	};
178     
179     	if (addr < 0)
180     		return;
181     
182     	vol = (100 - vol ) * scale / 100;
183     
184     	// Thanks to the comp.dsp newsgroup for this neat trick:
185     	vol = vol >= muting ? 0: log2lin[vol&3]>>(vol>>2);
186     
187     	spin_lock_irqsave(&mgr->lock, flags);
188     	emu10k1_set_control_gpr(card, addr, vol, 0);
189     	spin_unlock_irqrestore(&mgr->lock, flags);
190     }
191     
192     void emu10k1_dsp_irqhandler(struct emu10k1_card *card)
193     {
194     	unsigned long flags;
195     
196     	if (card->pt.state != PT_STATE_INACTIVE) {
197     		u32 bc;
198     		bc = sblive_readptr(card, GPR_BASE + card->pt.intr_gpr, 0);
199     		if (bc != 0) {
200     			spin_lock_irqsave(&card->lock, flags);
201     			card->pt.blocks_played = bc;
202     			if (card->pt.blocks_played >= card->pt.blocks_copied) {
203     				DPF(1, "buffer underrun in passthrough playback\n");
204     				emu10k1_pt_stop(card);
205     			}
206     			wake_up_interruptible(&card->pt.wait);
207     			spin_unlock_irqrestore(&card->lock, flags);
208     			DPD(3, "pt interrupt, bc = %d\n", bc);
209     		}
210     	}
211     }
212