Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ALSA soundcard driver for Miro miroSOUND PCM1 pro
4 * miroSOUND PCM12
5 * miroSOUND PCM20 Radio
6 *
7 * Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
8 *
9 * Based on OSS ACI and ALSA OPTi9xx drivers
10 */
11
12#include <linux/init.h>
13#include <linux/err.h>
14#include <linux/isa.h>
15#include <linux/pnp.h>
16#include <linux/delay.h>
17#include <linux/ioport.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <asm/dma.h>
21#include <sound/core.h>
22#include <sound/wss.h>
23#include <sound/mpu401.h>
24#include <sound/opl4.h>
25#include <sound/control.h>
26#include <sound/info.h>
27#define SNDRV_LEGACY_FIND_FREE_IOPORT
28#define SNDRV_LEGACY_FIND_FREE_IRQ
29#define SNDRV_LEGACY_FIND_FREE_DMA
30#include <sound/initval.h>
31#include <sound/aci.h>
32
33MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
34MODULE_LICENSE("GPL");
35MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
36MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
37 "{Miro,miroSOUND PCM12}, "
38 "{Miro,miroSOUND PCM20 Radio}}");
39
40static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
41static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
42static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
43static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
44static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
45static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
46static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
47static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
48static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
49static int wss;
50static int ide;
51#ifdef CONFIG_PNP
52static bool isapnp = 1; /* Enable ISA PnP detection */
53#endif
54
55module_param(index, int, 0444);
56MODULE_PARM_DESC(index, "Index value for miro soundcard.");
57module_param(id, charp, 0444);
58MODULE_PARM_DESC(id, "ID string for miro soundcard.");
59module_param_hw(port, long, ioport, 0444);
60MODULE_PARM_DESC(port, "WSS port # for miro driver.");
61module_param_hw(mpu_port, long, ioport, 0444);
62MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
63module_param_hw(fm_port, long, ioport, 0444);
64MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
65module_param_hw(irq, int, irq, 0444);
66MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
67module_param_hw(mpu_irq, int, irq, 0444);
68MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
69module_param_hw(dma1, int, dma, 0444);
70MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
71module_param_hw(dma2, int, dma, 0444);
72MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
73module_param(wss, int, 0444);
74MODULE_PARM_DESC(wss, "wss mode");
75module_param(ide, int, 0444);
76MODULE_PARM_DESC(ide, "enable ide port");
77#ifdef CONFIG_PNP
78module_param(isapnp, bool, 0444);
79MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
80#endif
81
82#define OPTi9XX_HW_DETECT 0
83#define OPTi9XX_HW_82C928 1
84#define OPTi9XX_HW_82C929 2
85#define OPTi9XX_HW_82C924 3
86#define OPTi9XX_HW_82C925 4
87#define OPTi9XX_HW_82C930 5
88#define OPTi9XX_HW_82C931 6
89#define OPTi9XX_HW_82C933 7
90#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
91
92#define OPTi9XX_MC_REG(n) n
93
94struct snd_miro {
95 unsigned short hardware;
96 unsigned char password;
97 char name[7];
98
99 struct resource *res_mc_base;
100 struct resource *res_aci_port;
101
102 unsigned long mc_base;
103 unsigned long mc_base_size;
104 unsigned long pwd_reg;
105
106 spinlock_t lock;
107 struct snd_pcm *pcm;
108
109 long wss_base;
110 int irq;
111 int dma1;
112 int dma2;
113
114 long mpu_port;
115 int mpu_irq;
116
117 struct snd_miro_aci *aci;
118};
119
120static struct snd_miro_aci aci_device;
121
122static const char * const snd_opti9xx_names[] = {
123 "unknown",
124 "82C928", "82C929",
125 "82C924", "82C925",
126 "82C930", "82C931", "82C933"
127};
128
129static int snd_miro_pnp_is_probed;
130
131#ifdef CONFIG_PNP
132
133static const struct pnp_card_device_id snd_miro_pnpids[] = {
134 /* PCM20 and PCM12 in PnP mode */
135 { .id = "MIR0924",
136 .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, },
137 { .id = "" }
138};
139
140MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids);
141
142#endif /* CONFIG_PNP */
143
144/*
145 * ACI control
146 */
147
148static int aci_busy_wait(struct snd_miro_aci *aci)
149{
150 long timeout;
151 unsigned char byte;
152
153 for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
154 byte = inb(aci->aci_port + ACI_REG_BUSY);
155 if ((byte & 1) == 0) {
156 if (timeout >= ACI_MINTIME)
157 snd_printd("aci ready in round %ld.\n",
158 timeout-ACI_MINTIME);
159 return byte;
160 }
161 if (timeout >= ACI_MINTIME) {
162 long out=10*HZ;
163 switch (timeout-ACI_MINTIME) {
164 case 0 ... 9:
165 out /= 10;
166 fallthrough;
167 case 10 ... 19:
168 out /= 10;
169 fallthrough;
170 case 20 ... 30:
171 out /= 10;
172 fallthrough;
173 default:
174 set_current_state(TASK_UNINTERRUPTIBLE);
175 schedule_timeout(out);
176 break;
177 }
178 }
179 }
180 snd_printk(KERN_ERR "aci_busy_wait() time out\n");
181 return -EBUSY;
182}
183
184static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
185{
186 if (aci_busy_wait(aci) >= 0) {
187 outb(byte, aci->aci_port + ACI_REG_COMMAND);
188 return 0;
189 } else {
190 snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
191 return -EBUSY;
192 }
193}
194
195static inline int aci_read(struct snd_miro_aci *aci)
196{
197 unsigned char byte;
198
199 if (aci_busy_wait(aci) >= 0) {
200 byte = inb(aci->aci_port + ACI_REG_STATUS);
201 return byte;
202 } else {
203 snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
204 return -EBUSY;
205 }
206}
207
208int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
209{
210 int write[] = {write1, write2, write3};
211 int value, i;
212
213 if (mutex_lock_interruptible(&aci->aci_mutex))
214 return -EINTR;
215
216 for (i=0; i<3; i++) {
217 if (write[i]< 0 || write[i] > 255)
218 break;
219 else {
220 value = aci_write(aci, write[i]);
221 if (value < 0)
222 goto out;
223 }
224 }
225
226 value = aci_read(aci);
227
228out: mutex_unlock(&aci->aci_mutex);
229 return value;
230}
231EXPORT_SYMBOL(snd_aci_cmd);
232
233static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
234{
235 return snd_aci_cmd(aci, ACI_STATUS, index, -1);
236}
237
238static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
239 int value)
240{
241 return snd_aci_cmd(aci, index, value, -1);
242}
243
244struct snd_miro_aci *snd_aci_get_aci(void)
245{
246 if (aci_device.aci_port == 0)
247 return NULL;
248 return &aci_device;
249}
250EXPORT_SYMBOL(snd_aci_get_aci);
251
252/*
253 * MIXER part
254 */
255
256#define snd_miro_info_capture snd_ctl_boolean_mono_info
257
258static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
259 struct snd_ctl_elem_value *ucontrol)
260{
261 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
262 int value;
263
264 value = aci_getvalue(miro->aci, ACI_S_GENERAL);
265 if (value < 0) {
266 snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
267 value);
268 return value;
269 }
270
271 ucontrol->value.integer.value[0] = value & 0x20;
272
273 return 0;
274}
275
276static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
277 struct snd_ctl_elem_value *ucontrol)
278{
279 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
280 int change, value, error;
281
282 value = !(ucontrol->value.integer.value[0]);
283
284 error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
285 if (error < 0) {
286 snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
287 error);
288 return error;
289 }
290
291 change = (value != miro->aci->aci_solomode);
292 miro->aci->aci_solomode = value;
293
294 return change;
295}
296
297static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_info *uinfo)
299{
300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
301 uinfo->count = 1;
302 uinfo->value.integer.min = 0;
303 uinfo->value.integer.max = 3;
304
305 return 0;
306}
307
308static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_value *ucontrol)
310{
311 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
312 int value;
313
314 if (miro->aci->aci_version <= 176) {
315
316 /*
317 OSS says it's not readable with versions < 176.
318 But it doesn't work on my card,
319 which is a PCM12 with aci_version = 176.
320 */
321
322 ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
323 return 0;
324 }
325
326 value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
327 if (value < 0) {
328 snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
329 value);
330 return value;
331 }
332
333 ucontrol->value.integer.value[0] = value;
334
335 return 0;
336}
337
338static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
340{
341 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
342 int error, value, change;
343
344 value = ucontrol->value.integer.value[0];
345
346 error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
347 if (error < 0) {
348 snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
349 error);
350 return error;
351 }
352
353 change = (value != miro->aci->aci_preamp);
354 miro->aci->aci_preamp = value;
355
356 return change;
357}
358
359#define snd_miro_info_amp snd_ctl_boolean_mono_info
360
361static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
362 struct snd_ctl_elem_value *ucontrol)
363{
364 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
365 ucontrol->value.integer.value[0] = miro->aci->aci_amp;
366
367 return 0;
368}
369
370static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
371 struct snd_ctl_elem_value *ucontrol)
372{
373 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
374 int error, value, change;
375
376 value = ucontrol->value.integer.value[0];
377
378 error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
379 if (error < 0) {
380 snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
381 return error;
382 }
383
384 change = (value != miro->aci->aci_amp);
385 miro->aci->aci_amp = value;
386
387 return change;
388}
389
390#define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
391{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
392 .name = ctl_name, \
393 .index = ctl_index, \
394 .info = snd_miro_info_double, \
395 .get = snd_miro_get_double, \
396 .put = snd_miro_put_double, \
397 .private_value = get_right_reg | (set_right_reg << 8) \
398}
399
400static int snd_miro_info_double(struct snd_kcontrol *kcontrol,
401 struct snd_ctl_elem_info *uinfo)
402{
403 int reg = kcontrol->private_value & 0xff;
404
405 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
406 uinfo->count = 2;
407
408 if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
409
410 /* equalizer elements */
411
412 uinfo->value.integer.min = - 0x7f;
413 uinfo->value.integer.max = 0x7f;
414 } else {
415
416 /* non-equalizer elements */
417
418 uinfo->value.integer.min = 0;
419 uinfo->value.integer.max = 0x20;
420 }
421
422 return 0;
423}
424
425static int snd_miro_get_double(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_value *uinfo)
427{
428 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
429 int left_val, right_val;
430
431 int right_reg = kcontrol->private_value & 0xff;
432 int left_reg = right_reg + 1;
433
434 right_val = aci_getvalue(miro->aci, right_reg);
435 if (right_val < 0) {
436 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
437 return right_val;
438 }
439
440 left_val = aci_getvalue(miro->aci, left_reg);
441 if (left_val < 0) {
442 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
443 return left_val;
444 }
445
446 if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
447
448 /* equalizer elements */
449
450 if (left_val < 0x80) {
451 uinfo->value.integer.value[0] = left_val;
452 } else {
453 uinfo->value.integer.value[0] = 0x80 - left_val;
454 }
455
456 if (right_val < 0x80) {
457 uinfo->value.integer.value[1] = right_val;
458 } else {
459 uinfo->value.integer.value[1] = 0x80 - right_val;
460 }
461
462 } else {
463
464 /* non-equalizer elements */
465
466 uinfo->value.integer.value[0] = 0x20 - left_val;
467 uinfo->value.integer.value[1] = 0x20 - right_val;
468 }
469
470 return 0;
471}
472
473static int snd_miro_put_double(struct snd_kcontrol *kcontrol,
474 struct snd_ctl_elem_value *ucontrol)
475{
476 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
477 struct snd_miro_aci *aci = miro->aci;
478 int left, right, left_old, right_old;
479 int setreg_left, setreg_right, getreg_left, getreg_right;
480 int change, error;
481
482 left = ucontrol->value.integer.value[0];
483 right = ucontrol->value.integer.value[1];
484
485 setreg_right = (kcontrol->private_value >> 8) & 0xff;
486 setreg_left = setreg_right + 8;
487 if (setreg_right == ACI_SET_MASTER)
488 setreg_left -= 7;
489
490 getreg_right = kcontrol->private_value & 0xff;
491 getreg_left = getreg_right + 1;
492
493 left_old = aci_getvalue(aci, getreg_left);
494 if (left_old < 0) {
495 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
496 return left_old;
497 }
498
499 right_old = aci_getvalue(aci, getreg_right);
500 if (right_old < 0) {
501 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
502 return right_old;
503 }
504
505 if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
506
507 /* equalizer elements */
508
509 if (left < -0x7f || left > 0x7f ||
510 right < -0x7f || right > 0x7f)
511 return -EINVAL;
512
513 if (left_old > 0x80)
514 left_old = 0x80 - left_old;
515 if (right_old > 0x80)
516 right_old = 0x80 - right_old;
517
518 if (left >= 0) {
519 error = aci_setvalue(aci, setreg_left, left);
520 if (error < 0) {
521 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
522 left, error);
523 return error;
524 }
525 } else {
526 error = aci_setvalue(aci, setreg_left, 0x80 - left);
527 if (error < 0) {
528 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
529 0x80 - left, error);
530 return error;
531 }
532 }
533
534 if (right >= 0) {
535 error = aci_setvalue(aci, setreg_right, right);
536 if (error < 0) {
537 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
538 right, error);
539 return error;
540 }
541 } else {
542 error = aci_setvalue(aci, setreg_right, 0x80 - right);
543 if (error < 0) {
544 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
545 0x80 - right, error);
546 return error;
547 }
548 }
549
550 } else {
551
552 /* non-equalizer elements */
553
554 if (left < 0 || left > 0x20 ||
555 right < 0 || right > 0x20)
556 return -EINVAL;
557
558 left_old = 0x20 - left_old;
559 right_old = 0x20 - right_old;
560
561 error = aci_setvalue(aci, setreg_left, 0x20 - left);
562 if (error < 0) {
563 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
564 0x20 - left, error);
565 return error;
566 }
567 error = aci_setvalue(aci, setreg_right, 0x20 - right);
568 if (error < 0) {
569 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
570 0x20 - right, error);
571 return error;
572 }
573 }
574
575 change = (left != left_old) || (right != right_old);
576
577 return change;
578}
579
580static const struct snd_kcontrol_new snd_miro_controls[] = {
581MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
582MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
583MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
584MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
585MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
586MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
587MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
588};
589
590/* Equalizer with seven bands (only PCM20)
591 from -12dB up to +12dB on each band */
592static const struct snd_kcontrol_new snd_miro_eq_controls[] = {
593MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
594MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
595MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
596MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
597MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
598MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
599MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
600};
601
602static const struct snd_kcontrol_new snd_miro_radio_control[] = {
603MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
604};
605
606static const struct snd_kcontrol_new snd_miro_line_control[] = {
607MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
608};
609
610static const struct snd_kcontrol_new snd_miro_preamp_control[] = {
611{
612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
613 .name = "Mic Boost",
614 .index = 1,
615 .info = snd_miro_info_preamp,
616 .get = snd_miro_get_preamp,
617 .put = snd_miro_put_preamp,
618}};
619
620static const struct snd_kcontrol_new snd_miro_amp_control[] = {
621{
622 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
623 .name = "Line Boost",
624 .index = 0,
625 .info = snd_miro_info_amp,
626 .get = snd_miro_get_amp,
627 .put = snd_miro_put_amp,
628}};
629
630static const struct snd_kcontrol_new snd_miro_capture_control[] = {
631{
632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
633 .name = "PCM Capture Switch",
634 .index = 0,
635 .info = snd_miro_info_capture,
636 .get = snd_miro_get_capture,
637 .put = snd_miro_put_capture,
638}};
639
640static const unsigned char aci_init_values[][2] = {
641 { ACI_SET_MUTE, 0x00 },
642 { ACI_SET_POWERAMP, 0x00 },
643 { ACI_SET_PREAMP, 0x00 },
644 { ACI_SET_SOLOMODE, 0x00 },
645 { ACI_SET_MIC + 0, 0x20 },
646 { ACI_SET_MIC + 8, 0x20 },
647 { ACI_SET_LINE + 0, 0x20 },
648 { ACI_SET_LINE + 8, 0x20 },
649 { ACI_SET_CD + 0, 0x20 },
650 { ACI_SET_CD + 8, 0x20 },
651 { ACI_SET_PCM + 0, 0x20 },
652 { ACI_SET_PCM + 8, 0x20 },
653 { ACI_SET_LINE1 + 0, 0x20 },
654 { ACI_SET_LINE1 + 8, 0x20 },
655 { ACI_SET_LINE2 + 0, 0x20 },
656 { ACI_SET_LINE2 + 8, 0x20 },
657 { ACI_SET_SYNTH + 0, 0x20 },
658 { ACI_SET_SYNTH + 8, 0x20 },
659 { ACI_SET_MASTER + 0, 0x20 },
660 { ACI_SET_MASTER + 1, 0x20 },
661};
662
663static int snd_set_aci_init_values(struct snd_miro *miro)
664{
665 int idx, error;
666 struct snd_miro_aci *aci = miro->aci;
667
668 /* enable WSS on PCM1 */
669
670 if ((aci->aci_product == 'A') && wss) {
671 error = aci_setvalue(aci, ACI_SET_WSS, wss);
672 if (error < 0) {
673 snd_printk(KERN_ERR "enabling WSS mode failed\n");
674 return error;
675 }
676 }
677
678 /* enable IDE port */
679
680 if (ide) {
681 error = aci_setvalue(aci, ACI_SET_IDE, ide);
682 if (error < 0) {
683 snd_printk(KERN_ERR "enabling IDE port failed\n");
684 return error;
685 }
686 }
687
688 /* set common aci values */
689
690 for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
691 error = aci_setvalue(aci, aci_init_values[idx][0],
692 aci_init_values[idx][1]);
693 if (error < 0) {
694 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
695 aci_init_values[idx][0], error);
696 return error;
697 }
698 }
699 aci->aci_amp = 0;
700 aci->aci_preamp = 0;
701 aci->aci_solomode = 1;
702
703 return 0;
704}
705
706static int snd_miro_mixer(struct snd_card *card,
707 struct snd_miro *miro)
708{
709 unsigned int idx;
710 int err;
711
712 if (snd_BUG_ON(!miro || !card))
713 return -EINVAL;
714
715 switch (miro->hardware) {
716 case OPTi9XX_HW_82C924:
717 strcpy(card->mixername, "ACI & OPTi924");
718 break;
719 case OPTi9XX_HW_82C929:
720 strcpy(card->mixername, "ACI & OPTi929");
721 break;
722 default:
723 snd_BUG();
724 break;
725 }
726
727 for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
728 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
729 return err;
730 }
731
732 if ((miro->aci->aci_product == 'A') ||
733 (miro->aci->aci_product == 'B')) {
734 /* PCM1/PCM12 with power-amp and Line 2 */
735 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
736 return err;
737 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
738 return err;
739 }
740
741 if ((miro->aci->aci_product == 'B') ||
742 (miro->aci->aci_product == 'C')) {
743 /* PCM12/PCM20 with mic-preamp */
744 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
745 return err;
746 if (miro->aci->aci_version >= 176)
747 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
748 return err;
749 }
750
751 if (miro->aci->aci_product == 'C') {
752 /* PCM20 with radio and 7 band equalizer */
753 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
754 return err;
755 for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
756 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
757 return err;
758 }
759 }
760
761 return 0;
762}
763
764static int snd_miro_init(struct snd_miro *chip,
765 unsigned short hardware)
766{
767 static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
768
769 chip->hardware = hardware;
770 strcpy(chip->name, snd_opti9xx_names[hardware]);
771
772 chip->mc_base_size = opti9xx_mc_size[hardware];
773
774 spin_lock_init(&chip->lock);
775
776 chip->wss_base = -1;
777 chip->irq = -1;
778 chip->dma1 = -1;
779 chip->dma2 = -1;
780 chip->mpu_port = -1;
781 chip->mpu_irq = -1;
782
783 chip->pwd_reg = 3;
784
785#ifdef CONFIG_PNP
786 if (isapnp && chip->mc_base)
787 /* PnP resource gives the least 10 bits */
788 chip->mc_base |= 0xc00;
789 else
790#endif
791 chip->mc_base = 0xf8c;
792
793 switch (hardware) {
794 case OPTi9XX_HW_82C929:
795 chip->password = 0xe3;
796 break;
797
798 case OPTi9XX_HW_82C924:
799 chip->password = 0xe5;
800 break;
801
802 default:
803 snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
804 return -ENODEV;
805 }
806
807 return 0;
808}
809
810static unsigned char snd_miro_read(struct snd_miro *chip,
811 unsigned char reg)
812{
813 unsigned long flags;
814 unsigned char retval = 0xff;
815
816 spin_lock_irqsave(&chip->lock, flags);
817 outb(chip->password, chip->mc_base + chip->pwd_reg);
818
819 switch (chip->hardware) {
820 case OPTi9XX_HW_82C924:
821 if (reg > 7) {
822 outb(reg, chip->mc_base + 8);
823 outb(chip->password, chip->mc_base + chip->pwd_reg);
824 retval = inb(chip->mc_base + 9);
825 break;
826 }
827 fallthrough;
828
829 case OPTi9XX_HW_82C929:
830 retval = inb(chip->mc_base + reg);
831 break;
832
833 default:
834 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
835 }
836
837 spin_unlock_irqrestore(&chip->lock, flags);
838 return retval;
839}
840
841static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
842 unsigned char value)
843{
844 unsigned long flags;
845
846 spin_lock_irqsave(&chip->lock, flags);
847 outb(chip->password, chip->mc_base + chip->pwd_reg);
848
849 switch (chip->hardware) {
850 case OPTi9XX_HW_82C924:
851 if (reg > 7) {
852 outb(reg, chip->mc_base + 8);
853 outb(chip->password, chip->mc_base + chip->pwd_reg);
854 outb(value, chip->mc_base + 9);
855 break;
856 }
857 fallthrough;
858
859 case OPTi9XX_HW_82C929:
860 outb(value, chip->mc_base + reg);
861 break;
862
863 default:
864 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
865 }
866
867 spin_unlock_irqrestore(&chip->lock, flags);
868}
869
870static inline void snd_miro_write_mask(struct snd_miro *chip,
871 unsigned char reg, unsigned char value, unsigned char mask)
872{
873 unsigned char oldval = snd_miro_read(chip, reg);
874
875 snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask));
876}
877
878/*
879 * Proc Interface
880 */
881
882static void snd_miro_proc_read(struct snd_info_entry * entry,
883 struct snd_info_buffer *buffer)
884{
885 struct snd_miro *miro = (struct snd_miro *) entry->private_data;
886 struct snd_miro_aci *aci = miro->aci;
887 char* model = "unknown";
888
889 /* miroSOUND PCM1 pro, early PCM12 */
890
891 if ((miro->hardware == OPTi9XX_HW_82C929) &&
892 (aci->aci_vendor == 'm') &&
893 (aci->aci_product == 'A')) {
894 switch (aci->aci_version) {
895 case 3:
896 model = "miroSOUND PCM1 pro";
897 break;
898 default:
899 model = "miroSOUND PCM1 pro / (early) PCM12";
900 break;
901 }
902 }
903
904 /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
905
906 if ((miro->hardware == OPTi9XX_HW_82C924) &&
907 (aci->aci_vendor == 'm') &&
908 (aci->aci_product == 'B')) {
909 switch (aci->aci_version) {
910 case 4:
911 model = "miroSOUND PCM12";
912 break;
913 case 176:
914 model = "miroSOUND PCM12 (Rev. E)";
915 break;
916 default:
917 model = "miroSOUND PCM12 / PCM12 pnp";
918 break;
919 }
920 }
921
922 /* miroSOUND PCM20 radio */
923
924 if ((miro->hardware == OPTi9XX_HW_82C924) &&
925 (aci->aci_vendor == 'm') &&
926 (aci->aci_product == 'C')) {
927 switch (aci->aci_version) {
928 case 7:
929 model = "miroSOUND PCM20 radio (Rev. E)";
930 break;
931 default:
932 model = "miroSOUND PCM20 radio";
933 break;
934 }
935 }
936
937 snd_iprintf(buffer, "\nGeneral information:\n");
938 snd_iprintf(buffer, " model : %s\n", model);
939 snd_iprintf(buffer, " opti : %s\n", miro->name);
940 snd_iprintf(buffer, " codec : %s\n", miro->pcm->name);
941 snd_iprintf(buffer, " port : 0x%lx\n", miro->wss_base);
942 snd_iprintf(buffer, " irq : %d\n", miro->irq);
943 snd_iprintf(buffer, " dma : %d,%d\n\n", miro->dma1, miro->dma2);
944
945 snd_iprintf(buffer, "MPU-401:\n");
946 snd_iprintf(buffer, " port : 0x%lx\n", miro->mpu_port);
947 snd_iprintf(buffer, " irq : %d\n\n", miro->mpu_irq);
948
949 snd_iprintf(buffer, "ACI information:\n");
950 snd_iprintf(buffer, " vendor : ");
951 switch (aci->aci_vendor) {
952 case 'm':
953 snd_iprintf(buffer, "Miro\n");
954 break;
955 default:
956 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
957 break;
958 }
959
960 snd_iprintf(buffer, " product : ");
961 switch (aci->aci_product) {
962 case 'A':
963 snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
964 break;
965 case 'B':
966 snd_iprintf(buffer, "miroSOUND PCM12\n");
967 break;
968 case 'C':
969 snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
970 break;
971 default:
972 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
973 break;
974 }
975
976 snd_iprintf(buffer, " firmware: %d (0x%x)\n",
977 aci->aci_version, aci->aci_version);
978 snd_iprintf(buffer, " port : 0x%lx-0x%lx\n",
979 aci->aci_port, aci->aci_port+2);
980 snd_iprintf(buffer, " wss : 0x%x\n", wss);
981 snd_iprintf(buffer, " ide : 0x%x\n", ide);
982 snd_iprintf(buffer, " solomode: 0x%x\n", aci->aci_solomode);
983 snd_iprintf(buffer, " amp : 0x%x\n", aci->aci_amp);
984 snd_iprintf(buffer, " preamp : 0x%x\n", aci->aci_preamp);
985}
986
987static void snd_miro_proc_init(struct snd_card *card,
988 struct snd_miro *miro)
989{
990 snd_card_ro_proc_new(card, "miro", miro, snd_miro_proc_read);
991}
992
993/*
994 * Init
995 */
996
997static int snd_miro_configure(struct snd_miro *chip)
998{
999 unsigned char wss_base_bits;
1000 unsigned char irq_bits;
1001 unsigned char dma_bits;
1002 unsigned char mpu_port_bits = 0;
1003 unsigned char mpu_irq_bits;
1004 unsigned long flags;
1005
1006 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1007 snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1008 snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1009
1010 switch (chip->hardware) {
1011 case OPTi9XX_HW_82C924:
1012 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
1013 snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
1014 break;
1015 case OPTi9XX_HW_82C929:
1016 /* untested init commands for OPTi929 */
1017 snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
1018 break;
1019 default:
1020 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
1021 return -EINVAL;
1022 }
1023
1024 /* PnP resource says it decodes only 10 bits of address */
1025 switch (chip->wss_base & 0x3ff) {
1026 case 0x130:
1027 chip->wss_base = 0x530;
1028 wss_base_bits = 0x00;
1029 break;
1030 case 0x204:
1031 chip->wss_base = 0x604;
1032 wss_base_bits = 0x03;
1033 break;
1034 case 0x280:
1035 chip->wss_base = 0xe80;
1036 wss_base_bits = 0x01;
1037 break;
1038 case 0x340:
1039 chip->wss_base = 0xf40;
1040 wss_base_bits = 0x02;
1041 break;
1042 default:
1043 snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
1044 goto __skip_base;
1045 }
1046 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1047
1048__skip_base:
1049 switch (chip->irq) {
1050 case 5:
1051 irq_bits = 0x05;
1052 break;
1053 case 7:
1054 irq_bits = 0x01;
1055 break;
1056 case 9:
1057 irq_bits = 0x02;
1058 break;
1059 case 10:
1060 irq_bits = 0x03;
1061 break;
1062 case 11:
1063 irq_bits = 0x04;
1064 break;
1065 default:
1066 snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
1067 goto __skip_resources;
1068 }
1069
1070 switch (chip->dma1) {
1071 case 0:
1072 dma_bits = 0x01;
1073 break;
1074 case 1:
1075 dma_bits = 0x02;
1076 break;
1077 case 3:
1078 dma_bits = 0x03;
1079 break;
1080 default:
1081 snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
1082 goto __skip_resources;
1083 }
1084
1085 if (chip->dma1 == chip->dma2) {
1086 snd_printk(KERN_ERR "don't want to share dmas\n");
1087 return -EBUSY;
1088 }
1089
1090 switch (chip->dma2) {
1091 case 0:
1092 case 1:
1093 break;
1094 default:
1095 snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
1096 goto __skip_resources;
1097 }
1098 dma_bits |= 0x04;
1099
1100 spin_lock_irqsave(&chip->lock, flags);
1101 outb(irq_bits << 3 | dma_bits, chip->wss_base);
1102 spin_unlock_irqrestore(&chip->lock, flags);
1103
1104__skip_resources:
1105 if (chip->hardware > OPTi9XX_HW_82C928) {
1106 switch (chip->mpu_port) {
1107 case 0:
1108 case -1:
1109 break;
1110 case 0x300:
1111 mpu_port_bits = 0x03;
1112 break;
1113 case 0x310:
1114 mpu_port_bits = 0x02;
1115 break;
1116 case 0x320:
1117 mpu_port_bits = 0x01;
1118 break;
1119 case 0x330:
1120 mpu_port_bits = 0x00;
1121 break;
1122 default:
1123 snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
1124 chip->mpu_port);
1125 goto __skip_mpu;
1126 }
1127
1128 switch (chip->mpu_irq) {
1129 case 5:
1130 mpu_irq_bits = 0x02;
1131 break;
1132 case 7:
1133 mpu_irq_bits = 0x03;
1134 break;
1135 case 9:
1136 mpu_irq_bits = 0x00;
1137 break;
1138 case 10:
1139 mpu_irq_bits = 0x01;
1140 break;
1141 default:
1142 snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
1143 chip->mpu_irq);
1144 goto __skip_mpu;
1145 }
1146
1147 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1148 (chip->mpu_port <= 0) ? 0x00 :
1149 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1150 0xf8);
1151 }
1152__skip_mpu:
1153
1154 return 0;
1155}
1156
1157static int snd_miro_opti_check(struct snd_miro *chip)
1158{
1159 unsigned char value;
1160
1161 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
1162 "OPTi9xx MC");
1163 if (chip->res_mc_base == NULL)
1164 return -ENOMEM;
1165
1166 value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1167 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
1168 if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1169 return 0;
1170
1171 release_and_free_resource(chip->res_mc_base);
1172 chip->res_mc_base = NULL;
1173
1174 return -ENODEV;
1175}
1176
1177static int snd_card_miro_detect(struct snd_card *card,
1178 struct snd_miro *chip)
1179{
1180 int i, err;
1181
1182 for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1183
1184 if ((err = snd_miro_init(chip, i)) < 0)
1185 return err;
1186
1187 err = snd_miro_opti_check(chip);
1188 if (err == 0)
1189 return 1;
1190 }
1191
1192 return -ENODEV;
1193}
1194
1195static int snd_card_miro_aci_detect(struct snd_card *card,
1196 struct snd_miro *miro)
1197{
1198 unsigned char regval;
1199 int i;
1200 struct snd_miro_aci *aci = &aci_device;
1201
1202 miro->aci = aci;
1203
1204 mutex_init(&aci->aci_mutex);
1205
1206 /* get ACI port from OPTi9xx MC 4 */
1207
1208 regval=inb(miro->mc_base + 4);
1209 aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
1210
1211 miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
1212 if (miro->res_aci_port == NULL) {
1213 snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n",
1214 aci->aci_port, aci->aci_port+2);
1215 return -ENOMEM;
1216 }
1217
1218 /* force ACI into a known state */
1219 for (i = 0; i < 3; i++)
1220 if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
1221 snd_printk(KERN_ERR "can't force aci into known state.\n");
1222 return -ENXIO;
1223 }
1224
1225 aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1226 aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1227 if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1228 snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
1229 aci->aci_port);
1230 return -ENXIO;
1231 }
1232
1233 aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1234 if (aci->aci_version < 0) {
1235 snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n",
1236 aci->aci_port);
1237 return -ENXIO;
1238 }
1239
1240 if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1241 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1242 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
1243 snd_printk(KERN_ERR "can't initialize aci.\n");
1244 return -ENXIO;
1245 }
1246
1247 return 0;
1248}
1249
1250static void snd_card_miro_free(struct snd_card *card)
1251{
1252 struct snd_miro *miro = card->private_data;
1253
1254 release_and_free_resource(miro->res_aci_port);
1255 if (miro->aci)
1256 miro->aci->aci_port = 0;
1257 release_and_free_resource(miro->res_mc_base);
1258}
1259
1260static int snd_miro_probe(struct snd_card *card)
1261{
1262 int error;
1263 struct snd_miro *miro = card->private_data;
1264 struct snd_wss *codec;
1265 struct snd_rawmidi *rmidi;
1266
1267 if (!miro->res_mc_base) {
1268 miro->res_mc_base = request_region(miro->mc_base,
1269 miro->mc_base_size,
1270 "miro (OPTi9xx MC)");
1271 if (miro->res_mc_base == NULL) {
1272 snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
1273 return -ENOMEM;
1274 }
1275 }
1276
1277 error = snd_card_miro_aci_detect(card, miro);
1278 if (error < 0) {
1279 snd_printk(KERN_ERR "unable to detect aci chip\n");
1280 return -ENODEV;
1281 }
1282
1283 miro->wss_base = port;
1284 miro->mpu_port = mpu_port;
1285 miro->irq = irq;
1286 miro->mpu_irq = mpu_irq;
1287 miro->dma1 = dma1;
1288 miro->dma2 = dma2;
1289
1290 /* init proc interface */
1291 snd_miro_proc_init(card, miro);
1292
1293 error = snd_miro_configure(miro);
1294 if (error)
1295 return error;
1296
1297 error = snd_wss_create(card, miro->wss_base + 4, -1,
1298 miro->irq, miro->dma1, miro->dma2,
1299 WSS_HW_DETECT, 0, &codec);
1300 if (error < 0)
1301 return error;
1302
1303 error = snd_wss_pcm(codec, 0);
1304 if (error < 0)
1305 return error;
1306
1307 error = snd_wss_mixer(codec);
1308 if (error < 0)
1309 return error;
1310
1311 error = snd_wss_timer(codec, 0);
1312 if (error < 0)
1313 return error;
1314
1315 miro->pcm = codec->pcm;
1316
1317 error = snd_miro_mixer(card, miro);
1318 if (error < 0)
1319 return error;
1320
1321 if (miro->aci->aci_vendor == 'm') {
1322 /* It looks like a miro sound card. */
1323 switch (miro->aci->aci_product) {
1324 case 'A':
1325 sprintf(card->shortname,
1326 "miroSOUND PCM1 pro / PCM12");
1327 break;
1328 case 'B':
1329 sprintf(card->shortname,
1330 "miroSOUND PCM12");
1331 break;
1332 case 'C':
1333 sprintf(card->shortname,
1334 "miroSOUND PCM20 radio");
1335 break;
1336 default:
1337 sprintf(card->shortname,
1338 "unknown miro");
1339 snd_printk(KERN_INFO "unknown miro aci id\n");
1340 break;
1341 }
1342 } else {
1343 snd_printk(KERN_INFO "found unsupported aci card\n");
1344 sprintf(card->shortname, "unknown Cardinal Technologies");
1345 }
1346
1347 strcpy(card->driver, "miro");
1348 snprintf(card->longname, sizeof(card->longname),
1349 "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1350 card->shortname, miro->name, codec->pcm->name,
1351 miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
1352
1353 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
1354 rmidi = NULL;
1355 else {
1356 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1357 mpu_port, 0, miro->mpu_irq, &rmidi);
1358 if (error < 0)
1359 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1360 mpu_port);
1361 }
1362
1363 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1364 struct snd_opl3 *opl3 = NULL;
1365 struct snd_opl4 *opl4;
1366
1367 if (snd_opl4_create(card, fm_port, fm_port - 8,
1368 2, &opl3, &opl4) < 0)
1369 snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
1370 fm_port);
1371 }
1372
1373 error = snd_set_aci_init_values(miro);
1374 if (error < 0)
1375 return error;
1376
1377 return snd_card_register(card);
1378}
1379
1380static int snd_miro_isa_match(struct device *devptr, unsigned int n)
1381{
1382#ifdef CONFIG_PNP
1383 if (snd_miro_pnp_is_probed)
1384 return 0;
1385 if (isapnp)
1386 return 0;
1387#endif
1388 return 1;
1389}
1390
1391static int snd_miro_isa_probe(struct device *devptr, unsigned int n)
1392{
1393 static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1394 static const long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1395 static const int possible_irqs[] = {11, 9, 10, 7, -1};
1396 static const int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1397 static const int possible_dma1s[] = {3, 1, 0, -1};
1398 static const int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
1399 {0, -1} };
1400
1401 int error;
1402 struct snd_miro *miro;
1403 struct snd_card *card;
1404
1405 error = snd_card_new(devptr, index, id, THIS_MODULE,
1406 sizeof(struct snd_miro), &card);
1407 if (error < 0)
1408 return error;
1409
1410 card->private_free = snd_card_miro_free;
1411 miro = card->private_data;
1412
1413 error = snd_card_miro_detect(card, miro);
1414 if (error < 0) {
1415 snd_card_free(card);
1416 snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
1417 return -ENODEV;
1418 }
1419
1420 if (port == SNDRV_AUTO_PORT) {
1421 port = snd_legacy_find_free_ioport(possible_ports, 4);
1422 if (port < 0) {
1423 snd_card_free(card);
1424 snd_printk(KERN_ERR "unable to find a free WSS port\n");
1425 return -EBUSY;
1426 }
1427 }
1428
1429 if (mpu_port == SNDRV_AUTO_PORT) {
1430 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
1431 if (mpu_port < 0) {
1432 snd_card_free(card);
1433 snd_printk(KERN_ERR
1434 "unable to find a free MPU401 port\n");
1435 return -EBUSY;
1436 }
1437 }
1438
1439 if (irq == SNDRV_AUTO_IRQ) {
1440 irq = snd_legacy_find_free_irq(possible_irqs);
1441 if (irq < 0) {
1442 snd_card_free(card);
1443 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1444 return -EBUSY;
1445 }
1446 }
1447 if (mpu_irq == SNDRV_AUTO_IRQ) {
1448 mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
1449 if (mpu_irq < 0) {
1450 snd_card_free(card);
1451 snd_printk(KERN_ERR
1452 "unable to find a free MPU401 IRQ\n");
1453 return -EBUSY;
1454 }
1455 }
1456 if (dma1 == SNDRV_AUTO_DMA) {
1457 dma1 = snd_legacy_find_free_dma(possible_dma1s);
1458 if (dma1 < 0) {
1459 snd_card_free(card);
1460 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1461 return -EBUSY;
1462 }
1463 }
1464 if (dma2 == SNDRV_AUTO_DMA) {
1465 dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
1466 if (dma2 < 0) {
1467 snd_card_free(card);
1468 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1469 return -EBUSY;
1470 }
1471 }
1472
1473 error = snd_miro_probe(card);
1474 if (error < 0) {
1475 snd_card_free(card);
1476 return error;
1477 }
1478
1479 dev_set_drvdata(devptr, card);
1480 return 0;
1481}
1482
1483static int snd_miro_isa_remove(struct device *devptr,
1484 unsigned int dev)
1485{
1486 snd_card_free(dev_get_drvdata(devptr));
1487 return 0;
1488}
1489
1490#define DEV_NAME "miro"
1491
1492static struct isa_driver snd_miro_driver = {
1493 .match = snd_miro_isa_match,
1494 .probe = snd_miro_isa_probe,
1495 .remove = snd_miro_isa_remove,
1496 /* FIXME: suspend/resume */
1497 .driver = {
1498 .name = DEV_NAME
1499 },
1500};
1501
1502#ifdef CONFIG_PNP
1503
1504static int snd_card_miro_pnp(struct snd_miro *chip,
1505 struct pnp_card_link *card,
1506 const struct pnp_card_device_id *pid)
1507{
1508 struct pnp_dev *pdev;
1509 int err;
1510 struct pnp_dev *devmpu;
1511 struct pnp_dev *devmc;
1512
1513 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1514 if (pdev == NULL)
1515 return -EBUSY;
1516
1517 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1518 if (devmpu == NULL)
1519 return -EBUSY;
1520
1521 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
1522 if (devmc == NULL)
1523 return -EBUSY;
1524
1525 err = pnp_activate_dev(pdev);
1526 if (err < 0) {
1527 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1528 return err;
1529 }
1530
1531 err = pnp_activate_dev(devmc);
1532 if (err < 0) {
1533 snd_printk(KERN_ERR "MC pnp configure failure: %d\n",
1534 err);
1535 return err;
1536 }
1537
1538 port = pnp_port_start(pdev, 1);
1539 fm_port = pnp_port_start(pdev, 2) + 8;
1540
1541 /*
1542 * The MC(0) is never accessed and the miroSOUND PCM20 card does not
1543 * include it in the PnP resource range. OPTI93x include it.
1544 */
1545 chip->mc_base = pnp_port_start(devmc, 0) - 1;
1546 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
1547
1548 irq = pnp_irq(pdev, 0);
1549 dma1 = pnp_dma(pdev, 0);
1550 dma2 = pnp_dma(pdev, 1);
1551
1552 if (mpu_port > 0) {
1553 err = pnp_activate_dev(devmpu);
1554 if (err < 0) {
1555 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
1556 mpu_port = -1;
1557 return err;
1558 }
1559 mpu_port = pnp_port_start(devmpu, 0);
1560 mpu_irq = pnp_irq(devmpu, 0);
1561 }
1562 return 0;
1563}
1564
1565static int snd_miro_pnp_probe(struct pnp_card_link *pcard,
1566 const struct pnp_card_device_id *pid)
1567{
1568 struct snd_card *card;
1569 int err;
1570 struct snd_miro *miro;
1571
1572 if (snd_miro_pnp_is_probed)
1573 return -EBUSY;
1574 if (!isapnp)
1575 return -ENODEV;
1576 err = snd_card_new(&pcard->card->dev, index, id, THIS_MODULE,
1577 sizeof(struct snd_miro), &card);
1578 if (err < 0)
1579 return err;
1580
1581 card->private_free = snd_card_miro_free;
1582 miro = card->private_data;
1583
1584 err = snd_card_miro_pnp(miro, pcard, pid);
1585 if (err) {
1586 snd_card_free(card);
1587 return err;
1588 }
1589
1590 /* only miroSOUND PCM20 and PCM12 == OPTi924 */
1591 err = snd_miro_init(miro, OPTi9XX_HW_82C924);
1592 if (err) {
1593 snd_card_free(card);
1594 return err;
1595 }
1596
1597 err = snd_miro_opti_check(miro);
1598 if (err) {
1599 snd_printk(KERN_ERR "OPTI chip not found\n");
1600 snd_card_free(card);
1601 return err;
1602 }
1603
1604 err = snd_miro_probe(card);
1605 if (err < 0) {
1606 snd_card_free(card);
1607 return err;
1608 }
1609 pnp_set_card_drvdata(pcard, card);
1610 snd_miro_pnp_is_probed = 1;
1611 return 0;
1612}
1613
1614static void snd_miro_pnp_remove(struct pnp_card_link *pcard)
1615{
1616 snd_card_free(pnp_get_card_drvdata(pcard));
1617 pnp_set_card_drvdata(pcard, NULL);
1618 snd_miro_pnp_is_probed = 0;
1619}
1620
1621static struct pnp_card_driver miro_pnpc_driver = {
1622 .flags = PNP_DRIVER_RES_DISABLE,
1623 .name = "miro",
1624 .id_table = snd_miro_pnpids,
1625 .probe = snd_miro_pnp_probe,
1626 .remove = snd_miro_pnp_remove,
1627};
1628#endif
1629
1630static int __init alsa_card_miro_init(void)
1631{
1632#ifdef CONFIG_PNP
1633 pnp_register_card_driver(&miro_pnpc_driver);
1634 if (snd_miro_pnp_is_probed)
1635 return 0;
1636 pnp_unregister_card_driver(&miro_pnpc_driver);
1637#endif
1638 return isa_register_driver(&snd_miro_driver, 1);
1639}
1640
1641static void __exit alsa_card_miro_exit(void)
1642{
1643 if (!snd_miro_pnp_is_probed) {
1644 isa_unregister_driver(&snd_miro_driver);
1645 return;
1646 }
1647#ifdef CONFIG_PNP
1648 pnp_unregister_card_driver(&miro_pnpc_driver);
1649#endif
1650}
1651
1652module_init(alsa_card_miro_init)
1653module_exit(alsa_card_miro_exit)
1/*
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
25#include <linux/init.h>
26#include <linux/err.h>
27#include <linux/isa.h>
28#include <linux/pnp.h>
29#include <linux/delay.h>
30#include <linux/ioport.h>
31#include <linux/module.h>
32#include <linux/io.h>
33#include <asm/dma.h>
34#include <sound/core.h>
35#include <sound/wss.h>
36#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_IOPORT
41#define SNDRV_LEGACY_FIND_FREE_IRQ
42#define SNDRV_LEGACY_FIND_FREE_DMA
43#include <sound/initval.h>
44#include <sound/aci.h>
45
46MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
47MODULE_LICENSE("GPL");
48MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
49MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
50 "{Miro,miroSOUND PCM12}, "
51 "{Miro,miroSOUND PCM20 Radio}}");
52
53static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
54static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
55static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
56static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
57static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
58static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
59static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
60static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
61static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
62static int wss;
63static int ide;
64#ifdef CONFIG_PNP
65static bool isapnp = 1; /* Enable ISA PnP detection */
66#endif
67
68module_param(index, int, 0444);
69MODULE_PARM_DESC(index, "Index value for miro soundcard.");
70module_param(id, charp, 0444);
71MODULE_PARM_DESC(id, "ID string for miro soundcard.");
72module_param_hw(port, long, ioport, 0444);
73MODULE_PARM_DESC(port, "WSS port # for miro driver.");
74module_param_hw(mpu_port, long, ioport, 0444);
75MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
76module_param_hw(fm_port, long, ioport, 0444);
77MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
78module_param_hw(irq, int, irq, 0444);
79MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
80module_param_hw(mpu_irq, int, irq, 0444);
81MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
82module_param_hw(dma1, int, dma, 0444);
83MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
84module_param_hw(dma2, int, dma, 0444);
85MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
86module_param(wss, int, 0444);
87MODULE_PARM_DESC(wss, "wss mode");
88module_param(ide, int, 0444);
89MODULE_PARM_DESC(ide, "enable ide port");
90#ifdef CONFIG_PNP
91module_param(isapnp, bool, 0444);
92MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
93#endif
94
95#define OPTi9XX_HW_DETECT 0
96#define OPTi9XX_HW_82C928 1
97#define OPTi9XX_HW_82C929 2
98#define OPTi9XX_HW_82C924 3
99#define OPTi9XX_HW_82C925 4
100#define OPTi9XX_HW_82C930 5
101#define OPTi9XX_HW_82C931 6
102#define OPTi9XX_HW_82C933 7
103#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
104
105#define OPTi9XX_MC_REG(n) n
106
107struct snd_miro {
108 unsigned short hardware;
109 unsigned char password;
110 char name[7];
111
112 struct resource *res_mc_base;
113 struct resource *res_aci_port;
114
115 unsigned long mc_base;
116 unsigned long mc_base_size;
117 unsigned long pwd_reg;
118
119 spinlock_t lock;
120 struct snd_pcm *pcm;
121
122 long wss_base;
123 int irq;
124 int dma1;
125 int dma2;
126
127 long mpu_port;
128 int mpu_irq;
129
130 struct snd_miro_aci *aci;
131};
132
133static struct snd_miro_aci aci_device;
134
135static char * snd_opti9xx_names[] = {
136 "unknown",
137 "82C928", "82C929",
138 "82C924", "82C925",
139 "82C930", "82C931", "82C933"
140};
141
142static int snd_miro_pnp_is_probed;
143
144#ifdef CONFIG_PNP
145
146static const struct pnp_card_device_id snd_miro_pnpids[] = {
147 /* PCM20 and PCM12 in PnP mode */
148 { .id = "MIR0924",
149 .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, },
150 { .id = "" }
151};
152
153MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids);
154
155#endif /* CONFIG_PNP */
156
157/*
158 * ACI control
159 */
160
161static int aci_busy_wait(struct snd_miro_aci *aci)
162{
163 long timeout;
164 unsigned char byte;
165
166 for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
167 byte = inb(aci->aci_port + ACI_REG_BUSY);
168 if ((byte & 1) == 0) {
169 if (timeout >= ACI_MINTIME)
170 snd_printd("aci ready in round %ld.\n",
171 timeout-ACI_MINTIME);
172 return byte;
173 }
174 if (timeout >= ACI_MINTIME) {
175 long out=10*HZ;
176 switch (timeout-ACI_MINTIME) {
177 case 0 ... 9:
178 out /= 10;
179 case 10 ... 19:
180 out /= 10;
181 case 20 ... 30:
182 out /= 10;
183 default:
184 set_current_state(TASK_UNINTERRUPTIBLE);
185 schedule_timeout(out);
186 break;
187 }
188 }
189 }
190 snd_printk(KERN_ERR "aci_busy_wait() time out\n");
191 return -EBUSY;
192}
193
194static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
195{
196 if (aci_busy_wait(aci) >= 0) {
197 outb(byte, aci->aci_port + ACI_REG_COMMAND);
198 return 0;
199 } else {
200 snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
201 return -EBUSY;
202 }
203}
204
205static inline int aci_read(struct snd_miro_aci *aci)
206{
207 unsigned char byte;
208
209 if (aci_busy_wait(aci) >= 0) {
210 byte = inb(aci->aci_port + ACI_REG_STATUS);
211 return byte;
212 } else {
213 snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
214 return -EBUSY;
215 }
216}
217
218int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
219{
220 int write[] = {write1, write2, write3};
221 int value, i;
222
223 if (mutex_lock_interruptible(&aci->aci_mutex))
224 return -EINTR;
225
226 for (i=0; i<3; i++) {
227 if (write[i]< 0 || write[i] > 255)
228 break;
229 else {
230 value = aci_write(aci, write[i]);
231 if (value < 0)
232 goto out;
233 }
234 }
235
236 value = aci_read(aci);
237
238out: mutex_unlock(&aci->aci_mutex);
239 return value;
240}
241EXPORT_SYMBOL(snd_aci_cmd);
242
243static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
244{
245 return snd_aci_cmd(aci, ACI_STATUS, index, -1);
246}
247
248static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
249 int value)
250{
251 return snd_aci_cmd(aci, index, value, -1);
252}
253
254struct snd_miro_aci *snd_aci_get_aci(void)
255{
256 if (aci_device.aci_port == 0)
257 return NULL;
258 return &aci_device;
259}
260EXPORT_SYMBOL(snd_aci_get_aci);
261
262/*
263 * MIXER part
264 */
265
266#define snd_miro_info_capture snd_ctl_boolean_mono_info
267
268static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
270{
271 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
272 int value;
273
274 value = aci_getvalue(miro->aci, ACI_S_GENERAL);
275 if (value < 0) {
276 snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
277 value);
278 return value;
279 }
280
281 ucontrol->value.integer.value[0] = value & 0x20;
282
283 return 0;
284}
285
286static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
287 struct snd_ctl_elem_value *ucontrol)
288{
289 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
290 int change, value, error;
291
292 value = !(ucontrol->value.integer.value[0]);
293
294 error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
295 if (error < 0) {
296 snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
297 error);
298 return error;
299 }
300
301 change = (value != miro->aci->aci_solomode);
302 miro->aci->aci_solomode = value;
303
304 return change;
305}
306
307static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
308 struct snd_ctl_elem_info *uinfo)
309{
310 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
311 uinfo->count = 1;
312 uinfo->value.integer.min = 0;
313 uinfo->value.integer.max = 3;
314
315 return 0;
316}
317
318static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
319 struct snd_ctl_elem_value *ucontrol)
320{
321 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
322 int value;
323
324 if (miro->aci->aci_version <= 176) {
325
326 /*
327 OSS says it's not readable with versions < 176.
328 But it doesn't work on my card,
329 which is a PCM12 with aci_version = 176.
330 */
331
332 ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
333 return 0;
334 }
335
336 value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
337 if (value < 0) {
338 snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
339 value);
340 return value;
341 }
342
343 ucontrol->value.integer.value[0] = value;
344
345 return 0;
346}
347
348static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
349 struct snd_ctl_elem_value *ucontrol)
350{
351 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
352 int error, value, change;
353
354 value = ucontrol->value.integer.value[0];
355
356 error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
357 if (error < 0) {
358 snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
359 error);
360 return error;
361 }
362
363 change = (value != miro->aci->aci_preamp);
364 miro->aci->aci_preamp = value;
365
366 return change;
367}
368
369#define snd_miro_info_amp snd_ctl_boolean_mono_info
370
371static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
372 struct snd_ctl_elem_value *ucontrol)
373{
374 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
375 ucontrol->value.integer.value[0] = miro->aci->aci_amp;
376
377 return 0;
378}
379
380static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
381 struct snd_ctl_elem_value *ucontrol)
382{
383 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
384 int error, value, change;
385
386 value = ucontrol->value.integer.value[0];
387
388 error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
389 if (error < 0) {
390 snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
391 return error;
392 }
393
394 change = (value != miro->aci->aci_amp);
395 miro->aci->aci_amp = value;
396
397 return change;
398}
399
400#define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
401{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
402 .name = ctl_name, \
403 .index = ctl_index, \
404 .info = snd_miro_info_double, \
405 .get = snd_miro_get_double, \
406 .put = snd_miro_put_double, \
407 .private_value = get_right_reg | (set_right_reg << 8) \
408}
409
410static int snd_miro_info_double(struct snd_kcontrol *kcontrol,
411 struct snd_ctl_elem_info *uinfo)
412{
413 int reg = kcontrol->private_value & 0xff;
414
415 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
416 uinfo->count = 2;
417
418 if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
419
420 /* equalizer elements */
421
422 uinfo->value.integer.min = - 0x7f;
423 uinfo->value.integer.max = 0x7f;
424 } else {
425
426 /* non-equalizer elements */
427
428 uinfo->value.integer.min = 0;
429 uinfo->value.integer.max = 0x20;
430 }
431
432 return 0;
433}
434
435static int snd_miro_get_double(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *uinfo)
437{
438 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
439 int left_val, right_val;
440
441 int right_reg = kcontrol->private_value & 0xff;
442 int left_reg = right_reg + 1;
443
444 right_val = aci_getvalue(miro->aci, right_reg);
445 if (right_val < 0) {
446 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
447 return right_val;
448 }
449
450 left_val = aci_getvalue(miro->aci, left_reg);
451 if (left_val < 0) {
452 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
453 return left_val;
454 }
455
456 if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
457
458 /* equalizer elements */
459
460 if (left_val < 0x80) {
461 uinfo->value.integer.value[0] = left_val;
462 } else {
463 uinfo->value.integer.value[0] = 0x80 - left_val;
464 }
465
466 if (right_val < 0x80) {
467 uinfo->value.integer.value[1] = right_val;
468 } else {
469 uinfo->value.integer.value[1] = 0x80 - right_val;
470 }
471
472 } else {
473
474 /* non-equalizer elements */
475
476 uinfo->value.integer.value[0] = 0x20 - left_val;
477 uinfo->value.integer.value[1] = 0x20 - right_val;
478 }
479
480 return 0;
481}
482
483static int snd_miro_put_double(struct snd_kcontrol *kcontrol,
484 struct snd_ctl_elem_value *ucontrol)
485{
486 struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
487 struct snd_miro_aci *aci = miro->aci;
488 int left, right, left_old, right_old;
489 int setreg_left, setreg_right, getreg_left, getreg_right;
490 int change, error;
491
492 left = ucontrol->value.integer.value[0];
493 right = ucontrol->value.integer.value[1];
494
495 setreg_right = (kcontrol->private_value >> 8) & 0xff;
496 setreg_left = setreg_right + 8;
497 if (setreg_right == ACI_SET_MASTER)
498 setreg_left -= 7;
499
500 getreg_right = kcontrol->private_value & 0xff;
501 getreg_left = getreg_right + 1;
502
503 left_old = aci_getvalue(aci, getreg_left);
504 if (left_old < 0) {
505 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
506 return left_old;
507 }
508
509 right_old = aci_getvalue(aci, getreg_right);
510 if (right_old < 0) {
511 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
512 return right_old;
513 }
514
515 if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
516
517 /* equalizer elements */
518
519 if (left < -0x7f || left > 0x7f ||
520 right < -0x7f || right > 0x7f)
521 return -EINVAL;
522
523 if (left_old > 0x80)
524 left_old = 0x80 - left_old;
525 if (right_old > 0x80)
526 right_old = 0x80 - right_old;
527
528 if (left >= 0) {
529 error = aci_setvalue(aci, setreg_left, left);
530 if (error < 0) {
531 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
532 left, error);
533 return error;
534 }
535 } else {
536 error = aci_setvalue(aci, setreg_left, 0x80 - left);
537 if (error < 0) {
538 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
539 0x80 - left, error);
540 return error;
541 }
542 }
543
544 if (right >= 0) {
545 error = aci_setvalue(aci, setreg_right, right);
546 if (error < 0) {
547 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
548 right, error);
549 return error;
550 }
551 } else {
552 error = aci_setvalue(aci, setreg_right, 0x80 - right);
553 if (error < 0) {
554 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
555 0x80 - right, error);
556 return error;
557 }
558 }
559
560 } else {
561
562 /* non-equalizer elements */
563
564 if (left < 0 || left > 0x20 ||
565 right < 0 || right > 0x20)
566 return -EINVAL;
567
568 left_old = 0x20 - left_old;
569 right_old = 0x20 - right_old;
570
571 error = aci_setvalue(aci, setreg_left, 0x20 - left);
572 if (error < 0) {
573 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
574 0x20 - left, error);
575 return error;
576 }
577 error = aci_setvalue(aci, setreg_right, 0x20 - right);
578 if (error < 0) {
579 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
580 0x20 - right, error);
581 return error;
582 }
583 }
584
585 change = (left != left_old) || (right != right_old);
586
587 return change;
588}
589
590static struct snd_kcontrol_new snd_miro_controls[] = {
591MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
592MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
593MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
594MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
595MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
596MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
597MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
598};
599
600/* Equalizer with seven bands (only PCM20)
601 from -12dB up to +12dB on each band */
602static struct snd_kcontrol_new snd_miro_eq_controls[] = {
603MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
604MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
605MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
606MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
607MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
608MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
609MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
610};
611
612static struct snd_kcontrol_new snd_miro_radio_control[] = {
613MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
614};
615
616static struct snd_kcontrol_new snd_miro_line_control[] = {
617MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
618};
619
620static struct snd_kcontrol_new snd_miro_preamp_control[] = {
621{
622 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
623 .name = "Mic Boost",
624 .index = 1,
625 .info = snd_miro_info_preamp,
626 .get = snd_miro_get_preamp,
627 .put = snd_miro_put_preamp,
628}};
629
630static struct snd_kcontrol_new snd_miro_amp_control[] = {
631{
632 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
633 .name = "Line Boost",
634 .index = 0,
635 .info = snd_miro_info_amp,
636 .get = snd_miro_get_amp,
637 .put = snd_miro_put_amp,
638}};
639
640static struct snd_kcontrol_new snd_miro_capture_control[] = {
641{
642 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
643 .name = "PCM Capture Switch",
644 .index = 0,
645 .info = snd_miro_info_capture,
646 .get = snd_miro_get_capture,
647 .put = snd_miro_put_capture,
648}};
649
650static unsigned char aci_init_values[][2] = {
651 { ACI_SET_MUTE, 0x00 },
652 { ACI_SET_POWERAMP, 0x00 },
653 { ACI_SET_PREAMP, 0x00 },
654 { ACI_SET_SOLOMODE, 0x00 },
655 { ACI_SET_MIC + 0, 0x20 },
656 { ACI_SET_MIC + 8, 0x20 },
657 { ACI_SET_LINE + 0, 0x20 },
658 { ACI_SET_LINE + 8, 0x20 },
659 { ACI_SET_CD + 0, 0x20 },
660 { ACI_SET_CD + 8, 0x20 },
661 { ACI_SET_PCM + 0, 0x20 },
662 { ACI_SET_PCM + 8, 0x20 },
663 { ACI_SET_LINE1 + 0, 0x20 },
664 { ACI_SET_LINE1 + 8, 0x20 },
665 { ACI_SET_LINE2 + 0, 0x20 },
666 { ACI_SET_LINE2 + 8, 0x20 },
667 { ACI_SET_SYNTH + 0, 0x20 },
668 { ACI_SET_SYNTH + 8, 0x20 },
669 { ACI_SET_MASTER + 0, 0x20 },
670 { ACI_SET_MASTER + 1, 0x20 },
671};
672
673static int snd_set_aci_init_values(struct snd_miro *miro)
674{
675 int idx, error;
676 struct snd_miro_aci *aci = miro->aci;
677
678 /* enable WSS on PCM1 */
679
680 if ((aci->aci_product == 'A') && wss) {
681 error = aci_setvalue(aci, ACI_SET_WSS, wss);
682 if (error < 0) {
683 snd_printk(KERN_ERR "enabling WSS mode failed\n");
684 return error;
685 }
686 }
687
688 /* enable IDE port */
689
690 if (ide) {
691 error = aci_setvalue(aci, ACI_SET_IDE, ide);
692 if (error < 0) {
693 snd_printk(KERN_ERR "enabling IDE port failed\n");
694 return error;
695 }
696 }
697
698 /* set common aci values */
699
700 for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
701 error = aci_setvalue(aci, aci_init_values[idx][0],
702 aci_init_values[idx][1]);
703 if (error < 0) {
704 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
705 aci_init_values[idx][0], error);
706 return error;
707 }
708 }
709 aci->aci_amp = 0;
710 aci->aci_preamp = 0;
711 aci->aci_solomode = 1;
712
713 return 0;
714}
715
716static int snd_miro_mixer(struct snd_card *card,
717 struct snd_miro *miro)
718{
719 unsigned int idx;
720 int err;
721
722 if (snd_BUG_ON(!miro || !card))
723 return -EINVAL;
724
725 switch (miro->hardware) {
726 case OPTi9XX_HW_82C924:
727 strcpy(card->mixername, "ACI & OPTi924");
728 break;
729 case OPTi9XX_HW_82C929:
730 strcpy(card->mixername, "ACI & OPTi929");
731 break;
732 default:
733 snd_BUG();
734 break;
735 }
736
737 for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
738 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
739 return err;
740 }
741
742 if ((miro->aci->aci_product == 'A') ||
743 (miro->aci->aci_product == 'B')) {
744 /* PCM1/PCM12 with power-amp and Line 2 */
745 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
746 return err;
747 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
748 return err;
749 }
750
751 if ((miro->aci->aci_product == 'B') ||
752 (miro->aci->aci_product == 'C')) {
753 /* PCM12/PCM20 with mic-preamp */
754 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
755 return err;
756 if (miro->aci->aci_version >= 176)
757 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
758 return err;
759 }
760
761 if (miro->aci->aci_product == 'C') {
762 /* PCM20 with radio and 7 band equalizer */
763 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
764 return err;
765 for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
766 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
767 return err;
768 }
769 }
770
771 return 0;
772}
773
774static int snd_miro_init(struct snd_miro *chip,
775 unsigned short hardware)
776{
777 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
778
779 chip->hardware = hardware;
780 strcpy(chip->name, snd_opti9xx_names[hardware]);
781
782 chip->mc_base_size = opti9xx_mc_size[hardware];
783
784 spin_lock_init(&chip->lock);
785
786 chip->wss_base = -1;
787 chip->irq = -1;
788 chip->dma1 = -1;
789 chip->dma2 = -1;
790 chip->mpu_port = -1;
791 chip->mpu_irq = -1;
792
793 chip->pwd_reg = 3;
794
795#ifdef CONFIG_PNP
796 if (isapnp && chip->mc_base)
797 /* PnP resource gives the least 10 bits */
798 chip->mc_base |= 0xc00;
799 else
800#endif
801 chip->mc_base = 0xf8c;
802
803 switch (hardware) {
804 case OPTi9XX_HW_82C929:
805 chip->password = 0xe3;
806 break;
807
808 case OPTi9XX_HW_82C924:
809 chip->password = 0xe5;
810 break;
811
812 default:
813 snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
814 return -ENODEV;
815 }
816
817 return 0;
818}
819
820static unsigned char snd_miro_read(struct snd_miro *chip,
821 unsigned char reg)
822{
823 unsigned long flags;
824 unsigned char retval = 0xff;
825
826 spin_lock_irqsave(&chip->lock, flags);
827 outb(chip->password, chip->mc_base + chip->pwd_reg);
828
829 switch (chip->hardware) {
830 case OPTi9XX_HW_82C924:
831 if (reg > 7) {
832 outb(reg, chip->mc_base + 8);
833 outb(chip->password, chip->mc_base + chip->pwd_reg);
834 retval = inb(chip->mc_base + 9);
835 break;
836 }
837
838 case OPTi9XX_HW_82C929:
839 retval = inb(chip->mc_base + reg);
840 break;
841
842 default:
843 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
844 }
845
846 spin_unlock_irqrestore(&chip->lock, flags);
847 return retval;
848}
849
850static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
851 unsigned char value)
852{
853 unsigned long flags;
854
855 spin_lock_irqsave(&chip->lock, flags);
856 outb(chip->password, chip->mc_base + chip->pwd_reg);
857
858 switch (chip->hardware) {
859 case OPTi9XX_HW_82C924:
860 if (reg > 7) {
861 outb(reg, chip->mc_base + 8);
862 outb(chip->password, chip->mc_base + chip->pwd_reg);
863 outb(value, chip->mc_base + 9);
864 break;
865 }
866
867 case OPTi9XX_HW_82C929:
868 outb(value, chip->mc_base + reg);
869 break;
870
871 default:
872 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
873 }
874
875 spin_unlock_irqrestore(&chip->lock, flags);
876}
877
878
879#define snd_miro_write_mask(chip, reg, value, mask) \
880 snd_miro_write(chip, reg, \
881 (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask)))
882
883/*
884 * Proc Interface
885 */
886
887static void snd_miro_proc_read(struct snd_info_entry * entry,
888 struct snd_info_buffer *buffer)
889{
890 struct snd_miro *miro = (struct snd_miro *) entry->private_data;
891 struct snd_miro_aci *aci = miro->aci;
892 char* model = "unknown";
893
894 /* miroSOUND PCM1 pro, early PCM12 */
895
896 if ((miro->hardware == OPTi9XX_HW_82C929) &&
897 (aci->aci_vendor == 'm') &&
898 (aci->aci_product == 'A')) {
899 switch (aci->aci_version) {
900 case 3:
901 model = "miroSOUND PCM1 pro";
902 break;
903 default:
904 model = "miroSOUND PCM1 pro / (early) PCM12";
905 break;
906 }
907 }
908
909 /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
910
911 if ((miro->hardware == OPTi9XX_HW_82C924) &&
912 (aci->aci_vendor == 'm') &&
913 (aci->aci_product == 'B')) {
914 switch (aci->aci_version) {
915 case 4:
916 model = "miroSOUND PCM12";
917 break;
918 case 176:
919 model = "miroSOUND PCM12 (Rev. E)";
920 break;
921 default:
922 model = "miroSOUND PCM12 / PCM12 pnp";
923 break;
924 }
925 }
926
927 /* miroSOUND PCM20 radio */
928
929 if ((miro->hardware == OPTi9XX_HW_82C924) &&
930 (aci->aci_vendor == 'm') &&
931 (aci->aci_product == 'C')) {
932 switch (aci->aci_version) {
933 case 7:
934 model = "miroSOUND PCM20 radio (Rev. E)";
935 break;
936 default:
937 model = "miroSOUND PCM20 radio";
938 break;
939 }
940 }
941
942 snd_iprintf(buffer, "\nGeneral information:\n");
943 snd_iprintf(buffer, " model : %s\n", model);
944 snd_iprintf(buffer, " opti : %s\n", miro->name);
945 snd_iprintf(buffer, " codec : %s\n", miro->pcm->name);
946 snd_iprintf(buffer, " port : 0x%lx\n", miro->wss_base);
947 snd_iprintf(buffer, " irq : %d\n", miro->irq);
948 snd_iprintf(buffer, " dma : %d,%d\n\n", miro->dma1, miro->dma2);
949
950 snd_iprintf(buffer, "MPU-401:\n");
951 snd_iprintf(buffer, " port : 0x%lx\n", miro->mpu_port);
952 snd_iprintf(buffer, " irq : %d\n\n", miro->mpu_irq);
953
954 snd_iprintf(buffer, "ACI information:\n");
955 snd_iprintf(buffer, " vendor : ");
956 switch (aci->aci_vendor) {
957 case 'm':
958 snd_iprintf(buffer, "Miro\n");
959 break;
960 default:
961 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
962 break;
963 }
964
965 snd_iprintf(buffer, " product : ");
966 switch (aci->aci_product) {
967 case 'A':
968 snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
969 break;
970 case 'B':
971 snd_iprintf(buffer, "miroSOUND PCM12\n");
972 break;
973 case 'C':
974 snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
975 break;
976 default:
977 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
978 break;
979 }
980
981 snd_iprintf(buffer, " firmware: %d (0x%x)\n",
982 aci->aci_version, aci->aci_version);
983 snd_iprintf(buffer, " port : 0x%lx-0x%lx\n",
984 aci->aci_port, aci->aci_port+2);
985 snd_iprintf(buffer, " wss : 0x%x\n", wss);
986 snd_iprintf(buffer, " ide : 0x%x\n", ide);
987 snd_iprintf(buffer, " solomode: 0x%x\n", aci->aci_solomode);
988 snd_iprintf(buffer, " amp : 0x%x\n", aci->aci_amp);
989 snd_iprintf(buffer, " preamp : 0x%x\n", aci->aci_preamp);
990}
991
992static void snd_miro_proc_init(struct snd_card *card,
993 struct snd_miro *miro)
994{
995 struct snd_info_entry *entry;
996
997 if (!snd_card_proc_new(card, "miro", &entry))
998 snd_info_set_text_ops(entry, miro, snd_miro_proc_read);
999}
1000
1001/*
1002 * Init
1003 */
1004
1005static int snd_miro_configure(struct snd_miro *chip)
1006{
1007 unsigned char wss_base_bits;
1008 unsigned char irq_bits;
1009 unsigned char dma_bits;
1010 unsigned char mpu_port_bits = 0;
1011 unsigned char mpu_irq_bits;
1012 unsigned long flags;
1013
1014 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1015 snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1016 snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1017
1018 switch (chip->hardware) {
1019 case OPTi9XX_HW_82C924:
1020 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
1021 snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
1022 break;
1023 case OPTi9XX_HW_82C929:
1024 /* untested init commands for OPTi929 */
1025 snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
1026 break;
1027 default:
1028 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
1029 return -EINVAL;
1030 }
1031
1032 /* PnP resource says it decodes only 10 bits of address */
1033 switch (chip->wss_base & 0x3ff) {
1034 case 0x130:
1035 chip->wss_base = 0x530;
1036 wss_base_bits = 0x00;
1037 break;
1038 case 0x204:
1039 chip->wss_base = 0x604;
1040 wss_base_bits = 0x03;
1041 break;
1042 case 0x280:
1043 chip->wss_base = 0xe80;
1044 wss_base_bits = 0x01;
1045 break;
1046 case 0x340:
1047 chip->wss_base = 0xf40;
1048 wss_base_bits = 0x02;
1049 break;
1050 default:
1051 snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
1052 goto __skip_base;
1053 }
1054 snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1055
1056__skip_base:
1057 switch (chip->irq) {
1058 case 5:
1059 irq_bits = 0x05;
1060 break;
1061 case 7:
1062 irq_bits = 0x01;
1063 break;
1064 case 9:
1065 irq_bits = 0x02;
1066 break;
1067 case 10:
1068 irq_bits = 0x03;
1069 break;
1070 case 11:
1071 irq_bits = 0x04;
1072 break;
1073 default:
1074 snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
1075 goto __skip_resources;
1076 }
1077
1078 switch (chip->dma1) {
1079 case 0:
1080 dma_bits = 0x01;
1081 break;
1082 case 1:
1083 dma_bits = 0x02;
1084 break;
1085 case 3:
1086 dma_bits = 0x03;
1087 break;
1088 default:
1089 snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
1090 goto __skip_resources;
1091 }
1092
1093 if (chip->dma1 == chip->dma2) {
1094 snd_printk(KERN_ERR "don't want to share dmas\n");
1095 return -EBUSY;
1096 }
1097
1098 switch (chip->dma2) {
1099 case 0:
1100 case 1:
1101 break;
1102 default:
1103 snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
1104 goto __skip_resources;
1105 }
1106 dma_bits |= 0x04;
1107
1108 spin_lock_irqsave(&chip->lock, flags);
1109 outb(irq_bits << 3 | dma_bits, chip->wss_base);
1110 spin_unlock_irqrestore(&chip->lock, flags);
1111
1112__skip_resources:
1113 if (chip->hardware > OPTi9XX_HW_82C928) {
1114 switch (chip->mpu_port) {
1115 case 0:
1116 case -1:
1117 break;
1118 case 0x300:
1119 mpu_port_bits = 0x03;
1120 break;
1121 case 0x310:
1122 mpu_port_bits = 0x02;
1123 break;
1124 case 0x320:
1125 mpu_port_bits = 0x01;
1126 break;
1127 case 0x330:
1128 mpu_port_bits = 0x00;
1129 break;
1130 default:
1131 snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
1132 chip->mpu_port);
1133 goto __skip_mpu;
1134 }
1135
1136 switch (chip->mpu_irq) {
1137 case 5:
1138 mpu_irq_bits = 0x02;
1139 break;
1140 case 7:
1141 mpu_irq_bits = 0x03;
1142 break;
1143 case 9:
1144 mpu_irq_bits = 0x00;
1145 break;
1146 case 10:
1147 mpu_irq_bits = 0x01;
1148 break;
1149 default:
1150 snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
1151 chip->mpu_irq);
1152 goto __skip_mpu;
1153 }
1154
1155 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1156 (chip->mpu_port <= 0) ? 0x00 :
1157 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1158 0xf8);
1159 }
1160__skip_mpu:
1161
1162 return 0;
1163}
1164
1165static int snd_miro_opti_check(struct snd_miro *chip)
1166{
1167 unsigned char value;
1168
1169 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
1170 "OPTi9xx MC");
1171 if (chip->res_mc_base == NULL)
1172 return -ENOMEM;
1173
1174 value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1175 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
1176 if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1177 return 0;
1178
1179 release_and_free_resource(chip->res_mc_base);
1180 chip->res_mc_base = NULL;
1181
1182 return -ENODEV;
1183}
1184
1185static int snd_card_miro_detect(struct snd_card *card,
1186 struct snd_miro *chip)
1187{
1188 int i, err;
1189
1190 for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1191
1192 if ((err = snd_miro_init(chip, i)) < 0)
1193 return err;
1194
1195 err = snd_miro_opti_check(chip);
1196 if (err == 0)
1197 return 1;
1198 }
1199
1200 return -ENODEV;
1201}
1202
1203static int snd_card_miro_aci_detect(struct snd_card *card,
1204 struct snd_miro *miro)
1205{
1206 unsigned char regval;
1207 int i;
1208 struct snd_miro_aci *aci = &aci_device;
1209
1210 miro->aci = aci;
1211
1212 mutex_init(&aci->aci_mutex);
1213
1214 /* get ACI port from OPTi9xx MC 4 */
1215
1216 regval=inb(miro->mc_base + 4);
1217 aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
1218
1219 miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
1220 if (miro->res_aci_port == NULL) {
1221 snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n",
1222 aci->aci_port, aci->aci_port+2);
1223 return -ENOMEM;
1224 }
1225
1226 /* force ACI into a known state */
1227 for (i = 0; i < 3; i++)
1228 if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
1229 snd_printk(KERN_ERR "can't force aci into known state.\n");
1230 return -ENXIO;
1231 }
1232
1233 aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1234 aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1235 if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1236 snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
1237 aci->aci_port);
1238 return -ENXIO;
1239 }
1240
1241 aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1242 if (aci->aci_version < 0) {
1243 snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n",
1244 aci->aci_port);
1245 return -ENXIO;
1246 }
1247
1248 if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1249 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1250 snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
1251 snd_printk(KERN_ERR "can't initialize aci.\n");
1252 return -ENXIO;
1253 }
1254
1255 return 0;
1256}
1257
1258static void snd_card_miro_free(struct snd_card *card)
1259{
1260 struct snd_miro *miro = card->private_data;
1261
1262 release_and_free_resource(miro->res_aci_port);
1263 if (miro->aci)
1264 miro->aci->aci_port = 0;
1265 release_and_free_resource(miro->res_mc_base);
1266}
1267
1268static int snd_miro_probe(struct snd_card *card)
1269{
1270 int error;
1271 struct snd_miro *miro = card->private_data;
1272 struct snd_wss *codec;
1273 struct snd_rawmidi *rmidi;
1274
1275 if (!miro->res_mc_base) {
1276 miro->res_mc_base = request_region(miro->mc_base,
1277 miro->mc_base_size,
1278 "miro (OPTi9xx MC)");
1279 if (miro->res_mc_base == NULL) {
1280 snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
1281 return -ENOMEM;
1282 }
1283 }
1284
1285 error = snd_card_miro_aci_detect(card, miro);
1286 if (error < 0) {
1287 snd_printk(KERN_ERR "unable to detect aci chip\n");
1288 return -ENODEV;
1289 }
1290
1291 miro->wss_base = port;
1292 miro->mpu_port = mpu_port;
1293 miro->irq = irq;
1294 miro->mpu_irq = mpu_irq;
1295 miro->dma1 = dma1;
1296 miro->dma2 = dma2;
1297
1298 /* init proc interface */
1299 snd_miro_proc_init(card, miro);
1300
1301 error = snd_miro_configure(miro);
1302 if (error)
1303 return error;
1304
1305 error = snd_wss_create(card, miro->wss_base + 4, -1,
1306 miro->irq, miro->dma1, miro->dma2,
1307 WSS_HW_DETECT, 0, &codec);
1308 if (error < 0)
1309 return error;
1310
1311 error = snd_wss_pcm(codec, 0);
1312 if (error < 0)
1313 return error;
1314
1315 error = snd_wss_mixer(codec);
1316 if (error < 0)
1317 return error;
1318
1319 error = snd_wss_timer(codec, 0);
1320 if (error < 0)
1321 return error;
1322
1323 miro->pcm = codec->pcm;
1324
1325 error = snd_miro_mixer(card, miro);
1326 if (error < 0)
1327 return error;
1328
1329 if (miro->aci->aci_vendor == 'm') {
1330 /* It looks like a miro sound card. */
1331 switch (miro->aci->aci_product) {
1332 case 'A':
1333 sprintf(card->shortname,
1334 "miroSOUND PCM1 pro / PCM12");
1335 break;
1336 case 'B':
1337 sprintf(card->shortname,
1338 "miroSOUND PCM12");
1339 break;
1340 case 'C':
1341 sprintf(card->shortname,
1342 "miroSOUND PCM20 radio");
1343 break;
1344 default:
1345 sprintf(card->shortname,
1346 "unknown miro");
1347 snd_printk(KERN_INFO "unknown miro aci id\n");
1348 break;
1349 }
1350 } else {
1351 snd_printk(KERN_INFO "found unsupported aci card\n");
1352 sprintf(card->shortname, "unknown Cardinal Technologies");
1353 }
1354
1355 strcpy(card->driver, "miro");
1356 snprintf(card->longname, sizeof(card->longname),
1357 "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1358 card->shortname, miro->name, codec->pcm->name,
1359 miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
1360
1361 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
1362 rmidi = NULL;
1363 else {
1364 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1365 mpu_port, 0, miro->mpu_irq, &rmidi);
1366 if (error < 0)
1367 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1368 mpu_port);
1369 }
1370
1371 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1372 struct snd_opl3 *opl3 = NULL;
1373 struct snd_opl4 *opl4;
1374
1375 if (snd_opl4_create(card, fm_port, fm_port - 8,
1376 2, &opl3, &opl4) < 0)
1377 snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
1378 fm_port);
1379 }
1380
1381 error = snd_set_aci_init_values(miro);
1382 if (error < 0)
1383 return error;
1384
1385 return snd_card_register(card);
1386}
1387
1388static int snd_miro_isa_match(struct device *devptr, unsigned int n)
1389{
1390#ifdef CONFIG_PNP
1391 if (snd_miro_pnp_is_probed)
1392 return 0;
1393 if (isapnp)
1394 return 0;
1395#endif
1396 return 1;
1397}
1398
1399static int snd_miro_isa_probe(struct device *devptr, unsigned int n)
1400{
1401 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1402 static long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1403 static int possible_irqs[] = {11, 9, 10, 7, -1};
1404 static int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1405 static int possible_dma1s[] = {3, 1, 0, -1};
1406 static int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
1407 {0, -1} };
1408
1409 int error;
1410 struct snd_miro *miro;
1411 struct snd_card *card;
1412
1413 error = snd_card_new(devptr, index, id, THIS_MODULE,
1414 sizeof(struct snd_miro), &card);
1415 if (error < 0)
1416 return error;
1417
1418 card->private_free = snd_card_miro_free;
1419 miro = card->private_data;
1420
1421 error = snd_card_miro_detect(card, miro);
1422 if (error < 0) {
1423 snd_card_free(card);
1424 snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
1425 return -ENODEV;
1426 }
1427
1428 if (port == SNDRV_AUTO_PORT) {
1429 port = snd_legacy_find_free_ioport(possible_ports, 4);
1430 if (port < 0) {
1431 snd_card_free(card);
1432 snd_printk(KERN_ERR "unable to find a free WSS port\n");
1433 return -EBUSY;
1434 }
1435 }
1436
1437 if (mpu_port == SNDRV_AUTO_PORT) {
1438 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
1439 if (mpu_port < 0) {
1440 snd_card_free(card);
1441 snd_printk(KERN_ERR
1442 "unable to find a free MPU401 port\n");
1443 return -EBUSY;
1444 }
1445 }
1446
1447 if (irq == SNDRV_AUTO_IRQ) {
1448 irq = snd_legacy_find_free_irq(possible_irqs);
1449 if (irq < 0) {
1450 snd_card_free(card);
1451 snd_printk(KERN_ERR "unable to find a free IRQ\n");
1452 return -EBUSY;
1453 }
1454 }
1455 if (mpu_irq == SNDRV_AUTO_IRQ) {
1456 mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
1457 if (mpu_irq < 0) {
1458 snd_card_free(card);
1459 snd_printk(KERN_ERR
1460 "unable to find a free MPU401 IRQ\n");
1461 return -EBUSY;
1462 }
1463 }
1464 if (dma1 == SNDRV_AUTO_DMA) {
1465 dma1 = snd_legacy_find_free_dma(possible_dma1s);
1466 if (dma1 < 0) {
1467 snd_card_free(card);
1468 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1469 return -EBUSY;
1470 }
1471 }
1472 if (dma2 == SNDRV_AUTO_DMA) {
1473 dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
1474 if (dma2 < 0) {
1475 snd_card_free(card);
1476 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1477 return -EBUSY;
1478 }
1479 }
1480
1481 error = snd_miro_probe(card);
1482 if (error < 0) {
1483 snd_card_free(card);
1484 return error;
1485 }
1486
1487 dev_set_drvdata(devptr, card);
1488 return 0;
1489}
1490
1491static int snd_miro_isa_remove(struct device *devptr,
1492 unsigned int dev)
1493{
1494 snd_card_free(dev_get_drvdata(devptr));
1495 return 0;
1496}
1497
1498#define DEV_NAME "miro"
1499
1500static struct isa_driver snd_miro_driver = {
1501 .match = snd_miro_isa_match,
1502 .probe = snd_miro_isa_probe,
1503 .remove = snd_miro_isa_remove,
1504 /* FIXME: suspend/resume */
1505 .driver = {
1506 .name = DEV_NAME
1507 },
1508};
1509
1510#ifdef CONFIG_PNP
1511
1512static int snd_card_miro_pnp(struct snd_miro *chip,
1513 struct pnp_card_link *card,
1514 const struct pnp_card_device_id *pid)
1515{
1516 struct pnp_dev *pdev;
1517 int err;
1518 struct pnp_dev *devmpu;
1519 struct pnp_dev *devmc;
1520
1521 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1522 if (pdev == NULL)
1523 return -EBUSY;
1524
1525 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1526 if (devmpu == NULL)
1527 return -EBUSY;
1528
1529 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
1530 if (devmc == NULL)
1531 return -EBUSY;
1532
1533 err = pnp_activate_dev(pdev);
1534 if (err < 0) {
1535 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1536 return err;
1537 }
1538
1539 err = pnp_activate_dev(devmc);
1540 if (err < 0) {
1541 snd_printk(KERN_ERR "MC pnp configure failure: %d\n",
1542 err);
1543 return err;
1544 }
1545
1546 port = pnp_port_start(pdev, 1);
1547 fm_port = pnp_port_start(pdev, 2) + 8;
1548
1549 /*
1550 * The MC(0) is never accessed and the miroSOUND PCM20 card does not
1551 * include it in the PnP resource range. OPTI93x include it.
1552 */
1553 chip->mc_base = pnp_port_start(devmc, 0) - 1;
1554 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
1555
1556 irq = pnp_irq(pdev, 0);
1557 dma1 = pnp_dma(pdev, 0);
1558 dma2 = pnp_dma(pdev, 1);
1559
1560 if (mpu_port > 0) {
1561 err = pnp_activate_dev(devmpu);
1562 if (err < 0) {
1563 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
1564 mpu_port = -1;
1565 return err;
1566 }
1567 mpu_port = pnp_port_start(devmpu, 0);
1568 mpu_irq = pnp_irq(devmpu, 0);
1569 }
1570 return 0;
1571}
1572
1573static int snd_miro_pnp_probe(struct pnp_card_link *pcard,
1574 const struct pnp_card_device_id *pid)
1575{
1576 struct snd_card *card;
1577 int err;
1578 struct snd_miro *miro;
1579
1580 if (snd_miro_pnp_is_probed)
1581 return -EBUSY;
1582 if (!isapnp)
1583 return -ENODEV;
1584 err = snd_card_new(&pcard->card->dev, index, id, THIS_MODULE,
1585 sizeof(struct snd_miro), &card);
1586 if (err < 0)
1587 return err;
1588
1589 card->private_free = snd_card_miro_free;
1590 miro = card->private_data;
1591
1592 err = snd_card_miro_pnp(miro, pcard, pid);
1593 if (err) {
1594 snd_card_free(card);
1595 return err;
1596 }
1597
1598 /* only miroSOUND PCM20 and PCM12 == OPTi924 */
1599 err = snd_miro_init(miro, OPTi9XX_HW_82C924);
1600 if (err) {
1601 snd_card_free(card);
1602 return err;
1603 }
1604
1605 err = snd_miro_opti_check(miro);
1606 if (err) {
1607 snd_printk(KERN_ERR "OPTI chip not found\n");
1608 snd_card_free(card);
1609 return err;
1610 }
1611
1612 err = snd_miro_probe(card);
1613 if (err < 0) {
1614 snd_card_free(card);
1615 return err;
1616 }
1617 pnp_set_card_drvdata(pcard, card);
1618 snd_miro_pnp_is_probed = 1;
1619 return 0;
1620}
1621
1622static void snd_miro_pnp_remove(struct pnp_card_link *pcard)
1623{
1624 snd_card_free(pnp_get_card_drvdata(pcard));
1625 pnp_set_card_drvdata(pcard, NULL);
1626 snd_miro_pnp_is_probed = 0;
1627}
1628
1629static struct pnp_card_driver miro_pnpc_driver = {
1630 .flags = PNP_DRIVER_RES_DISABLE,
1631 .name = "miro",
1632 .id_table = snd_miro_pnpids,
1633 .probe = snd_miro_pnp_probe,
1634 .remove = snd_miro_pnp_remove,
1635};
1636#endif
1637
1638static int __init alsa_card_miro_init(void)
1639{
1640#ifdef CONFIG_PNP
1641 pnp_register_card_driver(&miro_pnpc_driver);
1642 if (snd_miro_pnp_is_probed)
1643 return 0;
1644 pnp_unregister_card_driver(&miro_pnpc_driver);
1645#endif
1646 return isa_register_driver(&snd_miro_driver, 1);
1647}
1648
1649static void __exit alsa_card_miro_exit(void)
1650{
1651 if (!snd_miro_pnp_is_probed) {
1652 isa_unregister_driver(&snd_miro_driver);
1653 return;
1654 }
1655#ifdef CONFIG_PNP
1656 pnp_unregister_card_driver(&miro_pnpc_driver);
1657#endif
1658}
1659
1660module_init(alsa_card_miro_init)
1661module_exit(alsa_card_miro_exit)