63 #include <linux/list.h>
64 #include <linux/module.h>
65 #include <linux/string.h>
67 #include <linux/sched.h>
69 #include <linux/sound.h>
70 #include <linux/slab.h>
71 #include <linux/soundcard.h>
72 #include <linux/pci.h>
73 #include <linux/bitops.h>
76 #include <linux/poll.h>
78 #include <linux/kernel.h>
80 #include <asm/byteorder.h>
83 #include <asm/uaccess.h>
100 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
104 #define CS4297a_MAGIC 0xf00beef1
118 #define CSDEBUG_INTERFACE 1
120 #undef CSDEBUG_INTERFACE
125 #define CS_INIT 0x00000001 // initialization and probe functions
126 #define CS_ERROR 0x00000002 // tmp debugging bit placeholder
127 #define CS_INTERRUPT 0x00000004 // interrupt handler (separate from all other)
128 #define CS_FUNCTION 0x00000008 // enter/leave functions
129 #define CS_WAVE_WRITE 0x00000010 // write information for wave
130 #define CS_WAVE_READ 0x00000020 // read information for wave
131 #define CS_AC97 0x00000040 // AC97 register access
132 #define CS_DESCR 0x00000080 // descriptor management
133 #define CS_OPEN 0x00000400 // all open functions in the driver
134 #define CS_RELEASE 0x00000800 // all release functions in the driver
135 #define CS_PARMS 0x00001000 // functional and operational parameters
136 #define CS_IOCTL 0x00002000 // ioctl (non-mixer)
137 #define CS_TMP 0x10000000 // tmp debug mask bit
146 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask) ) {x;}
148 #define CS_DBGOUT(mask,level,x)
152 static unsigned long cs_debuglevel = 4;
153 static unsigned long cs_debugmask =
CS_INIT ;
160 #define CS_TYPE_ADC 0
161 #define CS_TYPE_DAC 1
163 #define SER_BASE (A_SER_BASE_1 + KSEG1)
164 #define SS_CSR(t) (SER_BASE+t)
165 #define SS_TXTBL(t) (SER_BASE+R_SER_TX_TABLE_BASE+(t*8))
166 #define SS_RXTBL(t) (SER_BASE+R_SER_RX_TABLE_BASE+(t*8))
168 #define FRAME_BYTES 32
169 #define FRAME_SAMPLE_BYTES 4
172 #define SAMPLE_BUF_SIZE (16*1024)
173 #define SAMPLE_FRAME_COUNT (SAMPLE_BUF_SIZE / FRAME_SAMPLE_BYTES)
176 #define DMA_BLOAT_FACTOR 1
177 #define DMA_DESCR (SAMPLE_FRAME_COUNT / DMA_BLOAT_FACTOR)
178 #define DMA_BUF_SIZE (DMA_DESCR * FRAME_BYTES)
181 #define DMA_INT_CNT ((1 << S_DMA_INT_PKTCNT) - 1)
184 #define REG_LATENCY 150
186 #define FRAME_TX_US 20
188 #define SERDMA_NEXTBUF(d,f) (((d)->f+1) % (d)->ringsz)
190 static const char invalid_magic[] =
191 KERN_CRIT "cs4297a: invalid magic value\n";
193 #define VALIDATE_STATE(s) \
195 if (!(s) || (s)->magic != CS4297a_MAGIC) { \
196 printk(invalid_magic); \
202 #define AC97_MASTER_VOL_STEREO 0x0002
203 #define AC97_PCBEEP_VOL 0x000a
204 #define AC97_PHONE_VOL 0x000c
205 #define AC97_MIC_VOL 0x000e
206 #define AC97_LINEIN_VOL 0x0010
207 #define AC97_CD_VOL 0x0012
208 #define AC97_AUX_VOL 0x0016
209 #define AC97_PCMOUT_VOL 0x0018
210 #define AC97_RECORD_SELECT 0x001a
211 #define AC97_RECORD_GAIN 0x001c
212 #define AC97_GENERAL_PURPOSE 0x0020
213 #define AC97_3D_CONTROL 0x0022
214 #define AC97_POWER_CONTROL 0x0026
215 #define AC97_VENDOR_ID1 0x007c
327 #define prog_codec(a,b)
328 #define dealloc_dmabuf(a,b);
344 static void clear_advance(
void *
buf,
unsigned bsize,
unsigned bptr,
345 unsigned len,
unsigned char c)
347 if (bptr + len > bsize) {
348 unsigned x = bsize - bptr;
349 memset(((
char *) buf) + bptr, c, x);
354 "cs4297a: clear_advance(): memset %d at 0x%.8x for %d size \n",
355 (
unsigned)c, (
unsigned)((
char *) buf) + bptr, len));
356 memset(((
char *) buf) + bptr, c, len);
363 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
364 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
365 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
366 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
368 static void cs_printioctl(
unsigned int x)
388 case SOUND_MIXER_CS_GETDBGMASK:
390 printk(
"SOUND_MIXER_CS_GETDBGMASK:\n"));
392 case SOUND_MIXER_CS_GETDBGLEVEL:
394 printk(
"SOUND_MIXER_CS_GETDBGLEVEL:\n"));
396 case SOUND_MIXER_CS_SETDBGMASK:
398 printk(
"SOUND_MIXER_CS_SETDBGMASK:\n"));
400 case SOUND_MIXER_CS_SETDBGLEVEL:
402 printk(
"SOUND_MIXER_CS_SETDBGLEVEL:\n"));
466 printk(
"SNDCTL_DSP_SETFRAGMENT:\n"));
476 printk(
"SOUND_PCM_READ_CHANNELS:\n"));
483 printk(
"SOUND_PCM_WRITE_FILTER:\n"));
517 printk(
"SOUND_MIXER_VOLUME:\n"));
521 printk(
"SOUND_MIXER_SPEAKER:\n"));
525 printk(
"SOUND_MIXER_RECLEV:\n"));
529 printk(
"SOUND_MIXER_MIC:\n"));
533 printk(
"SOUND_MIXER_SYNTH:\n"));
537 printk(
"SOUND_MIXER_RECSRC:\n"));
541 printk(
"SOUND_MIXER_DEVMASK:\n"));
545 printk(
"SOUND_MIXER_RECMASK:\n"));
549 printk(
"SOUND_MIXER_STEREODEVS:\n"));
557 || !(vidx = mixtable1[i])) {
559 (
"UNKNOWN IOCTL: 0x%.8x NR=%d\n",
563 (
"SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d\n",
614 for (i=4; i<16; i++) {
626 printk(
KERN_ERR "cs4297a: desc - %d sbufsize - %d dbufsize - %d\n",
664 printk(
KERN_ERR "cs4297a: descrtab - %08x dma_buf - %x sample_buf - %x\n",
678 if (init_serdma(&s->
dma_adc) ||
684 panic(
"DMA state corrupted?!");
700 SS_CSR(R_SER_DMA_CONFIG0_RX));
743 printk(
KERN_ERR "cs4297a: attempt to issue multiple reg_access\n");
758 printk(
KERN_ERR "cs4297a: reg access found bookkeeping error (hw/sw = %d/%d\n",
760 spin_unlock_irqrestore(&s->
lock, flags);
765 spin_unlock_irqrestore(&s->
lock, flags);
768 data_p = &d->
dma_buf[swptr * 4];
773 data_p, swptr, d->
hwptr));
790 if (serdma_reg_access(s, (0xCLL << 60) | (1
LL << 47) | ((
u64)(offset & 0x7F) << 40)))
810 return (serdma_reg_access(s, (0xELL << 60) | ((
u64)(offset & 0x7F) << 40) | ((value & 0xffff) << 12)));
825 SS_CSR(R_SER_DMA_ENABLE));
828 spin_unlock_irqrestore(&s->
lock, flags);
848 "cs4297a: start_dac(): start dma\n"));
851 spin_unlock_irqrestore(&s->
lock, flags);
873 spin_unlock_irqrestore(&s->
lock, flags);
920 spin_unlock_irqrestore(&s->
lock, flags);
923 "cs4297a: start_adc(): start adc\n"));
932 static void cs4297a_update_ptr(
struct cs4297a_state *s,
int intflag)
934 int good_diff, diff, diff2;
969 if (((data & 0x9800000000000000) != 0x9800000000000000) ||
977 if ((data >> 61) == 7) {
983 s->
stats.rx_overflow++;
1017 "cs4297a: update count -> %d\n", d->
count));
1040 (
long long)(descr->
descr_a & M_DMA_DSCRA_A_ADDR),
1043 if (!(data & (1
LL << 63)) ||
1044 !(descr->
descr_a & M_DMA_SERRX_SOP) ||
1050 if ((data >> 61) == 7) {
1056 descr->
descr_a &= ~M_DMA_SERRX_SOP;
1074 "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
1075 (
unsigned)s, d->
hwptr,
1090 hwptr = (unsigned) (((
__raw_readq(
SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
1094 "cs4297a: cs4297a_update_ptr(): hw/hw/sw %x/%x/%x diff %d count %d\n",
1109 if (d->
count <= 0) {
1115 "cs4297a: cs4297a_update_ptr(): memset %d at 0x%.8x for %d size \n",
1123 s->
stats.tx_underrun++;
1126 "cs4297a: cs4297a_update_ptr(): underrun\n"));
1128 }
else if (d->
count <=
1139 if ( (d->
count <= (
signed) d->
sbufsz/2) || intflag)
1143 "cs4297a: update count -> %d\n", d->
count));
1148 "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
1149 (
unsigned) s, d->
hwptr,
1159 static const unsigned int mixer_src[8] = {
1179 static const unsigned mixreg[] = {
1185 unsigned char l,
r, rl, rr, vidx;
1186 unsigned char attentbl[11] =
1187 { 63, 42, 26, 17, 14, 11, 8, 6, 4, 2, 0 };
1193 "cs4297a: mixer_ioctl(): s=0x%.8x cmd=0x%.8x\n",
1194 (
unsigned) s, cmd));
1198 #if CSDEBUG_INTERFACE
1200 if ((cmd == SOUND_MIXER_CS_GETDBGMASK) ||
1201 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
1202 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
1203 (cmd == SOUND_MIXER_CS_SETDBGLEVEL))
1207 case SOUND_MIXER_CS_GETDBGMASK:
1209 (
unsigned long *) arg);
1211 case SOUND_MIXER_CS_GETDBGLEVEL:
1213 (
unsigned long *) arg);
1215 case SOUND_MIXER_CS_SETDBGMASK:
1216 if (
get_user(val, (
unsigned long *) arg))
1221 case SOUND_MIXER_CS_SETDBGLEVEL:
1222 if (
get_user(val, (
unsigned long *) arg))
1224 cs_debuglevel =
val;
1228 "cs4297a: mixer_ioctl(): ERROR unknown debug cmd\n"));
1242 temp1 = (val & 0x3f) >> 2;
1250 return put_user((temp1 << 2) | 3, (
int *) arg);
1254 memset(&info, 0,
sizeof(info));
1264 memset(&info, 0,
sizeof(info));
1284 return put_user(mixer_src[temp1 & 7], (
int *) arg);
1306 || !(vidx = mixtable1[i]))
1308 return put_user(s->
mix.vol[vidx - 1], (
int *) arg);
1328 for (i = 0; i <
sizeof(mixer_src) /
sizeof(
int); i++) {
1329 if (val == mixer_src[i]) {
1330 temp1 = (i << 8) | i;
1331 cs4297a_write_ac97(s,
1349 rl = attentbl[(10 *
l) / 100];
1351 r = (val >> 8) & 0xff;
1358 rr = attentbl[(10 *
r) / 100];
1360 if ((rl > 60) && (rr > 60))
1365 temp1 |= (rl << 8) | rr;
1370 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1371 s->
mix.vol[8] = ((
unsigned int) r << 8) |
l;
1387 rl = (l * 2 - 5) / 13;
1388 l = (rl * 13 + 5) / 2;
1400 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1401 s->
mix.vol[6] = l << 8;
1413 r = (val >> 8) & 0xff;
1416 rl = (l * 2 - 5) / 13;
1417 rr = (r * 2 - 5) / 13;
1418 if (rl < 3 && rr < 3)
1423 temp1 = temp1 | (rl << 8) | rr;
1426 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1427 s->
mix.vol[7] = ((
unsigned int) r << 8) |
l;
1443 rl = ((unsigned) l * 5 - 4) / 16;
1444 l = (rl * 16 + 4) / 5;
1456 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1457 s->
mix.vol[5] = val << 8;
1472 r = (val >> 8) & 0xff;
1475 rl = (l * 2 - 11) / 3;
1476 rr = (r * 2 - 11) / 3;
1491 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1492 s->
mix.vol[4] = (r << 8) | l;
1501 "cs4297a: mixer_ioctl(): default\n"));
1515 rl = (attentbl[(l * 10) / 100]) >> 1;
1517 r = (val >> 8) & 0xff;
1524 rr = (attentbl[(r * 10) / 100]) >> 1;
1525 if ((rl > 30) && (rr > 30))
1529 temp1 = temp1 | (rl << 8) | rr;
1530 cs4297a_write_ac97(s, mixreg[vidx - 1], temp1);
1532 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1533 s->
mix.vol[vidx - 1] = ((
unsigned int) r << 8) |
l;
1535 s->
mix.vol[vidx - 1] =
val;
1537 return put_user(s->
mix.vol[vidx - 1], (
int *) arg);
1546 int minor = iminor(inode);
1579 static int cs4297a_release_mixdev(
struct inode *inode,
struct file *file)
1589 static int cs4297a_ioctl_mixdev(
struct file *file,
1590 unsigned int cmd,
unsigned long arg)
1607 .unlocked_ioctl = cs4297a_ioctl_mixdev,
1608 .open = cs4297a_open_mixdev,
1609 .release = cs4297a_release_mixdev,
1627 unsigned long flags;
1639 if (!signal_pending(
current)) {
1653 spin_unlock_irqrestore(&s->
lock, flags);
1662 static ssize_t cs4297a_read(
struct file *file,
char *
buffer,
size_t count,
1668 unsigned long flags;
1669 int cnt, count_fr, cnt_by;
1670 unsigned copied = 0;
1694 "_read() count>0 count=%d .count=%d .swptr=%d .hwptr=%d \n",
1714 spin_unlock_irqrestore(&s->
lock, flags);
1720 if (cnt_by > (count * 2)) {
1721 cnt = (count * 2) / FRAME_SAMPLE_BYTES;
1725 if (cnt_by > count) {
1742 return ret ? ret : -
EAGAIN;
1755 "_read() copy_to cnt=%d count=%d ", cnt_by, count));
1757 " .sbufsz=%d .count=%d buffer=0x%.8x ret=%d\n",
1759 (
unsigned) buffer, ret));
1762 return ret ? ret : -
EFAULT;
1773 spin_unlock_irqrestore(&s->
lock, flags);
1785 static ssize_t cs4297a_write(
struct file *file,
const char *buffer,
1786 size_t count, loff_t * ppos)
1791 unsigned long flags;
1792 unsigned swptr, hwptr;
1823 hwptr = (unsigned) (((
__raw_readq(
SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
1832 spin_unlock_irqrestore(&s->
lock, flags);
1838 return ret ? ret : -
EAGAIN;
1845 return ret ? ret : -
EFAULT;
1858 left = tmp & 0xffff;
1883 "cs4297a: copy in %d to swptr %x\n", cnt, swptr));
1891 spin_unlock_irqrestore(&s->
lock, flags);
1903 static unsigned int cs4297a_poll(
struct file *file,
1908 unsigned long flags;
1909 unsigned int mask = 0;
1917 "cs4297a: cs4297a_poll() wait on FMODE_WRITE\n"));
1925 "cs4297a: cs4297a_poll() wait on FMODE_READ\n"));
1955 spin_unlock_irqrestore(&s->
lock, flags);
1963 static int cs4297a_mmap(
struct file *file,
struct vm_area_struct *vma)
1971 static int cs4297a_ioctl(
struct file *file,
1972 unsigned int cmd,
unsigned long arg)
1976 unsigned long flags;
1982 "cs4297a: cs4297a_ioctl(): file=0x%.8x cmd=0x%.8x\n",
1983 (
unsigned) file, cmd));
1993 "cs4297a: cs4297a_ioctl(): SOUND_VERSION=0x%.8x\n",
1999 "cs4297a: cs4297a_ioctl(): DSP_SYNC\n"));
2016 "cs4297a: cs4297a_ioctl(): DSP_RESET\n"));
2041 "cs4297a: cs4297a_ioctl(): DSP_SPEED val=%d -> 48000\n", val));
2049 "cs4297a: cs4297a_ioctl(): DSP_STEREO val=%d\n", val));
2053 s->
prop_adc.channels = val ? 2 : 1;
2058 s->
prop_dac.channels = val ? 2 : 1;
2066 "cs4297a: cs4297a_ioctl(): DSP_CHANNELS val=%d\n",
2096 "cs4297a: cs4297a_ioctl(): DSP_GETFMT val=0x%.8x\n",
2106 "cs4297a: cs4297a_ioctl(): DSP_SETFMT val=0x%.8x\n",
2136 "cs4297a: cs4297a_ioctl(): DSP_SETFMT return val=0x%.8x\n",
2142 "cs4297a: cs4297a_ioctl(): DSP_POST\n"));
2159 && (ret = prog_dmabuf_adc(s)))
2168 && (ret = prog_dmabuf_dac(s)))
2192 "cs4297a: cs4297a_ioctl(): GETOSPACE .fragsize=%d .bytes=%d .fragstotal=%d .fragments=%d\n",
2193 abinfo.fragsize,abinfo.bytes,abinfo.fragstotal,
2195 spin_unlock_irqrestore(&s->
lock, flags);
2197 sizeof(abinfo)) ? -
EFAULT : 0;
2219 spin_unlock_irqrestore(&s->
lock, flags);
2221 sizeof(abinfo)) ? -
EFAULT : 0;
2224 spin_lock(&file->f_lock);
2226 spin_unlock(&file->f_lock);
2237 spin_unlock_irqrestore(&s->
lock, flags);
2270 spin_unlock_irqrestore(&s->
lock, flags);
2294 spin_unlock_irqrestore(&s->
lock, flags);
2299 if ((val = prog_dmabuf_dac(s)))
2303 if ((val = prog_dmabuf_adc(s)))
2322 if (val != 1 && val != 2 && val != 4)
2361 return mixer_ioctl(s, cmd, arg);
2364 static long cs4297a_unlocked_ioctl(
struct file *file,
u_int cmd,
u_long arg)
2369 ret = cs4297a_ioctl(file, cmd, arg);
2375 static int cs4297a_release(
struct inode *inode,
struct file *file)
2381 "cs4297a: cs4297a_release(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
2382 (
unsigned) inode, (
unsigned) file, file->
f_mode));
2406 static int cs4297a_locked_open(
struct inode *inode,
struct file *file)
2408 int minor = iminor(inode);
2413 "cs4297a: cs4297a_open(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
2414 (
unsigned) inode, (
unsigned) file, file->
f_mode));
2425 if (entry == &cs4297a_devs)
2429 "cs4297a: cs4297a_open(): Error - unable to find audio state struct\n"));
2438 "cs4297a: cs4297a_open(): Error - must open READ and/or WRITE\n"));
2457 if (signal_pending(
current)) {
2458 printk(
"open - sig pending\n");
2474 if (signal_pending(
current)) {
2475 printk(
"open - sig pending\n");
2493 if (prog_dmabuf_adc(s)) {
2495 "cs4297a: adc Program dmabufs failed.\n"));
2496 cs4297a_release(inode, file);
2511 if (prog_dmabuf_dac(s)) {
2513 "cs4297a: dac Program dmabufs failed.\n"));
2514 cs4297a_release(inode, file);
2523 static int cs4297a_open(
struct inode *inode,
struct file *file)
2528 ret = cs4297a_open(inode, file);
2540 .read = cs4297a_read,
2541 .write = cs4297a_write,
2542 .poll = cs4297a_poll,
2543 .unlocked_ioctl = cs4297a_unlocked_ioctl,
2544 .mmap = cs4297a_mmap,
2545 .open = cs4297a_open,
2546 .release = cs4297a_release,
2549 static void cs4297a_interrupt(
int irq,
void *
dev_id)
2557 "cs4297a: cs4297a_interrupt() HISR=0x%.8x\n", status));
2563 printk(
KERN_ERR "cs4297a: unexpected interrupt (status %08x)\n", status);
2570 printk(
KERN_ERR "cs4297a: rx sync error (status %08x)\n", status);
2576 s->
stats.rx_ovrrn++;
2583 newptr = (unsigned) (((
__raw_readq(
SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
2594 spin_lock(&s->
lock);
2595 cs4297a_update_ptr(s,
CS_TRUE);
2596 spin_unlock(&s->
lock);
2599 "cs4297a: cs4297a_interrupt()-\n"));
2603 static struct initvol {
2620 static int __init cs4297a_init(
void)
2626 #ifndef CONFIG_BCM_CS4297A_CSWARM
2632 "cs4297a: cs4297a_init_module()+ \n"));
2634 #ifndef CONFIG_BCM_CS4297A_CSWARM
2640 if (!(cfg & M_SYS_SER1_ENABLE)) {
2643 if (!(cfg & M_SYS_SER1_ENABLE)) {
2644 printk(
KERN_INFO "cs4297a: serial port 1 not configured for synchronous operation\n");
2648 printk(
KERN_INFO "cs4297a: serial port 1 switching to synchronous operation\n");
2664 "cs4297a: probe() no memory for state struct.\n"));
2682 (s->
irq, cs4297a_interrupt, 0,
"Crystal CS4297a", s)) {
2690 "cs4297a: probe() register_sound_dsp() failed.\n"));
2696 "cs4297a: probe() register_sound_mixer() failed.\n"));
2702 "cs4297a: ser_init failed.\n"));
2709 }
while (!rval && (pwr != 0xf));
2712 char *sb1250_duart_present;
2720 val = initvol[
i].vol;
2721 mixer_ioctl(s, initvol[i].mixch, (
unsigned long) &val);
2726 cs4297a_write_ac97(s, 0x02, 0x0808);
2727 cs4297a_write_ac97(s, 0x18, 0x0808);
2731 list_add(&s->
list, &cs4297a_devs);
2735 sb1250_duart_present =
symbol_get(sb1250_duart_present);
2736 if (sb1250_duart_present)
2737 sb1250_duart_present[1] = 0;
2761 static void __exit cs4297a_cleanup(
void)