1 /****************************************************************************/
4 * esssolo1.c -- ESS Technology Solo1 (ES1946) audio driver.
6 * Copyright (C) 1998-2001, 2003 Thomas Sailer (t.sailer@alumni.ethz.ch)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Module command line parameters:
26 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
27 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
28 * /dev/midi simple MIDI UART interface, no ioctl
31 * 10.11.1998 0.1 Initial release (without any hardware)
32 * 22.03.1999 0.2 cinfo.blocks should be reset after GETxPTR ioctl.
33 * reported by Johan Maes <joma@telindus.be>
34 * return EAGAIN instead of EBUSY when O_NONBLOCK
35 * read/write cannot be executed
36 * 07.04.1999 0.3 implemented the following ioctl's: SOUND_PCM_READ_RATE,
37 * SOUND_PCM_READ_CHANNELS, SOUND_PCM_READ_BITS;
38 * Alpha fixes reported by Peter Jones <pjones@redhat.com>
39 * 15.06.1999 0.4 Fix bad allocation bug.
40 * Thanks to Deti Fliegl <fliegl@in.tum.de>
41 * 28.06.1999 0.5 Add pci_set_master
42 * 12.08.1999 0.6 Fix MIDI UART crashing the driver
43 * Changed mixer semantics from OSS documented
44 * behaviour to OSS "code behaviour".
45 * Recording might actually work now.
46 * The real DDMA controller address register is at PCI config
47 * 0x60, while the register at 0x18 is used as a placeholder
48 * register for BIOS address allocation. This register
49 * is supposed to be copied into 0x60, according
50 * to the Solo1 datasheet. When I do that, I can access
51 * the DDMA registers except the mask bit, which
52 * is stuck at 1. When I copy the contents of 0x18 +0x10
53 * to the DDMA base register, everything seems to work.
54 * The fun part is that the Windows Solo1 driver doesn't
55 * seem to do these tricks.
56 * Bugs remaining: plops and clicks when starting/stopping playback
57 * 31.08.1999 0.7 add spin_lock_init
58 * replaced current->state = x with set_current_state(x)
59 * 03.09.1999 0.8 change read semantics for MIDI to match
60 * OSS more closely; remove possible wakeup race
61 * 07.10.1999 0.9 Fix initialization; complain if sequencer writes time out
62 * Revised resource grabbing for the FM synthesizer
63 * 28.10.1999 0.10 More waitqueue races fixed
64 * 09.12.1999 0.11 Work around stupid Alpha port issue (virt_to_bus(kmalloc(GFP_DMA)) > 16M)
65 * Disabling recording on Alpha
66 * 12.01.2000 0.12 Prevent some ioctl's from returning bad count values on underrun/overrun;
67 * Tim Janik's BSE (Bedevilled Sound Engine) found this
68 * Integrated (aka redid 8-)) APM support patch by Zach Brown
69 * 07.02.2000 0.13 Use pci_alloc_consistent and pci_register_driver
70 * 19.02.2000 0.14 Use pci_dma_supported to determine if recording should be disabled
71 * 13.03.2000 0.15 Reintroduce initialization of a couple of PCI config space registers
72 * 21.11.2000 0.16 Initialize dma buffers in poll, otherwise poll may return a bogus mask
73 * 12.12.2000 0.17 More dma buffer initializations, patch from
74 * Tjeerd Mulder <tjeerd.mulder@fujitsu-siemens.com>
75 * 31.01.2001 0.18 Register/Unregister gameport, original patch from
76 * Nathaniel Daw <daw@cs.cmu.edu>
77 * Fix SETTRIGGER non OSS API conformity
78 * 10.03.2001 provide abs function, prevent picking up a bogus kernel macro
79 * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au>
80 * 15.05.2001 pci_enable_device moved, return values in probe cleaned
81 * up. Marcus Meissner <mm@caldera.de>
82 * 22.05.2001 0.19 more cleanups, changed PM to PCI 2.4 style, got rid
83 * of global list of devices, using pci device data.
84 * Marcus Meissner <mm@caldera.de>
85 * 03.01.2003 0.20 open_mode fixes from Georg Acher <acher@in.tum.de>
88 /*****************************************************************************/
90 #include <linux/interrupt.h>
91 #include <linux/module.h>
92 #include <linux/string.h>
93 #include <linux/ioport.h>
94 #include <linux/sched.h>
95 #include <linux/delay.h>
96 #include <linux/sound.h>
97 #include <linux/slab.h>
98 #include <linux/soundcard.h>
99 #include <linux/pci.h>
100 #include <linux/bitops.h>
101 #include <linux/init.h>
102 #include <linux/poll.h>
103 #include <linux/spinlock.h>
104 #include <linux/smp_lock.h>
105 #include <linux/gameport.h>
106 #include <linux/wait.h>
109 #include <asm/page.h>
110 #include <asm/uaccess.h>
114 /* --------------------------------------------------------------------- */
116 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
118 /* --------------------------------------------------------------------- */
120 #ifndef PCI_VENDOR_ID_ESS
121 #define PCI_VENDOR_ID_ESS 0x125d
123 #ifndef PCI_DEVICE_ID_ESS_SOLO1
124 #define PCI_DEVICE_ID_ESS_SOLO1 0x1969
127 #define SOLO1_MAGIC ((PCI_VENDOR_ID_ESS<<16)|PCI_DEVICE_ID_ESS_SOLO1)
129 #define DDMABASE_OFFSET 0 /* chip bug workaround kludge */
130 #define DDMABASE_EXTENT 16
132 #define IOBASE_EXTENT 16
133 #define SBBASE_EXTENT 16
134 #define VCBASE_EXTENT (DDMABASE_EXTENT+DDMABASE_OFFSET)
135 #define MPUBASE_EXTENT 4
136 #define GPBASE_EXTENT 4
137 #define GAMEPORT_EXTENT 4
139 #define FMSYNTH_EXTENT 4
141 /* MIDI buffer sizes */
143 #define MIDIINBUF 256
144 #define MIDIOUTBUF 256
146 #define FMODE_MIDI_SHIFT 3
147 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
148 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
150 #define FMODE_DMFM 0x10
152 static struct pci_driver solo1_driver
;
154 /* --------------------------------------------------------------------- */
160 /* the corresponding pci_dev structure */
163 /* soundcore stuff */
169 /* hardware resources */
170 unsigned long iobase
, sbbase
, vcbase
, ddmabase
, mpubase
; /* long for SPARC */
173 /* mixer registers */
175 unsigned short vol
[10];
178 unsigned short micpreamp
;
185 unsigned char clkdiv
;
189 struct semaphore open_sem
;
191 wait_queue_head_t open_wait
;
199 unsigned hwptr
, swptr
;
200 unsigned total_bytes
;
202 unsigned error
; /* over/underrun */
203 wait_queue_head_t wait
;
204 /* redundant, but makes calculations easier */
207 unsigned fragsamples
;
211 unsigned endcleared
:1;
213 unsigned ossfragshift
;
215 unsigned subdivision
;
220 unsigned ird
, iwr
, icnt
;
221 unsigned ord
, owr
, ocnt
;
222 wait_queue_head_t iwait
;
223 wait_queue_head_t owait
;
224 struct timer_list timer
;
225 unsigned char ibuf
[MIDIINBUF
];
226 unsigned char obuf
[MIDIOUTBUF
];
229 struct gameport
*gameport
;
232 /* --------------------------------------------------------------------- */
234 static inline void write_seq(struct solo1_state
*s
, unsigned char data
)
239 /* the local_irq_save stunt is to send the data within the command window */
240 for (i
= 0; i
< 0xffff; i
++) {
241 local_irq_save(flags
);
242 if (!(inb(s
->sbbase
+0xc) & 0x80)) {
243 outb(data
, s
->sbbase
+0xc);
244 local_irq_restore(flags
);
247 local_irq_restore(flags
);
249 printk(KERN_ERR
"esssolo1: write_seq timeout\n");
250 outb(data
, s
->sbbase
+0xc);
253 static inline int read_seq(struct solo1_state
*s
, unsigned char *data
)
259 for (i
= 0; i
< 0xffff; i
++)
260 if (inb(s
->sbbase
+0xe) & 0x80) {
261 *data
= inb(s
->sbbase
+0xa);
264 printk(KERN_ERR
"esssolo1: read_seq timeout\n");
268 static inline int reset_ctrl(struct solo1_state
*s
)
272 outb(3, s
->sbbase
+6); /* clear sequencer and FIFO */
274 outb(0, s
->sbbase
+6);
275 for (i
= 0; i
< 0xffff; i
++)
276 if (inb(s
->sbbase
+0xe) & 0x80)
277 if (inb(s
->sbbase
+0xa) == 0xaa) {
278 write_seq(s
, 0xc6); /* enter enhanced mode */
284 static void write_ctrl(struct solo1_state
*s
, unsigned char reg
, unsigned char data
)
291 static unsigned char read_ctrl(struct solo1_state
*s
, unsigned char reg
)
302 static void write_mixer(struct solo1_state
*s
, unsigned char reg
, unsigned char data
)
304 outb(reg
, s
->sbbase
+4);
305 outb(data
, s
->sbbase
+5);
308 static unsigned char read_mixer(struct solo1_state
*s
, unsigned char reg
)
310 outb(reg
, s
->sbbase
+4);
311 return inb(s
->sbbase
+5);
314 /* --------------------------------------------------------------------- */
316 static inline unsigned ld2(unsigned int x
)
341 /* --------------------------------------------------------------------- */
343 static inline void stop_dac(struct solo1_state
*s
)
347 spin_lock_irqsave(&s
->lock
, flags
);
348 s
->ena
&= ~FMODE_WRITE
;
349 write_mixer(s
, 0x78, 0x10);
350 spin_unlock_irqrestore(&s
->lock
, flags
);
353 static void start_dac(struct solo1_state
*s
)
357 spin_lock_irqsave(&s
->lock
, flags
);
358 if (!(s
->ena
& FMODE_WRITE
) && (s
->dma_dac
.mapped
|| s
->dma_dac
.count
> 0) && s
->dma_dac
.ready
) {
359 s
->ena
|= FMODE_WRITE
;
360 write_mixer(s
, 0x78, 0x12);
362 write_mixer(s
, 0x78, 0x13);
364 spin_unlock_irqrestore(&s
->lock
, flags
);
367 static inline void stop_adc(struct solo1_state
*s
)
371 spin_lock_irqsave(&s
->lock
, flags
);
372 s
->ena
&= ~FMODE_READ
;
373 write_ctrl(s
, 0xb8, 0xe);
374 spin_unlock_irqrestore(&s
->lock
, flags
);
377 static void start_adc(struct solo1_state
*s
)
381 spin_lock_irqsave(&s
->lock
, flags
);
382 if (!(s
->ena
& FMODE_READ
) && (s
->dma_adc
.mapped
|| s
->dma_adc
.count
< (signed)(s
->dma_adc
.dmasize
- 2*s
->dma_adc
.fragsize
))
383 && s
->dma_adc
.ready
) {
384 s
->ena
|= FMODE_READ
;
385 write_ctrl(s
, 0xb8, 0xf);
387 printk(KERN_DEBUG
"solo1: DMAbuffer: 0x%08lx\n", (long)s
->dma_adc
.rawbuf
);
388 printk(KERN_DEBUG
"solo1: DMA: mask: 0x%02x cnt: 0x%04x addr: 0x%08x stat: 0x%02x\n",
389 inb(s
->ddmabase
+0xf), inw(s
->ddmabase
+4), inl(s
->ddmabase
), inb(s
->ddmabase
+8));
391 outb(0, s
->ddmabase
+0xd); /* master reset */
392 outb(1, s
->ddmabase
+0xf); /* mask */
393 outb(0x54/*0x14*/, s
->ddmabase
+0xb); /* DMA_MODE_READ | DMA_MODE_AUTOINIT */
394 outl(virt_to_bus(s
->dma_adc
.rawbuf
), s
->ddmabase
);
395 outw(s
->dma_adc
.dmasize
-1, s
->ddmabase
+4);
396 outb(0, s
->ddmabase
+0xf);
398 spin_unlock_irqrestore(&s
->lock
, flags
);
400 printk(KERN_DEBUG
"solo1: start DMA: reg B8: 0x%02x SBstat: 0x%02x\n"
401 KERN_DEBUG
"solo1: DMA: stat: 0x%02x cnt: 0x%04x mask: 0x%02x\n",
402 read_ctrl(s
, 0xb8), inb(s
->sbbase
+0xc),
403 inb(s
->ddmabase
+8), inw(s
->ddmabase
+4), inb(s
->ddmabase
+0xf));
404 printk(KERN_DEBUG
"solo1: A1: 0x%02x A2: 0x%02x A4: 0x%02x A5: 0x%02x A8: 0x%02x\n"
405 KERN_DEBUG
"solo1: B1: 0x%02x B2: 0x%02x B4: 0x%02x B7: 0x%02x B8: 0x%02x B9: 0x%02x\n",
406 read_ctrl(s
, 0xa1), read_ctrl(s
, 0xa2), read_ctrl(s
, 0xa4), read_ctrl(s
, 0xa5), read_ctrl(s
, 0xa8),
407 read_ctrl(s
, 0xb1), read_ctrl(s
, 0xb2), read_ctrl(s
, 0xb4), read_ctrl(s
, 0xb7), read_ctrl(s
, 0xb8),
412 /* --------------------------------------------------------------------- */
414 #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
415 #define DMABUF_MINORDER 1
417 static inline void dealloc_dmabuf(struct solo1_state
*s
, struct dmabuf
*db
)
419 struct page
*page
, *pend
;
422 /* undo marking the pages as reserved */
423 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
424 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
425 ClearPageReserved(page
);
426 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->buforder
, db
->rawbuf
, db
->dmaaddr
);
429 db
->mapped
= db
->ready
= 0;
432 static int prog_dmabuf(struct solo1_state
*s
, struct dmabuf
*db
)
435 unsigned bytespersec
;
436 unsigned bufs
, sample_shift
= 0;
437 struct page
*page
, *pend
;
439 db
->hwptr
= db
->swptr
= db
->total_bytes
= db
->count
= db
->error
= db
->endcleared
= 0;
441 db
->ready
= db
->mapped
= 0;
442 for (order
= DMABUF_DEFAULTORDER
; order
>= DMABUF_MINORDER
; order
--)
443 if ((db
->rawbuf
= pci_alloc_consistent(s
->dev
, PAGE_SIZE
<< order
, &db
->dmaaddr
)))
447 db
->buforder
= order
;
448 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
449 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
450 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
451 SetPageReserved(page
);
453 if (s
->fmt
& (AFMT_S16_LE
| AFMT_U16_LE
))
457 bytespersec
= s
->rate
<< sample_shift
;
458 bufs
= PAGE_SIZE
<< db
->buforder
;
459 if (db
->ossfragshift
) {
460 if ((1000 << db
->ossfragshift
) < bytespersec
)
461 db
->fragshift
= ld2(bytespersec
/1000);
463 db
->fragshift
= db
->ossfragshift
;
465 db
->fragshift
= ld2(bytespersec
/100/(db
->subdivision
? db
->subdivision
: 1));
466 if (db
->fragshift
< 3)
469 db
->numfrag
= bufs
>> db
->fragshift
;
470 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
472 db
->numfrag
= bufs
>> db
->fragshift
;
474 db
->fragsize
= 1 << db
->fragshift
;
475 if (db
->ossmaxfrags
>= 4 && db
->ossmaxfrags
< db
->numfrag
)
476 db
->numfrag
= db
->ossmaxfrags
;
477 db
->fragsamples
= db
->fragsize
>> sample_shift
;
478 db
->dmasize
= db
->numfrag
<< db
->fragshift
;
483 static inline int prog_dmabuf_adc(struct solo1_state
*s
)
489 /* check if PCI implementation supports 24bit busmaster DMA */
490 if (s
->dev
->dma_mask
> 0xffffff)
492 if ((c
= prog_dmabuf(s
, &s
->dma_adc
)))
494 va
= s
->dma_adc
.dmaaddr
;
495 if ((va
& ~((1<<24)-1)))
496 panic("solo1: buffer above 16M boundary");
497 outb(0, s
->ddmabase
+0xd); /* clear */
498 outb(1, s
->ddmabase
+0xf); /* mask */
499 /*outb(0, s->ddmabase+8);*/ /* enable (enable is active low!) */
500 outb(0x54, s
->ddmabase
+0xb); /* DMA_MODE_READ | DMA_MODE_AUTOINIT */
501 outl(va
, s
->ddmabase
);
502 outw(s
->dma_adc
.dmasize
-1, s
->ddmabase
+4);
503 c
= - s
->dma_adc
.fragsamples
;
504 write_ctrl(s
, 0xa4, c
);
505 write_ctrl(s
, 0xa5, c
>> 8);
506 outb(0, s
->ddmabase
+0xf);
507 s
->dma_adc
.ready
= 1;
511 static inline int prog_dmabuf_dac(struct solo1_state
*s
)
517 if ((c
= prog_dmabuf(s
, &s
->dma_dac
)))
519 memset(s
->dma_dac
.rawbuf
, (s
->fmt
& (AFMT_U8
| AFMT_U16_LE
)) ? 0 : 0x80, s
->dma_dac
.dmasize
); /* almost correct for U16 */
520 va
= s
->dma_dac
.dmaaddr
;
521 if ((va
^ (va
+ s
->dma_dac
.dmasize
- 1)) & ~((1<<20)-1))
522 panic("solo1: buffer crosses 1M boundary");
524 /* warning: s->dma_dac.dmasize & 0xffff must not be zero! i.e. this limits us to a 32k buffer */
525 outw(s
->dma_dac
.dmasize
, s
->iobase
+4);
526 c
= - s
->dma_dac
.fragsamples
;
527 write_mixer(s
, 0x74, c
);
528 write_mixer(s
, 0x76, c
>> 8);
529 outb(0xa, s
->iobase
+6);
530 s
->dma_dac
.ready
= 1;
534 static inline void clear_advance(void *buf
, unsigned bsize
, unsigned bptr
, unsigned len
, unsigned char c
)
536 if (bptr
+ len
> bsize
) {
537 unsigned x
= bsize
- bptr
;
538 memset(((char *)buf
) + bptr
, c
, x
);
542 memset(((char *)buf
) + bptr
, c
, len
);
545 /* call with spinlock held! */
547 static void solo1_update_ptr(struct solo1_state
*s
)
552 /* update ADC pointer */
553 if (s
->ena
& FMODE_READ
) {
554 hwptr
= (s
->dma_adc
.dmasize
- 1 - inw(s
->ddmabase
+4)) % s
->dma_adc
.dmasize
;
555 diff
= (s
->dma_adc
.dmasize
+ hwptr
- s
->dma_adc
.hwptr
) % s
->dma_adc
.dmasize
;
556 s
->dma_adc
.hwptr
= hwptr
;
557 s
->dma_adc
.total_bytes
+= diff
;
558 s
->dma_adc
.count
+= diff
;
560 printk(KERN_DEBUG
"solo1: rd: hwptr %u swptr %u dmasize %u count %u\n",
561 s
->dma_adc
.hwptr
, s
->dma_adc
.swptr
, s
->dma_adc
.dmasize
, s
->dma_adc
.count
);
563 if (s
->dma_adc
.mapped
) {
564 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
565 wake_up(&s
->dma_adc
.wait
);
567 if (s
->dma_adc
.count
> (signed)(s
->dma_adc
.dmasize
- ((3 * s
->dma_adc
.fragsize
) >> 1))) {
568 s
->ena
&= ~FMODE_READ
;
569 write_ctrl(s
, 0xb8, 0xe);
572 if (s
->dma_adc
.count
> 0)
573 wake_up(&s
->dma_adc
.wait
);
576 /* update DAC pointer */
577 if (s
->ena
& FMODE_WRITE
) {
578 hwptr
= (s
->dma_dac
.dmasize
- inw(s
->iobase
+4)) % s
->dma_dac
.dmasize
;
579 diff
= (s
->dma_dac
.dmasize
+ hwptr
- s
->dma_dac
.hwptr
) % s
->dma_dac
.dmasize
;
580 s
->dma_dac
.hwptr
= hwptr
;
581 s
->dma_dac
.total_bytes
+= diff
;
583 printk(KERN_DEBUG
"solo1: wr: hwptr %u swptr %u dmasize %u count %u\n",
584 s
->dma_dac
.hwptr
, s
->dma_dac
.swptr
, s
->dma_dac
.dmasize
, s
->dma_dac
.count
);
586 if (s
->dma_dac
.mapped
) {
587 s
->dma_dac
.count
+= diff
;
588 if (s
->dma_dac
.count
>= (signed)s
->dma_dac
.fragsize
)
589 wake_up(&s
->dma_dac
.wait
);
591 s
->dma_dac
.count
-= diff
;
592 if (s
->dma_dac
.count
<= 0) {
593 s
->ena
&= ~FMODE_WRITE
;
594 write_mixer(s
, 0x78, 0x12);
596 } else if (s
->dma_dac
.count
<= (signed)s
->dma_dac
.fragsize
&& !s
->dma_dac
.endcleared
) {
597 clear_advance(s
->dma_dac
.rawbuf
, s
->dma_dac
.dmasize
, s
->dma_dac
.swptr
,
598 s
->dma_dac
.fragsize
, (s
->fmt
& (AFMT_U8
| AFMT_U16_LE
)) ? 0 : 0x80);
599 s
->dma_dac
.endcleared
= 1;
601 if (s
->dma_dac
.count
< (signed)s
->dma_dac
.dmasize
)
602 wake_up(&s
->dma_dac
.wait
);
607 /* --------------------------------------------------------------------- */
609 static void prog_codec(struct solo1_state
*s
)
617 /* program sampling rates */
618 filter
= s
->rate
* 9 / 20; /* Set filter roll-off to 90% of rate/2 */
619 fdiv
= 256 - 7160000 / (filter
* 82);
620 spin_lock_irqsave(&s
->lock
, flags
);
621 write_ctrl(s
, 0xa1, s
->clkdiv
);
622 write_ctrl(s
, 0xa2, fdiv
);
623 write_mixer(s
, 0x70, s
->clkdiv
);
624 write_mixer(s
, 0x72, fdiv
);
625 /* program ADC parameters */
626 write_ctrl(s
, 0xb8, 0xe);
627 write_ctrl(s
, 0xb9, /*0x1*/0);
628 write_ctrl(s
, 0xa8, (s
->channels
> 1) ? 0x11 : 0x12);
630 if (s
->fmt
& (AFMT_S16_LE
| AFMT_U16_LE
))
632 if (s
->fmt
& (AFMT_S16_LE
| AFMT_S8
))
636 write_ctrl(s
, 0xb7, (c
& 0x70) | 1);
637 write_ctrl(s
, 0xb7, c
);
638 write_ctrl(s
, 0xb1, 0x50);
639 write_ctrl(s
, 0xb2, 0x50);
640 /* program DAC parameters */
642 if (s
->fmt
& (AFMT_S16_LE
| AFMT_U16_LE
))
644 if (s
->fmt
& (AFMT_S16_LE
| AFMT_S8
))
648 write_mixer(s
, 0x7a, c
);
649 write_mixer(s
, 0x78, 0x10);
651 spin_unlock_irqrestore(&s
->lock
, flags
);
654 /* --------------------------------------------------------------------- */
656 static const char invalid_magic
[] = KERN_CRIT
"solo1: invalid magic value\n";
658 #define VALIDATE_STATE(s) \
660 if (!(s) || (s)->magic != SOLO1_MAGIC) { \
661 printk(invalid_magic); \
666 /* --------------------------------------------------------------------- */
668 static int mixer_ioctl(struct solo1_state
*s
, unsigned int cmd
, unsigned long arg
)
670 static const unsigned int mixer_src
[8] = {
671 SOUND_MASK_MIC
, SOUND_MASK_MIC
, SOUND_MASK_CD
, SOUND_MASK_VOLUME
,
672 SOUND_MASK_MIC
, 0, SOUND_MASK_LINE
, 0
674 static const unsigned char mixtable1
[SOUND_MIXER_NRDEVICES
] = {
675 [SOUND_MIXER_PCM
] = 1, /* voice */
676 [SOUND_MIXER_SYNTH
] = 2, /* FM */
677 [SOUND_MIXER_CD
] = 3, /* CD */
678 [SOUND_MIXER_LINE
] = 4, /* Line */
679 [SOUND_MIXER_LINE1
] = 5, /* AUX */
680 [SOUND_MIXER_MIC
] = 6, /* Mic */
681 [SOUND_MIXER_LINE2
] = 7, /* Mono in */
682 [SOUND_MIXER_SPEAKER
] = 8, /* Speaker */
683 [SOUND_MIXER_RECLEV
] = 9, /* Recording level */
684 [SOUND_MIXER_VOLUME
] = 10 /* Master Volume */
686 static const unsigned char mixreg
[] = {
695 unsigned char l
, r
, rl
, rr
, vidx
;
697 int __user
*p
= (int __user
*)arg
;
701 if (cmd
== SOUND_MIXER_PRIVATE1
) {
702 /* enable/disable/query mixer preamp */
703 if (get_user(val
, p
))
706 val
= val
? 0xff : 0xf7;
707 write_mixer(s
, 0x7d, (read_mixer(s
, 0x7d) | 0x08) & val
);
709 val
= (read_mixer(s
, 0x7d) & 0x08) ? 1 : 0;
710 return put_user(val
, p
);
712 if (cmd
== SOUND_MIXER_PRIVATE2
) {
713 /* enable/disable/query spatializer */
714 if (get_user(val
, p
))
718 write_mixer(s
, 0x52, val
);
719 write_mixer(s
, 0x50, val
? 0x08 : 0);
721 return put_user(read_mixer(s
, 0x52), p
);
723 if (cmd
== SOUND_MIXER_INFO
) {
725 strncpy(info
.id
, "Solo1", sizeof(info
.id
));
726 strncpy(info
.name
, "ESS Solo1", sizeof(info
.name
));
727 info
.modify_counter
= s
->mix
.modcnt
;
728 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
732 if (cmd
== SOUND_OLD_MIXER_INFO
) {
733 _old_mixer_info info
;
734 strncpy(info
.id
, "Solo1", sizeof(info
.id
));
735 strncpy(info
.name
, "ESS Solo1", sizeof(info
.name
));
736 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
740 if (cmd
== OSS_GETVERSION
)
741 return put_user(SOUND_VERSION
, p
);
742 if (_IOC_TYPE(cmd
) != 'M' || _SIOC_SIZE(cmd
) != sizeof(int))
744 if (_SIOC_DIR(cmd
) == _SIOC_READ
) {
745 switch (_IOC_NR(cmd
)) {
746 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
747 return put_user(mixer_src
[read_mixer(s
, 0x1c) & 7], p
);
749 case SOUND_MIXER_DEVMASK
: /* Arg contains a bit for each supported device */
750 return put_user(SOUND_MASK_PCM
| SOUND_MASK_SYNTH
| SOUND_MASK_CD
|
751 SOUND_MASK_LINE
| SOUND_MASK_LINE1
| SOUND_MASK_MIC
|
752 SOUND_MASK_VOLUME
| SOUND_MASK_LINE2
| SOUND_MASK_RECLEV
|
753 SOUND_MASK_SPEAKER
, p
);
755 case SOUND_MIXER_RECMASK
: /* Arg contains a bit for each supported recording source */
756 return put_user(SOUND_MASK_LINE
| SOUND_MASK_MIC
| SOUND_MASK_CD
| SOUND_MASK_VOLUME
, p
);
758 case SOUND_MIXER_STEREODEVS
: /* Mixer channels supporting stereo */
759 return put_user(SOUND_MASK_PCM
| SOUND_MASK_SYNTH
| SOUND_MASK_CD
|
760 SOUND_MASK_LINE
| SOUND_MASK_LINE1
| SOUND_MASK_MIC
|
761 SOUND_MASK_VOLUME
| SOUND_MASK_LINE2
| SOUND_MASK_RECLEV
, p
);
763 case SOUND_MIXER_CAPS
:
764 return put_user(SOUND_CAP_EXCL_INPUT
, p
);
768 if (i
>= SOUND_MIXER_NRDEVICES
|| !(vidx
= mixtable1
[i
]))
770 return put_user(s
->mix
.vol
[vidx
-1], p
);
773 if (_SIOC_DIR(cmd
) != (_SIOC_READ
|_SIOC_WRITE
))
776 switch (_IOC_NR(cmd
)) {
777 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
780 static const unsigned char regs
[] = {
781 0x1c, 0x1a, 0x36, 0x38, 0x3a, 0x3c, 0x3e, 0x60, 0x62, 0x6d, 0x7c
785 for (i
= 0; i
< sizeof(regs
); i
++)
786 printk(KERN_DEBUG
"solo1: mixer reg 0x%02x: 0x%02x\n",
787 regs
[i
], read_mixer(s
, regs
[i
]));
788 printk(KERN_DEBUG
"solo1: ctrl reg 0x%02x: 0x%02x\n",
789 0xb4, read_ctrl(s
, 0xb4));
792 if (get_user(val
, p
))
798 val
&= ~mixer_src
[read_mixer(s
, 0x1c) & 7];
799 for (i
= 0; i
< 8; i
++) {
800 if (mixer_src
[i
] & val
)
805 write_mixer(s
, 0x1c, i
);
808 case SOUND_MIXER_VOLUME
:
809 if (get_user(val
, p
))
814 r
= (val
>> 8) & 0xff;
821 rl
= (l
* 2 - 11) / 3;
822 l
= (rl
* 3 + 11) / 2;
828 rr
= (r
* 2 - 11) / 3;
829 r
= (rr
* 3 + 11) / 2;
831 write_mixer(s
, 0x60, rl
);
832 write_mixer(s
, 0x62, rr
);
833 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
834 s
->mix
.vol
[9] = ((unsigned int)r
<< 8) | l
;
838 return put_user(s
->mix
.vol
[9], p
);
840 case SOUND_MIXER_SPEAKER
:
841 if (get_user(val
, p
))
850 write_mixer(s
, 0x3c, rl
);
851 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
852 s
->mix
.vol
[7] = l
* 0x101;
856 return put_user(s
->mix
.vol
[7], p
);
858 case SOUND_MIXER_RECLEV
:
859 if (get_user(val
, p
))
861 l
= (val
<< 1) & 0x1fe;
866 r
= (val
>> 7) & 0x1fe;
873 r
= (rl
* 13 + 5) / 2;
874 l
= (rr
* 13 + 5) / 2;
875 write_ctrl(s
, 0xb4, (rl
<< 4) | rr
);
876 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
877 s
->mix
.vol
[8] = ((unsigned int)r
<< 8) | l
;
881 return put_user(s
->mix
.vol
[8], p
);
885 if (i
>= SOUND_MIXER_NRDEVICES
|| !(vidx
= mixtable1
[i
]))
887 if (get_user(val
, p
))
889 l
= (val
<< 1) & 0x1fe;
894 r
= (val
>> 7) & 0x1fe;
901 r
= (rl
* 13 + 5) / 2;
902 l
= (rr
* 13 + 5) / 2;
903 write_mixer(s
, mixreg
[vidx
-1], (rl
<< 4) | rr
);
904 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
905 s
->mix
.vol
[vidx
-1] = ((unsigned int)r
<< 8) | l
;
907 s
->mix
.vol
[vidx
-1] = val
;
909 return put_user(s
->mix
.vol
[vidx
-1], p
);
913 /* --------------------------------------------------------------------- */
915 static int solo1_open_mixdev(struct inode
*inode
, struct file
*file
)
917 unsigned int minor
= iminor(inode
);
918 struct solo1_state
*s
= NULL
;
919 struct pci_dev
*pci_dev
= NULL
;
921 while ((pci_dev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pci_dev
)) != NULL
) {
922 struct pci_driver
*drvr
;
923 drvr
= pci_dev_driver (pci_dev
);
924 if (drvr
!= &solo1_driver
)
926 s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
929 if (s
->dev_mixer
== minor
)
935 file
->private_data
= s
;
936 return nonseekable_open(inode
, file
);
939 static int solo1_release_mixdev(struct inode
*inode
, struct file
*file
)
941 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
947 static int solo1_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
949 return mixer_ioctl((struct solo1_state
*)file
->private_data
, cmd
, arg
);
952 static /*const*/ struct file_operations solo1_mixer_fops
= {
953 .owner
= THIS_MODULE
,
955 .ioctl
= solo1_ioctl_mixdev
,
956 .open
= solo1_open_mixdev
,
957 .release
= solo1_release_mixdev
,
960 /* --------------------------------------------------------------------- */
962 static int drain_dac(struct solo1_state
*s
, int nonblock
)
964 DECLARE_WAITQUEUE(wait
, current
);
969 if (s
->dma_dac
.mapped
)
971 add_wait_queue(&s
->dma_dac
.wait
, &wait
);
973 set_current_state(TASK_INTERRUPTIBLE
);
974 spin_lock_irqsave(&s
->lock
, flags
);
975 count
= s
->dma_dac
.count
;
976 spin_unlock_irqrestore(&s
->lock
, flags
);
979 if (signal_pending(current
))
982 remove_wait_queue(&s
->dma_dac
.wait
, &wait
);
983 set_current_state(TASK_RUNNING
);
986 tmo
= 3 * HZ
* (count
+ s
->dma_dac
.fragsize
) / 2 / s
->rate
;
987 if (s
->fmt
& (AFMT_S16_LE
| AFMT_U16_LE
))
991 if (!schedule_timeout(tmo
+ 1))
992 printk(KERN_DEBUG
"solo1: dma timed out??\n");
994 remove_wait_queue(&s
->dma_dac
.wait
, &wait
);
995 set_current_state(TASK_RUNNING
);
996 if (signal_pending(current
))
1001 /* --------------------------------------------------------------------- */
1003 static ssize_t
solo1_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1005 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1006 DECLARE_WAITQUEUE(wait
, current
);
1008 unsigned long flags
;
1013 if (s
->dma_adc
.mapped
)
1015 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1017 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1020 add_wait_queue(&s
->dma_adc
.wait
, &wait
);
1022 spin_lock_irqsave(&s
->lock
, flags
);
1023 swptr
= s
->dma_adc
.swptr
;
1024 cnt
= s
->dma_adc
.dmasize
-swptr
;
1025 if (s
->dma_adc
.count
< cnt
)
1026 cnt
= s
->dma_adc
.count
;
1028 __set_current_state(TASK_INTERRUPTIBLE
);
1029 spin_unlock_irqrestore(&s
->lock
, flags
);
1033 printk(KERN_DEBUG
"solo1_read: reg B8: 0x%02x DMAstat: 0x%02x DMAcnt: 0x%04x SBstat: 0x%02x cnt: %u\n",
1034 read_ctrl(s
, 0xb8), inb(s
->ddmabase
+8), inw(s
->ddmabase
+4), inb(s
->sbbase
+0xc), cnt
);
1037 if (s
->dma_adc
.enabled
)
1040 printk(KERN_DEBUG
"solo1_read: regs: A1: 0x%02x A2: 0x%02x A4: 0x%02x A5: 0x%02x A8: 0x%02x\n"
1041 KERN_DEBUG
"solo1_read: regs: B1: 0x%02x B2: 0x%02x B7: 0x%02x B8: 0x%02x B9: 0x%02x\n"
1042 KERN_DEBUG
"solo1_read: DMA: addr: 0x%08x cnt: 0x%04x stat: 0x%02x mask: 0x%02x\n"
1043 KERN_DEBUG
"solo1_read: SBstat: 0x%02x cnt: %u\n",
1044 read_ctrl(s
, 0xa1), read_ctrl(s
, 0xa2), read_ctrl(s
, 0xa4), read_ctrl(s
, 0xa5), read_ctrl(s
, 0xa8),
1045 read_ctrl(s
, 0xb1), read_ctrl(s
, 0xb2), read_ctrl(s
, 0xb7), read_ctrl(s
, 0xb8), read_ctrl(s
, 0xb9),
1046 inl(s
->ddmabase
), inw(s
->ddmabase
+4), inb(s
->ddmabase
+8), inb(s
->ddmabase
+15), inb(s
->sbbase
+0xc), cnt
);
1048 if (inb(s
->ddmabase
+15) & 1)
1049 printk(KERN_ERR
"solo1: cannot start recording, DDMA mask bit stuck at 1\n");
1050 if (file
->f_flags
& O_NONBLOCK
) {
1057 printk(KERN_DEBUG
"solo1_read: regs: A1: 0x%02x A2: 0x%02x A4: 0x%02x A5: 0x%02x A8: 0x%02x\n"
1058 KERN_DEBUG
"solo1_read: regs: B1: 0x%02x B2: 0x%02x B7: 0x%02x B8: 0x%02x B9: 0x%02x\n"
1059 KERN_DEBUG
"solo1_read: DMA: addr: 0x%08x cnt: 0x%04x stat: 0x%02x mask: 0x%02x\n"
1060 KERN_DEBUG
"solo1_read: SBstat: 0x%02x cnt: %u\n",
1061 read_ctrl(s
, 0xa1), read_ctrl(s
, 0xa2), read_ctrl(s
, 0xa4), read_ctrl(s
, 0xa5), read_ctrl(s
, 0xa8),
1062 read_ctrl(s
, 0xb1), read_ctrl(s
, 0xb2), read_ctrl(s
, 0xb7), read_ctrl(s
, 0xb8), read_ctrl(s
, 0xb9),
1063 inl(s
->ddmabase
), inw(s
->ddmabase
+4), inb(s
->ddmabase
+8), inb(s
->ddmabase
+15), inb(s
->sbbase
+0xc), cnt
);
1065 if (signal_pending(current
)) {
1072 if (copy_to_user(buffer
, s
->dma_adc
.rawbuf
+ swptr
, cnt
)) {
1077 swptr
= (swptr
+ cnt
) % s
->dma_adc
.dmasize
;
1078 spin_lock_irqsave(&s
->lock
, flags
);
1079 s
->dma_adc
.swptr
= swptr
;
1080 s
->dma_adc
.count
-= cnt
;
1081 spin_unlock_irqrestore(&s
->lock
, flags
);
1085 if (s
->dma_adc
.enabled
)
1088 printk(KERN_DEBUG
"solo1_read: reg B8: 0x%02x DMAstat: 0x%02x DMAcnt: 0x%04x SBstat: 0x%02x\n",
1089 read_ctrl(s
, 0xb8), inb(s
->ddmabase
+8), inw(s
->ddmabase
+4), inb(s
->sbbase
+0xc));
1092 remove_wait_queue(&s
->dma_adc
.wait
, &wait
);
1093 set_current_state(TASK_RUNNING
);
1097 static ssize_t
solo1_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1099 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1100 DECLARE_WAITQUEUE(wait
, current
);
1102 unsigned long flags
;
1107 if (s
->dma_dac
.mapped
)
1109 if (!s
->dma_dac
.ready
&& (ret
= prog_dmabuf_dac(s
)))
1111 if (!access_ok(VERIFY_READ
, buffer
, count
))
1114 printk(KERN_DEBUG
"solo1_write: reg 70: 0x%02x 71: 0x%02x 72: 0x%02x 74: 0x%02x 76: 0x%02x 78: 0x%02x 7A: 0x%02x\n"
1115 KERN_DEBUG
"solo1_write: DMA: addr: 0x%08x cnt: 0x%04x stat: 0x%02x SBstat: 0x%02x\n",
1116 read_mixer(s
, 0x70), read_mixer(s
, 0x71), read_mixer(s
, 0x72), read_mixer(s
, 0x74), read_mixer(s
, 0x76),
1117 read_mixer(s
, 0x78), read_mixer(s
, 0x7a), inl(s
->iobase
), inw(s
->iobase
+4), inb(s
->iobase
+6), inb(s
->sbbase
+0xc));
1118 printk(KERN_DEBUG
"solo1_write: reg 78: 0x%02x reg 7A: 0x%02x DMAcnt: 0x%04x DMAstat: 0x%02x SBstat: 0x%02x\n",
1119 read_mixer(s
, 0x78), read_mixer(s
, 0x7a), inw(s
->iobase
+4), inb(s
->iobase
+6), inb(s
->sbbase
+0xc));
1122 add_wait_queue(&s
->dma_dac
.wait
, &wait
);
1124 spin_lock_irqsave(&s
->lock
, flags
);
1125 if (s
->dma_dac
.count
< 0) {
1126 s
->dma_dac
.count
= 0;
1127 s
->dma_dac
.swptr
= s
->dma_dac
.hwptr
;
1129 swptr
= s
->dma_dac
.swptr
;
1130 cnt
= s
->dma_dac
.dmasize
-swptr
;
1131 if (s
->dma_dac
.count
+ cnt
> s
->dma_dac
.dmasize
)
1132 cnt
= s
->dma_dac
.dmasize
- s
->dma_dac
.count
;
1134 __set_current_state(TASK_INTERRUPTIBLE
);
1135 spin_unlock_irqrestore(&s
->lock
, flags
);
1139 if (s
->dma_dac
.enabled
)
1141 if (file
->f_flags
& O_NONBLOCK
) {
1147 if (signal_pending(current
)) {
1154 if (copy_from_user(s
->dma_dac
.rawbuf
+ swptr
, buffer
, cnt
)) {
1159 swptr
= (swptr
+ cnt
) % s
->dma_dac
.dmasize
;
1160 spin_lock_irqsave(&s
->lock
, flags
);
1161 s
->dma_dac
.swptr
= swptr
;
1162 s
->dma_dac
.count
+= cnt
;
1163 s
->dma_dac
.endcleared
= 0;
1164 spin_unlock_irqrestore(&s
->lock
, flags
);
1168 if (s
->dma_dac
.enabled
)
1171 remove_wait_queue(&s
->dma_dac
.wait
, &wait
);
1172 set_current_state(TASK_RUNNING
);
1176 /* No kernel lock - we have our own spinlock */
1177 static unsigned int solo1_poll(struct file
*file
, struct poll_table_struct
*wait
)
1179 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1180 unsigned long flags
;
1181 unsigned int mask
= 0;
1184 if (file
->f_mode
& FMODE_WRITE
) {
1185 if (!s
->dma_dac
.ready
&& prog_dmabuf_dac(s
))
1187 poll_wait(file
, &s
->dma_dac
.wait
, wait
);
1189 if (file
->f_mode
& FMODE_READ
) {
1190 if (!s
->dma_adc
.ready
&& prog_dmabuf_adc(s
))
1192 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1194 spin_lock_irqsave(&s
->lock
, flags
);
1195 solo1_update_ptr(s
);
1196 if (file
->f_mode
& FMODE_READ
) {
1197 if (s
->dma_adc
.mapped
) {
1198 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
1199 mask
|= POLLIN
| POLLRDNORM
;
1201 if (s
->dma_adc
.count
> 0)
1202 mask
|= POLLIN
| POLLRDNORM
;
1205 if (file
->f_mode
& FMODE_WRITE
) {
1206 if (s
->dma_dac
.mapped
) {
1207 if (s
->dma_dac
.count
>= (signed)s
->dma_dac
.fragsize
)
1208 mask
|= POLLOUT
| POLLWRNORM
;
1210 if ((signed)s
->dma_dac
.dmasize
> s
->dma_dac
.count
)
1211 mask
|= POLLOUT
| POLLWRNORM
;
1214 spin_unlock_irqrestore(&s
->lock
, flags
);
1219 static int solo1_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1221 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1228 if (vma
->vm_flags
& VM_WRITE
) {
1229 if ((ret
= prog_dmabuf_dac(s
)) != 0)
1232 } else if (vma
->vm_flags
& VM_READ
) {
1233 if ((ret
= prog_dmabuf_adc(s
)) != 0)
1239 if (vma
->vm_pgoff
!= 0)
1241 size
= vma
->vm_end
- vma
->vm_start
;
1242 if (size
> (PAGE_SIZE
<< db
->buforder
))
1245 if (remap_pfn_range(vma
, vma
->vm_start
,
1246 virt_to_phys(db
->rawbuf
) >> PAGE_SHIFT
,
1247 size
, vma
->vm_page_prot
))
1256 static int solo1_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1258 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1259 unsigned long flags
;
1260 audio_buf_info abinfo
;
1262 int val
, mapped
, ret
, count
;
1264 unsigned rate1
, rate2
;
1265 void __user
*argp
= (void __user
*)arg
;
1266 int __user
*p
= argp
;
1269 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac
.mapped
) ||
1270 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1272 case OSS_GETVERSION
:
1273 return put_user(SOUND_VERSION
, p
);
1275 case SNDCTL_DSP_SYNC
:
1276 if (file
->f_mode
& FMODE_WRITE
)
1277 return drain_dac(s
, 0/*file->f_flags & O_NONBLOCK*/);
1280 case SNDCTL_DSP_SETDUPLEX
:
1283 case SNDCTL_DSP_GETCAPS
:
1284 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1286 case SNDCTL_DSP_RESET
:
1287 if (file
->f_mode
& FMODE_WRITE
) {
1289 synchronize_irq(s
->irq
);
1290 s
->dma_dac
.swptr
= s
->dma_dac
.hwptr
= s
->dma_dac
.count
= s
->dma_dac
.total_bytes
= 0;
1292 if (file
->f_mode
& FMODE_READ
) {
1294 synchronize_irq(s
->irq
);
1295 s
->dma_adc
.swptr
= s
->dma_adc
.hwptr
= s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1300 case SNDCTL_DSP_SPEED
:
1301 if (get_user(val
, p
))
1306 s
->dma_adc
.ready
= s
->dma_dac
.ready
= 0;
1307 /* program sampling rates */
1312 div1
= (768000 + val
/ 2) / val
;
1313 rate1
= (768000 + div1
/ 2) / div1
;
1315 div2
= (793800 + val
/ 2) / val
;
1316 rate2
= (793800 + div2
/ 2) / div2
;
1317 div2
= (-div2
) & 0x7f;
1318 if (abs(val
- rate2
) < abs(val
- rate1
)) {
1326 return put_user(s
->rate
, p
);
1328 case SNDCTL_DSP_STEREO
:
1329 if (get_user(val
, p
))
1333 s
->dma_adc
.ready
= s
->dma_dac
.ready
= 0;
1334 /* program channels */
1335 s
->channels
= val
? 2 : 1;
1339 case SNDCTL_DSP_CHANNELS
:
1340 if (get_user(val
, p
))
1345 s
->dma_adc
.ready
= s
->dma_dac
.ready
= 0;
1346 /* program channels */
1347 s
->channels
= (val
>= 2) ? 2 : 1;
1350 return put_user(s
->channels
, p
);
1352 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1353 return put_user(AFMT_S16_LE
|AFMT_U16_LE
|AFMT_S8
|AFMT_U8
, p
);
1355 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1356 if (get_user(val
, p
))
1358 if (val
!= AFMT_QUERY
) {
1361 s
->dma_adc
.ready
= s
->dma_dac
.ready
= 0;
1362 /* program format */
1363 if (val
!= AFMT_S16_LE
&& val
!= AFMT_U16_LE
&&
1364 val
!= AFMT_S8
&& val
!= AFMT_U8
)
1369 return put_user(s
->fmt
, p
);
1371 case SNDCTL_DSP_POST
:
1374 case SNDCTL_DSP_GETTRIGGER
:
1376 if (file
->f_mode
& s
->ena
& FMODE_READ
)
1377 val
|= PCM_ENABLE_INPUT
;
1378 if (file
->f_mode
& s
->ena
& FMODE_WRITE
)
1379 val
|= PCM_ENABLE_OUTPUT
;
1380 return put_user(val
, p
);
1382 case SNDCTL_DSP_SETTRIGGER
:
1383 if (get_user(val
, p
))
1385 if (file
->f_mode
& FMODE_READ
) {
1386 if (val
& PCM_ENABLE_INPUT
) {
1387 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1389 s
->dma_dac
.enabled
= 1;
1391 if (inb(s
->ddmabase
+15) & 1)
1392 printk(KERN_ERR
"solo1: cannot start recording, DDMA mask bit stuck at 1\n");
1394 s
->dma_dac
.enabled
= 0;
1398 if (file
->f_mode
& FMODE_WRITE
) {
1399 if (val
& PCM_ENABLE_OUTPUT
) {
1400 if (!s
->dma_dac
.ready
&& (ret
= prog_dmabuf_dac(s
)))
1402 s
->dma_dac
.enabled
= 1;
1405 s
->dma_dac
.enabled
= 0;
1411 case SNDCTL_DSP_GETOSPACE
:
1412 if (!(file
->f_mode
& FMODE_WRITE
))
1414 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf_dac(s
)) != 0)
1416 spin_lock_irqsave(&s
->lock
, flags
);
1417 solo1_update_ptr(s
);
1418 abinfo
.fragsize
= s
->dma_dac
.fragsize
;
1419 count
= s
->dma_dac
.count
;
1422 abinfo
.bytes
= s
->dma_dac
.dmasize
- count
;
1423 abinfo
.fragstotal
= s
->dma_dac
.numfrag
;
1424 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac
.fragshift
;
1425 spin_unlock_irqrestore(&s
->lock
, flags
);
1426 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1428 case SNDCTL_DSP_GETISPACE
:
1429 if (!(file
->f_mode
& FMODE_READ
))
1431 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1433 spin_lock_irqsave(&s
->lock
, flags
);
1434 solo1_update_ptr(s
);
1435 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1436 abinfo
.bytes
= s
->dma_adc
.count
;
1437 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1438 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1439 spin_unlock_irqrestore(&s
->lock
, flags
);
1440 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1442 case SNDCTL_DSP_NONBLOCK
:
1443 file
->f_flags
|= O_NONBLOCK
;
1446 case SNDCTL_DSP_GETODELAY
:
1447 if (!(file
->f_mode
& FMODE_WRITE
))
1449 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf_dac(s
)) != 0)
1451 spin_lock_irqsave(&s
->lock
, flags
);
1452 solo1_update_ptr(s
);
1453 count
= s
->dma_dac
.count
;
1454 spin_unlock_irqrestore(&s
->lock
, flags
);
1457 return put_user(count
, p
);
1459 case SNDCTL_DSP_GETIPTR
:
1460 if (!(file
->f_mode
& FMODE_READ
))
1462 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1464 spin_lock_irqsave(&s
->lock
, flags
);
1465 solo1_update_ptr(s
);
1466 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1467 cinfo
.blocks
= s
->dma_adc
.count
>> s
->dma_adc
.fragshift
;
1468 cinfo
.ptr
= s
->dma_adc
.hwptr
;
1469 if (s
->dma_adc
.mapped
)
1470 s
->dma_adc
.count
&= s
->dma_adc
.fragsize
-1;
1471 spin_unlock_irqrestore(&s
->lock
, flags
);
1472 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1476 case SNDCTL_DSP_GETOPTR
:
1477 if (!(file
->f_mode
& FMODE_WRITE
))
1479 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf_dac(s
)) != 0)
1481 spin_lock_irqsave(&s
->lock
, flags
);
1482 solo1_update_ptr(s
);
1483 cinfo
.bytes
= s
->dma_dac
.total_bytes
;
1484 count
= s
->dma_dac
.count
;
1487 cinfo
.blocks
= count
>> s
->dma_dac
.fragshift
;
1488 cinfo
.ptr
= s
->dma_dac
.hwptr
;
1489 if (s
->dma_dac
.mapped
)
1490 s
->dma_dac
.count
&= s
->dma_dac
.fragsize
-1;
1491 spin_unlock_irqrestore(&s
->lock
, flags
);
1493 printk(KERN_DEBUG
"esssolo1: GETOPTR: bytes %u blocks %u ptr %u, buforder %u numfrag %u fragshift %u\n"
1494 KERN_DEBUG
"esssolo1: swptr %u count %u fragsize %u dmasize %u fragsamples %u\n",
1495 cinfo
.bytes
, cinfo
.blocks
, cinfo
.ptr
, s
->dma_dac
.buforder
, s
->dma_dac
.numfrag
, s
->dma_dac
.fragshift
,
1496 s
->dma_dac
.swptr
, s
->dma_dac
.count
, s
->dma_dac
.fragsize
, s
->dma_dac
.dmasize
, s
->dma_dac
.fragsamples
);
1498 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1502 case SNDCTL_DSP_GETBLKSIZE
:
1503 if (file
->f_mode
& FMODE_WRITE
) {
1504 if ((val
= prog_dmabuf_dac(s
)))
1506 return put_user(s
->dma_dac
.fragsize
, p
);
1508 if ((val
= prog_dmabuf_adc(s
)))
1510 return put_user(s
->dma_adc
.fragsize
, p
);
1512 case SNDCTL_DSP_SETFRAGMENT
:
1513 if (get_user(val
, p
))
1515 if (file
->f_mode
& FMODE_READ
) {
1516 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1517 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1518 if (s
->dma_adc
.ossfragshift
< 4)
1519 s
->dma_adc
.ossfragshift
= 4;
1520 if (s
->dma_adc
.ossfragshift
> 15)
1521 s
->dma_adc
.ossfragshift
= 15;
1522 if (s
->dma_adc
.ossmaxfrags
< 4)
1523 s
->dma_adc
.ossmaxfrags
= 4;
1525 if (file
->f_mode
& FMODE_WRITE
) {
1526 s
->dma_dac
.ossfragshift
= val
& 0xffff;
1527 s
->dma_dac
.ossmaxfrags
= (val
>> 16) & 0xffff;
1528 if (s
->dma_dac
.ossfragshift
< 4)
1529 s
->dma_dac
.ossfragshift
= 4;
1530 if (s
->dma_dac
.ossfragshift
> 15)
1531 s
->dma_dac
.ossfragshift
= 15;
1532 if (s
->dma_dac
.ossmaxfrags
< 4)
1533 s
->dma_dac
.ossmaxfrags
= 4;
1537 case SNDCTL_DSP_SUBDIVIDE
:
1538 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1539 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac
.subdivision
))
1541 if (get_user(val
, p
))
1543 if (val
!= 1 && val
!= 2 && val
!= 4)
1545 if (file
->f_mode
& FMODE_READ
)
1546 s
->dma_adc
.subdivision
= val
;
1547 if (file
->f_mode
& FMODE_WRITE
)
1548 s
->dma_dac
.subdivision
= val
;
1551 case SOUND_PCM_READ_RATE
:
1552 return put_user(s
->rate
, p
);
1554 case SOUND_PCM_READ_CHANNELS
:
1555 return put_user(s
->channels
, p
);
1557 case SOUND_PCM_READ_BITS
:
1558 return put_user((s
->fmt
& (AFMT_S8
|AFMT_U8
)) ? 8 : 16, p
);
1560 case SOUND_PCM_WRITE_FILTER
:
1561 case SNDCTL_DSP_SETSYNCRO
:
1562 case SOUND_PCM_READ_FILTER
:
1566 return mixer_ioctl(s
, cmd
, arg
);
1569 static int solo1_release(struct inode
*inode
, struct file
*file
)
1571 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1575 if (file
->f_mode
& FMODE_WRITE
)
1576 drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1578 if (file
->f_mode
& FMODE_WRITE
) {
1580 outb(0, s
->iobase
+6); /* disable DMA */
1581 dealloc_dmabuf(s
, &s
->dma_dac
);
1583 if (file
->f_mode
& FMODE_READ
) {
1585 outb(1, s
->ddmabase
+0xf); /* mask DMA channel */
1586 outb(0, s
->ddmabase
+0xd); /* DMA master clear */
1587 dealloc_dmabuf(s
, &s
->dma_adc
);
1589 s
->open_mode
&= ~(FMODE_READ
| FMODE_WRITE
);
1590 wake_up(&s
->open_wait
);
1596 static int solo1_open(struct inode
*inode
, struct file
*file
)
1598 unsigned int minor
= iminor(inode
);
1599 DECLARE_WAITQUEUE(wait
, current
);
1600 struct solo1_state
*s
= NULL
;
1601 struct pci_dev
*pci_dev
= NULL
;
1603 while ((pci_dev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pci_dev
)) != NULL
) {
1604 struct pci_driver
*drvr
;
1606 drvr
= pci_dev_driver(pci_dev
);
1607 if (drvr
!= &solo1_driver
)
1609 s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
1612 if (!((s
->dev_audio
^ minor
) & ~0xf))
1618 file
->private_data
= s
;
1619 /* wait for device to become free */
1621 while (s
->open_mode
& (FMODE_READ
| FMODE_WRITE
)) {
1622 if (file
->f_flags
& O_NONBLOCK
) {
1626 add_wait_queue(&s
->open_wait
, &wait
);
1627 __set_current_state(TASK_INTERRUPTIBLE
);
1630 remove_wait_queue(&s
->open_wait
, &wait
);
1631 set_current_state(TASK_RUNNING
);
1632 if (signal_pending(current
))
1633 return -ERESTARTSYS
;
1639 s
->clkdiv
= 96 | 0x80;
1641 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
= s
->dma_adc
.subdivision
= 0;
1642 s
->dma_adc
.enabled
= 1;
1643 s
->dma_dac
.ossfragshift
= s
->dma_dac
.ossmaxfrags
= s
->dma_dac
.subdivision
= 0;
1644 s
->dma_dac
.enabled
= 1;
1645 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1648 return nonseekable_open(inode
, file
);
1651 static /*const*/ struct file_operations solo1_audio_fops
= {
1652 .owner
= THIS_MODULE
,
1653 .llseek
= no_llseek
,
1655 .write
= solo1_write
,
1657 .ioctl
= solo1_ioctl
,
1660 .release
= solo1_release
,
1663 /* --------------------------------------------------------------------- */
1665 /* hold spinlock for the following! */
1666 static void solo1_handle_midi(struct solo1_state
*s
)
1674 while (!(inb(s
->mpubase
+1) & 0x80)) {
1675 ch
= inb(s
->mpubase
);
1676 if (s
->midi
.icnt
< MIDIINBUF
) {
1677 s
->midi
.ibuf
[s
->midi
.iwr
] = ch
;
1678 s
->midi
.iwr
= (s
->midi
.iwr
+ 1) % MIDIINBUF
;
1684 wake_up(&s
->midi
.iwait
);
1686 while (!(inb(s
->mpubase
+1) & 0x40) && s
->midi
.ocnt
> 0) {
1687 outb(s
->midi
.obuf
[s
->midi
.ord
], s
->mpubase
);
1688 s
->midi
.ord
= (s
->midi
.ord
+ 1) % MIDIOUTBUF
;
1690 if (s
->midi
.ocnt
< MIDIOUTBUF
-16)
1694 wake_up(&s
->midi
.owait
);
1697 static irqreturn_t
solo1_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1699 struct solo1_state
*s
= (struct solo1_state
*)dev_id
;
1700 unsigned int intsrc
;
1702 /* fastpath out, to ease interrupt sharing */
1703 intsrc
= inb(s
->iobase
+7); /* get interrupt source(s) */
1706 (void)inb(s
->sbbase
+0xe); /* clear interrupt */
1707 spin_lock(&s
->lock
);
1708 /* clear audio interrupts first */
1710 write_mixer(s
, 0x7a, read_mixer(s
, 0x7a) & 0x7f);
1711 solo1_update_ptr(s
);
1712 solo1_handle_midi(s
);
1713 spin_unlock(&s
->lock
);
1717 static void solo1_midi_timer(unsigned long data
)
1719 struct solo1_state
*s
= (struct solo1_state
*)data
;
1720 unsigned long flags
;
1722 spin_lock_irqsave(&s
->lock
, flags
);
1723 solo1_handle_midi(s
);
1724 spin_unlock_irqrestore(&s
->lock
, flags
);
1725 s
->midi
.timer
.expires
= jiffies
+1;
1726 add_timer(&s
->midi
.timer
);
1729 /* --------------------------------------------------------------------- */
1731 static ssize_t
solo1_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1733 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1734 DECLARE_WAITQUEUE(wait
, current
);
1736 unsigned long flags
;
1741 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1746 add_wait_queue(&s
->midi
.iwait
, &wait
);
1748 spin_lock_irqsave(&s
->lock
, flags
);
1750 cnt
= MIDIINBUF
- ptr
;
1751 if (s
->midi
.icnt
< cnt
)
1754 __set_current_state(TASK_INTERRUPTIBLE
);
1755 spin_unlock_irqrestore(&s
->lock
, flags
);
1759 if (file
->f_flags
& O_NONBLOCK
) {
1765 if (signal_pending(current
)) {
1772 if (copy_to_user(buffer
, s
->midi
.ibuf
+ ptr
, cnt
)) {
1777 ptr
= (ptr
+ cnt
) % MIDIINBUF
;
1778 spin_lock_irqsave(&s
->lock
, flags
);
1780 s
->midi
.icnt
-= cnt
;
1781 spin_unlock_irqrestore(&s
->lock
, flags
);
1787 __set_current_state(TASK_RUNNING
);
1788 remove_wait_queue(&s
->midi
.iwait
, &wait
);
1792 static ssize_t
solo1_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1794 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1795 DECLARE_WAITQUEUE(wait
, current
);
1797 unsigned long flags
;
1802 if (!access_ok(VERIFY_READ
, buffer
, count
))
1807 add_wait_queue(&s
->midi
.owait
, &wait
);
1809 spin_lock_irqsave(&s
->lock
, flags
);
1811 cnt
= MIDIOUTBUF
- ptr
;
1812 if (s
->midi
.ocnt
+ cnt
> MIDIOUTBUF
)
1813 cnt
= MIDIOUTBUF
- s
->midi
.ocnt
;
1815 __set_current_state(TASK_INTERRUPTIBLE
);
1816 solo1_handle_midi(s
);
1818 spin_unlock_irqrestore(&s
->lock
, flags
);
1822 if (file
->f_flags
& O_NONBLOCK
) {
1828 if (signal_pending(current
)) {
1835 if (copy_from_user(s
->midi
.obuf
+ ptr
, buffer
, cnt
)) {
1840 ptr
= (ptr
+ cnt
) % MIDIOUTBUF
;
1841 spin_lock_irqsave(&s
->lock
, flags
);
1843 s
->midi
.ocnt
+= cnt
;
1844 spin_unlock_irqrestore(&s
->lock
, flags
);
1848 spin_lock_irqsave(&s
->lock
, flags
);
1849 solo1_handle_midi(s
);
1850 spin_unlock_irqrestore(&s
->lock
, flags
);
1852 __set_current_state(TASK_RUNNING
);
1853 remove_wait_queue(&s
->midi
.owait
, &wait
);
1857 /* No kernel lock - we have our own spinlock */
1858 static unsigned int solo1_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
1860 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1861 unsigned long flags
;
1862 unsigned int mask
= 0;
1865 if (file
->f_flags
& FMODE_WRITE
)
1866 poll_wait(file
, &s
->midi
.owait
, wait
);
1867 if (file
->f_flags
& FMODE_READ
)
1868 poll_wait(file
, &s
->midi
.iwait
, wait
);
1869 spin_lock_irqsave(&s
->lock
, flags
);
1870 if (file
->f_flags
& FMODE_READ
) {
1871 if (s
->midi
.icnt
> 0)
1872 mask
|= POLLIN
| POLLRDNORM
;
1874 if (file
->f_flags
& FMODE_WRITE
) {
1875 if (s
->midi
.ocnt
< MIDIOUTBUF
)
1876 mask
|= POLLOUT
| POLLWRNORM
;
1878 spin_unlock_irqrestore(&s
->lock
, flags
);
1882 static int solo1_midi_open(struct inode
*inode
, struct file
*file
)
1884 unsigned int minor
= iminor(inode
);
1885 DECLARE_WAITQUEUE(wait
, current
);
1886 unsigned long flags
;
1887 struct solo1_state
*s
= NULL
;
1888 struct pci_dev
*pci_dev
= NULL
;
1890 while ((pci_dev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pci_dev
)) != NULL
) {
1891 struct pci_driver
*drvr
;
1893 drvr
= pci_dev_driver(pci_dev
);
1894 if (drvr
!= &solo1_driver
)
1896 s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
1899 if (s
->dev_midi
== minor
)
1905 file
->private_data
= s
;
1906 /* wait for device to become free */
1908 while (s
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
1909 if (file
->f_flags
& O_NONBLOCK
) {
1913 add_wait_queue(&s
->open_wait
, &wait
);
1914 __set_current_state(TASK_INTERRUPTIBLE
);
1917 remove_wait_queue(&s
->open_wait
, &wait
);
1918 set_current_state(TASK_RUNNING
);
1919 if (signal_pending(current
))
1920 return -ERESTARTSYS
;
1923 spin_lock_irqsave(&s
->lock
, flags
);
1924 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
1925 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
1926 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
1927 outb(0xff, s
->mpubase
+1); /* reset command */
1928 outb(0x3f, s
->mpubase
+1); /* uart command */
1929 if (!(inb(s
->mpubase
+1) & 0x80))
1931 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
1932 outb(0xb0, s
->iobase
+ 7); /* enable A1, A2, MPU irq's */
1933 init_timer(&s
->midi
.timer
);
1934 s
->midi
.timer
.expires
= jiffies
+1;
1935 s
->midi
.timer
.data
= (unsigned long)s
;
1936 s
->midi
.timer
.function
= solo1_midi_timer
;
1937 add_timer(&s
->midi
.timer
);
1939 if (file
->f_mode
& FMODE_READ
) {
1940 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
1942 if (file
->f_mode
& FMODE_WRITE
) {
1943 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
1945 spin_unlock_irqrestore(&s
->lock
, flags
);
1946 s
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
1948 return nonseekable_open(inode
, file
);
1951 static int solo1_midi_release(struct inode
*inode
, struct file
*file
)
1953 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
1954 DECLARE_WAITQUEUE(wait
, current
);
1955 unsigned long flags
;
1956 unsigned count
, tmo
;
1961 if (file
->f_mode
& FMODE_WRITE
) {
1962 add_wait_queue(&s
->midi
.owait
, &wait
);
1964 __set_current_state(TASK_INTERRUPTIBLE
);
1965 spin_lock_irqsave(&s
->lock
, flags
);
1966 count
= s
->midi
.ocnt
;
1967 spin_unlock_irqrestore(&s
->lock
, flags
);
1970 if (signal_pending(current
))
1972 if (file
->f_flags
& O_NONBLOCK
)
1974 tmo
= (count
* HZ
) / 3100;
1975 if (!schedule_timeout(tmo
? : 1) && tmo
)
1976 printk(KERN_DEBUG
"solo1: midi timed out??\n");
1978 remove_wait_queue(&s
->midi
.owait
, &wait
);
1979 set_current_state(TASK_RUNNING
);
1982 s
->open_mode
&= ~((file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
|FMODE_MIDI_WRITE
));
1983 spin_lock_irqsave(&s
->lock
, flags
);
1984 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
1985 outb(0x30, s
->iobase
+ 7); /* enable A1, A2 irq's */
1986 del_timer(&s
->midi
.timer
);
1988 spin_unlock_irqrestore(&s
->lock
, flags
);
1989 wake_up(&s
->open_wait
);
1995 static /*const*/ struct file_operations solo1_midi_fops
= {
1996 .owner
= THIS_MODULE
,
1997 .llseek
= no_llseek
,
1998 .read
= solo1_midi_read
,
1999 .write
= solo1_midi_write
,
2000 .poll
= solo1_midi_poll
,
2001 .open
= solo1_midi_open
,
2002 .release
= solo1_midi_release
,
2005 /* --------------------------------------------------------------------- */
2007 static int solo1_dmfm_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2009 static const unsigned char op_offset
[18] = {
2010 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
2011 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
2012 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
2014 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
2015 struct dm_fm_voice v
;
2016 struct dm_fm_note n
;
2017 struct dm_fm_params p
;
2022 case FM_IOCTL_RESET
:
2023 for (regb
= 0xb0; regb
< 0xb9; regb
++) {
2024 outb(regb
, s
->sbbase
);
2025 outb(0, s
->sbbase
+1);
2026 outb(regb
, s
->sbbase
+2);
2027 outb(0, s
->sbbase
+3);
2031 case FM_IOCTL_PLAY_NOTE
:
2032 if (copy_from_user(&n
, (void __user
*)arg
, sizeof(n
)))
2043 outb(0xa0 + regb
, io
);
2044 outb(n
.fnum
& 0xff, io
+1);
2045 outb(0xb0 + regb
, io
);
2046 outb(((n
.fnum
>> 8) & 3) | ((n
.octave
& 7) << 2) | ((n
.key_on
& 1) << 5), io
+1);
2049 case FM_IOCTL_SET_VOICE
:
2050 if (copy_from_user(&v
, (void __user
*)arg
, sizeof(v
)))
2054 regb
= op_offset
[v
.voice
];
2055 io
= s
->sbbase
+ ((v
.op
& 1) << 1);
2056 outb(0x20 + regb
, io
);
2057 outb(((v
.am
& 1) << 7) | ((v
.vibrato
& 1) << 6) | ((v
.do_sustain
& 1) << 5) |
2058 ((v
.kbd_scale
& 1) << 4) | (v
.harmonic
& 0xf), io
+1);
2059 outb(0x40 + regb
, io
);
2060 outb(((v
.scale_level
& 0x3) << 6) | (v
.volume
& 0x3f), io
+1);
2061 outb(0x60 + regb
, io
);
2062 outb(((v
.attack
& 0xf) << 4) | (v
.decay
& 0xf), io
+1);
2063 outb(0x80 + regb
, io
);
2064 outb(((v
.sustain
& 0xf) << 4) | (v
.release
& 0xf), io
+1);
2065 outb(0xe0 + regb
, io
);
2066 outb(v
.waveform
& 0x7, io
+1);
2074 outb(0xc0 + regb
, io
);
2075 outb(((v
.right
& 1) << 5) | ((v
.left
& 1) << 4) | ((v
.feedback
& 7) << 1) |
2076 (v
.connection
& 1), io
+1);
2079 case FM_IOCTL_SET_PARAMS
:
2080 if (copy_from_user(&p
, (void __user
*)arg
, sizeof(p
)))
2082 outb(0x08, s
->sbbase
);
2083 outb((p
.kbd_split
& 1) << 6, s
->sbbase
+1);
2084 outb(0xbd, s
->sbbase
);
2085 outb(((p
.am_depth
& 1) << 7) | ((p
.vib_depth
& 1) << 6) | ((p
.rhythm
& 1) << 5) | ((p
.bass
& 1) << 4) |
2086 ((p
.snare
& 1) << 3) | ((p
.tomtom
& 1) << 2) | ((p
.cymbal
& 1) << 1) | (p
.hihat
& 1), s
->sbbase
+1);
2089 case FM_IOCTL_SET_OPL
:
2090 outb(4, s
->sbbase
+2);
2091 outb(arg
, s
->sbbase
+3);
2094 case FM_IOCTL_SET_MODE
:
2095 outb(5, s
->sbbase
+2);
2096 outb(arg
& 1, s
->sbbase
+3);
2104 static int solo1_dmfm_open(struct inode
*inode
, struct file
*file
)
2106 unsigned int minor
= iminor(inode
);
2107 DECLARE_WAITQUEUE(wait
, current
);
2108 struct solo1_state
*s
= NULL
;
2109 struct pci_dev
*pci_dev
= NULL
;
2111 while ((pci_dev
= pci_find_device(PCI_ANY_ID
, PCI_ANY_ID
, pci_dev
)) != NULL
) {
2112 struct pci_driver
*drvr
;
2114 drvr
= pci_dev_driver(pci_dev
);
2115 if (drvr
!= &solo1_driver
)
2117 s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
2120 if (s
->dev_dmfm
== minor
)
2126 file
->private_data
= s
;
2127 /* wait for device to become free */
2129 while (s
->open_mode
& FMODE_DMFM
) {
2130 if (file
->f_flags
& O_NONBLOCK
) {
2134 add_wait_queue(&s
->open_wait
, &wait
);
2135 __set_current_state(TASK_INTERRUPTIBLE
);
2138 remove_wait_queue(&s
->open_wait
, &wait
);
2139 set_current_state(TASK_RUNNING
);
2140 if (signal_pending(current
))
2141 return -ERESTARTSYS
;
2144 if (!request_region(s
->sbbase
, FMSYNTH_EXTENT
, "ESS Solo1")) {
2146 printk(KERN_ERR
"solo1: FM synth io ports in use, opl3 loaded?\n");
2149 /* init the stuff */
2151 outb(0x20, s
->sbbase
+1); /* enable waveforms */
2152 outb(4, s
->sbbase
+2);
2153 outb(0, s
->sbbase
+3); /* no 4op enabled */
2154 outb(5, s
->sbbase
+2);
2155 outb(1, s
->sbbase
+3); /* enable OPL3 */
2156 s
->open_mode
|= FMODE_DMFM
;
2158 return nonseekable_open(inode
, file
);
2161 static int solo1_dmfm_release(struct inode
*inode
, struct file
*file
)
2163 struct solo1_state
*s
= (struct solo1_state
*)file
->private_data
;
2169 s
->open_mode
&= ~FMODE_DMFM
;
2170 for (regb
= 0xb0; regb
< 0xb9; regb
++) {
2171 outb(regb
, s
->sbbase
);
2172 outb(0, s
->sbbase
+1);
2173 outb(regb
, s
->sbbase
+2);
2174 outb(0, s
->sbbase
+3);
2176 release_region(s
->sbbase
, FMSYNTH_EXTENT
);
2177 wake_up(&s
->open_wait
);
2183 static /*const*/ struct file_operations solo1_dmfm_fops
= {
2184 .owner
= THIS_MODULE
,
2185 .llseek
= no_llseek
,
2186 .ioctl
= solo1_dmfm_ioctl
,
2187 .open
= solo1_dmfm_open
,
2188 .release
= solo1_dmfm_release
,
2191 /* --------------------------------------------------------------------- */
2193 static struct initvol
{
2196 } initvol
[] __devinitdata
= {
2197 { SOUND_MIXER_WRITE_VOLUME
, 0x4040 },
2198 { SOUND_MIXER_WRITE_PCM
, 0x4040 },
2199 { SOUND_MIXER_WRITE_SYNTH
, 0x4040 },
2200 { SOUND_MIXER_WRITE_CD
, 0x4040 },
2201 { SOUND_MIXER_WRITE_LINE
, 0x4040 },
2202 { SOUND_MIXER_WRITE_LINE1
, 0x4040 },
2203 { SOUND_MIXER_WRITE_LINE2
, 0x4040 },
2204 { SOUND_MIXER_WRITE_RECLEV
, 0x4040 },
2205 { SOUND_MIXER_WRITE_SPEAKER
, 0x4040 },
2206 { SOUND_MIXER_WRITE_MIC
, 0x4040 }
2209 static int setup_solo1(struct solo1_state
*s
)
2211 struct pci_dev
*pcidev
= s
->dev
;
2215 /* initialize DDMA base address */
2216 printk(KERN_DEBUG
"solo1: ddma base address: 0x%lx\n", s
->ddmabase
);
2217 pci_write_config_word(pcidev
, 0x60, (s
->ddmabase
& (~0xf)) | 1);
2218 /* set DMA policy to DDMA, IRQ emulation off (CLKRUN disabled for now) */
2219 pci_write_config_dword(pcidev
, 0x50, 0);
2220 /* disable legacy audio address decode */
2221 pci_write_config_word(pcidev
, 0x40, 0x907f);
2223 /* initialize the chips */
2224 if (!reset_ctrl(s
)) {
2225 printk(KERN_ERR
"esssolo1: cannot reset controller\n");
2228 outb(0xb0, s
->iobase
+7); /* enable A1, A2, MPU irq's */
2230 /* initialize mixer regs */
2231 write_mixer(s
, 0x7f, 0); /* disable music digital recording */
2232 write_mixer(s
, 0x7d, 0x0c); /* enable mic preamp, MONO_OUT is 2nd DAC right channel */
2233 write_mixer(s
, 0x64, 0x45); /* volume control */
2234 write_mixer(s
, 0x48, 0x10); /* enable music DAC/ES6xx interface */
2235 write_mixer(s
, 0x50, 0); /* disable spatializer */
2236 write_mixer(s
, 0x52, 0);
2237 write_mixer(s
, 0x14, 0); /* DAC1 minimum volume */
2238 write_mixer(s
, 0x71, 0x20); /* enable new 0xA1 reg format */
2239 outb(0, s
->ddmabase
+0xd); /* DMA master clear */
2240 outb(1, s
->ddmabase
+0xf); /* mask channel */
2241 /*outb(0, s->ddmabase+0x8);*/ /* enable controller (enable is low active!!) */
2243 pci_set_master(pcidev
); /* enable bus mastering */
2247 val
= SOUND_MASK_LINE
;
2248 mixer_ioctl(s
, SOUND_MIXER_WRITE_RECSRC
, (unsigned long)&val
);
2249 for (i
= 0; i
< sizeof(initvol
)/sizeof(initvol
[0]); i
++) {
2250 val
= initvol
[i
].vol
;
2251 mixer_ioctl(s
, initvol
[i
].mixch
, (unsigned long)&val
);
2253 val
= 1; /* enable mic preamp */
2254 mixer_ioctl(s
, SOUND_MIXER_PRIVATE1
, (unsigned long)&val
);
2260 solo1_suspend(struct pci_dev
*pci_dev
, pm_message_t state
) {
2261 struct solo1_state
*s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
2264 outb(0, s
->iobase
+6);
2265 /* DMA master clear */
2266 outb(0, s
->ddmabase
+0xd);
2267 /* reset sequencer and FIFO */
2268 outb(3, s
->sbbase
+6);
2269 /* turn off DDMA controller address space */
2270 pci_write_config_word(s
->dev
, 0x60, 0);
2275 solo1_resume(struct pci_dev
*pci_dev
) {
2276 struct solo1_state
*s
= (struct solo1_state
*)pci_get_drvdata(pci_dev
);
2283 static int __devinit
solo1_register_gameport(struct solo1_state
*s
, int io_port
)
2285 struct gameport
*gp
;
2287 if (!request_region(io_port
, GAMEPORT_EXTENT
, "ESS Solo1")) {
2288 printk(KERN_ERR
"solo1: gameport io ports are in use\n");
2292 s
->gameport
= gp
= gameport_allocate_port();
2294 printk(KERN_ERR
"solo1: can not allocate memory for gameport\n");
2295 release_region(io_port
, GAMEPORT_EXTENT
);
2299 gameport_set_name(gp
, "ESS Solo1 Gameport");
2300 gameport_set_phys(gp
, "isa%04x/gameport0", io_port
);
2301 gp
->dev
.parent
= &s
->dev
->dev
;
2304 gameport_register_port(gp
);
2309 static int __devinit
solo1_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pciid
)
2311 struct solo1_state
*s
;
2315 if ((ret
=pci_enable_device(pcidev
)))
2317 if (!(pci_resource_flags(pcidev
, 0) & IORESOURCE_IO
) ||
2318 !(pci_resource_flags(pcidev
, 1) & IORESOURCE_IO
) ||
2319 !(pci_resource_flags(pcidev
, 2) & IORESOURCE_IO
) ||
2320 !(pci_resource_flags(pcidev
, 3) & IORESOURCE_IO
))
2322 if (pcidev
->irq
== 0)
2325 /* Recording requires 24-bit DMA, so attempt to set dma mask
2326 * to 24 bits first, then 32 bits (playback only) if that fails.
2328 if (pci_set_dma_mask(pcidev
, 0x00ffffff) &&
2329 pci_set_dma_mask(pcidev
, 0xffffffff)) {
2330 printk(KERN_WARNING
"solo1: architecture does not support 24bit or 32bit PCI busmaster DMA\n");
2334 if (!(s
= kmalloc(sizeof(struct solo1_state
), GFP_KERNEL
))) {
2335 printk(KERN_WARNING
"solo1: out of memory\n");
2338 memset(s
, 0, sizeof(struct solo1_state
));
2339 init_waitqueue_head(&s
->dma_adc
.wait
);
2340 init_waitqueue_head(&s
->dma_dac
.wait
);
2341 init_waitqueue_head(&s
->open_wait
);
2342 init_waitqueue_head(&s
->midi
.iwait
);
2343 init_waitqueue_head(&s
->midi
.owait
);
2344 init_MUTEX(&s
->open_sem
);
2345 spin_lock_init(&s
->lock
);
2346 s
->magic
= SOLO1_MAGIC
;
2348 s
->iobase
= pci_resource_start(pcidev
, 0);
2349 s
->sbbase
= pci_resource_start(pcidev
, 1);
2350 s
->vcbase
= pci_resource_start(pcidev
, 2);
2351 s
->ddmabase
= s
->vcbase
+ DDMABASE_OFFSET
;
2352 s
->mpubase
= pci_resource_start(pcidev
, 3);
2353 gpio
= pci_resource_start(pcidev
, 4);
2354 s
->irq
= pcidev
->irq
;
2356 if (!request_region(s
->iobase
, IOBASE_EXTENT
, "ESS Solo1")) {
2357 printk(KERN_ERR
"solo1: io ports in use\n");
2360 if (!request_region(s
->sbbase
+FMSYNTH_EXTENT
, SBBASE_EXTENT
-FMSYNTH_EXTENT
, "ESS Solo1")) {
2361 printk(KERN_ERR
"solo1: io ports in use\n");
2364 if (!request_region(s
->ddmabase
, DDMABASE_EXTENT
, "ESS Solo1")) {
2365 printk(KERN_ERR
"solo1: io ports in use\n");
2368 if (!request_region(s
->mpubase
, MPUBASE_EXTENT
, "ESS Solo1")) {
2369 printk(KERN_ERR
"solo1: io ports in use\n");
2372 if ((ret
=request_irq(s
->irq
,solo1_interrupt
,SA_SHIRQ
,"ESS Solo1",s
))) {
2373 printk(KERN_ERR
"solo1: irq %u in use\n", s
->irq
);
2376 /* register devices */
2377 if ((s
->dev_audio
= register_sound_dsp(&solo1_audio_fops
, -1)) < 0) {
2381 if ((s
->dev_mixer
= register_sound_mixer(&solo1_mixer_fops
, -1)) < 0) {
2385 if ((s
->dev_midi
= register_sound_midi(&solo1_midi_fops
, -1)) < 0) {
2389 if ((s
->dev_dmfm
= register_sound_special(&solo1_dmfm_fops
, 15 /* ?? */)) < 0) {
2393 if (setup_solo1(s
)) {
2397 /* register gameport */
2398 solo1_register_gameport(s
, gpio
);
2399 /* store it in the driver field */
2400 pci_set_drvdata(pcidev
, s
);
2404 unregister_sound_special(s
->dev_dmfm
);
2406 unregister_sound_midi(s
->dev_midi
);
2408 unregister_sound_mixer(s
->dev_mixer
);
2410 unregister_sound_dsp(s
->dev_audio
);
2412 printk(KERN_ERR
"solo1: initialisation error\n");
2413 free_irq(s
->irq
, s
);
2415 release_region(s
->mpubase
, MPUBASE_EXTENT
);
2417 release_region(s
->ddmabase
, DDMABASE_EXTENT
);
2419 release_region(s
->sbbase
+FMSYNTH_EXTENT
, SBBASE_EXTENT
-FMSYNTH_EXTENT
);
2421 release_region(s
->iobase
, IOBASE_EXTENT
);
2427 static void __devexit
solo1_remove(struct pci_dev
*dev
)
2429 struct solo1_state
*s
= pci_get_drvdata(dev
);
2433 /* stop DMA controller */
2434 outb(0, s
->iobase
+6);
2435 outb(0, s
->ddmabase
+0xd); /* DMA master clear */
2436 outb(3, s
->sbbase
+6); /* reset sequencer and FIFO */
2437 synchronize_irq(s
->irq
);
2438 pci_write_config_word(s
->dev
, 0x60, 0); /* turn off DDMA controller address space */
2439 free_irq(s
->irq
, s
);
2441 int gpio
= s
->gameport
->io
;
2442 gameport_unregister_port(s
->gameport
);
2443 release_region(gpio
, GAMEPORT_EXTENT
);
2445 release_region(s
->iobase
, IOBASE_EXTENT
);
2446 release_region(s
->sbbase
+FMSYNTH_EXTENT
, SBBASE_EXTENT
-FMSYNTH_EXTENT
);
2447 release_region(s
->ddmabase
, DDMABASE_EXTENT
);
2448 release_region(s
->mpubase
, MPUBASE_EXTENT
);
2449 unregister_sound_dsp(s
->dev_audio
);
2450 unregister_sound_mixer(s
->dev_mixer
);
2451 unregister_sound_midi(s
->dev_midi
);
2452 unregister_sound_special(s
->dev_dmfm
);
2454 pci_set_drvdata(dev
, NULL
);
2457 static struct pci_device_id id_table
[] = {
2458 { PCI_VENDOR_ID_ESS
, PCI_DEVICE_ID_ESS_SOLO1
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2462 MODULE_DEVICE_TABLE(pci
, id_table
);
2464 static struct pci_driver solo1_driver
= {
2465 .name
= "ESS Solo1",
2466 .id_table
= id_table
,
2467 .probe
= solo1_probe
,
2468 .remove
= __devexit_p(solo1_remove
),
2469 .suspend
= solo1_suspend
,
2470 .resume
= solo1_resume
,
2474 static int __init
init_solo1(void)
2476 printk(KERN_INFO
"solo1: version v0.20 time " __TIME__
" " __DATE__
"\n");
2477 return pci_register_driver(&solo1_driver
);
2480 /* --------------------------------------------------------------------- */
2482 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2483 MODULE_DESCRIPTION("ESS Solo1 Driver");
2484 MODULE_LICENSE("GPL");
2487 static void __exit
cleanup_solo1(void)
2489 printk(KERN_INFO
"solo1: unloading\n");
2490 pci_unregister_driver(&solo1_driver
);
2493 /* --------------------------------------------------------------------- */
2495 module_init(init_solo1
);
2496 module_exit(cleanup_solo1
);