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