1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/isa.h>
29#include <linux/delay.h>
30#include <linux/pnp.h>
31#include <linux/module.h>
32#include <linux/io.h>
33#include <asm/dma.h>
34#include <sound/core.h>
35#include <sound/tlv.h>
36#include <sound/wss.h>
37#include <sound/mpu401.h>
38#include <sound/opl3.h>
39#ifndef OPTi93X
40#include <sound/opl4.h>
41#endif
42#define SNDRV_LEGACY_FIND_FREE_IOPORT
43#define SNDRV_LEGACY_FIND_FREE_IRQ
44#define SNDRV_LEGACY_FIND_FREE_DMA
45#include <sound/initval.h>
46
47MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
48MODULE_LICENSE("GPL");
49#ifdef OPTi93X
50MODULE_DESCRIPTION("OPTi93X");
51MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
52#else
53#ifdef CS4231
54MODULE_DESCRIPTION("OPTi92X - CS4231");
55MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
56 "{OPTi,82C925 (CS4231)}}");
57#else
58MODULE_DESCRIPTION("OPTi92X - AD1848");
59MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
60 "{OPTi,82C925 (AD1848)},"
61 "{OAK,Mozart}}");
62#endif
63#endif
64
65static int index = SNDRV_DEFAULT_IDX1;
66static char *id = SNDRV_DEFAULT_STR1;
67
68#ifdef CONFIG_PNP
69static bool isapnp = true;
70#endif
71static long port = SNDRV_DEFAULT_PORT1;
72static long mpu_port = SNDRV_DEFAULT_PORT1;
73static long fm_port = SNDRV_DEFAULT_PORT1;
74static int irq = SNDRV_DEFAULT_IRQ1;
75static int mpu_irq = SNDRV_DEFAULT_IRQ1;
76static int dma1 = SNDRV_DEFAULT_DMA1;
77#if defined(CS4231) || defined(OPTi93X)
78static int dma2 = SNDRV_DEFAULT_DMA1;
79#endif
80
81module_param(index, int, 0444);
82MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
83module_param(id, charp, 0444);
84MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
85
86
87#ifdef CONFIG_PNP
88module_param(isapnp, bool, 0444);
89MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
90#endif
91module_param_hw(port, long, ioport, 0444);
92MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
93module_param_hw(mpu_port, long, ioport, 0444);
94MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
95module_param_hw(fm_port, long, ioport, 0444);
96MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
97module_param_hw(irq, int, irq, 0444);
98MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
99module_param_hw(mpu_irq, int, irq, 0444);
100MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
101module_param_hw(dma1, int, dma, 0444);
102MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
103#if defined(CS4231) || defined(OPTi93X)
104module_param_hw(dma2, int, dma, 0444);
105MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
106#endif
107
108#define OPTi9XX_HW_82C928 1
109#define OPTi9XX_HW_82C929 2
110#define OPTi9XX_HW_82C924 3
111#define OPTi9XX_HW_82C925 4
112#define OPTi9XX_HW_82C930 5
113#define OPTi9XX_HW_82C931 6
114#define OPTi9XX_HW_82C933 7
115#define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
116
117#define OPTi9XX_MC_REG(n) n
118
119#ifdef OPTi93X
120
121#define OPTi93X_STATUS 0x02
122#define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
123
124#define OPTi93X_IRQ_PLAYBACK 0x04
125#define OPTi93X_IRQ_CAPTURE 0x08
126
127#endif
128
129struct snd_opti9xx {
130 unsigned short hardware;
131 unsigned char password;
132 char name[7];
133
134 unsigned long mc_base;
135 struct resource *res_mc_base;
136 unsigned long mc_base_size;
137#ifdef OPTi93X
138 unsigned long mc_indir_index;
139 struct resource *res_mc_indir;
140#endif
141 struct snd_wss *codec;
142 unsigned long pwd_reg;
143
144 spinlock_t lock;
145
146 long wss_base;
147 int irq;
148};
149
150static int snd_opti9xx_pnp_is_probed;
151
152#ifdef CONFIG_PNP
153
154static const struct pnp_card_device_id snd_opti9xx_pnpids[] = {
155#ifndef OPTi93X
156
157 { .id = "OPT0924",
158 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } },
159 .driver_data = 0x0924 },
160
161 { .id = "OPT0925",
162 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } },
163 .driver_data = 0x0925 },
164#else
165
166 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } },
167 .driver_data = 0x0931 },
168#endif
169 { .id = "" }
170};
171
172MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
173
174#endif
175
176#define DEV_NAME KBUILD_MODNAME
177
178static char * snd_opti9xx_names[] = {
179 "unknown",
180 "82C928", "82C929",
181 "82C924", "82C925",
182 "82C930", "82C931", "82C933"
183};
184
185static int snd_opti9xx_init(struct snd_opti9xx *chip,
186 unsigned short hardware)
187{
188 static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
189
190 chip->hardware = hardware;
191 strcpy(chip->name, snd_opti9xx_names[hardware]);
192
193 spin_lock_init(&chip->lock);
194
195 chip->irq = -1;
196
197#ifndef OPTi93X
198#ifdef CONFIG_PNP
199 if (isapnp && chip->mc_base)
200
201 chip->mc_base |= 0xc00;
202 else
203#endif
204 {
205 chip->mc_base = 0xf8c;
206 chip->mc_base_size = opti9xx_mc_size[hardware];
207 }
208#else
209 chip->mc_base_size = opti9xx_mc_size[hardware];
210#endif
211
212 switch (hardware) {
213#ifndef OPTi93X
214 case OPTi9XX_HW_82C928:
215 case OPTi9XX_HW_82C929:
216 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
217 chip->pwd_reg = 3;
218 break;
219
220 case OPTi9XX_HW_82C924:
221 case OPTi9XX_HW_82C925:
222 chip->password = 0xe5;
223 chip->pwd_reg = 3;
224 break;
225#else
226
227 case OPTi9XX_HW_82C930:
228 case OPTi9XX_HW_82C931:
229 case OPTi9XX_HW_82C933:
230 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
231 if (!chip->mc_indir_index)
232 chip->mc_indir_index = 0xe0e;
233 chip->password = 0xe4;
234 chip->pwd_reg = 0;
235 break;
236#endif
237
238 default:
239 snd_printk(KERN_ERR "chip %d not supported\n", hardware);
240 return -ENODEV;
241 }
242 return 0;
243}
244
245static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
246 unsigned char reg)
247{
248 unsigned long flags;
249 unsigned char retval = 0xff;
250
251 spin_lock_irqsave(&chip->lock, flags);
252 outb(chip->password, chip->mc_base + chip->pwd_reg);
253
254 switch (chip->hardware) {
255#ifndef OPTi93X
256 case OPTi9XX_HW_82C924:
257 case OPTi9XX_HW_82C925:
258 if (reg > 7) {
259 outb(reg, chip->mc_base + 8);
260 outb(chip->password, chip->mc_base + chip->pwd_reg);
261 retval = inb(chip->mc_base + 9);
262 break;
263 }
264
265
266 case OPTi9XX_HW_82C928:
267 case OPTi9XX_HW_82C929:
268 retval = inb(chip->mc_base + reg);
269 break;
270#else
271
272 case OPTi9XX_HW_82C930:
273 case OPTi9XX_HW_82C931:
274 case OPTi9XX_HW_82C933:
275 outb(reg, chip->mc_indir_index);
276 outb(chip->password, chip->mc_base + chip->pwd_reg);
277 retval = inb(chip->mc_indir_index + 1);
278 break;
279#endif
280
281 default:
282 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
283 }
284
285 spin_unlock_irqrestore(&chip->lock, flags);
286 return retval;
287}
288
289static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
290 unsigned char value)
291{
292 unsigned long flags;
293
294 spin_lock_irqsave(&chip->lock, flags);
295 outb(chip->password, chip->mc_base + chip->pwd_reg);
296
297 switch (chip->hardware) {
298#ifndef OPTi93X
299 case OPTi9XX_HW_82C924:
300 case OPTi9XX_HW_82C925:
301 if (reg > 7) {
302 outb(reg, chip->mc_base + 8);
303 outb(chip->password, chip->mc_base + chip->pwd_reg);
304 outb(value, chip->mc_base + 9);
305 break;
306 }
307
308
309 case OPTi9XX_HW_82C928:
310 case OPTi9XX_HW_82C929:
311 outb(value, chip->mc_base + reg);
312 break;
313#else
314
315 case OPTi9XX_HW_82C930:
316 case OPTi9XX_HW_82C931:
317 case OPTi9XX_HW_82C933:
318 outb(reg, chip->mc_indir_index);
319 outb(chip->password, chip->mc_base + chip->pwd_reg);
320 outb(value, chip->mc_indir_index + 1);
321 break;
322#endif
323
324 default:
325 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
326 }
327
328 spin_unlock_irqrestore(&chip->lock, flags);
329}
330
331
332#define snd_opti9xx_write_mask(chip, reg, value, mask) \
333 snd_opti9xx_write(chip, reg, \
334 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
335
336
337static int snd_opti9xx_configure(struct snd_opti9xx *chip,
338 long port,
339 int irq, int dma1, int dma2,
340 long mpu_port, int mpu_irq)
341{
342 unsigned char wss_base_bits;
343 unsigned char irq_bits;
344 unsigned char dma_bits;
345 unsigned char mpu_port_bits = 0;
346 unsigned char mpu_irq_bits;
347
348 switch (chip->hardware) {
349#ifndef OPTi93X
350 case OPTi9XX_HW_82C924:
351
352 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
353
354 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
355
356
357 case OPTi9XX_HW_82C925:
358
359 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
360
361 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
362
363 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
364#ifdef CS4231
365
366 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
367#else
368
369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
370#endif
371 break;
372
373 case OPTi9XX_HW_82C928:
374 case OPTi9XX_HW_82C929:
375 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
377
378
379
380 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
381#ifdef CS4231
382 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
383#else
384 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
385#endif
386 break;
387
388#else
389 case OPTi9XX_HW_82C931:
390
391 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
392
393 case OPTi9XX_HW_82C933:
394
395
396
397
398 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
399
400
401
402
403 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
404
405 case OPTi9XX_HW_82C930:
406 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
407 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
408 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
409 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
410 0x34);
411 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
412 break;
413#endif
414
415 default:
416 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
417 return -EINVAL;
418 }
419
420
421 switch (port & 0x3ff) {
422 case 0x130:
423 chip->wss_base = 0x530;
424 wss_base_bits = 0x00;
425 break;
426 case 0x204:
427 chip->wss_base = 0x604;
428 wss_base_bits = 0x03;
429 break;
430 case 0x280:
431 chip->wss_base = 0xe80;
432 wss_base_bits = 0x01;
433 break;
434 case 0x340:
435 chip->wss_base = 0xf40;
436 wss_base_bits = 0x02;
437 break;
438 default:
439 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
440 goto __skip_base;
441 }
442 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
443
444__skip_base:
445 switch (irq) {
446
447 case 5:
448 irq_bits = 0x05;
449 break;
450
451 case 7:
452 irq_bits = 0x01;
453 break;
454 case 9:
455 irq_bits = 0x02;
456 break;
457 case 10:
458 irq_bits = 0x03;
459 break;
460 case 11:
461 irq_bits = 0x04;
462 break;
463 default:
464 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
465 goto __skip_resources;
466 }
467
468 switch (dma1) {
469 case 0:
470 dma_bits = 0x01;
471 break;
472 case 1:
473 dma_bits = 0x02;
474 break;
475 case 3:
476 dma_bits = 0x03;
477 break;
478 default:
479 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
480 goto __skip_resources;
481 }
482
483#if defined(CS4231) || defined(OPTi93X)
484 if (dma1 == dma2) {
485 snd_printk(KERN_ERR "don't want to share dmas\n");
486 return -EBUSY;
487 }
488
489 switch (dma2) {
490 case 0:
491 case 1:
492 break;
493 default:
494 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
495 goto __skip_resources;
496 }
497 dma_bits |= 0x04;
498#endif
499
500#ifndef OPTi93X
501 outb(irq_bits << 3 | dma_bits, chip->wss_base);
502#else
503 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
504#endif
505
506__skip_resources:
507 if (chip->hardware > OPTi9XX_HW_82C928) {
508 switch (mpu_port) {
509 case 0:
510 case -1:
511 break;
512 case 0x300:
513 mpu_port_bits = 0x03;
514 break;
515 case 0x310:
516 mpu_port_bits = 0x02;
517 break;
518 case 0x320:
519 mpu_port_bits = 0x01;
520 break;
521 case 0x330:
522 mpu_port_bits = 0x00;
523 break;
524 default:
525 snd_printk(KERN_WARNING
526 "MPU-401 port 0x%lx not valid\n", mpu_port);
527 goto __skip_mpu;
528 }
529
530 switch (mpu_irq) {
531 case 5:
532 mpu_irq_bits = 0x02;
533 break;
534 case 7:
535 mpu_irq_bits = 0x03;
536 break;
537 case 9:
538 mpu_irq_bits = 0x00;
539 break;
540 case 10:
541 mpu_irq_bits = 0x01;
542 break;
543 default:
544 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
545 mpu_irq);
546 goto __skip_mpu;
547 }
548
549 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
550 (mpu_port <= 0) ? 0x00 :
551 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
552 0xf8);
553 }
554__skip_mpu:
555
556 return 0;
557}
558
559#ifdef OPTi93X
560
561static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
562static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
563static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
564
565static struct snd_kcontrol_new snd_opti93x_controls[] = {
566WSS_DOUBLE("Master Playback Switch", 0,
567 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
568WSS_DOUBLE_TLV("Master Playback Volume", 0,
569 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
570 db_scale_5bit_3db_step),
571WSS_DOUBLE_TLV("PCM Playback Volume", 0,
572 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
573 db_scale_5bit),
574WSS_DOUBLE_TLV("FM Playback Volume", 0,
575 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
576 db_scale_4bit_12db_max),
577WSS_DOUBLE("Line Playback Switch", 0,
578 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
579WSS_DOUBLE_TLV("Line Playback Volume", 0,
580 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
581 db_scale_4bit_12db_max),
582WSS_DOUBLE("Mic Playback Switch", 0,
583 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
584WSS_DOUBLE_TLV("Mic Playback Volume", 0,
585 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
586 db_scale_4bit_12db_max),
587WSS_DOUBLE_TLV("CD Playback Volume", 0,
588 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
589 db_scale_4bit_12db_max),
590WSS_DOUBLE("Aux Playback Switch", 0,
591 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
592WSS_DOUBLE_TLV("Aux Playback Volume", 0,
593 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
594 db_scale_4bit_12db_max),
595};
596
597static int snd_opti93x_mixer(struct snd_wss *chip)
598{
599 struct snd_card *card;
600 unsigned int idx;
601 struct snd_ctl_elem_id id1, id2;
602 int err;
603
604 if (snd_BUG_ON(!chip || !chip->pcm))
605 return -EINVAL;
606
607 card = chip->card;
608
609 strcpy(card->mixername, chip->pcm->name);
610
611 memset(&id1, 0, sizeof(id1));
612 memset(&id2, 0, sizeof(id2));
613 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
614
615 strcpy(id1.name, "Aux Playback Switch");
616 strcpy(id2.name, "CD Playback Switch");
617 err = snd_ctl_rename_id(card, &id1, &id2);
618 if (err < 0) {
619 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
620 return err;
621 }
622
623 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
624 strcpy(id2.name, "FM Playback Switch");
625 err = snd_ctl_rename_id(card, &id1, &id2);
626 if (err < 0) {
627 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
628 return err;
629 }
630
631 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
632 snd_ctl_remove_id(card, &id1);
633
634
635 id1.index = 0;
636 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
637 strcpy(id1.name, snd_opti93x_controls[idx].name);
638 snd_ctl_remove_id(card, &id1);
639
640 err = snd_ctl_add(card,
641 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
642 if (err < 0)
643 return err;
644 }
645 return 0;
646}
647
648static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
649{
650 struct snd_opti9xx *chip = dev_id;
651 struct snd_wss *codec = chip->codec;
652 unsigned char status;
653
654 if (!codec)
655 return IRQ_HANDLED;
656
657 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
658 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
659 snd_pcm_period_elapsed(codec->playback_substream);
660 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
661 snd_wss_overrange(codec);
662 snd_pcm_period_elapsed(codec->capture_substream);
663 }
664 outb(0x00, OPTi93X_PORT(codec, STATUS));
665 return IRQ_HANDLED;
666}
667
668#endif
669
670static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
671{
672 unsigned char value;
673#ifdef OPTi93X
674 unsigned long flags;
675#endif
676
677 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
678 "OPTi9xx MC");
679 if (chip->res_mc_base == NULL)
680 return -EBUSY;
681#ifndef OPTi93X
682 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
683 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
684 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
685 return 0;
686#else
687 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
688 "OPTi93x MC");
689 if (chip->res_mc_indir == NULL)
690 return -EBUSY;
691
692 spin_lock_irqsave(&chip->lock, flags);
693 outb(chip->password, chip->mc_base + chip->pwd_reg);
694 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
695 spin_unlock_irqrestore(&chip->lock, flags);
696
697 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
698 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
699 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
700 return 0;
701
702 release_and_free_resource(chip->res_mc_indir);
703 chip->res_mc_indir = NULL;
704#endif
705 release_and_free_resource(chip->res_mc_base);
706 chip->res_mc_base = NULL;
707
708 return -ENODEV;
709}
710
711static int snd_card_opti9xx_detect(struct snd_card *card,
712 struct snd_opti9xx *chip)
713{
714 int i, err;
715
716#ifndef OPTi93X
717 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
718#else
719 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
720#endif
721 err = snd_opti9xx_init(chip, i);
722 if (err < 0)
723 return err;
724
725 err = snd_opti9xx_read_check(chip);
726 if (err == 0)
727 return 1;
728#ifdef OPTi93X
729 chip->mc_indir_index = 0;
730#endif
731 }
732 return -ENODEV;
733}
734
735#ifdef CONFIG_PNP
736static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
737 struct pnp_card_link *card,
738 const struct pnp_card_device_id *pid)
739{
740 struct pnp_dev *pdev;
741 int err;
742 struct pnp_dev *devmpu;
743#ifndef OPTi93X
744 struct pnp_dev *devmc;
745#endif
746
747 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
748 if (pdev == NULL)
749 return -EBUSY;
750
751 err = pnp_activate_dev(pdev);
752 if (err < 0) {
753 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
754 return err;
755 }
756
757#ifdef OPTi93X
758 port = pnp_port_start(pdev, 0) - 4;
759 fm_port = pnp_port_start(pdev, 1) + 8;
760
761
762 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
763#else
764 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
765 if (devmc == NULL)
766 return -EBUSY;
767
768 err = pnp_activate_dev(devmc);
769 if (err < 0) {
770 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
771 return err;
772 }
773
774 port = pnp_port_start(pdev, 1);
775 fm_port = pnp_port_start(pdev, 2) + 8;
776
777
778
779
780 chip->mc_base = pnp_port_start(devmc, 0) - 1;
781 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
782#endif
783 irq = pnp_irq(pdev, 0);
784 dma1 = pnp_dma(pdev, 0);
785#if defined(CS4231) || defined(OPTi93X)
786 dma2 = pnp_dma(pdev, 1);
787#endif
788
789 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
790
791 if (devmpu && mpu_port > 0) {
792 err = pnp_activate_dev(devmpu);
793 if (err < 0) {
794 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
795 mpu_port = -1;
796 } else {
797 mpu_port = pnp_port_start(devmpu, 0);
798 mpu_irq = pnp_irq(devmpu, 0);
799 }
800 }
801 return pid->driver_data;
802}
803#endif
804
805static void snd_card_opti9xx_free(struct snd_card *card)
806{
807 struct snd_opti9xx *chip = card->private_data;
808
809 if (chip) {
810#ifdef OPTi93X
811 if (chip->irq > 0) {
812 disable_irq(chip->irq);
813 free_irq(chip->irq, chip);
814 }
815 release_and_free_resource(chip->res_mc_indir);
816#endif
817 release_and_free_resource(chip->res_mc_base);
818 }
819}
820
821static int snd_opti9xx_probe(struct snd_card *card)
822{
823 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
824 int error;
825 int xdma2;
826 struct snd_opti9xx *chip = card->private_data;
827 struct snd_wss *codec;
828 struct snd_rawmidi *rmidi;
829 struct snd_hwdep *synth;
830
831#if defined(CS4231) || defined(OPTi93X)
832 xdma2 = dma2;
833#else
834 xdma2 = -1;
835#endif
836
837 if (port == SNDRV_AUTO_PORT) {
838 port = snd_legacy_find_free_ioport(possible_ports, 4);
839 if (port < 0) {
840 snd_printk(KERN_ERR "unable to find a free WSS port\n");
841 return -EBUSY;
842 }
843 }
844 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
845 mpu_port, mpu_irq);
846 if (error)
847 return error;
848
849 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
850#ifdef OPTi93X
851 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
852#else
853 WSS_HW_DETECT, 0,
854#endif
855 &codec);
856 if (error < 0)
857 return error;
858 chip->codec = codec;
859 error = snd_wss_pcm(codec, 0);
860 if (error < 0)
861 return error;
862 error = snd_wss_mixer(codec);
863 if (error < 0)
864 return error;
865#ifdef OPTi93X
866 error = snd_opti93x_mixer(codec);
867 if (error < 0)
868 return error;
869#endif
870#ifdef CS4231
871 error = snd_wss_timer(codec, 0);
872 if (error < 0)
873 return error;
874#endif
875#ifdef OPTi93X
876 error = request_irq(irq, snd_opti93x_interrupt,
877 0, DEV_NAME" - WSS", chip);
878 if (error < 0) {
879 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
880 return error;
881 }
882#endif
883 chip->irq = irq;
884 strcpy(card->driver, chip->name);
885 sprintf(card->shortname, "OPTi %s", card->driver);
886#if defined(CS4231) || defined(OPTi93X)
887 snprintf(card->longname, sizeof(card->longname),
888 "%s, %s at 0x%lx, irq %d, dma %d&%d",
889 card->shortname, codec->pcm->name,
890 chip->wss_base + 4, irq, dma1, xdma2);
891#else
892 snprintf(card->longname, sizeof(card->longname),
893 "%s, %s at 0x%lx, irq %d, dma %d",
894 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
895 dma1);
896#endif
897
898 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
899 rmidi = NULL;
900 else {
901 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
902 mpu_port, 0, mpu_irq, &rmidi);
903 if (error)
904 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
905 mpu_port);
906 }
907
908 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
909 struct snd_opl3 *opl3 = NULL;
910#ifndef OPTi93X
911 if (chip->hardware == OPTi9XX_HW_82C928 ||
912 chip->hardware == OPTi9XX_HW_82C929 ||
913 chip->hardware == OPTi9XX_HW_82C924) {
914 struct snd_opl4 *opl4;
915
916 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
917 0x20, 0x20);
918 if (snd_opl4_create(card, fm_port, fm_port - 8,
919 2, &opl3, &opl4) < 0) {
920
921 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
922 0x00, 0x20);
923 }
924 }
925#endif
926 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
927 OPL3_HW_AUTO, 0, &opl3) < 0) {
928 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
929 fm_port, fm_port + 4 - 1);
930 }
931 if (opl3) {
932 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
933 if (error < 0)
934 return error;
935 }
936 }
937
938 return snd_card_register(card);
939}
940
941static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
942{
943 struct snd_card *card;
944 int err;
945
946 err = snd_card_new(pdev, index, id, THIS_MODULE,
947 sizeof(struct snd_opti9xx), &card);
948 if (err < 0)
949 return err;
950 card->private_free = snd_card_opti9xx_free;
951 *cardp = card;
952 return 0;
953}
954
955static int snd_opti9xx_isa_match(struct device *devptr,
956 unsigned int dev)
957{
958#ifdef CONFIG_PNP
959 if (snd_opti9xx_pnp_is_probed)
960 return 0;
961 if (isapnp)
962 return 0;
963#endif
964 return 1;
965}
966
967static int snd_opti9xx_isa_probe(struct device *devptr,
968 unsigned int dev)
969{
970 struct snd_card *card;
971 int error;
972 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
973#ifdef OPTi93X
974 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
975#else
976 static int possible_irqs[] = {9, 10, 11, 7, -1};
977#endif
978 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
979 static int possible_dma1s[] = {3, 1, 0, -1};
980#if defined(CS4231) || defined(OPTi93X)
981 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
982#endif
983
984 if (mpu_port == SNDRV_AUTO_PORT) {
985 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
986 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
987 return -EBUSY;
988 }
989 }
990 if (irq == SNDRV_AUTO_IRQ) {
991 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
992 snd_printk(KERN_ERR "unable to find a free IRQ\n");
993 return -EBUSY;
994 }
995 }
996 if (mpu_irq == SNDRV_AUTO_IRQ) {
997 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
998 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
999 return -EBUSY;
1000 }
1001 }
1002 if (dma1 == SNDRV_AUTO_DMA) {
1003 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
1004 snd_printk(KERN_ERR "unable to find a free DMA1\n");
1005 return -EBUSY;
1006 }
1007 }
1008#if defined(CS4231) || defined(OPTi93X)
1009 if (dma2 == SNDRV_AUTO_DMA) {
1010 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
1011 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1012 return -EBUSY;
1013 }
1014 }
1015#endif
1016
1017 error = snd_opti9xx_card_new(devptr, &card);
1018 if (error < 0)
1019 return error;
1020
1021 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1022 snd_card_free(card);
1023 return error;
1024 }
1025 if ((error = snd_opti9xx_probe(card)) < 0) {
1026 snd_card_free(card);
1027 return error;
1028 }
1029 dev_set_drvdata(devptr, card);
1030 return 0;
1031}
1032
1033static int snd_opti9xx_isa_remove(struct device *devptr,
1034 unsigned int dev)
1035{
1036 snd_card_free(dev_get_drvdata(devptr));
1037 return 0;
1038}
1039
1040#ifdef CONFIG_PM
1041static int snd_opti9xx_suspend(struct snd_card *card)
1042{
1043 struct snd_opti9xx *chip = card->private_data;
1044
1045 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1046 chip->codec->suspend(chip->codec);
1047 return 0;
1048}
1049
1050static int snd_opti9xx_resume(struct snd_card *card)
1051{
1052 struct snd_opti9xx *chip = card->private_data;
1053 int error, xdma2;
1054#if defined(CS4231) || defined(OPTi93X)
1055 xdma2 = dma2;
1056#else
1057 xdma2 = -1;
1058#endif
1059
1060 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1061 mpu_port, mpu_irq);
1062 if (error)
1063 return error;
1064 chip->codec->resume(chip->codec);
1065 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1066 return 0;
1067}
1068
1069static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1070 pm_message_t state)
1071{
1072 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1073}
1074
1075static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1076{
1077 return snd_opti9xx_resume(dev_get_drvdata(dev));
1078}
1079#endif
1080
1081static struct isa_driver snd_opti9xx_driver = {
1082 .match = snd_opti9xx_isa_match,
1083 .probe = snd_opti9xx_isa_probe,
1084 .remove = snd_opti9xx_isa_remove,
1085#ifdef CONFIG_PM
1086 .suspend = snd_opti9xx_isa_suspend,
1087 .resume = snd_opti9xx_isa_resume,
1088#endif
1089 .driver = {
1090 .name = DEV_NAME
1091 },
1092};
1093
1094#ifdef CONFIG_PNP
1095static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1096 const struct pnp_card_device_id *pid)
1097{
1098 struct snd_card *card;
1099 int error, hw;
1100 struct snd_opti9xx *chip;
1101
1102 if (snd_opti9xx_pnp_is_probed)
1103 return -EBUSY;
1104 if (! isapnp)
1105 return -ENODEV;
1106 error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1107 if (error < 0)
1108 return error;
1109 chip = card->private_data;
1110
1111 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1112 switch (hw) {
1113 case 0x0924:
1114 hw = OPTi9XX_HW_82C924;
1115 break;
1116 case 0x0925:
1117 hw = OPTi9XX_HW_82C925;
1118 break;
1119 case 0x0931:
1120 hw = OPTi9XX_HW_82C931;
1121 break;
1122 default:
1123 snd_card_free(card);
1124 return -ENODEV;
1125 }
1126
1127 if ((error = snd_opti9xx_init(chip, hw))) {
1128 snd_card_free(card);
1129 return error;
1130 }
1131 error = snd_opti9xx_read_check(chip);
1132 if (error) {
1133 snd_printk(KERN_ERR "OPTI chip not found\n");
1134 snd_card_free(card);
1135 return error;
1136 }
1137 if ((error = snd_opti9xx_probe(card)) < 0) {
1138 snd_card_free(card);
1139 return error;
1140 }
1141 pnp_set_card_drvdata(pcard, card);
1142 snd_opti9xx_pnp_is_probed = 1;
1143 return 0;
1144}
1145
1146static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1147{
1148 snd_card_free(pnp_get_card_drvdata(pcard));
1149 pnp_set_card_drvdata(pcard, NULL);
1150 snd_opti9xx_pnp_is_probed = 0;
1151}
1152
1153#ifdef CONFIG_PM
1154static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1155 pm_message_t state)
1156{
1157 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1158}
1159
1160static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1161{
1162 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1163}
1164#endif
1165
1166static struct pnp_card_driver opti9xx_pnpc_driver = {
1167 .flags = PNP_DRIVER_RES_DISABLE,
1168 .name = DEV_NAME,
1169 .id_table = snd_opti9xx_pnpids,
1170 .probe = snd_opti9xx_pnp_probe,
1171 .remove = snd_opti9xx_pnp_remove,
1172#ifdef CONFIG_PM
1173 .suspend = snd_opti9xx_pnp_suspend,
1174 .resume = snd_opti9xx_pnp_resume,
1175#endif
1176};
1177#endif
1178
1179#ifdef OPTi93X
1180#define CHIP_NAME "82C93x"
1181#else
1182#define CHIP_NAME "82C92x"
1183#endif
1184
1185static int __init alsa_card_opti9xx_init(void)
1186{
1187#ifdef CONFIG_PNP
1188 pnp_register_card_driver(&opti9xx_pnpc_driver);
1189 if (snd_opti9xx_pnp_is_probed)
1190 return 0;
1191 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1192#endif
1193 return isa_register_driver(&snd_opti9xx_driver, 1);
1194}
1195
1196static void __exit alsa_card_opti9xx_exit(void)
1197{
1198 if (!snd_opti9xx_pnp_is_probed) {
1199 isa_unregister_driver(&snd_opti9xx_driver);
1200 return;
1201 }
1202#ifdef CONFIG_PNP
1203 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1204#endif
1205}
1206
1207module_init(alsa_card_opti9xx_init)
1208module_exit(alsa_card_opti9xx_exit)
1209