added mtd driver
[linux-2.4.git] / drivers / sound / pss.c
1 /*
2  * sound/pss.c
3  *
4  * The low level driver for the Personal Sound System (ECHO ESC614).
5  *
6  *
7  * Copyright (C) by Hannu Savolainen 1993-1997
8  *
9  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10  * Version 2 (June 1991). See the "COPYING" file distributed with this software
11  * for more info.
12  *
13  *
14  * Thomas Sailer        ioctl code reworked (vmalloc/vfree removed)
15  * Alan Cox             modularisation, clean up.
16  *
17  * 98-02-21: Vladimir Michl <vladimir.michl@upol.cz>
18  *          Added mixer device for Beethoven ADSP-16 (master volume,
19  *          bass, treble, synth), only for speakers.
20  *          Fixed bug in pss_write (exchange parameters)
21  *          Fixed config port of SB
22  *          Requested two regions for PSS (PSS mixer, PSS config)
23  *          Modified pss_download_boot
24  *          To probe_pss_mss added test for initialize AD1848
25  * 98-05-28: Vladimir Michl <vladimir.michl@upol.cz>
26  *          Fixed computation of mixer volumes
27  * 04-05-1999: Anthony Barbachan <barbcode@xmen.cis.fordham.edu>
28  *          Added code that allows the user to enable his cdrom and/or 
29  *          joystick through the module parameters pss_cdrom_port and 
30  *          pss_enable_joystick.  pss_cdrom_port takes a port address as its
31  *          argument.  pss_enable_joystick takes either a 0 or a non-0 as its
32  *          argument.
33  * 04-06-1999: Anthony Barbachan <barbcode@xmen.cis.fordham.edu>
34  *          Separated some code into new functions for easier reuse.  
35  *          Cleaned up and streamlined new code.  Added code to allow a user 
36  *          to only use this driver for enabling non-sound components 
37  *          through the new module parameter pss_no_sound (flag).  Added 
38  *          code that would allow a user to decide whether the driver should 
39  *          reset the configured hardware settings for the PSS board through 
40  *          the module parameter pss_keep_settings (flag).   This flag will 
41  *          allow a user to free up resources in use by this card if needbe, 
42  *          furthermore it allows him to use this driver to just enable the 
43  *          emulations and then be unloaded as it is no longer needed.  Both 
44  *          new settings are only available to this driver if compiled as a 
45  *          module.  The default settings of all new parameters are set to 
46  *          load the driver as it did in previous versions.
47  * 04-07-1999: Anthony Barbachan <barbcode@xmen.cis.fordham.edu>
48  *          Added module parameter pss_firmware to allow the user to tell 
49  *          the driver where the fireware file is located.  The default 
50  *          setting is the previous hardcoded setting "/etc/sound/pss_synth".
51  * 00-03-03: Christoph Hellwig <chhellwig@infradead.org>
52  *          Adapted to module_init/module_exit
53  * 11-10-2000: Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
54  *          Added __init to probe_pss(), attach_pss() and probe_pss_mpu()
55  * 02-Jan-2001: Chris Rankin
56  *          Specify that this module owns the coprocessor
57  */
58
59
60 #include <linux/config.h>
61 #include <linux/init.h>
62 #include <linux/module.h>
63
64 #include "sound_config.h"
65 #include "sound_firmware.h"
66
67 #include "ad1848.h"
68 #include "mpu401.h"
69
70 /*
71  * PSS registers.
72  */
73 #define REG(x)  (devc->base+x)
74 #define PSS_DATA        0
75 #define PSS_STATUS      2
76 #define PSS_CONTROL     2
77 #define PSS_ID          4
78 #define PSS_IRQACK      4
79 #define PSS_PIO         0x1a
80
81 /*
82  * Config registers
83  */
84 #define CONF_PSS        0x10
85 #define CONF_WSS        0x12
86 #define CONF_SB         0x14
87 #define CONF_CDROM      0x16
88 #define CONF_MIDI       0x18
89
90 /*
91  * Status bits.
92  */
93 #define PSS_FLAG3     0x0800
94 #define PSS_FLAG2     0x0400
95 #define PSS_FLAG1     0x1000
96 #define PSS_FLAG0     0x0800
97 #define PSS_WRITE_EMPTY  0x8000
98 #define PSS_READ_FULL    0x4000
99
100 /*
101  * WSS registers
102  */
103 #define WSS_INDEX 4
104 #define WSS_DATA 5
105
106 /*
107  * WSS status bits
108  */
109 #define WSS_INITIALIZING 0x80
110 #define WSS_AUTOCALIBRATION 0x20
111
112 #define NO_WSS_MIXER    -1
113
114 #include "coproc.h"
115
116 #include "pss_boot.h"
117
118 /* If compiled into kernel, it enable or disable pss mixer */
119 #ifdef CONFIG_PSS_MIXER
120 static unsigned char pss_mixer = 1;
121 #else
122 static unsigned char pss_mixer = 0;
123 #endif
124
125
126 typedef struct pss_mixerdata {
127         unsigned int volume_l;
128         unsigned int volume_r;
129         unsigned int bass;
130         unsigned int treble;
131         unsigned int synth;
132 } pss_mixerdata;
133
134 typedef struct pss_confdata {
135         int             base;
136         int             irq;
137         int             dma;
138         int            *osp;
139         pss_mixerdata   mixer;
140         int             ad_mixer_dev;
141 } pss_confdata;
142   
143 static pss_confdata pss_data;
144 static pss_confdata *devc = &pss_data;
145
146 static int      pss_initialized = 0;
147 static int      nonstandard_microcode = 0;
148 static int      pss_cdrom_port = -1;    /* Parameter for the PSS cdrom port */
149 static int      pss_enable_joystick = 0;/* Parameter for enabling the joystick */
150
151 static void pss_write(pss_confdata *devc, int data)
152 {
153         int i, limit;
154
155         limit = jiffies + HZ/10;        /* The timeout is 0.1 seconds */
156         /*
157          * Note! the i<5000000 is an emergency exit. The dsp_command() is sometimes
158          * called while interrupts are disabled. This means that the timer is
159          * disabled also. However the timeout situation is a abnormal condition.
160          * Normally the DSP should be ready to accept commands after just couple of
161          * loops.
162          */
163
164         for (i = 0; i < 5000000 && time_before(jiffies, limit); i++)
165         {
166                 if (inw(REG(PSS_STATUS)) & PSS_WRITE_EMPTY)
167                 {
168                         outw(data, REG(PSS_DATA));
169                         return;
170                 }
171         }
172         printk(KERN_WARNING "PSS: DSP Command (%04x) Timeout.\n", data);
173 }
174
175 int __init probe_pss(struct address_info *hw_config)
176 {
177         unsigned short id;
178         int irq, dma;
179
180         devc->base = hw_config->io_base;
181         irq = devc->irq = hw_config->irq;
182         dma = devc->dma = hw_config->dma;
183         devc->osp = hw_config->osp;
184
185         if (devc->base != 0x220 && devc->base != 0x240)
186                 if (devc->base != 0x230 && devc->base != 0x250)         /* Some cards use these */
187                         return 0;
188
189         if (check_region(devc->base, 0x19 /*16*/)) { 
190                 printk(KERN_ERR "PSS: I/O port conflict\n");
191                 return 0;
192         }
193         id = inw(REG(PSS_ID));
194         if ((id >> 8) != 'E') {
195                 printk(KERN_ERR "No PSS signature detected at 0x%x (0x%x)\n",  devc->base,  id); 
196                 return 0;
197         }
198         return 1;
199 }
200
201 static int set_irq(pss_confdata * devc, int dev, int irq)
202 {
203         static unsigned short irq_bits[16] =
204         {
205                 0x0000, 0x0000, 0x0000, 0x0008,
206                 0x0000, 0x0010, 0x0000, 0x0018,
207                 0x0000, 0x0020, 0x0028, 0x0030,
208                 0x0038, 0x0000, 0x0000, 0x0000
209         };
210
211         unsigned short  tmp, bits;
212
213         if (irq < 0 || irq > 15)
214                 return 0;
215
216         tmp = inw(REG(dev)) & ~0x38;    /* Load confreg, mask IRQ bits out */
217
218         if ((bits = irq_bits[irq]) == 0 && irq != 0)
219         {
220                 printk(KERN_ERR "PSS: Invalid IRQ %d\n", irq);
221                 return 0;
222         }
223         outw(tmp | bits, REG(dev));
224         return 1;
225 }
226
227 static int set_io_base(pss_confdata * devc, int dev, int base)
228 {
229         unsigned short  tmp = inw(REG(dev)) & 0x003f;
230         unsigned short  bits = (base & 0x0ffc) << 4;
231
232         outw(bits | tmp, REG(dev));
233
234         return 1;
235 }
236
237 static int set_dma(pss_confdata * devc, int dev, int dma)
238 {
239         static unsigned short dma_bits[8] =
240         {
241                 0x0001, 0x0002, 0x0000, 0x0003,
242                 0x0000, 0x0005, 0x0006, 0x0007
243         };
244
245         unsigned short  tmp, bits;
246
247         if (dma < 0 || dma > 7)
248                 return 0;
249
250         tmp = inw(REG(dev)) & ~0x07;    /* Load confreg, mask DMA bits out */
251
252         if ((bits = dma_bits[dma]) == 0 && dma != 4)
253         {
254                   printk(KERN_ERR "PSS: Invalid DMA %d\n", dma);
255                   return 0;
256         }
257         outw(tmp | bits, REG(dev));
258         return 1;
259 }
260
261 static int pss_reset_dsp(pss_confdata * devc)
262 {
263         unsigned long   i, limit = jiffies + HZ/10;
264
265         outw(0x2000, REG(PSS_CONTROL));
266         for (i = 0; i < 32768 && (limit-jiffies >= 0); i++)
267                 inw(REG(PSS_CONTROL));
268         outw(0x0000, REG(PSS_CONTROL));
269         return 1;
270 }
271
272 static int pss_put_dspword(pss_confdata * devc, unsigned short word)
273 {
274         int i, val;
275
276         for (i = 0; i < 327680; i++)
277         {
278                 val = inw(REG(PSS_STATUS));
279                 if (val & PSS_WRITE_EMPTY)
280                 {
281                         outw(word, REG(PSS_DATA));
282                         return 1;
283                 }
284         }
285         return 0;
286 }
287
288 static int pss_get_dspword(pss_confdata * devc, unsigned short *word)
289 {
290         int i, val;
291
292         for (i = 0; i < 327680; i++)
293         {
294                 val = inw(REG(PSS_STATUS));
295                 if (val & PSS_READ_FULL)
296                 {
297                         *word = inw(REG(PSS_DATA));
298                         return 1;
299                 }
300         }
301         return 0;
302 }
303
304 static int pss_download_boot(pss_confdata * devc, unsigned char *block, int size, int flags)
305 {
306         int i, limit, val, count;
307
308         if (flags & CPF_FIRST)
309         {
310 /*_____ Warn DSP software that a boot is coming */
311                 outw(0x00fe, REG(PSS_DATA));
312
313                 limit = jiffies + HZ/10;
314                 for (i = 0; i < 32768 && time_before(jiffies, limit); i++)
315                         if (inw(REG(PSS_DATA)) == 0x5500)
316                                 break;
317
318                 outw(*block++, REG(PSS_DATA));
319                 pss_reset_dsp(devc);
320         }
321         count = 1;
322         while ((flags&CPF_LAST) || count<size )
323         {
324                 int j;
325
326                 for (j = 0; j < 327670; j++)
327                 {
328 /*_____ Wait for BG to appear */
329                         if (inw(REG(PSS_STATUS)) & PSS_FLAG3)
330                                 break;
331                 }
332
333                 if (j == 327670)
334                 {
335                         /* It's ok we timed out when the file was empty */
336                         if (count >= size && flags & CPF_LAST)
337                                 break;
338                         else
339                         {
340                                 printk("\n");
341                                 printk(KERN_ERR "PSS: Download timeout problems, byte %d=%d\n", count, size);
342                                 return 0;
343                         }
344                 }
345 /*_____ Send the next byte */
346                 if (count >= size) 
347                 {
348                         /* If not data in block send 0xffff */
349                         outw (0xffff, REG (PSS_DATA));
350                 }
351                 else
352                 {
353                         /*_____ Send the next byte */
354                         outw (*block++, REG (PSS_DATA));
355                 };
356                 count++;
357         }
358
359         if (flags & CPF_LAST)
360         {
361 /*_____ Why */
362                 outw(0, REG(PSS_DATA));
363
364                 limit = jiffies + HZ/10;
365                 for (i = 0; i < 32768 && (limit - jiffies >= 0); i++)
366                         val = inw(REG(PSS_STATUS));
367
368                 limit = jiffies + HZ/10;
369                 for (i = 0; i < 32768 && (limit-jiffies >= 0); i++)
370                 {
371                         val = inw(REG(PSS_STATUS));
372                         if (val & 0x4000)
373                                 break;
374                 }
375
376                 /* now read the version */
377                 for (i = 0; i < 32000; i++)
378                 {
379                         val = inw(REG(PSS_STATUS));
380                         if (val & PSS_READ_FULL)
381                                 break;
382                 }
383                 if (i == 32000)
384                         return 0;
385
386                 val = inw(REG(PSS_DATA));
387                 /* printk( "<PSS: microcode version %d.%d loaded>",  val/16,  val % 16); */
388         }
389         return 1;
390 }
391
392 /* Mixer */
393 static void set_master_volume(pss_confdata *devc, int left, int right)
394 {
395         static unsigned char log_scale[101] =  {
396                 0xdb, 0xe0, 0xe3, 0xe5, 0xe7, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xed, 0xee,
397                 0xef, 0xef, 0xf0, 0xf0, 0xf1, 0xf1, 0xf2, 0xf2, 0xf2, 0xf3, 0xf3, 0xf3,
398                 0xf4, 0xf4, 0xf4, 0xf5, 0xf5, 0xf5, 0xf5, 0xf6, 0xf6, 0xf6, 0xf6, 0xf7,
399                 0xf7, 0xf7, 0xf7, 0xf7, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf9, 0xf9, 0xf9,
400                 0xf9, 0xf9, 0xf9, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfa, 0xfb, 0xfb,
401                 0xfb, 0xfb, 0xfb, 0xfb, 0xfb, 0xfb, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc,
402                 0xfc, 0xfc, 0xfc, 0xfc, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd,
403                 0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
404                 0xfe, 0xfe, 0xff, 0xff, 0xff
405         };
406         pss_write(devc, 0x0010);
407         pss_write(devc, log_scale[left] | 0x0000);
408         pss_write(devc, 0x0010);
409         pss_write(devc, log_scale[right] | 0x0100);
410 }
411
412 static void set_synth_volume(pss_confdata *devc, int volume)
413 {
414         int vol = ((0x8000*volume)/100L);
415         pss_write(devc, 0x0080);
416         pss_write(devc, vol);
417         pss_write(devc, 0x0081);
418         pss_write(devc, vol);
419 }
420
421 static void set_bass(pss_confdata *devc, int level)
422 {
423         int vol = (int)(((0xfd - 0xf0) * level)/100L) + 0xf0;
424         pss_write(devc, 0x0010);
425         pss_write(devc, vol | 0x0200);
426 };
427
428 static void set_treble(pss_confdata *devc, int level)
429 {       
430         int vol = (((0xfd - 0xf0) * level)/100L) + 0xf0;
431         pss_write(devc, 0x0010);
432         pss_write(devc, vol | 0x0300);
433 };
434
435 static void pss_mixer_reset(pss_confdata *devc)
436 {
437         set_master_volume(devc, 33, 33);
438         set_bass(devc, 50);
439         set_treble(devc, 50);
440         set_synth_volume(devc, 30);
441         pss_write (devc, 0x0010);
442         pss_write (devc, 0x0800 | 0xce);        /* Stereo */
443         
444         if(pss_mixer)
445         {
446                 devc->mixer.volume_l = devc->mixer.volume_r = 33;
447                 devc->mixer.bass = 50;
448                 devc->mixer.treble = 50;
449                 devc->mixer.synth = 30;
450         }
451 }
452
453 static int set_volume_mono(caddr_t p, int *aleft)
454 {
455         int left;
456         unsigned volume;
457         if (get_user(volume, (unsigned *)p))
458                 return -EFAULT;
459         
460         left = volume & 0xff;
461         if (left > 100)
462                 left = 100;
463         *aleft = left;
464         return 0;
465 }
466
467 static int set_volume_stereo(caddr_t p, int *aleft, int *aright)
468 {
469         int left, right;
470         unsigned volume;
471         if (get_user(volume, (unsigned *)p))
472                 return -EFAULT;
473
474         left = volume & 0xff;
475         if (left > 100)
476                 left = 100;
477         right = (volume >> 8) & 0xff;
478         if (right > 100)
479                 right = 100;
480         *aleft = left;
481         *aright = right;
482         return 0;
483 }
484
485 static int ret_vol_mono(int left)
486 {
487         return ((left << 8) | left);
488 }
489
490 static int ret_vol_stereo(int left, int right)
491 {
492         return ((right << 8) | left);
493 }
494
495 static int call_ad_mixer(pss_confdata *devc,unsigned int cmd, caddr_t arg)
496 {
497         if (devc->ad_mixer_dev != NO_WSS_MIXER) 
498                 return mixer_devs[devc->ad_mixer_dev]->ioctl(devc->ad_mixer_dev, cmd, arg);
499         else 
500                 return -EINVAL;
501 }
502
503 static int pss_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
504 {
505         pss_confdata *devc = mixer_devs[dev]->devc;
506         int cmdf = cmd & 0xff;
507         
508         if ((cmdf != SOUND_MIXER_VOLUME) && (cmdf != SOUND_MIXER_BASS) &&
509                 (cmdf != SOUND_MIXER_TREBLE) && (cmdf != SOUND_MIXER_SYNTH) &&
510                 (cmdf != SOUND_MIXER_DEVMASK) && (cmdf != SOUND_MIXER_STEREODEVS) &&
511                 (cmdf != SOUND_MIXER_RECMASK) && (cmdf != SOUND_MIXER_CAPS) &&
512                 (cmdf != SOUND_MIXER_RECSRC)) 
513         {
514                 return call_ad_mixer(devc, cmd, arg);
515         }
516         
517         if (((cmd >> 8) & 0xff) != 'M') 
518                 return -EINVAL;
519                 
520         if (_SIOC_DIR (cmd) & _SIOC_WRITE)
521         {
522                 switch (cmdf)   
523                 {
524                         case SOUND_MIXER_RECSRC:
525                                 if (devc->ad_mixer_dev != NO_WSS_MIXER)
526                                         return call_ad_mixer(devc, cmd, arg);
527                                 else
528                                 {
529                                         int v;
530                                         if (get_user(v, (int *)arg))
531                                                 return -EFAULT;
532                                         if (v != 0)
533                                                 return -EINVAL;
534                                         return 0;
535                                 }
536                         case SOUND_MIXER_VOLUME:
537                                 if (set_volume_stereo(arg,
538                                         &devc->mixer.volume_l,
539                                         &devc->mixer.volume_r))
540                                         return -EFAULT;
541                                 set_master_volume(devc, devc->mixer.volume_l,
542                                         devc->mixer.volume_r);
543                                 return ret_vol_stereo(devc->mixer.volume_l,
544                                         devc->mixer.volume_r);
545                   
546                         case SOUND_MIXER_BASS:
547                                 if (set_volume_mono(arg, &devc->mixer.bass))
548                                         return -EFAULT;
549                                 set_bass(devc, devc->mixer.bass);
550                                 return ret_vol_mono(devc->mixer.bass);
551                   
552                         case SOUND_MIXER_TREBLE:
553                                 if (set_volume_mono(arg, &devc->mixer.treble))
554                                         return -EFAULT;
555                                 set_treble(devc, devc->mixer.treble);
556                                 return ret_vol_mono(devc->mixer.treble);
557                   
558                         case SOUND_MIXER_SYNTH:
559                                 if (set_volume_mono(arg, &devc->mixer.synth))
560                                         return -EFAULT;
561                                 set_synth_volume(devc, devc->mixer.synth);
562                                 return ret_vol_mono(devc->mixer.synth);
563                   
564                         default:
565                                 return -EINVAL;
566                 }
567         }
568         else                    
569         {
570                 int val, and_mask = 0, or_mask = 0;
571                 /*
572                  * Return parameters
573                  */
574                 switch (cmdf)
575                 {
576                         case SOUND_MIXER_DEVMASK:
577                                 if (call_ad_mixer(devc, cmd, arg) == -EINVAL)
578                                         break;
579                                 and_mask = ~0;
580                                 or_mask = SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_SYNTH;
581                                 break;
582                   
583                         case SOUND_MIXER_STEREODEVS:
584                                 if (call_ad_mixer(devc, cmd, arg) == -EINVAL)
585                                         break;
586                                 and_mask = ~0;
587                                 or_mask = SOUND_MASK_VOLUME;
588                                 break;
589                   
590                         case SOUND_MIXER_RECMASK:
591                                 if (devc->ad_mixer_dev != NO_WSS_MIXER)
592                                         return call_ad_mixer(devc, cmd, arg);
593                                 break;
594
595                         case SOUND_MIXER_CAPS:
596                                 if (devc->ad_mixer_dev != NO_WSS_MIXER)
597                                         return call_ad_mixer(devc, cmd, arg);
598                                 or_mask = SOUND_CAP_EXCL_INPUT;
599                                 break;
600
601                         case SOUND_MIXER_RECSRC:
602                                 if (devc->ad_mixer_dev != NO_WSS_MIXER)
603                                         return call_ad_mixer(devc, cmd, arg);
604                                 break;
605
606                         case SOUND_MIXER_VOLUME:
607                                 or_mask =  ret_vol_stereo(devc->mixer.volume_l, devc->mixer.volume_r);
608                                 break;
609                           
610                         case SOUND_MIXER_BASS:
611                                 or_mask =  ret_vol_mono(devc->mixer.bass);
612                                 break;
613                           
614                         case SOUND_MIXER_TREBLE:
615                                 or_mask = ret_vol_mono(devc->mixer.treble);
616                                 break;
617                           
618                         case SOUND_MIXER_SYNTH:
619                                 or_mask = ret_vol_mono(devc->mixer.synth);
620                                 break;
621                         default:
622                                 return -EINVAL;
623                 }
624                 if (get_user(val, (int *)arg))
625                         return -EFAULT;
626                 val &= and_mask;
627                 val |= or_mask;
628                 if (put_user(val, (int *)arg))
629                         return -EFAULT;
630                 return val;
631         }
632 }
633
634 static struct mixer_operations pss_mixer_operations =
635 {
636         owner:  THIS_MODULE,
637         id:     "SOUNDPORT",
638         name:   "PSS-AD1848",
639         ioctl:  pss_mixer_ioctl
640 };
641
642 void disable_all_emulations(void)
643 {
644         outw(0x0000, REG(CONF_PSS));    /* 0x0400 enables joystick */
645         outw(0x0000, REG(CONF_WSS));
646         outw(0x0000, REG(CONF_SB));
647         outw(0x0000, REG(CONF_MIDI));
648         outw(0x0000, REG(CONF_CDROM));
649 }
650
651 void configure_nonsound_components(void)
652 {
653         /* Configure Joystick port */
654
655         if(pss_enable_joystick)
656         {
657                 outw(0x0400, REG(CONF_PSS));    /* 0x0400 enables joystick */
658                 printk(KERN_INFO "PSS: joystick enabled.\n");
659         }
660         else
661         {
662                 printk(KERN_INFO "PSS: joystick port not enabled.\n");
663         }
664
665         /* Configure CDROM port */
666
667         if(pss_cdrom_port == -1)        /* If cdrom port enablation wasn't requested */
668         {
669                 printk(KERN_INFO "PSS: CDROM port not enabled.\n");
670         }
671         else if(check_region(pss_cdrom_port, 2))
672         {
673                 printk(KERN_ERR "PSS: CDROM I/O port conflict.\n");
674         }
675         else if(!set_io_base(devc, CONF_CDROM, pss_cdrom_port))
676         {
677                 printk(KERN_ERR "PSS: CDROM I/O port could not be set.\n");
678         }
679         else                                    /* CDROM port successfully configured */
680         {
681                 printk(KERN_INFO "PSS: CDROM I/O port set to 0x%x.\n", pss_cdrom_port);
682         }
683 }
684
685 void __init attach_pss(struct address_info *hw_config)
686 {
687         unsigned short  id;
688         char tmp[100];
689
690         devc->base = hw_config->io_base;
691         devc->irq = hw_config->irq;
692         devc->dma = hw_config->dma;
693         devc->osp = hw_config->osp;
694         devc->ad_mixer_dev = NO_WSS_MIXER;
695
696         if (!probe_pss(hw_config))
697                 return;
698
699         request_region(hw_config->io_base, 0x10, "PSS mixer, SB emulation");
700         request_region(hw_config->io_base + 0x10, 0x9, "PSS config");
701
702         id = inw(REG(PSS_ID)) & 0x00ff;
703
704         /*
705          * Disable all emulations. Will be enabled later (if required).
706          */
707          
708         disable_all_emulations();
709
710 #if YOU_REALLY_WANT_TO_ALLOCATE_THESE_RESOURCES
711         if (sound_alloc_dma(hw_config->dma, "PSS"))
712         {
713                 printk("pss.c: Can't allocate DMA channel.\n");
714                 return;
715         }
716         if (!set_irq(devc, CONF_PSS, devc->irq))
717         {
718                 printk("PSS: IRQ allocation error.\n");
719                 return;
720         }
721         if (!set_dma(devc, CONF_PSS, devc->dma))
722         {
723                 printk(KERN_ERR "PSS: DMA allocation error\n");
724                 return;
725         }
726 #endif
727
728         configure_nonsound_components();
729         pss_initialized = 1;
730         sprintf(tmp, "ECHO-PSS  Rev. %d", id);
731         conf_printf(tmp, hw_config);
732 }
733
734 int __init probe_pss_mpu(struct address_info *hw_config)
735 {
736         int timeout;
737
738         if (!pss_initialized)
739                 return 0;
740
741         if (check_region(hw_config->io_base, 2))
742         {
743                 printk(KERN_ERR "PSS: MPU I/O port conflict\n");
744                 return 0;
745         }
746         if (!set_io_base(devc, CONF_MIDI, hw_config->io_base))
747         {
748                   printk(KERN_ERR "PSS: MIDI base could not be set.\n");
749                   return 0;
750         }
751         if (!set_irq(devc, CONF_MIDI, hw_config->irq))
752         {
753                   printk(KERN_ERR "PSS: MIDI IRQ allocation error.\n");
754                   return 0;
755         }
756         if (!pss_synthLen)
757         {
758                 printk(KERN_ERR "PSS: Can't enable MPU. MIDI synth microcode not available.\n");
759                 return 0;
760         }
761         if (!pss_download_boot(devc, pss_synth, pss_synthLen, CPF_FIRST | CPF_LAST))
762         {
763                 printk(KERN_ERR "PSS: Unable to load MIDI synth microcode to DSP.\n");
764                 return 0;
765         }
766
767         /*
768          * Finally wait until the DSP algorithm has initialized itself and
769          * deactivates receive interrupt.
770          */
771
772         for (timeout = 900000; timeout > 0; timeout--)
773         {
774                 if ((inb(hw_config->io_base + 1) & 0x80) == 0)  /* Input data avail */
775                         inb(hw_config->io_base);        /* Discard it */
776                 else
777                         break;  /* No more input */
778         }
779
780         return probe_mpu401(hw_config);
781 }
782
783 static int pss_coproc_open(void *dev_info, int sub_device)
784 {
785         switch (sub_device)
786         {
787                 case COPR_MIDI:
788                         if (pss_synthLen == 0)
789                         {
790                                 printk(KERN_ERR "PSS: MIDI synth microcode not available.\n");
791                                 return -EIO;
792                         }
793                         if (nonstandard_microcode)
794                                 if (!pss_download_boot(devc, pss_synth, pss_synthLen, CPF_FIRST | CPF_LAST))
795                         {
796                                 printk(KERN_ERR "PSS: Unable to load MIDI synth microcode to DSP.\n");
797                                 return -EIO;
798                         }
799                         nonstandard_microcode = 0;
800                         break;
801
802                 default:
803                         break;
804         }
805         return 0;
806 }
807
808 static void pss_coproc_close(void *dev_info, int sub_device)
809 {
810         return;
811 }
812
813 static void pss_coproc_reset(void *dev_info)
814 {
815         if (pss_synthLen)
816                 if (!pss_download_boot(devc, pss_synth, pss_synthLen, CPF_FIRST | CPF_LAST))
817                 {
818                         printk(KERN_ERR "PSS: Unable to load MIDI synth microcode to DSP.\n");
819                 }
820         nonstandard_microcode = 0;
821 }
822
823 static int download_boot_block(void *dev_info, copr_buffer * buf)
824 {
825         if (buf->len <= 0 || buf->len > sizeof(buf->data))
826                 return -EINVAL;
827
828         if (!pss_download_boot(devc, buf->data, buf->len, buf->flags))
829         {
830                 printk(KERN_ERR "PSS: Unable to load microcode block to DSP.\n");
831                 return -EIO;
832         }
833         nonstandard_microcode = 1;      /* The MIDI microcode has been overwritten */
834         return 0;
835 }
836
837 static int pss_coproc_ioctl(void *dev_info, unsigned int cmd, caddr_t arg, int local)
838 {
839         copr_buffer *buf;
840         copr_msg *mbuf;
841         copr_debug_buf dbuf;
842         unsigned short tmp;
843         unsigned long flags;
844         unsigned short *data;
845         int i, err;
846         /* printk( "PSS coproc ioctl %x %x %d\n",  cmd,  arg,  local); */
847         
848         switch (cmd) 
849         {
850                 case SNDCTL_COPR_RESET:
851                         pss_coproc_reset(dev_info);
852                         return 0;
853
854                 case SNDCTL_COPR_LOAD:
855                         buf = (copr_buffer *) vmalloc(sizeof(copr_buffer));
856                         if (buf == NULL)
857                                 return -ENOSPC;
858                         if (copy_from_user(buf, arg, sizeof(copr_buffer))) {
859                                 vfree(buf);
860                                 return -EFAULT;
861                         }
862                         err = download_boot_block(dev_info, buf);
863                         vfree(buf);
864                         return err;
865                 
866                 case SNDCTL_COPR_SENDMSG:
867                         mbuf = (copr_msg *)vmalloc(sizeof(copr_msg));
868                         if (mbuf == NULL)
869                                 return -ENOSPC;
870                         if (copy_from_user(mbuf, arg, sizeof(copr_msg))) {
871                                 vfree(mbuf);
872                                 return -EFAULT;
873                         }
874                         data = (unsigned short *)(mbuf->data);
875                         save_flags(flags);
876                         cli();
877                         for (i = 0; i < mbuf->len; i++) {
878                                 if (!pss_put_dspword(devc, *data++)) {
879                                         restore_flags(flags);
880                                         mbuf->len = i;  /* feed back number of WORDs sent */
881                                         err = copy_to_user(arg, mbuf, sizeof(copr_msg));
882                                         vfree(mbuf);
883                                         return err ? -EFAULT : -EIO;
884                                 }
885                         }
886                         restore_flags(flags);
887                         vfree(mbuf);
888                         return 0;
889
890                 case SNDCTL_COPR_RCVMSG:
891                         err = 0;
892                         mbuf = (copr_msg *)vmalloc(sizeof(copr_msg));
893                         if (mbuf == NULL)
894                                 return -ENOSPC;
895                         data = (unsigned short *)mbuf->data;
896                         save_flags(flags);
897                         cli();
898                         for (i = 0; i < sizeof(mbuf->data)/sizeof(unsigned short); i++) {
899                                 mbuf->len = i;  /* feed back number of WORDs read */
900                                 if (!pss_get_dspword(devc, data++)) {
901                                         if (i == 0)
902                                                 err = -EIO;
903                                         break;
904                                 }
905                         }
906                         restore_flags(flags);
907                         if (copy_to_user(arg, mbuf, sizeof(copr_msg)))
908                                 err = -EFAULT;
909                         vfree(mbuf);
910                         return err;
911                 
912                 case SNDCTL_COPR_RDATA:
913                         if (copy_from_user(&dbuf, arg, sizeof(dbuf)))
914                                 return -EFAULT;
915                         save_flags(flags);
916                         cli();
917                         if (!pss_put_dspword(devc, 0x00d0)) {
918                                 restore_flags(flags);
919                                 return -EIO;
920                         }
921                         if (!pss_put_dspword(devc, (unsigned short)(dbuf.parm1 & 0xffff))) {
922                                 restore_flags(flags);
923                                 return -EIO;
924                         }
925                         if (!pss_get_dspword(devc, &tmp)) {
926                                 restore_flags(flags);
927                                 return -EIO;
928                         }
929                         dbuf.parm1 = tmp;
930                         restore_flags(flags);
931                         if (copy_to_user(arg, &dbuf, sizeof(dbuf)))
932                                 return -EFAULT;
933                         return 0;
934                 
935                 case SNDCTL_COPR_WDATA:
936                         if (copy_from_user(&dbuf, arg, sizeof(dbuf)))
937                                 return -EFAULT;
938                         save_flags(flags);
939                         cli();
940                         if (!pss_put_dspword(devc, 0x00d1)) {
941                                 restore_flags(flags);
942                                 return -EIO;
943                         }
944                         if (!pss_put_dspword(devc, (unsigned short) (dbuf.parm1 & 0xffff))) {
945                                 restore_flags(flags);
946                                 return -EIO;
947                         }
948                         tmp = (unsigned int)dbuf.parm2 & 0xffff;
949                         if (!pss_put_dspword(devc, tmp)) {
950                                 restore_flags(flags);
951                                 return -EIO;
952                         }
953                         restore_flags(flags);
954                         return 0;
955                 
956                 case SNDCTL_COPR_WCODE:
957                         if (copy_from_user(&dbuf, arg, sizeof(dbuf)))
958                                 return -EFAULT;
959                         save_flags(flags);
960                         cli();
961                         if (!pss_put_dspword(devc, 0x00d3)) {
962                                 restore_flags(flags);
963                                 return -EIO;
964                         }
965                         if (!pss_put_dspword(devc, (unsigned short)(dbuf.parm1 & 0xffff))) {
966                                 restore_flags(flags);
967                                 return -EIO;
968                         }
969                         tmp = (unsigned int)dbuf.parm2 & 0x00ff;
970                         if (!pss_put_dspword(devc, tmp)) {
971                                 restore_flags(flags);
972                                 return -EIO;
973                         }
974                         tmp = ((unsigned int)dbuf.parm2 >> 8) & 0xffff;
975                         if (!pss_put_dspword(devc, tmp)) {
976                                 restore_flags(flags);
977                                 return -EIO;
978                         }
979                         restore_flags(flags);
980                         return 0;
981                 
982                 case SNDCTL_COPR_RCODE:
983                         if (copy_from_user(&dbuf, arg, sizeof(dbuf)))
984                                 return -EFAULT;
985                         save_flags(flags);
986                         cli();
987                         if (!pss_put_dspword(devc, 0x00d2)) {
988                                 restore_flags(flags);
989                                 return -EIO;
990                         }
991                         if (!pss_put_dspword(devc, (unsigned short)(dbuf.parm1 & 0xffff))) {
992                                 restore_flags(flags);
993                                 return -EIO;
994                         }
995                         if (!pss_get_dspword(devc, &tmp)) { /* Read MSB */
996                                 restore_flags(flags);
997                                 return -EIO;
998                         }
999                         dbuf.parm1 = tmp << 8;
1000                         if (!pss_get_dspword(devc, &tmp)) { /* Read LSB */
1001                                 restore_flags(flags);
1002                                 return -EIO;
1003                         }
1004                         dbuf.parm1 |= tmp & 0x00ff;
1005                         restore_flags(flags);
1006                         if (copy_to_user(arg, &dbuf, sizeof(dbuf)))
1007                                 return -EFAULT;
1008                         return 0;
1009
1010                 default:
1011                         return -EINVAL;
1012         }
1013         return -EINVAL;
1014 }
1015
1016 static coproc_operations pss_coproc_operations =
1017 {
1018         "ADSP-2115",
1019         THIS_MODULE,
1020         pss_coproc_open,
1021         pss_coproc_close,
1022         pss_coproc_ioctl,
1023         pss_coproc_reset,
1024         &pss_data
1025 };
1026
1027 static void __init attach_pss_mpu(struct address_info *hw_config)
1028 {
1029         attach_mpu401(hw_config, THIS_MODULE);  /* Slot 1 */
1030         if (hw_config->slots[1] != -1)  /* The MPU driver installed itself */
1031                 midi_devs[hw_config->slots[1]]->coproc = &pss_coproc_operations;
1032 }
1033
1034 static int __init probe_pss_mss(struct address_info *hw_config)
1035 {
1036         volatile int timeout;
1037
1038         if (!pss_initialized)
1039                 return 0;
1040
1041         if (check_region(hw_config->io_base, 8))
1042         {
1043                   printk(KERN_ERR "PSS: WSS I/O port conflicts.\n");
1044                   return 0;
1045         }
1046         if (!set_io_base(devc, CONF_WSS, hw_config->io_base))
1047         {
1048                 printk("PSS: WSS base not settable.\n");
1049                 return 0;
1050         }
1051         if (!set_irq(devc, CONF_WSS, hw_config->irq))
1052         {
1053                 printk("PSS: WSS IRQ allocation error.\n");
1054                 return 0;
1055         }
1056         if (!set_dma(devc, CONF_WSS, hw_config->dma))
1057         {
1058                 printk(KERN_ERR "PSS: WSS DMA allocation error\n");
1059                 return 0;
1060         }
1061         /*
1062          * For some reason the card returns 0xff in the WSS status register
1063          * immediately after boot. Probably MIDI+SB emulation algorithm
1064          * downloaded to the ADSP2115 spends some time initializing the card.
1065          * Let's try to wait until it finishes this task.
1066          */
1067         for (timeout = 0; timeout < 100000 && (inb(hw_config->io_base + WSS_INDEX) &
1068           WSS_INITIALIZING); timeout++)
1069                 ;
1070
1071         outb((0x0b), hw_config->io_base + WSS_INDEX);   /* Required by some cards */
1072
1073         for (timeout = 0; (inb(hw_config->io_base + WSS_DATA) & WSS_AUTOCALIBRATION) &&
1074           (timeout < 100000); timeout++)
1075                 ;
1076
1077         return probe_ms_sound(hw_config);
1078 }
1079
1080 static void __init attach_pss_mss(struct address_info *hw_config)
1081 {
1082         int        my_mix = -999;       /* gcc shut up */
1083         
1084         devc->ad_mixer_dev = NO_WSS_MIXER;
1085         if (pss_mixer) 
1086         {
1087                 if ((my_mix = sound_install_mixer (MIXER_DRIVER_VERSION,
1088                         "PSS-SPEAKERS and AD1848 (through MSS audio codec)",
1089                         &pss_mixer_operations,
1090                         sizeof (struct mixer_operations),
1091                         devc)) < 0) 
1092                 {
1093                         printk(KERN_ERR "Could not install PSS mixer\n");
1094                         return;
1095                 }
1096         }
1097         pss_mixer_reset(devc);
1098         attach_ms_sound(hw_config, THIS_MODULE);        /* Slot 0 */
1099
1100         if (hw_config->slots[0] != -1)
1101         {
1102                 /* The MSS driver installed itself */
1103                 audio_devs[hw_config->slots[0]]->coproc = &pss_coproc_operations;
1104                 if (pss_mixer && (num_mixers == (my_mix + 2)))
1105                 {
1106                         /* The MSS mixer installed */
1107                         devc->ad_mixer_dev = audio_devs[hw_config->slots[0]]->mixer_dev;
1108                 }
1109         }
1110 }
1111
1112 static inline void __exit unload_pss(struct address_info *hw_config)
1113 {
1114         release_region(hw_config->io_base, 0x10);
1115         release_region(hw_config->io_base+0x10, 0x9);
1116 }
1117
1118 static inline void __exit unload_pss_mpu(struct address_info *hw_config)
1119 {
1120         unload_mpu401(hw_config);
1121 }
1122
1123 static inline void __exit unload_pss_mss(struct address_info *hw_config)
1124 {
1125         unload_ms_sound(hw_config);
1126 }
1127
1128
1129 static struct address_info cfg;
1130 static struct address_info cfg2;
1131 static struct address_info cfg_mpu;
1132
1133 static int pss_io __initdata    = -1;
1134 static int mss_io __initdata    = -1;
1135 static int mss_irq __initdata   = -1;
1136 static int mss_dma __initdata   = -1;
1137 static int mpu_io __initdata    = -1;
1138 static int mpu_irq __initdata   = -1;
1139 static int pss_no_sound __initdata = 0; /* Just configure non-sound components */
1140 static int pss_keep_settings  = 1;      /* Keep hardware settings at module exit */
1141 static char *pss_firmware = "/etc/sound/pss_synth";
1142
1143 MODULE_PARM(pss_io, "i");
1144 MODULE_PARM_DESC(pss_io, "Set i/o base of PSS card (probably 0x220 or 0x240)");
1145 MODULE_PARM(mss_io, "i");
1146 MODULE_PARM_DESC(mss_io, "Set WSS (audio) i/o base (0x530, 0x604, 0xE80, 0xF40, or other. Address must end in 0 or 4 and must be from 0x100 to 0xFF4)");
1147 MODULE_PARM(mss_irq, "i");
1148 MODULE_PARM_DESC(mss_irq, "Set WSS (audio) IRQ (3, 5, 7, 9, 10, 11, 12)");
1149 MODULE_PARM(mss_dma, "i");
1150 MODULE_PARM_DESC(mss_dma, "Set WSS (audio) DMA (0, 1, 3)");
1151 MODULE_PARM(mpu_io, "i");
1152 MODULE_PARM_DESC(mpu_io, "Set MIDI i/o base (0x330 or other. Address must be on 4 location boundaries and must be from 0x100 to 0xFFC)");
1153 MODULE_PARM(mpu_irq, "i");
1154 MODULE_PARM_DESC(mpu_irq, "Set MIDI IRQ (3, 5, 7, 9, 10, 11, 12)");
1155 MODULE_PARM(pss_cdrom_port, "i");
1156 MODULE_PARM_DESC(pss_cdrom_port, "Set the PSS CDROM port i/o base (0x340 or other)");
1157 MODULE_PARM(pss_enable_joystick, "i");
1158 MODULE_PARM_DESC(pss_enable_joystick, "Enables the PSS joystick port (1 to enable, 0 to disable)");
1159 MODULE_PARM(pss_no_sound, "i");
1160 MODULE_PARM_DESC(pss_no_sound, "Configure sound compoents (0 - no, 1 - yes)");
1161 MODULE_PARM(pss_keep_settings, "i");
1162 MODULE_PARM_DESC(pss_keep_settings, "Keep hardware setting at driver unloading (0 - no, 1 - yes)");
1163 MODULE_PARM(pss_firmware, "s");
1164 MODULE_PARM_DESC(pss_firmware, "Location of the firmware file (default - /etc/sound/pss_synth)");
1165 MODULE_PARM(pss_mixer, "b");
1166 MODULE_PARM_DESC(pss_mixer, "Enable (1) or disable (0) PSS mixer (controlling of output volume, bass, treble, synth volume). The mixer is not available on all PSS cards.");
1167 MODULE_AUTHOR("Hannu Savolainen, Vladimir Michl");
1168 MODULE_DESCRIPTION("Module for PSS sound cards (based on AD1848, ADSP-2115 and ESC614). This module includes control of output amplifier and synth volume of the Beethoven ADSP-16 card (this may work with other PSS cards).");
1169 MODULE_LICENSE("GPL");
1170
1171
1172 static int fw_load = 0;
1173 static int pssmpu = 0, pssmss = 0;
1174
1175 /*
1176  *    Load a PSS sound card module
1177  */
1178
1179 static int __init init_pss(void)
1180 {
1181
1182         if(pss_no_sound)                /* If configuring only nonsound components */
1183         {
1184                 cfg.io_base = pss_io;
1185                 if(!probe_pss(&cfg))
1186                         return -ENODEV;
1187                 printk(KERN_INFO "ECHO-PSS  Rev. %d\n", inw(REG(PSS_ID)) & 0x00ff);
1188                 printk(KERN_INFO "PSS: loading in no sound mode.\n");
1189                 disable_all_emulations();
1190                 configure_nonsound_components();
1191                 return 0;
1192         }
1193
1194         cfg.io_base = pss_io;
1195
1196         cfg2.io_base = mss_io;
1197         cfg2.irq = mss_irq;
1198         cfg2.dma = mss_dma;
1199
1200         cfg_mpu.io_base = mpu_io;
1201         cfg_mpu.irq = mpu_irq;
1202
1203         if (cfg.io_base == -1 || cfg2.io_base == -1 || cfg2.irq == -1 || cfg.dma == -1) {
1204                 printk(KERN_INFO "pss: mss_io, mss_dma, mss_irq and pss_io must be set.\n");
1205                 return -EINVAL;
1206         }
1207
1208         if (!pss_synth) {
1209                 fw_load = 1;
1210                 pss_synthLen = mod_firmware_load(pss_firmware, (void *) &pss_synth);
1211         }
1212         if (!probe_pss(&cfg))
1213                 return -ENODEV;
1214         attach_pss(&cfg);
1215         /*
1216          *    Attach stuff
1217          */
1218         if (probe_pss_mpu(&cfg_mpu)) {
1219                 pssmpu = 1;
1220                 attach_pss_mpu(&cfg_mpu);
1221         }
1222         if (probe_pss_mss(&cfg2)) {
1223                 pssmss = 1;
1224                 attach_pss_mss(&cfg2);
1225         }
1226
1227         return 0;
1228 }
1229
1230 static void __exit cleanup_pss(void)
1231 {
1232         if(!pss_no_sound)
1233         {
1234                 if(fw_load && pss_synth)
1235                         vfree(pss_synth);
1236                 if(pssmss)
1237                         unload_pss_mss(&cfg2);
1238                 if(pssmpu)
1239                         unload_pss_mpu(&cfg_mpu);
1240                 unload_pss(&cfg);
1241         }
1242
1243         if(!pss_keep_settings)  /* Keep hardware settings if asked */
1244         {
1245                 disable_all_emulations();
1246                 printk(KERN_INFO "Resetting PSS sound card configurations.\n");
1247         }
1248 }
1249
1250 module_init(init_pss);
1251 module_exit(cleanup_pss);
1252
1253 #ifndef MODULE
1254 static int __init setup_pss(char *str)
1255 {
1256         /* io, mss_io, mss_irq, mss_dma, mpu_io, mpu_irq */
1257         int ints[7];
1258         
1259         str = get_options(str, ARRAY_SIZE(ints), ints);
1260
1261         pss_io  = ints[1];
1262         mss_io  = ints[2];
1263         mss_irq = ints[3];
1264         mss_dma = ints[4];
1265         mpu_io  = ints[5];
1266         mpu_irq = ints[6];
1267
1268         return 1;
1269 }
1270
1271 __setup("pss=", setup_pss);
1272 #endif