Blame view

sound/drivers/opl4/opl4_lib.c 7.21 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Functions for accessing OPL4 devices
   * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   */
  
  #include "opl4_local.h"
  #include <sound/initval.h>
  #include <linux/ioport.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  #include <linux/init.h>
da155d5b4   Paul Gortmaker   sound: Add module...
25
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
  #include <asm/io.h>
  
  MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  MODULE_DESCRIPTION("OPL4 driver");
  MODULE_LICENSE("GPL");
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
31
  static void inline snd_opl4_wait(struct snd_opl4 *opl4)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
  {
  	int timeout = 10;
  	while ((inb(opl4->fm_port) & OPL4_STATUS_BUSY) && --timeout > 0)
  		;
  }
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
37
  void snd_opl4_write(struct snd_opl4 *opl4, u8 reg, u8 value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
40
41
42
43
44
  {
  	snd_opl4_wait(opl4);
  	outb(reg, opl4->pcm_port);
  
  	snd_opl4_wait(opl4);
  	outb(value, opl4->pcm_port + 1);
  }
4181e5fe4   Takashi Iwai   [ALSA] opl4 - Mov...
45
  EXPORT_SYMBOL(snd_opl4_write);
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
46
  u8 snd_opl4_read(struct snd_opl4 *opl4, u8 reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
50
51
52
53
  {
  	snd_opl4_wait(opl4);
  	outb(reg, opl4->pcm_port);
  
  	snd_opl4_wait(opl4);
  	return inb(opl4->pcm_port + 1);
  }
4181e5fe4   Takashi Iwai   [ALSA] opl4 - Mov...
54
  EXPORT_SYMBOL(snd_opl4_read);
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
55
  void snd_opl4_read_memory(struct snd_opl4 *opl4, char *buf, int offset, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  {
  	unsigned long flags;
  	u8 memcfg;
  
  	spin_lock_irqsave(&opl4->reg_lock, flags);
  
  	memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
  
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
  
  	snd_opl4_wait(opl4);
  	outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
  	snd_opl4_wait(opl4);
  	insb(opl4->pcm_port + 1, buf, size);
  
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
  
  	spin_unlock_irqrestore(&opl4->reg_lock, flags);
  }
4181e5fe4   Takashi Iwai   [ALSA] opl4 - Mov...
78
  EXPORT_SYMBOL(snd_opl4_read_memory);
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
79
  void snd_opl4_write_memory(struct snd_opl4 *opl4, const char *buf, int offset, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  {
  	unsigned long flags;
  	u8 memcfg;
  
  	spin_lock_irqsave(&opl4->reg_lock, flags);
  
  	memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
  
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
  
  	snd_opl4_wait(opl4);
  	outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
  	snd_opl4_wait(opl4);
  	outsb(opl4->pcm_port + 1, buf, size);
  
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
  
  	spin_unlock_irqrestore(&opl4->reg_lock, flags);
  }
4181e5fe4   Takashi Iwai   [ALSA] opl4 - Mov...
102
  EXPORT_SYMBOL(snd_opl4_write_memory);
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
103
  static void snd_opl4_enable_opl4(struct snd_opl4 *opl4)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
110
111
  {
  	outb(OPL3_REG_MODE, opl4->fm_port + 2);
  	inb(opl4->fm_port);
  	inb(opl4->fm_port);
  	outb(OPL3_OPL3_ENABLE | OPL3_OPL4_ENABLE, opl4->fm_port + 3);
  	inb(opl4->fm_port);
  	inb(opl4->fm_port);
  }
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
112
  static int snd_opl4_detect(struct snd_opl4 *opl4)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  {
  	u8 id1, id2;
  
  	snd_opl4_enable_opl4(opl4);
  
  	id1 = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  	snd_printdd("OPL4[02]=%02x
  ", id1);
  	switch (id1 & OPL4_DEVICE_ID_MASK) {
  	case 0x20:
  		opl4->hardware = OPL3_HW_OPL4;
  		break;
  	case 0x40:
  		opl4->hardware = OPL3_HW_OPL4_ML;
  		break;
  	default:
  		return -ENODEV;
  	}
  
  	snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x00);
  	snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0xff);
  	id1 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_FM);
  	id2 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_PCM);
  	snd_printdd("OPL4 id1=%02x id2=%02x
  ", id1, id2);
         	if (id1 != 0x00 || id2 != 0xff)
  		return -ENODEV;
  
  	snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x3f);
  	snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0x3f);
  	snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, 0x00);
  	return 0;
  }
  
  #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
148
  static void snd_opl4_seq_dev_free(struct snd_seq_device *seq_dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  {
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
150
  	struct snd_opl4 *opl4 = seq_dev->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
  	opl4->seq_dev = NULL;
  }
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
153
  static int snd_opl4_create_seq_dev(struct snd_opl4 *opl4, int seq_device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
  {
  	opl4->seq_dev_num = seq_device;
  	if (snd_seq_device_new(opl4->card, seq_device, SNDRV_SEQ_DEV_ID_OPL4,
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
157
  			       sizeof(struct snd_opl4 *), &opl4->seq_dev) >= 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  		strcpy(opl4->seq_dev->name, "OPL4 Wavetable");
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
159
  		*(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(opl4->seq_dev) = opl4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
165
  		opl4->seq_dev->private_data = opl4;
  		opl4->seq_dev->private_free = snd_opl4_seq_dev_free;
  	}
  	return 0;
  }
  #endif
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
166
  static void snd_opl4_free(struct snd_opl4 *opl4)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
  {
  #ifdef CONFIG_PROC_FS
  	snd_opl4_free_proc(opl4);
  #endif
b1d5776d8   Takashi Iwai   [ALSA] Remove vma...
171
172
  	release_and_free_resource(opl4->res_fm_port);
  	release_and_free_resource(opl4->res_pcm_port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
  	kfree(opl4);
  }
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
175
  static int snd_opl4_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  {
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
177
  	struct snd_opl4 *opl4 = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
  	snd_opl4_free(opl4);
  	return 0;
  }
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
181
  int snd_opl4_create(struct snd_card *card,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
  		    unsigned long fm_port, unsigned long pcm_port,
  		    int seq_device,
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
184
  		    struct snd_opl3 **ropl3, struct snd_opl4 **ropl4)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
  {
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
186
187
  	struct snd_opl4 *opl4;
  	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
  	int err;
a42dd420b   Takashi Iwai   [ALSA] Remove xxx...
189
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
193
194
195
196
  		.dev_free = snd_opl4_dev_free
  	};
  
  	if (ropl3)
  		*ropl3 = NULL;
  	if (ropl4)
  		*ropl4 = NULL;
561b220a4   Takashi Iwai   [ALSA] Replace wi...
197
  	opl4 = kzalloc(sizeof(*opl4), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  	if (!opl4)
  		return -ENOMEM;
  
  	opl4->res_fm_port = request_region(fm_port, 8, "OPL4 FM");
  	opl4->res_pcm_port = request_region(pcm_port, 8, "OPL4 PCM/MIX");
  	if (!opl4->res_fm_port || !opl4->res_pcm_port) {
  		snd_printk(KERN_ERR "opl4: can't grab ports 0x%lx, 0x%lx
  ", fm_port, pcm_port);
  		snd_opl4_free(opl4);
  		return -EBUSY;
  	}
  
  	opl4->card = card;
  	opl4->fm_port = fm_port;
  	opl4->pcm_port = pcm_port;
  	spin_lock_init(&opl4->reg_lock);
ef9f0a42d   Ingo Molnar   [ALSA] semaphore ...
214
  	mutex_init(&opl4->access_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  
  	err = snd_opl4_detect(opl4);
  	if (err < 0) {
  		snd_opl4_free(opl4);
  		snd_printd("OPL4 chip not detected at %#lx/%#lx
  ", fm_port, pcm_port);
  		return err;
  	}
  
  	err = snd_device_new(card, SNDRV_DEV_CODEC, opl4, &ops);
  	if (err < 0) {
  		snd_opl4_free(opl4);
  		return err;
  	}
  
  	err = snd_opl3_create(card, fm_port, fm_port + 2, opl4->hardware, 1, &opl3);
  	if (err < 0) {
  		snd_device_free(card, opl4);
  		return err;
  	}
  
  	/* opl3 initialization disabled opl4, so reenable */
  	snd_opl4_enable_opl4(opl4);
  
  	snd_opl4_create_mixer(opl4);
  #ifdef CONFIG_PROC_FS
  	snd_opl4_create_proc(opl4);
  #endif
  
  #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  	opl4->seq_client = -1;
  	if (opl4->hardware < OPL3_HW_OPL4_ML)
  		snd_opl4_create_seq_dev(opl4, seq_device);
  #endif
  
  	if (ropl3)
  		*ropl3 = opl3;
  	if (ropl4)
  		*ropl4 = opl4;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
259
260
261
262
263
264
265
266
267
268
  EXPORT_SYMBOL(snd_opl4_create);
  
  static int __init alsa_opl4_init(void)
  {
  	return 0;
  }
  
  static void __exit alsa_opl4_exit(void)
  {
  }
  
  module_init(alsa_opl4_init)
  module_exit(alsa_opl4_exit)