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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49#include <linux/kernel.h>
50#include <linux/module.h>
51#include <linux/interrupt.h>
52#include <linux/types.h>
53#include <linux/delay.h>
54#include <linux/ioport.h>
55#include <linux/firmware.h>
56#include <linux/isa.h>
57#include <linux/isapnp.h>
58#include <linux/irq.h>
59#include <linux/io.h>
60
61#include <sound/core.h>
62#include <sound/initval.h>
63#include <sound/asound.h>
64#include <sound/pcm.h>
65#include <sound/mpu401.h>
66
67#ifdef MSND_CLASSIC
68# ifndef __alpha__
69# define SLOWIO
70# endif
71#endif
72#include "msnd.h"
73#ifdef MSND_CLASSIC
74# include "msnd_classic.h"
75# define LOGNAME "msnd_classic"
76#else
77# include "msnd_pinnacle.h"
78# define LOGNAME "snd_msnd_pinnacle"
79#endif
80
81static void set_default_audio_parameters(struct snd_msnd *chip)
82{
83 chip->play_sample_size = DEFSAMPLESIZE;
84 chip->play_sample_rate = DEFSAMPLERATE;
85 chip->play_channels = DEFCHANNELS;
86 chip->capture_sample_size = DEFSAMPLESIZE;
87 chip->capture_sample_rate = DEFSAMPLERATE;
88 chip->capture_channels = DEFCHANNELS;
89}
90
91static void snd_msnd_eval_dsp_msg(struct snd_msnd *chip, u16 wMessage)
92{
93 switch (HIBYTE(wMessage)) {
94 case HIMT_PLAY_DONE: {
95 if (chip->banksPlayed < 3)
96 snd_printdd("%08X: HIMT_PLAY_DONE: %i\n",
97 (unsigned)jiffies, LOBYTE(wMessage));
98
99 if (chip->last_playbank == LOBYTE(wMessage)) {
100 snd_printdd("chip.last_playbank == LOBYTE(wMessage)\n");
101 break;
102 }
103 chip->banksPlayed++;
104
105 if (test_bit(F_WRITING, &chip->flags))
106 snd_msnd_DAPQ(chip, 0);
107
108 chip->last_playbank = LOBYTE(wMessage);
109 chip->playDMAPos += chip->play_period_bytes;
110 if (chip->playDMAPos > chip->playLimit)
111 chip->playDMAPos = 0;
112 snd_pcm_period_elapsed(chip->playback_substream);
113
114 break;
115 }
116 case HIMT_RECORD_DONE:
117 if (chip->last_recbank == LOBYTE(wMessage))
118 break;
119 chip->last_recbank = LOBYTE(wMessage);
120 chip->captureDMAPos += chip->capturePeriodBytes;
121 if (chip->captureDMAPos > (chip->captureLimit))
122 chip->captureDMAPos = 0;
123
124 if (test_bit(F_READING, &chip->flags))
125 snd_msnd_DARQ(chip, chip->last_recbank);
126
127 snd_pcm_period_elapsed(chip->capture_substream);
128 break;
129
130 case HIMT_DSP:
131 switch (LOBYTE(wMessage)) {
132#ifndef MSND_CLASSIC
133 case HIDSP_PLAY_UNDER:
134#endif
135 case HIDSP_INT_PLAY_UNDER:
136 snd_printd(KERN_WARNING LOGNAME ": Play underflow %i\n",
137 chip->banksPlayed);
138 if (chip->banksPlayed > 2)
139 clear_bit(F_WRITING, &chip->flags);
140 break;
141
142 case HIDSP_INT_RECORD_OVER:
143 snd_printd(KERN_WARNING LOGNAME ": Record overflow\n");
144 clear_bit(F_READING, &chip->flags);
145 break;
146
147 default:
148 snd_printd(KERN_WARNING LOGNAME
149 ": DSP message %d 0x%02x\n",
150 LOBYTE(wMessage), LOBYTE(wMessage));
151 break;
152 }
153 break;
154
155 case HIMT_MIDI_IN_UCHAR:
156 if (chip->msndmidi_mpu)
157 snd_msndmidi_input_read(chip->msndmidi_mpu);
158 break;
159
160 default:
161 snd_printd(KERN_WARNING LOGNAME ": HIMT message %d 0x%02x\n",
162 HIBYTE(wMessage), HIBYTE(wMessage));
163 break;
164 }
165}
166
167static irqreturn_t snd_msnd_interrupt(int irq, void *dev_id)
168{
169 struct snd_msnd *chip = dev_id;
170 void *pwDSPQData = chip->mappedbase + DSPQ_DATA_BUFF;
171
172
173
174
175
176 while (readw(chip->DSPQ + JQS_wTail) != readw(chip->DSPQ + JQS_wHead)) {
177 u16 wTmp;
178
179 snd_msnd_eval_dsp_msg(chip,
180 readw(pwDSPQData + 2 * readw(chip->DSPQ + JQS_wHead)));
181
182 wTmp = readw(chip->DSPQ + JQS_wHead) + 1;
183 if (wTmp > readw(chip->DSPQ + JQS_wSize))
184 writew(0, chip->DSPQ + JQS_wHead);
185 else
186 writew(wTmp, chip->DSPQ + JQS_wHead);
187 }
188
189 inb(chip->io + HP_RXL);
190 return IRQ_HANDLED;
191}
192
193
194static int snd_msnd_reset_dsp(long io, unsigned char *info)
195{
196 int timeout = 100;
197
198 outb(HPDSPRESET_ON, io + HP_DSPR);
199 msleep(1);
200#ifndef MSND_CLASSIC
201 if (info)
202 *info = inb(io + HP_INFO);
203#endif
204 outb(HPDSPRESET_OFF, io + HP_DSPR);
205 msleep(1);
206 while (timeout-- > 0) {
207 if (inb(io + HP_CVR) == HP_CVR_DEF)
208 return 0;
209 msleep(1);
210 }
211 snd_printk(KERN_ERR LOGNAME ": Cannot reset DSP\n");
212
213 return -EIO;
214}
215
216static int snd_msnd_probe(struct snd_card *card)
217{
218 struct snd_msnd *chip = card->private_data;
219 unsigned char info;
220#ifndef MSND_CLASSIC
221 char *xv, *rev = NULL;
222 char *pin = "TB Pinnacle", *fiji = "TB Fiji";
223 char *pinfiji = "TB Pinnacle/Fiji";
224#endif
225
226 if (!request_region(chip->io, DSP_NUMIO, "probing")) {
227 snd_printk(KERN_ERR LOGNAME ": I/O port conflict\n");
228 return -ENODEV;
229 }
230
231 if (snd_msnd_reset_dsp(chip->io, &info) < 0) {
232 release_region(chip->io, DSP_NUMIO);
233 return -ENODEV;
234 }
235
236#ifdef MSND_CLASSIC
237 strcpy(card->shortname, "Classic/Tahiti/Monterey");
238 strcpy(card->longname, "Turtle Beach Multisound");
239 printk(KERN_INFO LOGNAME ": %s, "
240 "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
241 card->shortname,
242 chip->io, chip->io + DSP_NUMIO - 1,
243 chip->irq,
244 chip->base, chip->base + 0x7fff);
245#else
246 switch (info >> 4) {
247 case 0xf:
248 xv = "<= 1.15";
249 break;
250 case 0x1:
251 xv = "1.18/1.2";
252 break;
253 case 0x2:
254 xv = "1.3";
255 break;
256 case 0x3:
257 xv = "1.4";
258 break;
259 default:
260 xv = "unknown";
261 break;
262 }
263
264 switch (info & 0x7) {
265 case 0x0:
266 rev = "I";
267 strcpy(card->shortname, pin);
268 break;
269 case 0x1:
270 rev = "F";
271 strcpy(card->shortname, pin);
272 break;
273 case 0x2:
274 rev = "G";
275 strcpy(card->shortname, pin);
276 break;
277 case 0x3:
278 rev = "H";
279 strcpy(card->shortname, pin);
280 break;
281 case 0x4:
282 rev = "E";
283 strcpy(card->shortname, fiji);
284 break;
285 case 0x5:
286 rev = "C";
287 strcpy(card->shortname, fiji);
288 break;
289 case 0x6:
290 rev = "D";
291 strcpy(card->shortname, fiji);
292 break;
293 case 0x7:
294 rev = "A-B (Fiji) or A-E (Pinnacle)";
295 strcpy(card->shortname, pinfiji);
296 break;
297 }
298 strcpy(card->longname, "Turtle Beach Multisound Pinnacle");
299 printk(KERN_INFO LOGNAME ": %s revision %s, Xilinx version %s, "
300 "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
301 card->shortname,
302 rev, xv,
303 chip->io, chip->io + DSP_NUMIO - 1,
304 chip->irq,
305 chip->base, chip->base + 0x7fff);
306#endif
307
308 release_region(chip->io, DSP_NUMIO);
309 return 0;
310}
311
312static int snd_msnd_init_sma(struct snd_msnd *chip)
313{
314 static int initted;
315 u16 mastVolLeft, mastVolRight;
316 unsigned long flags;
317
318#ifdef MSND_CLASSIC
319 outb(chip->memid, chip->io + HP_MEMM);
320#endif
321 outb(HPBLKSEL_0, chip->io + HP_BLKS);
322
323 chip->SMA = chip->mappedbase + SMA_STRUCT_START;
324
325 if (initted) {
326 mastVolLeft = readw(chip->SMA + SMA_wCurrMastVolLeft);
327 mastVolRight = readw(chip->SMA + SMA_wCurrMastVolRight);
328 } else
329 mastVolLeft = mastVolRight = 0;
330 memset_io(chip->mappedbase, 0, 0x8000);
331
332
333 spin_lock_irqsave(&chip->lock, flags);
334 outb(HPBLKSEL_1, chip->io + HP_BLKS);
335 memset_io(chip->mappedbase, 0, 0x8000);
336 outb(HPBLKSEL_0, chip->io + HP_BLKS);
337 spin_unlock_irqrestore(&chip->lock, flags);
338
339
340 chip->DAPQ = chip->mappedbase + DAPQ_OFFSET;
341 snd_msnd_init_queue(chip->DAPQ, DAPQ_DATA_BUFF, DAPQ_BUFF_SIZE);
342
343
344 chip->DARQ = chip->mappedbase + DARQ_OFFSET;
345 snd_msnd_init_queue(chip->DARQ, DARQ_DATA_BUFF, DARQ_BUFF_SIZE);
346
347
348 chip->MODQ = chip->mappedbase + MODQ_OFFSET;
349 snd_msnd_init_queue(chip->MODQ, MODQ_DATA_BUFF, MODQ_BUFF_SIZE);
350
351
352 chip->MIDQ = chip->mappedbase + MIDQ_OFFSET;
353 snd_msnd_init_queue(chip->MIDQ, MIDQ_DATA_BUFF, MIDQ_BUFF_SIZE);
354
355
356 chip->DSPQ = chip->mappedbase + DSPQ_OFFSET;
357 snd_msnd_init_queue(chip->DSPQ, DSPQ_DATA_BUFF, DSPQ_BUFF_SIZE);
358
359
360#ifndef MSND_CLASSIC
361 writew(1, chip->SMA + SMA_wCurrPlayFormat);
362 writew(chip->play_sample_size, chip->SMA + SMA_wCurrPlaySampleSize);
363 writew(chip->play_channels, chip->SMA + SMA_wCurrPlayChannels);
364 writew(chip->play_sample_rate, chip->SMA + SMA_wCurrPlaySampleRate);
365#endif
366 writew(chip->play_sample_rate, chip->SMA + SMA_wCalFreqAtoD);
367 writew(mastVolLeft, chip->SMA + SMA_wCurrMastVolLeft);
368 writew(mastVolRight, chip->SMA + SMA_wCurrMastVolRight);
369#ifndef MSND_CLASSIC
370 writel(0x00010000, chip->SMA + SMA_dwCurrPlayPitch);
371 writel(0x00000001, chip->SMA + SMA_dwCurrPlayRate);
372#endif
373 writew(0x303, chip->SMA + SMA_wCurrInputTagBits);
374
375 initted = 1;
376
377 return 0;
378}
379
380
381static int upload_dsp_code(struct snd_card *card)
382{
383 struct snd_msnd *chip = card->private_data;
384 const struct firmware *init_fw = NULL, *perm_fw = NULL;
385 int err;
386
387 outb(HPBLKSEL_0, chip->io + HP_BLKS);
388
389 err = request_firmware(&init_fw, INITCODEFILE, card->dev);
390 if (err < 0) {
391 printk(KERN_ERR LOGNAME ": Error loading " INITCODEFILE);
392 goto cleanup1;
393 }
394 err = request_firmware(&perm_fw, PERMCODEFILE, card->dev);
395 if (err < 0) {
396 printk(KERN_ERR LOGNAME ": Error loading " PERMCODEFILE);
397 goto cleanup;
398 }
399
400 memcpy_toio(chip->mappedbase, perm_fw->data, perm_fw->size);
401 if (snd_msnd_upload_host(chip, init_fw->data, init_fw->size) < 0) {
402 printk(KERN_WARNING LOGNAME ": Error uploading to DSP\n");
403 err = -ENODEV;
404 goto cleanup;
405 }
406 printk(KERN_INFO LOGNAME ": DSP firmware uploaded\n");
407 err = 0;
408
409cleanup:
410 release_firmware(perm_fw);
411cleanup1:
412 release_firmware(init_fw);
413 return err;
414}
415
416#ifdef MSND_CLASSIC
417static void reset_proteus(struct snd_msnd *chip)
418{
419 outb(HPPRORESET_ON, chip->io + HP_PROR);
420 msleep(TIME_PRO_RESET);
421 outb(HPPRORESET_OFF, chip->io + HP_PROR);
422 msleep(TIME_PRO_RESET_DONE);
423}
424#endif
425
426static int snd_msnd_initialize(struct snd_card *card)
427{
428 struct snd_msnd *chip = card->private_data;
429 int err, timeout;
430
431#ifdef MSND_CLASSIC
432 outb(HPWAITSTATE_0, chip->io + HP_WAIT);
433 outb(HPBITMODE_16, chip->io + HP_BITM);
434
435 reset_proteus(chip);
436#endif
437 err = snd_msnd_init_sma(chip);
438 if (err < 0) {
439 printk(KERN_WARNING LOGNAME ": Cannot initialize SMA\n");
440 return err;
441 }
442
443 err = snd_msnd_reset_dsp(chip->io, NULL);
444 if (err < 0)
445 return err;
446
447 err = upload_dsp_code(card);
448 if (err < 0) {
449 printk(KERN_WARNING LOGNAME ": Cannot upload DSP code\n");
450 return err;
451 }
452
453 timeout = 200;
454
455 while (readw(chip->mappedbase)) {
456 msleep(1);
457 if (!timeout--) {
458 snd_printd(KERN_ERR LOGNAME ": DSP reset timeout\n");
459 return -EIO;
460 }
461 }
462
463 snd_msndmix_setup(chip);
464 return 0;
465}
466
467static int snd_msnd_dsp_full_reset(struct snd_card *card)
468{
469 struct snd_msnd *chip = card->private_data;
470 int rv;
471
472 if (test_bit(F_RESETTING, &chip->flags) || ++chip->nresets > 10)
473 return 0;
474
475 set_bit(F_RESETTING, &chip->flags);
476 snd_msnd_dsp_halt(chip, NULL);
477
478 rv = snd_msnd_initialize(card);
479 if (rv)
480 printk(KERN_WARNING LOGNAME ": DSP reset failed\n");
481 snd_msndmix_force_recsrc(chip, 0);
482 clear_bit(F_RESETTING, &chip->flags);
483 return rv;
484}
485
486static int snd_msnd_dev_free(struct snd_device *device)
487{
488 snd_printdd("snd_msnd_chip_free()\n");
489 return 0;
490}
491
492static int snd_msnd_send_dsp_cmd_chk(struct snd_msnd *chip, u8 cmd)
493{
494 if (snd_msnd_send_dsp_cmd(chip, cmd) == 0)
495 return 0;
496 snd_msnd_dsp_full_reset(chip->card);
497 return snd_msnd_send_dsp_cmd(chip, cmd);
498}
499
500static int snd_msnd_calibrate_adc(struct snd_msnd *chip, u16 srate)
501{
502 snd_printdd("snd_msnd_calibrate_adc(%i)\n", srate);
503 writew(srate, chip->SMA + SMA_wCalFreqAtoD);
504 if (chip->calibrate_signal == 0)
505 writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
506 | 0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
507 else
508 writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
509 & ~0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
510 if (snd_msnd_send_word(chip, 0, 0, HDEXAR_CAL_A_TO_D) == 0 &&
511 snd_msnd_send_dsp_cmd_chk(chip, HDEX_AUX_REQ) == 0) {
512 schedule_timeout_interruptible(msecs_to_jiffies(333));
513 return 0;
514 }
515 printk(KERN_WARNING LOGNAME ": ADC calibration failed\n");
516 return -EIO;
517}
518
519
520
521
522static int snd_msnd_mpu401_open(struct snd_mpu401 *mpu)
523{
524 snd_msnd_enable_irq(mpu->private_data);
525 snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_START);
526 return 0;
527}
528
529static void snd_msnd_mpu401_close(struct snd_mpu401 *mpu)
530{
531 snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_STOP);
532 snd_msnd_disable_irq(mpu->private_data);
533}
534
535static long mpu_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
536static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
537
538static int snd_msnd_attach(struct snd_card *card)
539{
540 struct snd_msnd *chip = card->private_data;
541 int err;
542 static struct snd_device_ops ops = {
543 .dev_free = snd_msnd_dev_free,
544 };
545
546 err = request_irq(chip->irq, snd_msnd_interrupt, 0, card->shortname,
547 chip);
548 if (err < 0) {
549 printk(KERN_ERR LOGNAME ": Couldn't grab IRQ %d\n", chip->irq);
550 return err;
551 }
552 if (request_region(chip->io, DSP_NUMIO, card->shortname) == NULL) {
553 free_irq(chip->irq, chip);
554 return -EBUSY;
555 }
556
557 if (!request_mem_region(chip->base, BUFFSIZE, card->shortname)) {
558 printk(KERN_ERR LOGNAME
559 ": unable to grab memory region 0x%lx-0x%lx\n",
560 chip->base, chip->base + BUFFSIZE - 1);
561 release_region(chip->io, DSP_NUMIO);
562 free_irq(chip->irq, chip);
563 return -EBUSY;
564 }
565 chip->mappedbase = ioremap_nocache(chip->base, 0x8000);
566 if (!chip->mappedbase) {
567 printk(KERN_ERR LOGNAME
568 ": unable to map memory region 0x%lx-0x%lx\n",
569 chip->base, chip->base + BUFFSIZE - 1);
570 err = -EIO;
571 goto err_release_region;
572 }
573
574 err = snd_msnd_dsp_full_reset(card);
575 if (err < 0)
576 goto err_release_region;
577
578
579 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
580 if (err < 0)
581 goto err_release_region;
582
583 err = snd_msnd_pcm(card, 0, NULL);
584 if (err < 0) {
585 printk(KERN_ERR LOGNAME ": error creating new PCM device\n");
586 goto err_release_region;
587 }
588
589 err = snd_msndmix_new(card);
590 if (err < 0) {
591 printk(KERN_ERR LOGNAME ": error creating new Mixer device\n");
592 goto err_release_region;
593 }
594
595
596 if (mpu_io[0] != SNDRV_AUTO_PORT) {
597 struct snd_mpu401 *mpu;
598
599 err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
600 mpu_io[0],
601 MPU401_MODE_INPUT |
602 MPU401_MODE_OUTPUT,
603 mpu_irq[0],
604 &chip->rmidi);
605 if (err < 0) {
606 printk(KERN_ERR LOGNAME
607 ": error creating new Midi device\n");
608 goto err_release_region;
609 }
610 mpu = chip->rmidi->private_data;
611
612 mpu->open_input = snd_msnd_mpu401_open;
613 mpu->close_input = snd_msnd_mpu401_close;
614 mpu->private_data = chip;
615 }
616
617 disable_irq(chip->irq);
618 snd_msnd_calibrate_adc(chip, chip->play_sample_rate);
619 snd_msndmix_force_recsrc(chip, 0);
620
621 err = snd_card_register(card);
622 if (err < 0)
623 goto err_release_region;
624
625 return 0;
626
627err_release_region:
628 iounmap(chip->mappedbase);
629 release_mem_region(chip->base, BUFFSIZE);
630 release_region(chip->io, DSP_NUMIO);
631 free_irq(chip->irq, chip);
632 return err;
633}
634
635
636static void snd_msnd_unload(struct snd_card *card)
637{
638 struct snd_msnd *chip = card->private_data;
639
640 iounmap(chip->mappedbase);
641 release_mem_region(chip->base, BUFFSIZE);
642 release_region(chip->io, DSP_NUMIO);
643 free_irq(chip->irq, chip);
644 snd_card_free(card);
645}
646
647#ifndef MSND_CLASSIC
648
649
650
651static int snd_msnd_write_cfg(int cfg, int reg, int value)
652{
653 outb(reg, cfg);
654 outb(value, cfg + 1);
655 if (value != inb(cfg + 1)) {
656 printk(KERN_ERR LOGNAME ": snd_msnd_write_cfg: I/O error\n");
657 return -EIO;
658 }
659 return 0;
660}
661
662static int snd_msnd_write_cfg_io0(int cfg, int num, u16 io)
663{
664 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
665 return -EIO;
666 if (snd_msnd_write_cfg(cfg, IREG_IO0_BASEHI, HIBYTE(io)))
667 return -EIO;
668 if (snd_msnd_write_cfg(cfg, IREG_IO0_BASELO, LOBYTE(io)))
669 return -EIO;
670 return 0;
671}
672
673static int snd_msnd_write_cfg_io1(int cfg, int num, u16 io)
674{
675 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
676 return -EIO;
677 if (snd_msnd_write_cfg(cfg, IREG_IO1_BASEHI, HIBYTE(io)))
678 return -EIO;
679 if (snd_msnd_write_cfg(cfg, IREG_IO1_BASELO, LOBYTE(io)))
680 return -EIO;
681 return 0;
682}
683
684static int snd_msnd_write_cfg_irq(int cfg, int num, u16 irq)
685{
686 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
687 return -EIO;
688 if (snd_msnd_write_cfg(cfg, IREG_IRQ_NUMBER, LOBYTE(irq)))
689 return -EIO;
690 if (snd_msnd_write_cfg(cfg, IREG_IRQ_TYPE, IRQTYPE_EDGE))
691 return -EIO;
692 return 0;
693}
694
695static int snd_msnd_write_cfg_mem(int cfg, int num, int mem)
696{
697 u16 wmem;
698
699 mem >>= 8;
700 wmem = (u16)(mem & 0xfff);
701 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
702 return -EIO;
703 if (snd_msnd_write_cfg(cfg, IREG_MEMBASEHI, HIBYTE(wmem)))
704 return -EIO;
705 if (snd_msnd_write_cfg(cfg, IREG_MEMBASELO, LOBYTE(wmem)))
706 return -EIO;
707 if (wmem && snd_msnd_write_cfg(cfg, IREG_MEMCONTROL,
708 MEMTYPE_HIADDR | MEMTYPE_16BIT))
709 return -EIO;
710 return 0;
711}
712
713static int snd_msnd_activate_logical(int cfg, int num)
714{
715 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
716 return -EIO;
717 if (snd_msnd_write_cfg(cfg, IREG_ACTIVATE, LD_ACTIVATE))
718 return -EIO;
719 return 0;
720}
721
722static int snd_msnd_write_cfg_logical(int cfg, int num, u16 io0,
723 u16 io1, u16 irq, int mem)
724{
725 if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
726 return -EIO;
727 if (snd_msnd_write_cfg_io0(cfg, num, io0))
728 return -EIO;
729 if (snd_msnd_write_cfg_io1(cfg, num, io1))
730 return -EIO;
731 if (snd_msnd_write_cfg_irq(cfg, num, irq))
732 return -EIO;
733 if (snd_msnd_write_cfg_mem(cfg, num, mem))
734 return -EIO;
735 if (snd_msnd_activate_logical(cfg, num))
736 return -EIO;
737 return 0;
738}
739
740static int snd_msnd_pinnacle_cfg_reset(int cfg)
741{
742 int i;
743
744
745 printk(KERN_INFO LOGNAME ": Resetting all devices\n");
746 for (i = 0; i < 4; ++i)
747 if (snd_msnd_write_cfg_logical(cfg, i, 0, 0, 0, 0))
748 return -EIO;
749
750 return 0;
751}
752#endif
753
754static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
755static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
756
757module_param_array(index, int, NULL, 0444);
758MODULE_PARM_DESC(index, "Index value for msnd_pinnacle soundcard.");
759module_param_array(id, charp, NULL, 0444);
760MODULE_PARM_DESC(id, "ID string for msnd_pinnacle soundcard.");
761
762static long io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
763static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
764static long mem[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
765
766#ifndef MSND_CLASSIC
767static long cfg[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
768
769
770static long ide_io0[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
771static long ide_io1[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
772static int ide_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
773
774static long joystick_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
775
776static int digital[SNDRV_CARDS];
777
778
779static int reset[SNDRV_CARDS];
780#endif
781
782static int write_ndelay[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
783
784static int calibrate_signal;
785
786#ifdef CONFIG_PNP
787static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
788module_param_array(isapnp, bool, NULL, 0444);
789MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
790#define has_isapnp(x) isapnp[x]
791#else
792#define has_isapnp(x) 0
793#endif
794
795MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
796MODULE_DESCRIPTION("Turtle Beach " LONGNAME " Linux Driver");
797MODULE_LICENSE("GPL");
798MODULE_FIRMWARE(INITCODEFILE);
799MODULE_FIRMWARE(PERMCODEFILE);
800
801module_param_array(io, long, NULL, 0444);
802MODULE_PARM_DESC(io, "IO port #");
803module_param_array(irq, int, NULL, 0444);
804module_param_array(mem, long, NULL, 0444);
805module_param_array(write_ndelay, int, NULL, 0444);
806module_param(calibrate_signal, int, 0444);
807#ifndef MSND_CLASSIC
808module_param_array(digital, int, NULL, 0444);
809module_param_array(cfg, long, NULL, 0444);
810module_param_array(reset, int, 0, 0444);
811module_param_array(mpu_io, long, NULL, 0444);
812module_param_array(mpu_irq, int, NULL, 0444);
813module_param_array(ide_io0, long, NULL, 0444);
814module_param_array(ide_io1, long, NULL, 0444);
815module_param_array(ide_irq, int, NULL, 0444);
816module_param_array(joystick_io, long, NULL, 0444);
817#endif
818
819
820static int snd_msnd_isa_match(struct device *pdev, unsigned int i)
821{
822 if (io[i] == SNDRV_AUTO_PORT)
823 return 0;
824
825 if (irq[i] == SNDRV_AUTO_PORT || mem[i] == SNDRV_AUTO_PORT) {
826 printk(KERN_WARNING LOGNAME ": io, irq and mem must be set\n");
827 return 0;
828 }
829
830#ifdef MSND_CLASSIC
831 if (!(io[i] == 0x290 ||
832 io[i] == 0x260 ||
833 io[i] == 0x250 ||
834 io[i] == 0x240 ||
835 io[i] == 0x230 ||
836 io[i] == 0x220 ||
837 io[i] == 0x210 ||
838 io[i] == 0x3e0)) {
839 printk(KERN_ERR LOGNAME ": \"io\" - DSP I/O base must be set "
840 " to 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x290, "
841 "or 0x3E0\n");
842 return 0;
843 }
844#else
845 if (io[i] < 0x100 || io[i] > 0x3e0 || (io[i] % 0x10) != 0) {
846 printk(KERN_ERR LOGNAME
847 ": \"io\" - DSP I/O base must within the range 0x100 "
848 "to 0x3E0 and must be evenly divisible by 0x10\n");
849 return 0;
850 }
851#endif
852
853 if (!(irq[i] == 5 ||
854 irq[i] == 7 ||
855 irq[i] == 9 ||
856 irq[i] == 10 ||
857 irq[i] == 11 ||
858 irq[i] == 12)) {
859 printk(KERN_ERR LOGNAME
860 ": \"irq\" - must be set to 5, 7, 9, 10, 11 or 12\n");
861 return 0;
862 }
863
864 if (!(mem[i] == 0xb0000 ||
865 mem[i] == 0xc8000 ||
866 mem[i] == 0xd0000 ||
867 mem[i] == 0xd8000 ||
868 mem[i] == 0xe0000 ||
869 mem[i] == 0xe8000)) {
870 printk(KERN_ERR LOGNAME ": \"mem\" - must be set to "
871 "0xb0000, 0xc8000, 0xd0000, 0xd8000, 0xe0000 or "
872 "0xe8000\n");
873 return 0;
874 }
875
876#ifndef MSND_CLASSIC
877 if (cfg[i] == SNDRV_AUTO_PORT) {
878 printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
879 } else if (cfg[i] != 0x250 && cfg[i] != 0x260 && cfg[i] != 0x270) {
880 printk(KERN_INFO LOGNAME
881 ": Config port must be 0x250, 0x260 or 0x270 "
882 "(or unspecified for PnP mode)\n");
883 return 0;
884 }
885#endif
886
887 return 1;
888}
889
890static int snd_msnd_isa_probe(struct device *pdev, unsigned int idx)
891{
892 int err;
893 struct snd_card *card;
894 struct snd_msnd *chip;
895
896 if (has_isapnp(idx)
897#ifndef MSND_CLASSIC
898 || cfg[idx] == SNDRV_AUTO_PORT
899#endif
900 ) {
901 printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
902 return -ENODEV;
903 }
904
905 err = snd_card_new(pdev, index[idx], id[idx], THIS_MODULE,
906 sizeof(struct snd_msnd), &card);
907 if (err < 0)
908 return err;
909
910 chip = card->private_data;
911 chip->card = card;
912
913#ifdef MSND_CLASSIC
914 switch (irq[idx]) {
915 case 5:
916 chip->irqid = HPIRQ_5; break;
917 case 7:
918 chip->irqid = HPIRQ_7; break;
919 case 9:
920 chip->irqid = HPIRQ_9; break;
921 case 10:
922 chip->irqid = HPIRQ_10; break;
923 case 11:
924 chip->irqid = HPIRQ_11; break;
925 case 12:
926 chip->irqid = HPIRQ_12; break;
927 }
928
929 switch (mem[idx]) {
930 case 0xb0000:
931 chip->memid = HPMEM_B000; break;
932 case 0xc8000:
933 chip->memid = HPMEM_C800; break;
934 case 0xd0000:
935 chip->memid = HPMEM_D000; break;
936 case 0xd8000:
937 chip->memid = HPMEM_D800; break;
938 case 0xe0000:
939 chip->memid = HPMEM_E000; break;
940 case 0xe8000:
941 chip->memid = HPMEM_E800; break;
942 }
943#else
944 printk(KERN_INFO LOGNAME ": Non-PnP mode: configuring at port 0x%lx\n",
945 cfg[idx]);
946
947 if (!request_region(cfg[idx], 2, "Pinnacle/Fiji Config")) {
948 printk(KERN_ERR LOGNAME ": Config port 0x%lx conflict\n",
949 cfg[idx]);
950 snd_card_free(card);
951 return -EIO;
952 }
953 if (reset[idx])
954 if (snd_msnd_pinnacle_cfg_reset(cfg[idx])) {
955 err = -EIO;
956 goto cfg_error;
957 }
958
959
960 err = snd_msnd_write_cfg_logical(cfg[idx], 0,
961 io[idx], 0,
962 irq[idx], mem[idx]);
963
964 if (err)
965 goto cfg_error;
966
967
968
969
970 if (mpu_io[idx] != SNDRV_AUTO_PORT
971 && mpu_irq[idx] != SNDRV_AUTO_IRQ) {
972 printk(KERN_INFO LOGNAME
973 ": Configuring MPU to I/O 0x%lx IRQ %d\n",
974 mpu_io[idx], mpu_irq[idx]);
975 err = snd_msnd_write_cfg_logical(cfg[idx], 1,
976 mpu_io[idx], 0,
977 mpu_irq[idx], 0);
978
979 if (err)
980 goto cfg_error;
981 }
982
983
984 if (ide_io0[idx] != SNDRV_AUTO_PORT
985 && ide_io1[idx] != SNDRV_AUTO_PORT
986 && ide_irq[idx] != SNDRV_AUTO_IRQ) {
987 printk(KERN_INFO LOGNAME
988 ": Configuring IDE to I/O 0x%lx, 0x%lx IRQ %d\n",
989 ide_io0[idx], ide_io1[idx], ide_irq[idx]);
990 err = snd_msnd_write_cfg_logical(cfg[idx], 2,
991 ide_io0[idx], ide_io1[idx],
992 ide_irq[idx], 0);
993
994 if (err)
995 goto cfg_error;
996 }
997
998
999 if (joystick_io[idx] != SNDRV_AUTO_PORT) {
1000 printk(KERN_INFO LOGNAME
1001 ": Configuring joystick to I/O 0x%lx\n",
1002 joystick_io[idx]);
1003 err = snd_msnd_write_cfg_logical(cfg[idx], 3,
1004 joystick_io[idx], 0,
1005 0, 0);
1006
1007 if (err)
1008 goto cfg_error;
1009 }
1010 release_region(cfg[idx], 2);
1011
1012#endif
1013
1014 set_default_audio_parameters(chip);
1015#ifdef MSND_CLASSIC
1016 chip->type = msndClassic;
1017#else
1018 chip->type = msndPinnacle;
1019#endif
1020 chip->io = io[idx];
1021 chip->irq = irq[idx];
1022 chip->base = mem[idx];
1023
1024 chip->calibrate_signal = calibrate_signal ? 1 : 0;
1025 chip->recsrc = 0;
1026 chip->dspq_data_buff = DSPQ_DATA_BUFF;
1027 chip->dspq_buff_size = DSPQ_BUFF_SIZE;
1028 if (write_ndelay[idx])
1029 clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1030 else
1031 set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1032#ifndef MSND_CLASSIC
1033 if (digital[idx])
1034 set_bit(F_HAVEDIGITAL, &chip->flags);
1035#endif
1036 spin_lock_init(&chip->lock);
1037 err = snd_msnd_probe(card);
1038 if (err < 0) {
1039 printk(KERN_ERR LOGNAME ": Probe failed\n");
1040 snd_card_free(card);
1041 return err;
1042 }
1043
1044 err = snd_msnd_attach(card);
1045 if (err < 0) {
1046 printk(KERN_ERR LOGNAME ": Attach failed\n");
1047 snd_card_free(card);
1048 return err;
1049 }
1050 dev_set_drvdata(pdev, card);
1051
1052 return 0;
1053
1054#ifndef MSND_CLASSIC
1055cfg_error:
1056 release_region(cfg[idx], 2);
1057 snd_card_free(card);
1058 return err;
1059#endif
1060}
1061
1062static int snd_msnd_isa_remove(struct device *pdev, unsigned int dev)
1063{
1064 snd_msnd_unload(dev_get_drvdata(pdev));
1065 dev_set_drvdata(pdev, NULL);
1066 return 0;
1067}
1068
1069#define DEV_NAME "msnd-pinnacle"
1070
1071static struct isa_driver snd_msnd_driver = {
1072 .match = snd_msnd_isa_match,
1073 .probe = snd_msnd_isa_probe,
1074 .remove = snd_msnd_isa_remove,
1075
1076 .driver = {
1077 .name = DEV_NAME
1078 },
1079};
1080
1081#ifdef CONFIG_PNP
1082static int snd_msnd_pnp_detect(struct pnp_card_link *pcard,
1083 const struct pnp_card_device_id *pid)
1084{
1085 static int idx;
1086 struct pnp_dev *pnp_dev;
1087 struct pnp_dev *mpu_dev;
1088 struct snd_card *card;
1089 struct snd_msnd *chip;
1090 int ret;
1091
1092 for ( ; idx < SNDRV_CARDS; idx++) {
1093 if (has_isapnp(idx))
1094 break;
1095 }
1096 if (idx >= SNDRV_CARDS)
1097 return -ENODEV;
1098
1099
1100
1101
1102 pnp_dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1103 if (!pnp_dev)
1104 return -ENODEV;
1105
1106 mpu_dev = pnp_request_card_device(pcard, pid->devs[1].id, NULL);
1107 if (!mpu_dev)
1108 return -ENODEV;
1109
1110 if (!pnp_is_active(pnp_dev) && pnp_activate_dev(pnp_dev) < 0) {
1111 printk(KERN_INFO "msnd_pinnacle: device is inactive\n");
1112 return -EBUSY;
1113 }
1114
1115 if (!pnp_is_active(mpu_dev) && pnp_activate_dev(mpu_dev) < 0) {
1116 printk(KERN_INFO "msnd_pinnacle: MPU device is inactive\n");
1117 return -EBUSY;
1118 }
1119
1120
1121
1122
1123
1124 ret = snd_card_new(&pcard->card->dev,
1125 index[idx], id[idx], THIS_MODULE,
1126 sizeof(struct snd_msnd), &card);
1127 if (ret < 0)
1128 return ret;
1129
1130 chip = card->private_data;
1131 chip->card = card;
1132
1133
1134
1135
1136 io[idx] = pnp_port_start(pnp_dev, 0);
1137 irq[idx] = pnp_irq(pnp_dev, 0);
1138 mem[idx] = pnp_mem_start(pnp_dev, 0);
1139 mpu_io[idx] = pnp_port_start(mpu_dev, 0);
1140 mpu_irq[idx] = pnp_irq(mpu_dev, 0);
1141
1142 set_default_audio_parameters(chip);
1143#ifdef MSND_CLASSIC
1144 chip->type = msndClassic;
1145#else
1146 chip->type = msndPinnacle;
1147#endif
1148 chip->io = io[idx];
1149 chip->irq = irq[idx];
1150 chip->base = mem[idx];
1151
1152 chip->calibrate_signal = calibrate_signal ? 1 : 0;
1153 chip->recsrc = 0;
1154 chip->dspq_data_buff = DSPQ_DATA_BUFF;
1155 chip->dspq_buff_size = DSPQ_BUFF_SIZE;
1156 if (write_ndelay[idx])
1157 clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1158 else
1159 set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1160#ifndef MSND_CLASSIC
1161 if (digital[idx])
1162 set_bit(F_HAVEDIGITAL, &chip->flags);
1163#endif
1164 spin_lock_init(&chip->lock);
1165 ret = snd_msnd_probe(card);
1166 if (ret < 0) {
1167 printk(KERN_ERR LOGNAME ": Probe failed\n");
1168 goto _release_card;
1169 }
1170
1171 ret = snd_msnd_attach(card);
1172 if (ret < 0) {
1173 printk(KERN_ERR LOGNAME ": Attach failed\n");
1174 goto _release_card;
1175 }
1176
1177 pnp_set_card_drvdata(pcard, card);
1178 ++idx;
1179 return 0;
1180
1181_release_card:
1182 snd_card_free(card);
1183 return ret;
1184}
1185
1186static void snd_msnd_pnp_remove(struct pnp_card_link *pcard)
1187{
1188 snd_msnd_unload(pnp_get_card_drvdata(pcard));
1189 pnp_set_card_drvdata(pcard, NULL);
1190}
1191
1192static int isa_registered;
1193static int pnp_registered;
1194
1195static struct pnp_card_device_id msnd_pnpids[] = {
1196
1197 { .id = "BVJ0440", .devs = { { "TBS0000" }, { "TBS0001" } } },
1198 { .id = "" }
1199};
1200
1201MODULE_DEVICE_TABLE(pnp_card, msnd_pnpids);
1202
1203static struct pnp_card_driver msnd_pnpc_driver = {
1204 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1205 .name = "msnd_pinnacle",
1206 .id_table = msnd_pnpids,
1207 .probe = snd_msnd_pnp_detect,
1208 .remove = snd_msnd_pnp_remove,
1209};
1210#endif
1211
1212static int __init snd_msnd_init(void)
1213{
1214 int err;
1215
1216 err = isa_register_driver(&snd_msnd_driver, SNDRV_CARDS);
1217#ifdef CONFIG_PNP
1218 if (!err)
1219 isa_registered = 1;
1220
1221 err = pnp_register_card_driver(&msnd_pnpc_driver);
1222 if (!err)
1223 pnp_registered = 1;
1224
1225 if (isa_registered)
1226 err = 0;
1227#endif
1228 return err;
1229}
1230
1231static void __exit snd_msnd_exit(void)
1232{
1233#ifdef CONFIG_PNP
1234 if (pnp_registered)
1235 pnp_unregister_card_driver(&msnd_pnpc_driver);
1236 if (isa_registered)
1237#endif
1238 isa_unregister_driver(&snd_msnd_driver);
1239}
1240
1241module_init(snd_msnd_init);
1242module_exit(snd_msnd_exit);
1243
1244