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