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