📄 2410audio_rec.c
字号:
/* * Philips UDA1380 Audio Device Driver for S3C2410 Linux * * Copyright (C) 2002 MIZI Research, Inc. * */#include <linux/module.h>#include <linux/init.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/sched.h>#include <linux/poll.h>#include <linux/interrupt.h>#include <linux/errno.h>#include <linux/sound.h>#include <linux/soundcard.h>#include <linux/pm.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/hardware.h>#include <asm/semaphore.h>#include <asm/dma.h>#include <asm/arch/cpu_s3c2410.h>#include "khead.h"#define UDA1380#undef DEBUG//#define DEBUG#ifdef DEBUG#define DPRINTK( x... ) printk( ##x )#else#define DPRINTK( x... )#endif/* UDA1380 Register bits */#define UDA1380_ADDR 0x14#define UDA1380_REG_DATA 0x0#define UDA1380_REG_STATUS 0x2#define SC_512fs (0x0 << 4)#define SC_384fs (0x1 << 4)#define SC_256fs (0x2 << 4)#define IF_IIS (0x0 << 1)#define IF_LSB_16 (0x1 << 1)#define IF_LSB_18 (0x2 << 1)#define IF_LSB_20 (0x3 << 1)#define IF_MSB (0x4 << 1)#define MUTE (0x1 << 2)#define NO_DE_EMPHASIS (0x10 << 3)#define DE_EMPHASIS_32 (0x11 << 3)#define DE_EMPHASIS_441 (0x12 << 3)#define DE_EMPHASIS_48 (0x13 << 3)#define GPIO_L3CLOCK (GPIO_MODE_OUT | GPIO_PULLUP_DIS | GPIO_B4)#define GPIO_L3DATA (GPIO_MODE_OUT | GPIO_PULLUP_DIS | GPIO_B3)#define GPIO_L3MODE (GPIO_MODE_OUT | GPIO_PULLUP_DIS | GPIO_B2)#define AUDIO_NAME "UDA1380"#define AUDIO_NAME_VERBOSE "UDA1380 audio driver"#define AUDIO_FMT_MASK (AFMT_S16_LE)#define AUDIO_FMT_DEFAULT (AFMT_S16_LE)#define AUDIO_CHANNELS_DEFAULT 2#define AUDIO_RATE_DEFAULT 22050 //8000#define AUDIO_NBFRAGS_DEFAULT 8 //16#define AUDIO_FRAGSIZE_DEFAULT 8192#define S_CLOCK_FREQ 384#define PCM_ABS(a) (a < 0 ? -a : a)typedef struct { int size; /* buffer size */ char *start; /* point to actual buffer */ dma_addr_t dma_addr; /* physical buffer address */ struct semaphore sem; /* down before touching the buffer */ int master; /* owner for buffer allocation, contain size when true */} audio_buf_t;typedef struct { audio_buf_t *buffers; /* pointer to audio buffer structures */ audio_buf_t *buf; /* current buffer used by read/write */ u_int buf_idx; /* index for the pointer above */ u_int fragsize; /* fragment i.e. buffer size */ u_int nbfrags; /* nbr of fragments */ dmach_t dma_ch; /* DMA channel (channel2 for audio) */} audio_stream_t;static audio_stream_t output_stream;static audio_stream_t input_stream; #define NEXT_BUF(_s_,_b_) { \ (_s_)->_b_##_idx++; \ (_s_)->_b_##_idx %= (_s_)->nbfrags; \ (_s_)->_b_ = (_s_)->buffers + (_s_)->_b_##_idx; }static u_int audio_rate;static int audio_channels;static int audio_fmt;static u_int audio_fragsize;static u_int audio_nbfrags;static int audio_rd_refcount;static int audio_wr_refcount;#define audio_active (audio_rd_refcount | audio_wr_refcount)//static int audio_dev_dsp;static int audio_dev_mixer;static int audio_mix_modcnt;static int uda1380_volume;//static u8 uda_sampling;static void audio_clear_buf(audio_stream_t * s){ DPRINTK("audio_clear_buf\n"); s3c2410_dma_flush_all(s->dma_ch); if (s->buffers) { int frag; for (frag = 0; frag < s->nbfrags; frag++) { if (!s->buffers[frag].master) //查看该缓冲区是否有数据 continue; consistent_free(s->buffers[frag].start, s->buffers[frag].master, s->buffers[frag].dma_addr); } kfree(s->buffers); s->buffers = NULL; } s->buf_idx = 0; s->buf = NULL;}static int audio_setup_buf(audio_stream_t * s){ int frag; int dmasize = 0; char *dmabuf = 0; dma_addr_t dmaphys = 0; if (s->buffers) return -EBUSY; s->nbfrags = audio_nbfrags; s->fragsize = audio_fragsize; s->buffers = (audio_buf_t *) kmalloc(sizeof(audio_buf_t) * s->nbfrags, GFP_KERNEL); if (!s->buffers) goto err; memset(s->buffers, 0, sizeof(audio_buf_t) * s->nbfrags); for (frag = 0; frag < s->nbfrags; frag++) { audio_buf_t *b = &s->buffers[frag]; if (!dmasize) { dmasize = (s->nbfrags - frag) * s->fragsize; do { dmabuf = consistent_alloc(GFP_KERNEL|GFP_DMA, dmasize, &dmaphys); if (!dmabuf) dmasize -= s->fragsize; } while (!dmabuf && dmasize); if (!dmabuf) goto err; b->master = dmasize; } b->start = dmabuf; b->dma_addr = dmaphys; sema_init(&b->sem, 1); DPRINTK("buf %d: start %p dma %p\n", frag, b->start, b->dma_addr); dmabuf += s->fragsize; dmaphys += s->fragsize; dmasize -= s->fragsize; } s->buf_idx = 0; s->buf = &s->buffers[0]; return 0; err: printk(AUDIO_NAME ": unable to allocate audio memory\n "); audio_clear_buf(s); return -ENOMEM;}static void audio_dmaout_done_callback(void *buf_id, int size){ audio_buf_t *b = (audio_buf_t *) buf_id; DPRINTK("DMA-OUT%d\n", size); up(&b->sem); wake_up(&b->sem.wait);}static void audio_dmain_done_callback(void *buf_id, int size){ audio_buf_t *b = (audio_buf_t *) buf_id; DPRINTK("DMA-IN%d\n", size); b->size = size; up(&b->sem); wake_up(&b->sem.wait);}static int audio_sync(struct file *file){ audio_stream_t *s = &output_stream; audio_buf_t *b = s->buf; DPRINTK("audio_sync\n"); if (!s->buffers) return 0; if (b->size != 0) { down(&b->sem); s3c2410_dma_queue_buffer(s->dma_ch, (void *) b, b->dma_addr, b->size, DMA_BUF_WR); b->size = 0; NEXT_BUF(s, buf); } b = s->buffers + ((s->nbfrags + s->buf_idx - 1) % s->nbfrags); if (down_interruptible(&b->sem)) return -EINTR; up(&b->sem); return 0;}static inline int copy_from_user_mono_stereo(char *to, const char *from, int count){ u_int *dst = (u_int *)to; const char *end = from + count; if (verify_area(VERIFY_READ, from, count)) return -EFAULT; if ((int)from & 0x2) { u_int v; __get_user(v, (const u_short *)from); from += 2; *dst++ = v | (v << 16); } while (from < end-2) { u_int v, x, y; __get_user(v, (const u_int *)from); from += 4; x = v << 16; x |= x >> 16; y = v >> 16; y |= y << 16; *dst++ = x; *dst++ = y; } if (from < end) { u_int v; __get_user(v, (const u_short *)from); *dst = v | (v << 16); } return 0;}static ssize_t smdk2410_mixer_read(struct file *file, char *buffer, size_t count, loff_t * ppos){ char *buffer0 = buffer; audio_stream_t *s = &input_stream; int chunksize, ret = 0; DPRINTK("audio_read: count=%d\n", count); switch (file->f_flags & O_ACCMODE) { case O_RDONLY: case O_RDWR: break; default: return -EPERM; } if (!s->buffers && audio_setup_buf(s)) return -ENOMEM; count &= ~0x03; while (count > 0) { audio_buf_t *b = s->buf; /* Wait for a buffer to become full */ if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; if (down_trylock(&b->sem)) break; } else { ret = -ERESTARTSYS; if (down_interruptible(&b->sem)) break; } /* Grab data from the current buffer */ chunksize = b->size; if (chunksize > count) chunksize = count;//************add******* DPRINTK("read %d from %d\n", chunksize, s->buf_idx); if (copy_to_user (buffer, b->start + s->fragsize - b->size, chunksize)) { up(&b->sem); return -EFAULT; } b->size -= chunksize; buffer += chunksize; count -= chunksize; if (b->size > 0) { up(&b->sem); break; } /* Make current buffer available for DMA again */ DPRINTK("b->dma_addr %x\n",b->dma_addr); s3c2410_dma_queue_buffer(s->dma_ch, (void *) b, b->dma_addr, s->fragsize, DMA_BUF_RD); NEXT_BUF(s, buf); } if ((buffer - buffer0)) ret = buffer - buffer0; DPRINTK("audio_read: return=%d\n", ret); return ret;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -