blob: e374869e3e21e5fcd568c5752711ce15e05a9e54 [file] [log] [blame]
Martin Langer1841f6132006-03-27 12:41:01 +02001/*
2 * ALSA soundcard driver for Miro miroSOUND PCM1 pro
3 * miroSOUND PCM12
4 * miroSOUND PCM20 Radio
5 *
6 * Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
7 *
8 * Based on OSS ACI and ALSA OPTi9xx drivers
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
Martin Langer1841f6132006-03-27 12:41:01 +020025#include <linux/init.h>
26#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010027#include <linux/isa.h>
Martin Langer1841f6132006-03-27 12:41:01 +020028#include <linux/delay.h>
29#include <linux/slab.h>
30#include <linux/ioport.h>
31#include <linux/moduleparam.h>
32#include <asm/io.h>
33#include <asm/dma.h>
34#include <sound/core.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020035#include <sound/wss.h>
Martin Langer1841f6132006-03-27 12:41:01 +020036#include <sound/mpu401.h>
37#include <sound/opl4.h>
38#include <sound/control.h>
39#include <sound/info.h>
40#define SNDRV_LEGACY_FIND_FREE_IRQ
41#define SNDRV_LEGACY_FIND_FREE_DMA
42#include <sound/initval.h>
Krzysztof Helt9aeba622009-11-22 17:23:45 +010043#include <sound/aci.h>
Martin Langer1841f6132006-03-27 12:41:01 +020044
45MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
46MODULE_LICENSE("GPL");
47MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
48MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
49 "{Miro,miroSOUND PCM12}, "
50 "{Miro,miroSOUND PCM20 Radio}}");
51
52static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
53static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
54static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
55static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
56static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
57static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
58static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
59static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
60static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
61static int wss;
62static int ide;
63
64module_param(index, int, 0444);
65MODULE_PARM_DESC(index, "Index value for miro soundcard.");
66module_param(id, charp, 0444);
67MODULE_PARM_DESC(id, "ID string for miro soundcard.");
68module_param(port, long, 0444);
69MODULE_PARM_DESC(port, "WSS port # for miro driver.");
70module_param(mpu_port, long, 0444);
71MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
72module_param(fm_port, long, 0444);
73MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
74module_param(irq, int, 0444);
75MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
76module_param(mpu_irq, int, 0444);
77MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
78module_param(dma1, int, 0444);
79MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
80module_param(dma2, int, 0444);
81MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
82module_param(wss, int, 0444);
83MODULE_PARM_DESC(wss, "wss mode");
84module_param(ide, int, 0444);
85MODULE_PARM_DESC(ide, "enable ide port");
86
87#define OPTi9XX_HW_DETECT 0
88#define OPTi9XX_HW_82C928 1
89#define OPTi9XX_HW_82C929 2
90#define OPTi9XX_HW_82C924 3
91#define OPTi9XX_HW_82C925 4
92#define OPTi9XX_HW_82C930 5
93#define OPTi9XX_HW_82C931 6
94#define OPTi9XX_HW_82C933 7
95#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
96
97#define OPTi9XX_MC_REG(n) n
98
Martin Langer1841f6132006-03-27 12:41:01 +020099struct snd_miro {
100 unsigned short hardware;
101 unsigned char password;
102 char name[7];
103
104 struct resource *res_mc_base;
105 struct resource *res_aci_port;
106
107 unsigned long mc_base;
108 unsigned long mc_base_size;
109 unsigned long pwd_reg;
110
111 spinlock_t lock;
Martin Langer1841f6132006-03-27 12:41:01 +0200112 struct snd_pcm *pcm;
113
114 long wss_base;
115 int irq;
116 int dma1;
117 int dma2;
118
Martin Langer1841f6132006-03-27 12:41:01 +0200119 long mpu_port;
120 int mpu_irq;
121
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100122 struct snd_miro_aci *aci;
Martin Langer1841f6132006-03-27 12:41:01 +0200123};
124
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100125static struct snd_miro_aci aci_device;
126
Martin Langer1841f6132006-03-27 12:41:01 +0200127static char * snd_opti9xx_names[] = {
128 "unkown",
129 "82C928", "82C929",
130 "82C924", "82C925",
131 "82C930", "82C931", "82C933"
132};
133
134/*
135 * ACI control
136 */
137
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100138static int aci_busy_wait(struct snd_miro_aci *aci)
Martin Langer1841f6132006-03-27 12:41:01 +0200139{
140 long timeout;
141 unsigned char byte;
142
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100143 for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
144 byte = inb(aci->aci_port + ACI_REG_BUSY);
145 if ((byte & 1) == 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200146 if (timeout >= ACI_MINTIME)
147 snd_printd("aci ready in round %ld.\n",
148 timeout-ACI_MINTIME);
149 return byte;
150 }
151 if (timeout >= ACI_MINTIME) {
152 long out=10*HZ;
153 switch (timeout-ACI_MINTIME) {
154 case 0 ... 9:
155 out /= 10;
156 case 10 ... 19:
157 out /= 10;
158 case 20 ... 30:
159 out /= 10;
160 default:
161 set_current_state(TASK_UNINTERRUPTIBLE);
162 schedule_timeout(out);
163 break;
164 }
165 }
166 }
167 snd_printk(KERN_ERR "aci_busy_wait() time out\n");
168 return -EBUSY;
169}
170
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100171static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
Martin Langer1841f6132006-03-27 12:41:01 +0200172{
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100173 if (aci_busy_wait(aci) >= 0) {
174 outb(byte, aci->aci_port + ACI_REG_COMMAND);
Martin Langer1841f6132006-03-27 12:41:01 +0200175 return 0;
176 } else {
177 snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
178 return -EBUSY;
179 }
180}
181
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100182static inline int aci_read(struct snd_miro_aci *aci)
Martin Langer1841f6132006-03-27 12:41:01 +0200183{
184 unsigned char byte;
185
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100186 if (aci_busy_wait(aci) >= 0) {
187 byte = inb(aci->aci_port + ACI_REG_STATUS);
Martin Langer1841f6132006-03-27 12:41:01 +0200188 return byte;
189 } else {
190 snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
191 return -EBUSY;
192 }
193}
194
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100195int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
Martin Langer1841f6132006-03-27 12:41:01 +0200196{
197 int write[] = {write1, write2, write3};
198 int value, i;
199
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100200 if (mutex_lock_interruptible(&aci->aci_mutex))
Martin Langer1841f6132006-03-27 12:41:01 +0200201 return -EINTR;
202
203 for (i=0; i<3; i++) {
204 if (write[i]< 0 || write[i] > 255)
205 break;
206 else {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100207 value = aci_write(aci, write[i]);
Martin Langer1841f6132006-03-27 12:41:01 +0200208 if (value < 0)
209 goto out;
210 }
211 }
212
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100213 value = aci_read(aci);
Martin Langer1841f6132006-03-27 12:41:01 +0200214
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100215out: mutex_unlock(&aci->aci_mutex);
Martin Langer1841f6132006-03-27 12:41:01 +0200216 return value;
217}
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100218EXPORT_SYMBOL(snd_aci_cmd);
Martin Langer1841f6132006-03-27 12:41:01 +0200219
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100220static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
Martin Langer1841f6132006-03-27 12:41:01 +0200221{
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100222 return snd_aci_cmd(aci, ACI_STATUS, index, -1);
Martin Langer1841f6132006-03-27 12:41:01 +0200223}
224
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100225static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
226 int value)
Martin Langer1841f6132006-03-27 12:41:01 +0200227{
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100228 return snd_aci_cmd(aci, index, value, -1);
Martin Langer1841f6132006-03-27 12:41:01 +0200229}
230
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100231struct snd_miro_aci *snd_aci_get_aci(void)
232{
233 if (aci_device.aci_port == 0)
234 return NULL;
235 return &aci_device;
236}
237EXPORT_SYMBOL(snd_aci_get_aci);
238
Martin Langer1841f6132006-03-27 12:41:01 +0200239/*
240 * MIXER part
241 */
242
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200243#define snd_miro_info_capture snd_ctl_boolean_mono_info
Martin Langer1841f6132006-03-27 12:41:01 +0200244
245static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_value *ucontrol)
247{
248 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
249 int value;
250
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100251 value = aci_getvalue(miro->aci, ACI_S_GENERAL);
252 if (value < 0) {
253 snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
254 value);
Martin Langer1841f6132006-03-27 12:41:01 +0200255 return value;
256 }
257
258 ucontrol->value.integer.value[0] = value & 0x20;
259
260 return 0;
261}
262
263static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
264 struct snd_ctl_elem_value *ucontrol)
265{
266 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
267 int change, value, error;
268
269 value = !(ucontrol->value.integer.value[0]);
270
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100271 error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
272 if (error < 0) {
273 snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
274 error);
Martin Langer1841f6132006-03-27 12:41:01 +0200275 return error;
276 }
277
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100278 change = (value != miro->aci->aci_solomode);
279 miro->aci->aci_solomode = value;
Martin Langer1841f6132006-03-27 12:41:01 +0200280
281 return change;
282}
283
284static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_info *uinfo)
286{
287 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
288 uinfo->count = 1;
289 uinfo->value.integer.min = 0;
290 uinfo->value.integer.max = 3;
291
292 return 0;
293}
294
295static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
297{
298 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
299 int value;
300
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100301 if (miro->aci->aci_version <= 176) {
Martin Langer1841f6132006-03-27 12:41:01 +0200302
303 /*
304 OSS says it's not readable with versions < 176.
305 But it doesn't work on my card,
306 which is a PCM12 with aci_version = 176.
307 */
308
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100309 ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
Martin Langer1841f6132006-03-27 12:41:01 +0200310 return 0;
311 }
312
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100313 value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
314 if (value < 0) {
315 snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
316 value);
Martin Langer1841f6132006-03-27 12:41:01 +0200317 return value;
318 }
319
320 ucontrol->value.integer.value[0] = value;
321
322 return 0;
323}
324
325static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
327{
328 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
329 int error, value, change;
330
331 value = ucontrol->value.integer.value[0];
332
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100333 error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
334 if (error < 0) {
335 snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
336 error);
Martin Langer1841f6132006-03-27 12:41:01 +0200337 return error;
338 }
339
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100340 change = (value != miro->aci->aci_preamp);
341 miro->aci->aci_preamp = value;
Martin Langer1841f6132006-03-27 12:41:01 +0200342
343 return change;
344}
345
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200346#define snd_miro_info_amp snd_ctl_boolean_mono_info
Martin Langer1841f6132006-03-27 12:41:01 +0200347
348static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
350{
351 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100352 ucontrol->value.integer.value[0] = miro->aci->aci_amp;
Martin Langer1841f6132006-03-27 12:41:01 +0200353
354 return 0;
355}
356
357static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
358 struct snd_ctl_elem_value *ucontrol)
359{
360 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
361 int error, value, change;
362
363 value = ucontrol->value.integer.value[0];
364
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100365 error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
366 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200367 snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
368 return error;
369 }
370
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100371 change = (value != miro->aci->aci_amp);
372 miro->aci->aci_amp = value;
Martin Langer1841f6132006-03-27 12:41:01 +0200373
374 return change;
375}
376
377#define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
378{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
379 .name = ctl_name, \
380 .index = ctl_index, \
381 .info = snd_miro_info_double, \
382 .get = snd_miro_get_double, \
383 .put = snd_miro_put_double, \
384 .private_value = get_right_reg | (set_right_reg << 8) \
385}
386
387static int snd_miro_info_double(struct snd_kcontrol *kcontrol,
388 struct snd_ctl_elem_info *uinfo)
389{
390 int reg = kcontrol->private_value & 0xff;
391
392 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
393 uinfo->count = 2;
394
395 if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
396
397 /* equalizer elements */
398
399 uinfo->value.integer.min = - 0x7f;
400 uinfo->value.integer.max = 0x7f;
401 } else {
402
403 /* non-equalizer elements */
404
405 uinfo->value.integer.min = 0;
406 uinfo->value.integer.max = 0x20;
407 }
408
409 return 0;
410}
411
412static int snd_miro_get_double(struct snd_kcontrol *kcontrol,
413 struct snd_ctl_elem_value *uinfo)
414{
415 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
416 int left_val, right_val;
417
418 int right_reg = kcontrol->private_value & 0xff;
419 int left_reg = right_reg + 1;
420
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100421 right_val = aci_getvalue(miro->aci, right_reg);
422 if (right_val < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200423 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
424 return right_val;
425 }
426
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100427 left_val = aci_getvalue(miro->aci, left_reg);
428 if (left_val < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200429 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
430 return left_val;
431 }
432
433 if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
434
435 /* equalizer elements */
436
437 if (left_val < 0x80) {
438 uinfo->value.integer.value[0] = left_val;
439 } else {
440 uinfo->value.integer.value[0] = 0x80 - left_val;
441 }
442
443 if (right_val < 0x80) {
444 uinfo->value.integer.value[1] = right_val;
445 } else {
446 uinfo->value.integer.value[1] = 0x80 - right_val;
447 }
448
449 } else {
450
451 /* non-equalizer elements */
452
453 uinfo->value.integer.value[0] = 0x20 - left_val;
454 uinfo->value.integer.value[1] = 0x20 - right_val;
455 }
456
457 return 0;
458}
459
460static int snd_miro_put_double(struct snd_kcontrol *kcontrol,
461 struct snd_ctl_elem_value *ucontrol)
462{
463 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100464 struct snd_miro_aci *aci = miro->aci;
Martin Langer1841f6132006-03-27 12:41:01 +0200465 int left, right, left_old, right_old;
466 int setreg_left, setreg_right, getreg_left, getreg_right;
467 int change, error;
468
469 left = ucontrol->value.integer.value[0];
470 right = ucontrol->value.integer.value[1];
471
472 setreg_right = (kcontrol->private_value >> 8) & 0xff;
Krzysztof Helt616ad592009-11-21 01:01:18 +0100473 setreg_left = setreg_right + 8;
474 if (setreg_right == ACI_SET_MASTER)
475 setreg_left -= 7;
Martin Langer1841f6132006-03-27 12:41:01 +0200476
477 getreg_right = kcontrol->private_value & 0xff;
478 getreg_left = getreg_right + 1;
479
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100480 left_old = aci_getvalue(aci, getreg_left);
481 if (left_old < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200482 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
483 return left_old;
484 }
485
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100486 right_old = aci_getvalue(aci, getreg_right);
487 if (right_old < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200488 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
489 return right_old;
490 }
491
492 if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
493
494 /* equalizer elements */
495
Takashi Iwai3b892462007-11-15 16:17:24 +0100496 if (left < -0x7f || left > 0x7f ||
497 right < -0x7f || right > 0x7f)
498 return -EINVAL;
499
Martin Langer1841f6132006-03-27 12:41:01 +0200500 if (left_old > 0x80)
501 left_old = 0x80 - left_old;
502 if (right_old > 0x80)
503 right_old = 0x80 - right_old;
504
505 if (left >= 0) {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100506 error = aci_setvalue(aci, setreg_left, left);
507 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200508 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
509 left, error);
510 return error;
511 }
512 } else {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100513 error = aci_setvalue(aci, setreg_left, 0x80 - left);
514 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200515 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
516 0x80 - left, error);
517 return error;
518 }
519 }
520
521 if (right >= 0) {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100522 error = aci_setvalue(aci, setreg_right, right);
523 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200524 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
525 right, error);
526 return error;
527 }
528 } else {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100529 error = aci_setvalue(aci, setreg_right, 0x80 - right);
530 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200531 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
532 0x80 - right, error);
533 return error;
534 }
535 }
536
537 } else {
538
539 /* non-equalizer elements */
540
Takashi Iwai3b892462007-11-15 16:17:24 +0100541 if (left < 0 || left > 0x20 ||
542 right < 0 || right > 0x20)
543 return -EINVAL;
544
Martin Langer1841f6132006-03-27 12:41:01 +0200545 left_old = 0x20 - left_old;
546 right_old = 0x20 - right_old;
547
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100548 error = aci_setvalue(aci, setreg_left, 0x20 - left);
549 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200550 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
551 0x20 - left, error);
552 return error;
553 }
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100554 error = aci_setvalue(aci, setreg_right, 0x20 - right);
555 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200556 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
557 0x20 - right, error);
558 return error;
559 }
560 }
561
562 change = (left != left_old) || (right != right_old);
563
564 return change;
565}
566
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100567static struct snd_kcontrol_new snd_miro_controls[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200568MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
569MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
570MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
571MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
572MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
573MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
574MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
575};
576
577/* Equalizer with seven bands (only PCM20)
578 from -12dB up to +12dB on each band */
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100579static struct snd_kcontrol_new snd_miro_eq_controls[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200580MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
581MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
582MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
583MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
584MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
585MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
586MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
587};
588
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100589static struct snd_kcontrol_new snd_miro_radio_control[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200590MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
591};
592
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100593static struct snd_kcontrol_new snd_miro_line_control[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200594MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
595};
596
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100597static struct snd_kcontrol_new snd_miro_preamp_control[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200598{
599 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
600 .name = "Mic Boost",
601 .index = 1,
602 .info = snd_miro_info_preamp,
603 .get = snd_miro_get_preamp,
604 .put = snd_miro_put_preamp,
605}};
606
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100607static struct snd_kcontrol_new snd_miro_amp_control[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200608{
609 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
610 .name = "Line Boost",
611 .index = 0,
612 .info = snd_miro_info_amp,
613 .get = snd_miro_get_amp,
614 .put = snd_miro_put_amp,
615}};
616
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100617static struct snd_kcontrol_new snd_miro_capture_control[] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200618{
619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
620 .name = "PCM Capture Switch",
621 .index = 0,
622 .info = snd_miro_info_capture,
623 .get = snd_miro_get_capture,
624 .put = snd_miro_put_capture,
625}};
626
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100627static unsigned char aci_init_values[][2] __devinitdata = {
Martin Langer1841f6132006-03-27 12:41:01 +0200628 { ACI_SET_MUTE, 0x00 },
629 { ACI_SET_POWERAMP, 0x00 },
630 { ACI_SET_PREAMP, 0x00 },
631 { ACI_SET_SOLOMODE, 0x00 },
632 { ACI_SET_MIC + 0, 0x20 },
633 { ACI_SET_MIC + 8, 0x20 },
634 { ACI_SET_LINE + 0, 0x20 },
635 { ACI_SET_LINE + 8, 0x20 },
636 { ACI_SET_CD + 0, 0x20 },
637 { ACI_SET_CD + 8, 0x20 },
638 { ACI_SET_PCM + 0, 0x20 },
639 { ACI_SET_PCM + 8, 0x20 },
640 { ACI_SET_LINE1 + 0, 0x20 },
641 { ACI_SET_LINE1 + 8, 0x20 },
642 { ACI_SET_LINE2 + 0, 0x20 },
643 { ACI_SET_LINE2 + 8, 0x20 },
644 { ACI_SET_SYNTH + 0, 0x20 },
645 { ACI_SET_SYNTH + 8, 0x20 },
646 { ACI_SET_MASTER + 0, 0x20 },
647 { ACI_SET_MASTER + 1, 0x20 },
648};
649
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100650static int __devinit snd_set_aci_init_values(struct snd_miro *miro)
Martin Langer1841f6132006-03-27 12:41:01 +0200651{
652 int idx, error;
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100653 struct snd_miro_aci *aci = miro->aci;
Martin Langer1841f6132006-03-27 12:41:01 +0200654
655 /* enable WSS on PCM1 */
656
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100657 if ((aci->aci_product == 'A') && wss) {
658 error = aci_setvalue(aci, ACI_SET_WSS, wss);
659 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200660 snd_printk(KERN_ERR "enabling WSS mode failed\n");
661 return error;
662 }
663 }
664
665 /* enable IDE port */
666
667 if (ide) {
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100668 error = aci_setvalue(aci, ACI_SET_IDE, ide);
669 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200670 snd_printk(KERN_ERR "enabling IDE port failed\n");
671 return error;
672 }
673 }
674
675 /* set common aci values */
676
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100677 for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
678 error = aci_setvalue(aci, aci_init_values[idx][0],
679 aci_init_values[idx][1]);
680 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +0200681 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
682 aci_init_values[idx][0], error);
683 return error;
684 }
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100685 }
686 aci->aci_amp = 0;
687 aci->aci_preamp = 0;
688 aci->aci_solomode = 1;
Martin Langer1841f6132006-03-27 12:41:01 +0200689
690 return 0;
691}
692
Krzysztof Helt616ad592009-11-21 01:01:18 +0100693static int __devinit snd_miro_mixer(struct snd_card *card,
694 struct snd_miro *miro)
Martin Langer1841f6132006-03-27 12:41:01 +0200695{
Martin Langer1841f6132006-03-27 12:41:01 +0200696 unsigned int idx;
697 int err;
698
Krzysztof Helt616ad592009-11-21 01:01:18 +0100699 if (snd_BUG_ON(!miro || !card))
Takashi Iwai622207d2008-08-08 17:11:45 +0200700 return -EINVAL;
Martin Langer1841f6132006-03-27 12:41:01 +0200701
Martin Langer1841f6132006-03-27 12:41:01 +0200702 switch (miro->hardware) {
703 case OPTi9XX_HW_82C924:
704 strcpy(card->mixername, "ACI & OPTi924");
705 break;
706 case OPTi9XX_HW_82C929:
707 strcpy(card->mixername, "ACI & OPTi929");
708 break;
709 default:
710 snd_BUG();
711 break;
712 }
713
714 for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
715 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
716 return err;
717 }
718
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100719 if ((miro->aci->aci_product == 'A') ||
720 (miro->aci->aci_product == 'B')) {
Martin Langer1841f6132006-03-27 12:41:01 +0200721 /* PCM1/PCM12 with power-amp and Line 2 */
722 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
723 return err;
724 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
725 return err;
726 }
727
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100728 if ((miro->aci->aci_product == 'B') ||
729 (miro->aci->aci_product == 'C')) {
Martin Langer1841f6132006-03-27 12:41:01 +0200730 /* PCM12/PCM20 with mic-preamp */
731 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
732 return err;
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100733 if (miro->aci->aci_version >= 176)
Martin Langer1841f6132006-03-27 12:41:01 +0200734 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
735 return err;
736 }
737
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100738 if (miro->aci->aci_product == 'C') {
Martin Langer1841f6132006-03-27 12:41:01 +0200739 /* PCM20 with radio and 7 band equalizer */
740 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
741 return err;
742 for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
743 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
744 return err;
745 }
746 }
747
748 return 0;
749}
750
751static long snd_legacy_find_free_ioport(long *port_table, long size)
752{
753 while (*port_table != -1) {
754 struct resource *res;
755 if ((res = request_region(*port_table, size,
756 "ALSA test")) != NULL) {
Takashi Iwai10d150e2006-03-27 13:42:39 +0200757 release_and_free_resource(res);
Martin Langer1841f6132006-03-27 12:41:01 +0200758 return *port_table;
759 }
760 port_table++;
761 }
762 return -1;
763}
764
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100765static int __devinit snd_miro_init(struct snd_miro *chip,
766 unsigned short hardware)
Martin Langer1841f6132006-03-27 12:41:01 +0200767{
768 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
769
770 chip->hardware = hardware;
771 strcpy(chip->name, snd_opti9xx_names[hardware]);
772
773 chip->mc_base_size = opti9xx_mc_size[hardware];
774
775 spin_lock_init(&chip->lock);
776
777 chip->wss_base = -1;
778 chip->irq = -1;
779 chip->dma1 = -1;
780 chip->dma2 = -1;
Martin Langer1841f6132006-03-27 12:41:01 +0200781 chip->mpu_port = -1;
782 chip->mpu_irq = -1;
783
784 switch (hardware) {
785 case OPTi9XX_HW_82C929:
786 chip->mc_base = 0xf8c;
787 chip->password = 0xe3;
788 chip->pwd_reg = 3;
789 break;
790
791 case OPTi9XX_HW_82C924:
792 chip->mc_base = 0xf8c;
793 chip->password = 0xe5;
794 chip->pwd_reg = 3;
795 break;
796
797 default:
798 snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
799 return -ENODEV;
800 }
801
802 return 0;
803}
804
805static unsigned char snd_miro_read(struct snd_miro *chip,
806 unsigned char reg)
807{
808 unsigned long flags;
809 unsigned char retval = 0xff;
810
811 spin_lock_irqsave(&chip->lock, flags);
812 outb(chip->password, chip->mc_base + chip->pwd_reg);
813
814 switch (chip->hardware) {
815 case OPTi9XX_HW_82C924:
816 if (reg > 7) {
817 outb(reg, chip->mc_base + 8);
818 outb(chip->password, chip->mc_base + chip->pwd_reg);
819 retval = inb(chip->mc_base + 9);
820 break;
821 }
822
823 case OPTi9XX_HW_82C929:
824 retval = inb(chip->mc_base + reg);
825 break;
826
827 default:
828 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
829 }
830
831 spin_unlock_irqrestore(&chip->lock, flags);
832 return retval;
833}
834
835static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
836 unsigned char value)
837{
838 unsigned long flags;
839
840 spin_lock_irqsave(&chip->lock, flags);
841 outb(chip->password, chip->mc_base + chip->pwd_reg);
842
843 switch (chip->hardware) {
844 case OPTi9XX_HW_82C924:
845 if (reg > 7) {
846 outb(reg, chip->mc_base + 8);
847 outb(chip->password, chip->mc_base + chip->pwd_reg);
848 outb(value, chip->mc_base + 9);
849 break;
850 }
851
852 case OPTi9XX_HW_82C929:
853 outb(value, chip->mc_base + reg);
854 break;
855
856 default:
857 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
858 }
859
860 spin_unlock_irqrestore(&chip->lock, flags);
861}
862
863
864#define snd_miro_write_mask(chip, reg, value, mask) \
865 snd_miro_write(chip, reg, \
866 (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask)))
867
868/*
869 * Proc Interface
870 */
871
872static void snd_miro_proc_read(struct snd_info_entry * entry,
873 struct snd_info_buffer *buffer)
874{
875 struct snd_miro *miro = (struct snd_miro *) entry->private_data;
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100876 struct snd_miro_aci *aci = miro->aci;
Martin Langer1841f6132006-03-27 12:41:01 +0200877 char* model = "unknown";
878
879 /* miroSOUND PCM1 pro, early PCM12 */
880
881 if ((miro->hardware == OPTi9XX_HW_82C929) &&
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100882 (aci->aci_vendor == 'm') &&
883 (aci->aci_product == 'A')) {
884 switch (aci->aci_version) {
Martin Langer1841f6132006-03-27 12:41:01 +0200885 case 3:
886 model = "miroSOUND PCM1 pro";
887 break;
888 default:
889 model = "miroSOUND PCM1 pro / (early) PCM12";
890 break;
891 }
892 }
893
894 /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
895
896 if ((miro->hardware == OPTi9XX_HW_82C924) &&
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100897 (aci->aci_vendor == 'm') &&
898 (aci->aci_product == 'B')) {
899 switch (aci->aci_version) {
Martin Langer1841f6132006-03-27 12:41:01 +0200900 case 4:
901 model = "miroSOUND PCM12";
902 break;
903 case 176:
904 model = "miroSOUND PCM12 (Rev. E)";
905 break;
906 default:
907 model = "miroSOUND PCM12 / PCM12 pnp";
908 break;
909 }
910 }
911
912 /* miroSOUND PCM20 radio */
913
914 if ((miro->hardware == OPTi9XX_HW_82C924) &&
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100915 (aci->aci_vendor == 'm') &&
916 (aci->aci_product == 'C')) {
917 switch (aci->aci_version) {
Martin Langer1841f6132006-03-27 12:41:01 +0200918 case 7:
919 model = "miroSOUND PCM20 radio (Rev. E)";
920 break;
921 default:
922 model = "miroSOUND PCM20 radio";
923 break;
924 }
925 }
926
927 snd_iprintf(buffer, "\nGeneral information:\n");
928 snd_iprintf(buffer, " model : %s\n", model);
929 snd_iprintf(buffer, " opti : %s\n", miro->name);
930 snd_iprintf(buffer, " codec : %s\n", miro->pcm->name);
931 snd_iprintf(buffer, " port : 0x%lx\n", miro->wss_base);
932 snd_iprintf(buffer, " irq : %d\n", miro->irq);
933 snd_iprintf(buffer, " dma : %d,%d\n\n", miro->dma1, miro->dma2);
934
935 snd_iprintf(buffer, "MPU-401:\n");
936 snd_iprintf(buffer, " port : 0x%lx\n", miro->mpu_port);
937 snd_iprintf(buffer, " irq : %d\n\n", miro->mpu_irq);
938
939 snd_iprintf(buffer, "ACI information:\n");
940 snd_iprintf(buffer, " vendor : ");
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100941 switch (aci->aci_vendor) {
Martin Langer1841f6132006-03-27 12:41:01 +0200942 case 'm':
943 snd_iprintf(buffer, "Miro\n");
944 break;
945 default:
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100946 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
Martin Langer1841f6132006-03-27 12:41:01 +0200947 break;
948 }
949
950 snd_iprintf(buffer, " product : ");
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100951 switch (aci->aci_product) {
Martin Langer1841f6132006-03-27 12:41:01 +0200952 case 'A':
953 snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
954 break;
955 case 'B':
956 snd_iprintf(buffer, "miroSOUND PCM12\n");
957 break;
958 case 'C':
959 snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
960 break;
961 default:
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100962 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
Martin Langer1841f6132006-03-27 12:41:01 +0200963 break;
964 }
965
966 snd_iprintf(buffer, " firmware: %d (0x%x)\n",
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100967 aci->aci_version, aci->aci_version);
Martin Langer1841f6132006-03-27 12:41:01 +0200968 snd_iprintf(buffer, " port : 0x%lx-0x%lx\n",
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100969 aci->aci_port, aci->aci_port+2);
Martin Langer1841f6132006-03-27 12:41:01 +0200970 snd_iprintf(buffer, " wss : 0x%x\n", wss);
971 snd_iprintf(buffer, " ide : 0x%x\n", ide);
Krzysztof Helt9dc91202009-11-22 17:26:34 +0100972 snd_iprintf(buffer, " solomode: 0x%x\n", aci->aci_solomode);
973 snd_iprintf(buffer, " amp : 0x%x\n", aci->aci_amp);
974 snd_iprintf(buffer, " preamp : 0x%x\n", aci->aci_preamp);
Martin Langer1841f6132006-03-27 12:41:01 +0200975}
976
Krzysztof Helt616ad592009-11-21 01:01:18 +0100977static void __devinit snd_miro_proc_init(struct snd_card *card,
978 struct snd_miro *miro)
Martin Langer1841f6132006-03-27 12:41:01 +0200979{
980 struct snd_info_entry *entry;
981
Krzysztof Helt616ad592009-11-21 01:01:18 +0100982 if (!snd_card_proc_new(card, "miro", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +0200983 snd_info_set_text_ops(entry, miro, snd_miro_proc_read);
Martin Langer1841f6132006-03-27 12:41:01 +0200984}
985
986/*
987 * Init
988 */
989
Takashi Iwai5e24c1c2007-02-22 12:50:54 +0100990static int __devinit snd_miro_configure(struct snd_miro *chip)
Martin Langer1841f6132006-03-27 12:41:01 +0200991{
992 unsigned char wss_base_bits;
993 unsigned char irq_bits;
994 unsigned char dma_bits;
995 unsigned char mpu_port_bits = 0;
996 unsigned char mpu_irq_bits;
997 unsigned long flags;
998
Krzysztof Helt616ad592009-11-21 01:01:18 +0100999 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1000 snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1001 snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1002
Martin Langer1841f6132006-03-27 12:41:01 +02001003 switch (chip->hardware) {
1004 case OPTi9XX_HW_82C924:
1005 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
Martin Langer1841f6132006-03-27 12:41:01 +02001006 snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
Martin Langer1841f6132006-03-27 12:41:01 +02001007 break;
1008 case OPTi9XX_HW_82C929:
1009 /* untested init commands for OPTi929 */
Martin Langer1841f6132006-03-27 12:41:01 +02001010 snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
Martin Langer1841f6132006-03-27 12:41:01 +02001011 break;
1012 default:
1013 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
1014 return -EINVAL;
1015 }
1016
1017 switch (chip->wss_base) {
1018 case 0x530:
1019 wss_base_bits = 0x00;
1020 break;
1021 case 0x604:
1022 wss_base_bits = 0x03;
1023 break;
1024 case 0xe80:
1025 wss_base_bits = 0x01;
1026 break;
1027 case 0xf40:
1028 wss_base_bits = 0x02;
1029 break;
1030 default:
1031 snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
1032 goto __skip_base;
1033 }
1034 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1035
1036__skip_base:
1037 switch (chip->irq) {
1038 case 5:
1039 irq_bits = 0x05;
1040 break;
1041 case 7:
1042 irq_bits = 0x01;
1043 break;
1044 case 9:
1045 irq_bits = 0x02;
1046 break;
1047 case 10:
1048 irq_bits = 0x03;
1049 break;
1050 case 11:
1051 irq_bits = 0x04;
1052 break;
1053 default:
1054 snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
1055 goto __skip_resources;
1056 }
1057
1058 switch (chip->dma1) {
1059 case 0:
1060 dma_bits = 0x01;
1061 break;
1062 case 1:
1063 dma_bits = 0x02;
1064 break;
1065 case 3:
1066 dma_bits = 0x03;
1067 break;
1068 default:
1069 snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
1070 goto __skip_resources;
1071 }
1072
1073 if (chip->dma1 == chip->dma2) {
1074 snd_printk(KERN_ERR "don't want to share dmas\n");
1075 return -EBUSY;
1076 }
1077
1078 switch (chip->dma2) {
1079 case 0:
1080 case 1:
1081 break;
1082 default:
1083 snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
1084 goto __skip_resources;
1085 }
1086 dma_bits |= 0x04;
1087
1088 spin_lock_irqsave(&chip->lock, flags);
1089 outb(irq_bits << 3 | dma_bits, chip->wss_base);
1090 spin_unlock_irqrestore(&chip->lock, flags);
1091
1092__skip_resources:
1093 if (chip->hardware > OPTi9XX_HW_82C928) {
1094 switch (chip->mpu_port) {
1095 case 0:
1096 case -1:
1097 break;
1098 case 0x300:
1099 mpu_port_bits = 0x03;
1100 break;
1101 case 0x310:
1102 mpu_port_bits = 0x02;
1103 break;
1104 case 0x320:
1105 mpu_port_bits = 0x01;
1106 break;
1107 case 0x330:
1108 mpu_port_bits = 0x00;
1109 break;
1110 default:
1111 snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
1112 chip->mpu_port);
1113 goto __skip_mpu;
1114 }
1115
1116 switch (chip->mpu_irq) {
1117 case 5:
1118 mpu_irq_bits = 0x02;
1119 break;
1120 case 7:
1121 mpu_irq_bits = 0x03;
1122 break;
1123 case 9:
1124 mpu_irq_bits = 0x00;
1125 break;
1126 case 10:
1127 mpu_irq_bits = 0x01;
1128 break;
1129 default:
1130 snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
1131 chip->mpu_irq);
1132 goto __skip_mpu;
1133 }
1134
1135 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1136 (chip->mpu_port <= 0) ? 0x00 :
1137 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1138 0xf8);
1139 }
1140__skip_mpu:
1141
1142 return 0;
1143}
1144
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001145static int __devinit snd_card_miro_detect(struct snd_card *card,
1146 struct snd_miro *chip)
Martin Langer1841f6132006-03-27 12:41:01 +02001147{
1148 int i, err;
1149 unsigned char value;
1150
1151 for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1152
1153 if ((err = snd_miro_init(chip, i)) < 0)
1154 return err;
1155
1156 if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
1157 continue;
1158
1159 value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1160 if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
1161 if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1162 return 1;
1163
Takashi Iwai10d150e2006-03-27 13:42:39 +02001164 release_and_free_resource(chip->res_mc_base);
Martin Langer1841f6132006-03-27 12:41:01 +02001165 chip->res_mc_base = NULL;
1166
1167 }
1168
1169 return -ENODEV;
1170}
1171
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001172static int __devinit snd_card_miro_aci_detect(struct snd_card *card,
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001173 struct snd_miro *miro)
Martin Langer1841f6132006-03-27 12:41:01 +02001174{
1175 unsigned char regval;
1176 int i;
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001177 struct snd_miro_aci *aci = &aci_device;
Martin Langer1841f6132006-03-27 12:41:01 +02001178
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001179 miro->aci = aci;
1180
1181 mutex_init(&aci->aci_mutex);
Martin Langer1841f6132006-03-27 12:41:01 +02001182
1183 /* get ACI port from OPTi9xx MC 4 */
1184
Martin Langer1841f6132006-03-27 12:41:01 +02001185 regval=inb(miro->mc_base + 4);
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001186 aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
Martin Langer1841f6132006-03-27 12:41:01 +02001187
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001188 miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
1189 if (miro->res_aci_port == NULL) {
Martin Langer1841f6132006-03-27 12:41:01 +02001190 snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n",
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001191 aci->aci_port, aci->aci_port+2);
Martin Langer1841f6132006-03-27 12:41:01 +02001192 return -ENOMEM;
1193 }
1194
1195 /* force ACI into a known state */
1196 for (i = 0; i < 3; i++)
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001197 if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001198 snd_printk(KERN_ERR "can't force aci into known state.\n");
1199 return -ENXIO;
1200 }
1201
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001202 aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1203 aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1204 if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1205 snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
1206 aci->aci_port);
Martin Langer1841f6132006-03-27 12:41:01 +02001207 return -ENXIO;
1208 }
1209
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001210 aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1211 if (aci->aci_version < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001212 snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n",
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001213 aci->aci_port);
Martin Langer1841f6132006-03-27 12:41:01 +02001214 return -ENXIO;
1215 }
1216
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001217 if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1218 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1219 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001220 snd_printk(KERN_ERR "can't initialize aci.\n");
1221 return -ENXIO;
1222 }
1223
1224 return 0;
1225}
1226
1227static void snd_card_miro_free(struct snd_card *card)
1228{
1229 struct snd_miro *miro = card->private_data;
1230
1231 release_and_free_resource(miro->res_aci_port);
Krzysztof Helt87000552009-11-27 11:20:56 +01001232 if (miro->aci)
1233 miro->aci->aci_port = 0;
Martin Langer1841f6132006-03-27 12:41:01 +02001234 release_and_free_resource(miro->res_mc_base);
1235}
1236
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001237static int __devinit snd_miro_match(struct device *devptr, unsigned int n)
1238{
1239 return 1;
1240}
1241
1242static int __devinit snd_miro_probe(struct device *devptr, unsigned int n)
Martin Langer1841f6132006-03-27 12:41:01 +02001243{
1244 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1245 static long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1246 static int possible_irqs[] = {11, 9, 10, 7, -1};
1247 static int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1248 static int possible_dma1s[] = {3, 1, 0, -1};
1249 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
1250
1251 int error;
1252 struct snd_miro *miro;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001253 struct snd_wss *codec;
Martin Langer1841f6132006-03-27 12:41:01 +02001254 struct snd_timer *timer;
1255 struct snd_card *card;
1256 struct snd_pcm *pcm;
1257 struct snd_rawmidi *rmidi;
1258
Takashi Iwaic95eadd2008-12-28 16:43:35 +01001259 error = snd_card_create(index, id, THIS_MODULE,
1260 sizeof(struct snd_miro), &card);
1261 if (error < 0)
1262 return error;
Martin Langer1841f6132006-03-27 12:41:01 +02001263
1264 card->private_free = snd_card_miro_free;
1265 miro = card->private_data;
Krzysztof Helt616ad592009-11-21 01:01:18 +01001266
1267 error = snd_card_miro_detect(card, miro);
1268 if (error < 0) {
1269 snd_card_free(card);
1270 snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
1271 return -ENODEV;
1272 }
Martin Langer1841f6132006-03-27 12:41:01 +02001273
1274 if ((error = snd_card_miro_aci_detect(card, miro)) < 0) {
1275 snd_card_free(card);
1276 snd_printk(KERN_ERR "unable to detect aci chip\n");
1277 return -ENODEV;
1278 }
1279
1280 /* init proc interface */
Krzysztof Helt616ad592009-11-21 01:01:18 +01001281 snd_miro_proc_init(card, miro);
Martin Langer1841f6132006-03-27 12:41:01 +02001282
Martin Langer1841f6132006-03-27 12:41:01 +02001283
1284 if (! miro->res_mc_base &&
1285 (miro->res_mc_base = request_region(miro->mc_base, miro->mc_base_size,
1286 "miro (OPTi9xx MC)")) == NULL) {
1287 snd_card_free(card);
1288 snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
1289 return -ENOMEM;
1290 }
1291
1292 miro->wss_base = port;
Martin Langer1841f6132006-03-27 12:41:01 +02001293 miro->irq = irq;
1294 miro->mpu_irq = mpu_irq;
1295 miro->dma1 = dma1;
1296 miro->dma2 = dma2;
1297
1298 if (miro->wss_base == SNDRV_AUTO_PORT) {
1299 if ((miro->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
1300 snd_card_free(card);
1301 snd_printk(KERN_ERR "unable to find a free WSS port\n");
1302 return -EBUSY;
1303 }
1304 }
1305
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001306 if (mpu_port == SNDRV_AUTO_PORT) {
1307 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
1308 if (mpu_port < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001309 snd_card_free(card);
1310 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
Takashi Iwai67f2db22009-11-18 08:37:59 +01001311 return -EBUSY;
Martin Langer1841f6132006-03-27 12:41:01 +02001312 }
1313 }
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001314 miro->mpu_port = mpu_port;
1315
Martin Langer1841f6132006-03-27 12:41:01 +02001316 if (miro->irq == SNDRV_AUTO_IRQ) {
1317 if ((miro->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
1318 snd_card_free(card);
1319 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1320 return -EBUSY;
1321 }
1322 }
1323 if (miro->mpu_irq == SNDRV_AUTO_IRQ) {
1324 if ((miro->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
1325 snd_card_free(card);
1326 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
1327 return -EBUSY;
1328 }
1329 }
1330 if (miro->dma1 == SNDRV_AUTO_DMA) {
1331 if ((miro->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1332 snd_card_free(card);
1333 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1334 return -EBUSY;
1335 }
1336 }
1337 if (miro->dma2 == SNDRV_AUTO_DMA) {
1338 if ((miro->dma2 = snd_legacy_find_free_dma(possible_dma2s[miro->dma1 % 4])) < 0) {
1339 snd_card_free(card);
1340 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1341 return -EBUSY;
1342 }
1343 }
1344
Krzysztof Helt7779f752008-07-31 21:03:41 +02001345 error = snd_miro_configure(miro);
1346 if (error) {
Martin Langer1841f6132006-03-27 12:41:01 +02001347 snd_card_free(card);
1348 return error;
1349 }
1350
Krzysztof Helt7779f752008-07-31 21:03:41 +02001351 error = snd_wss_create(card, miro->wss_base + 4, -1,
1352 miro->irq, miro->dma1, miro->dma2,
1353 WSS_HW_AD1845, 0, &codec);
1354 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001355 snd_card_free(card);
1356 return error;
1357 }
1358
Krzysztof Helt7779f752008-07-31 21:03:41 +02001359 error = snd_wss_pcm(codec, 0, &pcm);
1360 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001361 snd_card_free(card);
1362 return error;
1363 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001364 error = snd_wss_mixer(codec);
1365 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001366 snd_card_free(card);
1367 return error;
1368 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001369 error = snd_wss_timer(codec, 0, &timer);
1370 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001371 snd_card_free(card);
1372 return error;
1373 }
1374
1375 miro->pcm = pcm;
1376
Krzysztof Helt616ad592009-11-21 01:01:18 +01001377 error = snd_miro_mixer(card, miro);
1378 if (error < 0) {
Martin Langer1841f6132006-03-27 12:41:01 +02001379 snd_card_free(card);
1380 return error;
1381 }
1382
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001383 if (miro->aci->aci_vendor == 'm') {
Martin Langer1841f6132006-03-27 12:41:01 +02001384 /* It looks like a miro sound card. */
Krzysztof Helt9dc91202009-11-22 17:26:34 +01001385 switch (miro->aci->aci_product) {
Martin Langer1841f6132006-03-27 12:41:01 +02001386 case 'A':
1387 sprintf(card->shortname,
1388 "miroSOUND PCM1 pro / PCM12");
1389 break;
1390 case 'B':
1391 sprintf(card->shortname,
1392 "miroSOUND PCM12");
1393 break;
1394 case 'C':
1395 sprintf(card->shortname,
1396 "miroSOUND PCM20 radio");
1397 break;
1398 default:
1399 sprintf(card->shortname,
1400 "unknown miro");
1401 snd_printk(KERN_INFO "unknown miro aci id\n");
1402 break;
1403 }
1404 } else {
1405 snd_printk(KERN_INFO "found unsupported aci card\n");
1406 sprintf(card->shortname, "unknown Cardinal Technologies");
1407 }
1408
1409 strcpy(card->driver, "miro");
1410 sprintf(card->longname, "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1411 card->shortname, miro->name, pcm->name, miro->wss_base + 4,
1412 miro->irq, miro->dma1, miro->dma2);
1413
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001414 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
Martin Langer1841f6132006-03-27 12:41:01 +02001415 rmidi = NULL;
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001416 else {
1417 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1418 mpu_port, 0, miro->mpu_irq, IRQF_DISABLED,
1419 &rmidi);
1420 if (error < 0)
1421 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1422 mpu_port);
1423 }
Martin Langer1841f6132006-03-27 12:41:01 +02001424
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001425 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
Martin Langer1841f6132006-03-27 12:41:01 +02001426 struct snd_opl3 *opl3 = NULL;
1427 struct snd_opl4 *opl4;
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001428 if (snd_opl4_create(card, fm_port, fm_port - 8,
Martin Langer1841f6132006-03-27 12:41:01 +02001429 2, &opl3, &opl4) < 0)
Krzysztof Heltb67cad92009-11-17 18:35:41 +01001430 snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
1431 fm_port);
Martin Langer1841f6132006-03-27 12:41:01 +02001432 }
1433
1434 if ((error = snd_set_aci_init_values(miro)) < 0) {
1435 snd_card_free(card);
1436 return error;
1437 }
1438
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001439 snd_card_set_dev(card, devptr);
Martin Langer1841f6132006-03-27 12:41:01 +02001440
1441 if ((error = snd_card_register(card))) {
1442 snd_card_free(card);
1443 return error;
1444 }
1445
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001446 dev_set_drvdata(devptr, card);
Martin Langer1841f6132006-03-27 12:41:01 +02001447 return 0;
1448}
1449
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001450static int __devexit snd_miro_remove(struct device *devptr, unsigned int dev)
Martin Langer1841f6132006-03-27 12:41:01 +02001451{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001452 snd_card_free(dev_get_drvdata(devptr));
1453 dev_set_drvdata(devptr, NULL);
Martin Langer1841f6132006-03-27 12:41:01 +02001454 return 0;
1455}
1456
Rene Herman83c51c02007-03-20 11:33:46 +01001457#define DEV_NAME "miro"
1458
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001459static struct isa_driver snd_miro_driver = {
1460 .match = snd_miro_match,
Martin Langer1841f6132006-03-27 12:41:01 +02001461 .probe = snd_miro_probe,
1462 .remove = __devexit_p(snd_miro_remove),
1463 /* FIXME: suspend/resume */
1464 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001465 .name = DEV_NAME
Martin Langer1841f6132006-03-27 12:41:01 +02001466 },
1467};
1468
1469static int __init alsa_card_miro_init(void)
1470{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001471 return isa_register_driver(&snd_miro_driver, 1);
Martin Langer1841f6132006-03-27 12:41:01 +02001472}
1473
1474static void __exit alsa_card_miro_exit(void)
1475{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001476 isa_unregister_driver(&snd_miro_driver);
Martin Langer1841f6132006-03-27 12:41:01 +02001477}
1478
1479module_init(alsa_card_miro_init)
1480module_exit(alsa_card_miro_exit)