GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / drivers / opl3 / opl3_lib.c
blobf6ed5d1aaa6faff0d57196fadcdc9269675cc2b3
1 /*
2 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
3 * Hannu Savolainen 1993-1996,
4 * Rob Hooft
5 *
6 * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)
8 * Most if code is ported from OSS/Lite.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sound/opl3.h>
27 #include <asm/io.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <sound/minors.h>
34 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Hannu Savolainen 1993-1996, Rob Hooft");
35 MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)");
36 MODULE_LICENSE("GPL");
38 extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
40 static void snd_opl2_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
42 unsigned long flags;
43 unsigned long port;
46 * The original 2-OP synth requires a quite long delay
47 * after writing to a register.
50 port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
52 spin_lock_irqsave(&opl3->reg_lock, flags);
54 outb((unsigned char) cmd, port);
55 udelay(10);
57 outb((unsigned char) val, port + 1);
58 udelay(30);
60 spin_unlock_irqrestore(&opl3->reg_lock, flags);
63 static void snd_opl3_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
65 unsigned long flags;
66 unsigned long port;
69 * The OPL-3 survives with just two INBs
70 * after writing to a register.
73 port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
75 spin_lock_irqsave(&opl3->reg_lock, flags);
77 outb((unsigned char) cmd, port);
78 inb(opl3->l_port);
79 inb(opl3->l_port);
81 outb((unsigned char) val, port + 1);
82 inb(opl3->l_port);
83 inb(opl3->l_port);
85 spin_unlock_irqrestore(&opl3->reg_lock, flags);
88 static int snd_opl3_detect(struct snd_opl3 * opl3)
91 * This function returns 1 if the FM chip is present at the given I/O port
92 * The detection algorithm plays with the timer built in the FM chip and
93 * looks for a change in the status register.
95 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
96 * boards.
98 * Note2! The chip is initialized if detected.
101 unsigned char stat1, stat2, signature;
103 /* Reset timers 1 and 2 */
104 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
105 /* Reset the IRQ of the FM chip */
106 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
107 signature = stat1 = inb(opl3->l_port); /* Status register */
108 if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */
109 snd_printd("OPL3: stat1 = 0x%x\n", stat1);
110 return -ENODEV;
112 /* Set timer1 to 0xff */
113 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff);
114 /* Unmask and start timer 1 */
115 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START);
116 /* Now we have to delay at least 80us */
117 udelay(200);
118 /* Read status after timers have expired */
119 stat2 = inb(opl3->l_port);
120 /* Stop the timers */
121 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
122 /* Reset the IRQ of the FM chip */
123 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
124 if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */
125 snd_printd("OPL3: stat2 = 0x%x\n", stat2);
126 return -ENODEV;
129 /* If the toplevel code knows exactly the type of chip, don't try
130 to detect it. */
131 if (opl3->hardware != OPL3_HW_AUTO)
132 return 0;
134 /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */
135 if (signature == 0x06) { /* OPL2 */
136 opl3->hardware = OPL3_HW_OPL2;
137 } else {
139 * If we had an OPL4 chip, opl3->hardware would have been set
140 * by the OPL4 driver; so we can assume OPL3 here.
142 if (snd_BUG_ON(!opl3->r_port))
143 return -ENODEV;
144 opl3->hardware = OPL3_HW_OPL3;
146 return 0;
150 * AdLib timers
154 * Timer 1 - 80us
157 static int snd_opl3_timer1_start(struct snd_timer * timer)
159 unsigned long flags;
160 unsigned char tmp;
161 unsigned int ticks;
162 struct snd_opl3 *opl3;
164 opl3 = snd_timer_chip(timer);
165 spin_lock_irqsave(&opl3->timer_lock, flags);
166 ticks = timer->sticks;
167 tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK;
168 opl3->timer_enable = tmp;
169 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */
170 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
171 spin_unlock_irqrestore(&opl3->timer_lock, flags);
172 return 0;
175 static int snd_opl3_timer1_stop(struct snd_timer * timer)
177 unsigned long flags;
178 unsigned char tmp;
179 struct snd_opl3 *opl3;
181 opl3 = snd_timer_chip(timer);
182 spin_lock_irqsave(&opl3->timer_lock, flags);
183 tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START;
184 opl3->timer_enable = tmp;
185 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
186 spin_unlock_irqrestore(&opl3->timer_lock, flags);
187 return 0;
191 * Timer 2 - 320us
194 static int snd_opl3_timer2_start(struct snd_timer * timer)
196 unsigned long flags;
197 unsigned char tmp;
198 unsigned int ticks;
199 struct snd_opl3 *opl3;
201 opl3 = snd_timer_chip(timer);
202 spin_lock_irqsave(&opl3->timer_lock, flags);
203 ticks = timer->sticks;
204 tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK;
205 opl3->timer_enable = tmp;
206 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */
207 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
208 spin_unlock_irqrestore(&opl3->timer_lock, flags);
209 return 0;
212 static int snd_opl3_timer2_stop(struct snd_timer * timer)
214 unsigned long flags;
215 unsigned char tmp;
216 struct snd_opl3 *opl3;
218 opl3 = snd_timer_chip(timer);
219 spin_lock_irqsave(&opl3->timer_lock, flags);
220 tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START;
221 opl3->timer_enable = tmp;
222 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
223 spin_unlock_irqrestore(&opl3->timer_lock, flags);
224 return 0;
231 static struct snd_timer_hardware snd_opl3_timer1 =
233 .flags = SNDRV_TIMER_HW_STOP,
234 .resolution = 80000,
235 .ticks = 256,
236 .start = snd_opl3_timer1_start,
237 .stop = snd_opl3_timer1_stop,
240 static struct snd_timer_hardware snd_opl3_timer2 =
242 .flags = SNDRV_TIMER_HW_STOP,
243 .resolution = 320000,
244 .ticks = 256,
245 .start = snd_opl3_timer2_start,
246 .stop = snd_opl3_timer2_stop,
249 static int snd_opl3_timer1_init(struct snd_opl3 * opl3, int timer_no)
251 struct snd_timer *timer = NULL;
252 struct snd_timer_id tid;
253 int err;
255 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
256 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
257 tid.card = opl3->card->number;
258 tid.device = timer_no;
259 tid.subdevice = 0;
260 if ((err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer)) >= 0) {
261 strcpy(timer->name, "AdLib timer #1");
262 timer->private_data = opl3;
263 timer->hw = snd_opl3_timer1;
265 opl3->timer1 = timer;
266 return err;
269 static int snd_opl3_timer2_init(struct snd_opl3 * opl3, int timer_no)
271 struct snd_timer *timer = NULL;
272 struct snd_timer_id tid;
273 int err;
275 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
276 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
277 tid.card = opl3->card->number;
278 tid.device = timer_no;
279 tid.subdevice = 0;
280 if ((err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer)) >= 0) {
281 strcpy(timer->name, "AdLib timer #2");
282 timer->private_data = opl3;
283 timer->hw = snd_opl3_timer2;
285 opl3->timer2 = timer;
286 return err;
293 void snd_opl3_interrupt(struct snd_hwdep * hw)
295 unsigned char status;
296 struct snd_opl3 *opl3;
297 struct snd_timer *timer;
299 if (hw == NULL)
300 return;
302 opl3 = hw->private_data;
303 status = inb(opl3->l_port);
304 if (!(status & 0x80))
305 return;
307 if (status & 0x40) {
308 timer = opl3->timer1;
309 snd_timer_interrupt(timer, timer->sticks);
311 if (status & 0x20) {
312 timer = opl3->timer2;
313 snd_timer_interrupt(timer, timer->sticks);
317 EXPORT_SYMBOL(snd_opl3_interrupt);
323 static int snd_opl3_free(struct snd_opl3 *opl3)
325 if (snd_BUG_ON(!opl3))
326 return -ENXIO;
327 if (opl3->private_free)
328 opl3->private_free(opl3);
329 snd_opl3_clear_patches(opl3);
330 release_and_free_resource(opl3->res_l_port);
331 release_and_free_resource(opl3->res_r_port);
332 kfree(opl3);
333 return 0;
336 static int snd_opl3_dev_free(struct snd_device *device)
338 struct snd_opl3 *opl3 = device->device_data;
339 return snd_opl3_free(opl3);
342 int snd_opl3_new(struct snd_card *card,
343 unsigned short hardware,
344 struct snd_opl3 **ropl3)
346 static struct snd_device_ops ops = {
347 .dev_free = snd_opl3_dev_free,
349 struct snd_opl3 *opl3;
350 int err;
352 *ropl3 = NULL;
353 opl3 = kzalloc(sizeof(*opl3), GFP_KERNEL);
354 if (opl3 == NULL) {
355 snd_printk(KERN_ERR "opl3: cannot allocate\n");
356 return -ENOMEM;
359 opl3->card = card;
360 opl3->hardware = hardware;
361 spin_lock_init(&opl3->reg_lock);
362 spin_lock_init(&opl3->timer_lock);
364 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops)) < 0) {
365 snd_opl3_free(opl3);
366 return err;
369 *ropl3 = opl3;
370 return 0;
373 EXPORT_SYMBOL(snd_opl3_new);
375 int snd_opl3_init(struct snd_opl3 *opl3)
377 if (! opl3->command) {
378 printk(KERN_ERR "snd_opl3_init: command not defined!\n");
379 return -EINVAL;
382 opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
383 /* Melodic mode */
384 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);
386 switch (opl3->hardware & OPL3_HW_MASK) {
387 case OPL3_HW_OPL2:
388 opl3->max_voices = MAX_OPL2_VOICES;
389 break;
390 case OPL3_HW_OPL3:
391 case OPL3_HW_OPL4:
392 opl3->max_voices = MAX_OPL3_VOICES;
393 /* Enter OPL3 mode */
394 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE);
396 return 0;
399 EXPORT_SYMBOL(snd_opl3_init);
401 int snd_opl3_create(struct snd_card *card,
402 unsigned long l_port,
403 unsigned long r_port,
404 unsigned short hardware,
405 int integrated,
406 struct snd_opl3 ** ropl3)
408 struct snd_opl3 *opl3;
409 int err;
411 *ropl3 = NULL;
412 if ((err = snd_opl3_new(card, hardware, &opl3)) < 0)
413 return err;
414 if (! integrated) {
415 if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) {
416 snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
417 snd_device_free(card, opl3);
418 return -EBUSY;
420 if (r_port != 0 &&
421 (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) {
422 snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
423 snd_device_free(card, opl3);
424 return -EBUSY;
427 opl3->l_port = l_port;
428 opl3->r_port = r_port;
430 switch (opl3->hardware) {
431 /* some hardware doesn't support timers */
432 case OPL3_HW_OPL3_SV:
433 case OPL3_HW_OPL3_CS:
434 case OPL3_HW_OPL3_FM801:
435 opl3->command = &snd_opl3_command;
436 break;
437 default:
438 opl3->command = &snd_opl2_command;
439 if ((err = snd_opl3_detect(opl3)) < 0) {
440 snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
441 opl3->l_port, opl3->r_port);
442 snd_device_free(card, opl3);
443 return err;
445 /* detect routine returns correct hardware type */
446 switch (opl3->hardware & OPL3_HW_MASK) {
447 case OPL3_HW_OPL3:
448 case OPL3_HW_OPL4:
449 opl3->command = &snd_opl3_command;
453 snd_opl3_init(opl3);
455 *ropl3 = opl3;
456 return 0;
459 EXPORT_SYMBOL(snd_opl3_create);
461 int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev)
463 int err;
465 if (timer1_dev >= 0)
466 if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0)
467 return err;
468 if (timer2_dev >= 0) {
469 if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) {
470 snd_device_free(opl3->card, opl3->timer1);
471 opl3->timer1 = NULL;
472 return err;
475 return 0;
478 EXPORT_SYMBOL(snd_opl3_timer_new);
480 int snd_opl3_hwdep_new(struct snd_opl3 * opl3,
481 int device, int seq_device,
482 struct snd_hwdep ** rhwdep)
484 struct snd_hwdep *hw;
485 struct snd_card *card = opl3->card;
486 int err;
488 if (rhwdep)
489 *rhwdep = NULL;
491 /* create hardware dependent device (direct FM) */
493 if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) {
494 snd_device_free(card, opl3);
495 return err;
497 hw->private_data = opl3;
498 hw->exclusive = 1;
499 #ifdef CONFIG_SND_OSSEMUL
500 if (device == 0) {
501 hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
502 sprintf(hw->oss_dev, "dmfm%i", card->number);
504 #endif
505 strcpy(hw->name, hw->id);
506 switch (opl3->hardware & OPL3_HW_MASK) {
507 case OPL3_HW_OPL2:
508 strcpy(hw->name, "OPL2 FM");
509 hw->iface = SNDRV_HWDEP_IFACE_OPL2;
510 break;
511 case OPL3_HW_OPL3:
512 strcpy(hw->name, "OPL3 FM");
513 hw->iface = SNDRV_HWDEP_IFACE_OPL3;
514 break;
515 case OPL3_HW_OPL4:
516 strcpy(hw->name, "OPL4 FM");
517 hw->iface = SNDRV_HWDEP_IFACE_OPL4;
518 break;
521 /* operators - only ioctl */
522 hw->ops.open = snd_opl3_open;
523 hw->ops.ioctl = snd_opl3_ioctl;
524 hw->ops.write = snd_opl3_write;
525 hw->ops.release = snd_opl3_release;
527 opl3->hwdep = hw;
528 opl3->seq_dev_num = seq_device;
529 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && \
530 defined(CONFIG_SND_SEQUENCER_MODULE))
531 if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
532 sizeof(struct snd_opl3 *), &opl3->seq_dev) >= 0) {
533 strcpy(opl3->seq_dev->name, hw->name);
534 *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
536 #endif
537 if (rhwdep)
538 *rhwdep = hw;
539 return 0;
542 EXPORT_SYMBOL(snd_opl3_hwdep_new);
545 * INIT part
548 static int __init alsa_opl3_init(void)
550 return 0;
553 static void __exit alsa_opl3_exit(void)
557 module_init(alsa_opl3_init)
558 module_exit(alsa_opl3_exit)