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 char * 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 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
320#define snd_opti9xx_write_mask(chip, reg, value, mask) \
321 snd_opti9xx_write(chip, reg, \
322 (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
323
324
325static int snd_opti9xx_configure(struct snd_opti9xx *chip,
326 long port,
327 int irq, int dma1, int dma2,
328 long mpu_port, int mpu_irq)
329{
330 unsigned char wss_base_bits;
331 unsigned char irq_bits;
332 unsigned char dma_bits;
333 unsigned char mpu_port_bits = 0;
334 unsigned char mpu_irq_bits;
335
336 switch (chip->hardware) {
337#ifndef OPTi93X
338 case OPTi9XX_HW_82C924:
339
340 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
341
342 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
343
344
345 case OPTi9XX_HW_82C925:
346
347 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
348
349 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
350
351 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
352#ifdef CS4231
353
354 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
355#else
356
357 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
358#endif
359 break;
360
361 case OPTi9XX_HW_82C928:
362 case OPTi9XX_HW_82C929:
363 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
364 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
365
366
367
368 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
369#ifdef CS4231
370 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
371#else
372 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
373#endif
374 break;
375
376#else
377 case OPTi9XX_HW_82C931:
378
379 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c);
380
381 case OPTi9XX_HW_82C933:
382
383
384
385
386 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
387
388
389
390
391 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
392
393 case OPTi9XX_HW_82C930:
394 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
396 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
397 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
398 0x34);
399 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
400 break;
401#endif
402
403 default:
404 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
405 return -EINVAL;
406 }
407
408
409 switch (port & 0x3ff) {
410 case 0x130:
411 chip->wss_base = 0x530;
412 wss_base_bits = 0x00;
413 break;
414 case 0x204:
415 chip->wss_base = 0x604;
416 wss_base_bits = 0x03;
417 break;
418 case 0x280:
419 chip->wss_base = 0xe80;
420 wss_base_bits = 0x01;
421 break;
422 case 0x340:
423 chip->wss_base = 0xf40;
424 wss_base_bits = 0x02;
425 break;
426 default:
427 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port);
428 goto __skip_base;
429 }
430 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
431
432__skip_base:
433 switch (irq) {
434
435 case 5:
436 irq_bits = 0x05;
437 break;
438
439 case 7:
440 irq_bits = 0x01;
441 break;
442 case 9:
443 irq_bits = 0x02;
444 break;
445 case 10:
446 irq_bits = 0x03;
447 break;
448 case 11:
449 irq_bits = 0x04;
450 break;
451 default:
452 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
453 goto __skip_resources;
454 }
455
456 switch (dma1) {
457 case 0:
458 dma_bits = 0x01;
459 break;
460 case 1:
461 dma_bits = 0x02;
462 break;
463 case 3:
464 dma_bits = 0x03;
465 break;
466 default:
467 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
468 goto __skip_resources;
469 }
470
471#if defined(CS4231) || defined(OPTi93X)
472 if (dma1 == dma2) {
473 snd_printk(KERN_ERR "don't want to share dmas\n");
474 return -EBUSY;
475 }
476
477 switch (dma2) {
478 case 0:
479 case 1:
480 break;
481 default:
482 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
483 goto __skip_resources;
484 }
485 dma_bits |= 0x04;
486#endif
487
488#ifndef OPTi93X
489 outb(irq_bits << 3 | dma_bits, chip->wss_base);
490#else
491 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
492#endif
493
494__skip_resources:
495 if (chip->hardware > OPTi9XX_HW_82C928) {
496 switch (mpu_port) {
497 case 0:
498 case -1:
499 break;
500 case 0x300:
501 mpu_port_bits = 0x03;
502 break;
503 case 0x310:
504 mpu_port_bits = 0x02;
505 break;
506 case 0x320:
507 mpu_port_bits = 0x01;
508 break;
509 case 0x330:
510 mpu_port_bits = 0x00;
511 break;
512 default:
513 snd_printk(KERN_WARNING
514 "MPU-401 port 0x%lx not valid\n", mpu_port);
515 goto __skip_mpu;
516 }
517
518 switch (mpu_irq) {
519 case 5:
520 mpu_irq_bits = 0x02;
521 break;
522 case 7:
523 mpu_irq_bits = 0x03;
524 break;
525 case 9:
526 mpu_irq_bits = 0x00;
527 break;
528 case 10:
529 mpu_irq_bits = 0x01;
530 break;
531 default:
532 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
533 mpu_irq);
534 goto __skip_mpu;
535 }
536
537 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
538 (mpu_port <= 0) ? 0x00 :
539 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
540 0xf8);
541 }
542__skip_mpu:
543
544 return 0;
545}
546
547#ifdef OPTi93X
548
549static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
550static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
551static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
552
553static struct snd_kcontrol_new snd_opti93x_controls[] = {
554WSS_DOUBLE("Master Playback Switch", 0,
555 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
556WSS_DOUBLE_TLV("Master Playback Volume", 0,
557 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
558 db_scale_5bit_3db_step),
559WSS_DOUBLE_TLV("PCM Playback Volume", 0,
560 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
561 db_scale_5bit),
562WSS_DOUBLE_TLV("FM Playback Volume", 0,
563 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
564 db_scale_4bit_12db_max),
565WSS_DOUBLE("Line Playback Switch", 0,
566 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
567WSS_DOUBLE_TLV("Line Playback Volume", 0,
568 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
569 db_scale_4bit_12db_max),
570WSS_DOUBLE("Mic Playback Switch", 0,
571 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
572WSS_DOUBLE_TLV("Mic Playback Volume", 0,
573 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
574 db_scale_4bit_12db_max),
575WSS_DOUBLE_TLV("CD Playback Volume", 0,
576 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
577 db_scale_4bit_12db_max),
578WSS_DOUBLE("Aux Playback Switch", 0,
579 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
580WSS_DOUBLE_TLV("Aux Playback Volume", 0,
581 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
582 db_scale_4bit_12db_max),
583};
584
585static int snd_opti93x_mixer(struct snd_wss *chip)
586{
587 struct snd_card *card;
588 unsigned int idx;
589 struct snd_ctl_elem_id id1, id2;
590 int err;
591
592 if (snd_BUG_ON(!chip || !chip->pcm))
593 return -EINVAL;
594
595 card = chip->card;
596
597 strcpy(card->mixername, chip->pcm->name);
598
599 memset(&id1, 0, sizeof(id1));
600 memset(&id2, 0, sizeof(id2));
601 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
602
603 strcpy(id1.name, "Aux Playback Switch");
604 strcpy(id2.name, "CD Playback Switch");
605 err = snd_ctl_rename_id(card, &id1, &id2);
606 if (err < 0) {
607 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
608 return err;
609 }
610
611 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
612 strcpy(id2.name, "FM Playback Switch");
613 err = snd_ctl_rename_id(card, &id1, &id2);
614 if (err < 0) {
615 snd_printk(KERN_ERR "Cannot rename opti93x control\n");
616 return err;
617 }
618
619 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
620 snd_ctl_remove_id(card, &id1);
621
622
623 id1.index = 0;
624 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
625 strcpy(id1.name, snd_opti93x_controls[idx].name);
626 snd_ctl_remove_id(card, &id1);
627
628 err = snd_ctl_add(card,
629 snd_ctl_new1(&snd_opti93x_controls[idx], chip));
630 if (err < 0)
631 return err;
632 }
633 return 0;
634}
635
636static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
637{
638 struct snd_opti9xx *chip = dev_id;
639 struct snd_wss *codec = chip->codec;
640 unsigned char status;
641
642 if (!codec)
643 return IRQ_HANDLED;
644
645 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
646 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
647 snd_pcm_period_elapsed(codec->playback_substream);
648 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
649 snd_wss_overrange(codec);
650 snd_pcm_period_elapsed(codec->capture_substream);
651 }
652 outb(0x00, OPTi93X_PORT(codec, STATUS));
653 return IRQ_HANDLED;
654}
655
656#endif
657
658static int snd_opti9xx_read_check(struct snd_opti9xx *chip)
659{
660 unsigned char value;
661#ifdef OPTi93X
662 unsigned long flags;
663#endif
664
665 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
666 "OPTi9xx MC");
667 if (chip->res_mc_base == NULL)
668 return -EBUSY;
669#ifndef OPTi93X
670 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
671 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
672 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
673 return 0;
674#else
675 chip->res_mc_indir = request_region(chip->mc_indir_index, 2,
676 "OPTi93x MC");
677 if (chip->res_mc_indir == NULL)
678 return -EBUSY;
679
680 spin_lock_irqsave(&chip->lock, flags);
681 outb(chip->password, chip->mc_base + chip->pwd_reg);
682 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
683 spin_unlock_irqrestore(&chip->lock, flags);
684
685 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
686 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
687 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
688 return 0;
689
690 release_and_free_resource(chip->res_mc_indir);
691 chip->res_mc_indir = NULL;
692#endif
693 release_and_free_resource(chip->res_mc_base);
694 chip->res_mc_base = NULL;
695
696 return -ENODEV;
697}
698
699static int snd_card_opti9xx_detect(struct snd_card *card,
700 struct snd_opti9xx *chip)
701{
702 int i, err;
703
704#ifndef OPTi93X
705 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
706#else
707 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
708#endif
709 err = snd_opti9xx_init(chip, i);
710 if (err < 0)
711 return err;
712
713 err = snd_opti9xx_read_check(chip);
714 if (err == 0)
715 return 1;
716#ifdef OPTi93X
717 chip->mc_indir_index = 0;
718#endif
719 }
720 return -ENODEV;
721}
722
723#ifdef CONFIG_PNP
724static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
725 struct pnp_card_link *card,
726 const struct pnp_card_device_id *pid)
727{
728 struct pnp_dev *pdev;
729 int err;
730 struct pnp_dev *devmpu;
731#ifndef OPTi93X
732 struct pnp_dev *devmc;
733#endif
734
735 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
736 if (pdev == NULL)
737 return -EBUSY;
738
739 err = pnp_activate_dev(pdev);
740 if (err < 0) {
741 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
742 return err;
743 }
744
745#ifdef OPTi93X
746 port = pnp_port_start(pdev, 0) - 4;
747 fm_port = pnp_port_start(pdev, 1) + 8;
748
749
750 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe;
751#else
752 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
753 if (devmc == NULL)
754 return -EBUSY;
755
756 err = pnp_activate_dev(devmc);
757 if (err < 0) {
758 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err);
759 return err;
760 }
761
762 port = pnp_port_start(pdev, 1);
763 fm_port = pnp_port_start(pdev, 2) + 8;
764
765
766
767
768 chip->mc_base = pnp_port_start(devmc, 0) - 1;
769 chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
770#endif
771 irq = pnp_irq(pdev, 0);
772 dma1 = pnp_dma(pdev, 0);
773#if defined(CS4231) || defined(OPTi93X)
774 dma2 = pnp_dma(pdev, 1);
775#endif
776
777 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
778
779 if (devmpu && mpu_port > 0) {
780 err = pnp_activate_dev(devmpu);
781 if (err < 0) {
782 snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
783 mpu_port = -1;
784 } else {
785 mpu_port = pnp_port_start(devmpu, 0);
786 mpu_irq = pnp_irq(devmpu, 0);
787 }
788 }
789 return pid->driver_data;
790}
791#endif
792
793static void snd_card_opti9xx_free(struct snd_card *card)
794{
795 struct snd_opti9xx *chip = card->private_data;
796
797 if (chip) {
798#ifdef OPTi93X
799 if (chip->irq > 0) {
800 disable_irq(chip->irq);
801 free_irq(chip->irq, chip);
802 }
803 release_and_free_resource(chip->res_mc_indir);
804#endif
805 release_and_free_resource(chip->res_mc_base);
806 }
807}
808
809static int snd_opti9xx_probe(struct snd_card *card)
810{
811 static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
812 int error;
813 int xdma2;
814 struct snd_opti9xx *chip = card->private_data;
815 struct snd_wss *codec;
816 struct snd_rawmidi *rmidi;
817 struct snd_hwdep *synth;
818
819#if defined(CS4231) || defined(OPTi93X)
820 xdma2 = dma2;
821#else
822 xdma2 = -1;
823#endif
824
825 if (port == SNDRV_AUTO_PORT) {
826 port = snd_legacy_find_free_ioport(possible_ports, 4);
827 if (port < 0) {
828 snd_printk(KERN_ERR "unable to find a free WSS port\n");
829 return -EBUSY;
830 }
831 }
832 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
833 mpu_port, mpu_irq);
834 if (error)
835 return error;
836
837 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2,
838#ifdef OPTi93X
839 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
840#else
841 WSS_HW_DETECT, 0,
842#endif
843 &codec);
844 if (error < 0)
845 return error;
846 chip->codec = codec;
847 error = snd_wss_pcm(codec, 0);
848 if (error < 0)
849 return error;
850 error = snd_wss_mixer(codec);
851 if (error < 0)
852 return error;
853#ifdef OPTi93X
854 error = snd_opti93x_mixer(codec);
855 if (error < 0)
856 return error;
857#endif
858#ifdef CS4231
859 error = snd_wss_timer(codec, 0);
860 if (error < 0)
861 return error;
862#endif
863#ifdef OPTi93X
864 error = request_irq(irq, snd_opti93x_interrupt,
865 0, DEV_NAME" - WSS", chip);
866 if (error < 0) {
867 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
868 return error;
869 }
870#endif
871 chip->irq = irq;
872 strcpy(card->driver, chip->name);
873 sprintf(card->shortname, "OPTi %s", card->driver);
874#if defined(CS4231) || defined(OPTi93X)
875 snprintf(card->longname, sizeof(card->longname),
876 "%s, %s at 0x%lx, irq %d, dma %d&%d",
877 card->shortname, codec->pcm->name,
878 chip->wss_base + 4, irq, dma1, xdma2);
879#else
880 snprintf(card->longname, sizeof(card->longname),
881 "%s, %s at 0x%lx, irq %d, dma %d",
882 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
883 dma1);
884#endif
885
886 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
887 rmidi = NULL;
888 else {
889 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
890 mpu_port, 0, mpu_irq, &rmidi);
891 if (error)
892 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
893 mpu_port);
894 }
895
896 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
897 struct snd_opl3 *opl3 = NULL;
898#ifndef OPTi93X
899 if (chip->hardware == OPTi9XX_HW_82C928 ||
900 chip->hardware == OPTi9XX_HW_82C929 ||
901 chip->hardware == OPTi9XX_HW_82C924) {
902 struct snd_opl4 *opl4;
903
904 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
905 0x20, 0x20);
906 if (snd_opl4_create(card, fm_port, fm_port - 8,
907 2, &opl3, &opl4) < 0) {
908
909 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
910 0x00, 0x20);
911 }
912 }
913#endif
914 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
915 OPL3_HW_AUTO, 0, &opl3) < 0) {
916 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
917 fm_port, fm_port + 4 - 1);
918 }
919 if (opl3) {
920 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
921 if (error < 0)
922 return error;
923 }
924 }
925
926 return snd_card_register(card);
927}
928
929static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp)
930{
931 struct snd_card *card;
932 int err;
933
934 err = snd_card_new(pdev, index, id, THIS_MODULE,
935 sizeof(struct snd_opti9xx), &card);
936 if (err < 0)
937 return err;
938 card->private_free = snd_card_opti9xx_free;
939 *cardp = card;
940 return 0;
941}
942
943static int snd_opti9xx_isa_match(struct device *devptr,
944 unsigned int dev)
945{
946#ifdef CONFIG_PNP
947 if (snd_opti9xx_pnp_is_probed)
948 return 0;
949 if (isapnp)
950 return 0;
951#endif
952 return 1;
953}
954
955static int snd_opti9xx_isa_probe(struct device *devptr,
956 unsigned int dev)
957{
958 struct snd_card *card;
959 int error;
960 static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
961#ifdef OPTi93X
962 static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
963#else
964 static int possible_irqs[] = {9, 10, 11, 7, -1};
965#endif
966 static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
967 static int possible_dma1s[] = {3, 1, 0, -1};
968#if defined(CS4231) || defined(OPTi93X)
969 static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
970#endif
971
972 if (mpu_port == SNDRV_AUTO_PORT) {
973 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
974 snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
975 return -EBUSY;
976 }
977 }
978 if (irq == SNDRV_AUTO_IRQ) {
979 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
980 snd_printk(KERN_ERR "unable to find a free IRQ\n");
981 return -EBUSY;
982 }
983 }
984 if (mpu_irq == SNDRV_AUTO_IRQ) {
985 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
986 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
987 return -EBUSY;
988 }
989 }
990 if (dma1 == SNDRV_AUTO_DMA) {
991 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
992 snd_printk(KERN_ERR "unable to find a free DMA1\n");
993 return -EBUSY;
994 }
995 }
996#if defined(CS4231) || defined(OPTi93X)
997 if (dma2 == SNDRV_AUTO_DMA) {
998 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
999 snd_printk(KERN_ERR "unable to find a free DMA2\n");
1000 return -EBUSY;
1001 }
1002 }
1003#endif
1004
1005 error = snd_opti9xx_card_new(devptr, &card);
1006 if (error < 0)
1007 return error;
1008
1009 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
1010 snd_card_free(card);
1011 return error;
1012 }
1013 if ((error = snd_opti9xx_probe(card)) < 0) {
1014 snd_card_free(card);
1015 return error;
1016 }
1017 dev_set_drvdata(devptr, card);
1018 return 0;
1019}
1020
1021static int snd_opti9xx_isa_remove(struct device *devptr,
1022 unsigned int dev)
1023{
1024 snd_card_free(dev_get_drvdata(devptr));
1025 return 0;
1026}
1027
1028#ifdef CONFIG_PM
1029static int snd_opti9xx_suspend(struct snd_card *card)
1030{
1031 struct snd_opti9xx *chip = card->private_data;
1032
1033 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1034 chip->codec->suspend(chip->codec);
1035 return 0;
1036}
1037
1038static int snd_opti9xx_resume(struct snd_card *card)
1039{
1040 struct snd_opti9xx *chip = card->private_data;
1041 int error, xdma2;
1042#if defined(CS4231) || defined(OPTi93X)
1043 xdma2 = dma2;
1044#else
1045 xdma2 = -1;
1046#endif
1047
1048 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
1049 mpu_port, mpu_irq);
1050 if (error)
1051 return error;
1052 chip->codec->resume(chip->codec);
1053 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1054 return 0;
1055}
1056
1057static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n,
1058 pm_message_t state)
1059{
1060 return snd_opti9xx_suspend(dev_get_drvdata(dev));
1061}
1062
1063static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n)
1064{
1065 return snd_opti9xx_resume(dev_get_drvdata(dev));
1066}
1067#endif
1068
1069static struct isa_driver snd_opti9xx_driver = {
1070 .match = snd_opti9xx_isa_match,
1071 .probe = snd_opti9xx_isa_probe,
1072 .remove = snd_opti9xx_isa_remove,
1073#ifdef CONFIG_PM
1074 .suspend = snd_opti9xx_isa_suspend,
1075 .resume = snd_opti9xx_isa_resume,
1076#endif
1077 .driver = {
1078 .name = DEV_NAME
1079 },
1080};
1081
1082#ifdef CONFIG_PNP
1083static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
1084 const struct pnp_card_device_id *pid)
1085{
1086 struct snd_card *card;
1087 int error, hw;
1088 struct snd_opti9xx *chip;
1089
1090 if (snd_opti9xx_pnp_is_probed)
1091 return -EBUSY;
1092 if (! isapnp)
1093 return -ENODEV;
1094 error = snd_opti9xx_card_new(&pcard->card->dev, &card);
1095 if (error < 0)
1096 return error;
1097 chip = card->private_data;
1098
1099 hw = snd_card_opti9xx_pnp(chip, pcard, pid);
1100 switch (hw) {
1101 case 0x0924:
1102 hw = OPTi9XX_HW_82C924;
1103 break;
1104 case 0x0925:
1105 hw = OPTi9XX_HW_82C925;
1106 break;
1107 case 0x0931:
1108 hw = OPTi9XX_HW_82C931;
1109 break;
1110 default:
1111 snd_card_free(card);
1112 return -ENODEV;
1113 }
1114
1115 if ((error = snd_opti9xx_init(chip, hw))) {
1116 snd_card_free(card);
1117 return error;
1118 }
1119 error = snd_opti9xx_read_check(chip);
1120 if (error) {
1121 snd_printk(KERN_ERR "OPTI chip not found\n");
1122 snd_card_free(card);
1123 return error;
1124 }
1125 if ((error = snd_opti9xx_probe(card)) < 0) {
1126 snd_card_free(card);
1127 return error;
1128 }
1129 pnp_set_card_drvdata(pcard, card);
1130 snd_opti9xx_pnp_is_probed = 1;
1131 return 0;
1132}
1133
1134static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard)
1135{
1136 snd_card_free(pnp_get_card_drvdata(pcard));
1137 pnp_set_card_drvdata(pcard, NULL);
1138 snd_opti9xx_pnp_is_probed = 0;
1139}
1140
1141#ifdef CONFIG_PM
1142static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard,
1143 pm_message_t state)
1144{
1145 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard));
1146}
1147
1148static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard)
1149{
1150 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard));
1151}
1152#endif
1153
1154static struct pnp_card_driver opti9xx_pnpc_driver = {
1155 .flags = PNP_DRIVER_RES_DISABLE,
1156 .name = DEV_NAME,
1157 .id_table = snd_opti9xx_pnpids,
1158 .probe = snd_opti9xx_pnp_probe,
1159 .remove = snd_opti9xx_pnp_remove,
1160#ifdef CONFIG_PM
1161 .suspend = snd_opti9xx_pnp_suspend,
1162 .resume = snd_opti9xx_pnp_resume,
1163#endif
1164};
1165#endif
1166
1167#ifdef OPTi93X
1168#define CHIP_NAME "82C93x"
1169#else
1170#define CHIP_NAME "82C92x"
1171#endif
1172
1173static int __init alsa_card_opti9xx_init(void)
1174{
1175#ifdef CONFIG_PNP
1176 pnp_register_card_driver(&opti9xx_pnpc_driver);
1177 if (snd_opti9xx_pnp_is_probed)
1178 return 0;
1179 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1180#endif
1181 return isa_register_driver(&snd_opti9xx_driver, 1);
1182}
1183
1184static void __exit alsa_card_opti9xx_exit(void)
1185{
1186 if (!snd_opti9xx_pnp_is_probed) {
1187 isa_unregister_driver(&snd_opti9xx_driver);
1188 return;
1189 }
1190#ifdef CONFIG_PNP
1191 pnp_unregister_card_driver(&opti9xx_pnpc_driver);
1192#endif
1193}
1194
1195module_init(alsa_card_opti9xx_init)
1196module_exit(alsa_card_opti9xx_exit)
1197