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