📄 harmony.c
字号:
/* drivers/sound/harmony.c This is a sound driver for ASP's and Lasi's Harmony sound chip and is unlikely to be used for anything other than on a HP PA-RISC. Harmony is found in HP 712s, 715/new and many other GSC based machines. On older 715 machines you'll find the technically identical chip called 'Vivace'. Both Harmony and Vicace are supported by this driver. Copyright 2000 (c) Linuxcare Canada, Alex deVries <alex@onefishtwo.ca> Copyright 2000-2003 (c) Helge Deller <deller@gmx.de> Copyright 2001 (c) Matthieu Delahaye <delahaym@esiee.fr> Copyright 2001 (c) Jean-Christophe Vaugeois <vaugeoij@esiee.fr> Copyright 2004 (c) Stuart Brady <sdbrady@ntlworld.com> TODO: - fix SNDCTL_DSP_GETOSPACE and SNDCTL_DSP_GETISPACE ioctls to return the real values - add private ioctl for selecting line- or microphone input (only one of them is available at the same time) - add module parameters - implement mmap functionality - implement gain meter ? - ...*/#include <linux/delay.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/ioport.h>#include <linux/types.h>#include <linux/mm.h>#include <linux/pci.h>#include <asm/parisc-device.h>#include <asm/io.h>#include "sound_config.h"#define PFX "harmony: "#define HARMONY_VERSION "V0.9a"#undef DEBUG#ifdef DEBUG# define DPRINTK printk #else# define DPRINTK(x,...)#endif#define MAX_BUFS 10 /* maximum number of rotating buffers */#define HARMONY_BUF_SIZE 4096 /* needs to be a multiple of PAGE_SIZE (4096)! */#define CNTL_C 0x80000000#define CNTL_ST 0x00000020#define CNTL_44100 0x00000015 /* HARMONY_SR_44KHZ */#define CNTL_8000 0x00000008 /* HARMONY_SR_8KHZ */#define GAINCTL_HE 0x08000000#define GAINCTL_LE 0x04000000#define GAINCTL_SE 0x02000000#define DSTATUS_PN 0x00000200#define DSTATUS_RN 0x00000002#define DSTATUS_IE 0x80000000#define HARMONY_DF_16BIT_LINEAR 0#define HARMONY_DF_8BIT_ULAW 1#define HARMONY_DF_8BIT_ALAW 2#define HARMONY_SS_MONO 0#define HARMONY_SS_STEREO 1#define HARMONY_SR_8KHZ 0x08#define HARMONY_SR_16KHZ 0x09#define HARMONY_SR_27KHZ 0x0A#define HARMONY_SR_32KHZ 0x0B#define HARMONY_SR_48KHZ 0x0E#define HARMONY_SR_9KHZ 0x0F#define HARMONY_SR_5KHZ 0x10#define HARMONY_SR_11KHZ 0x11#define HARMONY_SR_18KHZ 0x12#define HARMONY_SR_22KHZ 0x13#define HARMONY_SR_37KHZ 0x14#define HARMONY_SR_44KHZ 0x15#define HARMONY_SR_33KHZ 0x16#define HARMONY_SR_6KHZ 0x17/* * Some magics numbers used to auto-detect file formats */#define HARMONY_MAGIC_8B_ULAW 1#define HARMONY_MAGIC_8B_ALAW 27#define HARMONY_MAGIC_16B_LINEAR 3#define HARMONY_MAGIC_MONO 1#define HARMONY_MAGIC_STEREO 2/* * Channels Positions in mixer register */#define GAIN_HE_SHIFT 27#define GAIN_HE_MASK ( 1 << GAIN_HE_SHIFT) #define GAIN_LE_SHIFT 26#define GAIN_LE_MASK ( 1 << GAIN_LE_SHIFT) #define GAIN_SE_SHIFT 25#define GAIN_SE_MASK ( 1 << GAIN_SE_SHIFT) #define GAIN_IS_SHIFT 24#define GAIN_IS_MASK ( 1 << GAIN_IS_SHIFT) #define GAIN_MA_SHIFT 20#define GAIN_MA_MASK ( 0x0f << GAIN_MA_SHIFT) #define GAIN_LI_SHIFT 16#define GAIN_LI_MASK ( 0x0f << GAIN_LI_SHIFT) #define GAIN_RI_SHIFT 12#define GAIN_RI_MASK ( 0x0f << GAIN_RI_SHIFT) #define GAIN_LO_SHIFT 6#define GAIN_LO_MASK ( 0x3f << GAIN_LO_SHIFT) #define GAIN_RO_SHIFT 0#define GAIN_RO_MASK ( 0x3f << GAIN_RO_SHIFT) #define MAX_OUTPUT_LEVEL (GAIN_RO_MASK >> GAIN_RO_SHIFT)#define MAX_INPUT_LEVEL (GAIN_RI_MASK >> GAIN_RI_SHIFT)#define MAX_MONITOR_LEVEL (GAIN_MA_MASK >> GAIN_MA_SHIFT)#define MIXER_INTERNAL SOUND_MIXER_LINE1#define MIXER_LINEOUT SOUND_MIXER_LINE2#define MIXER_HEADPHONES SOUND_MIXER_LINE3#define MASK_INTERNAL SOUND_MASK_LINE1#define MASK_LINEOUT SOUND_MASK_LINE2#define MASK_HEADPHONES SOUND_MASK_LINE3/* * Channels Mask in mixer register */#define GAIN_TOTAL_SILENCE 0x00F00FFF#define GAIN_DEFAULT 0x0FF00000struct harmony_hpa { u8 unused000; u8 id; u8 teleshare_id; u8 unused003; u32 reset; u32 cntl; u32 gainctl; u32 pnxtadd; u32 pcuradd; u32 rnxtadd; u32 rcuradd; u32 dstatus; u32 ov; u32 pio; u32 unused02c; u32 unused030[3]; u32 diag;};struct harmony_dev { struct harmony_hpa *hpa; struct parisc_device *dev; u32 current_gain; u32 dac_rate; /* 8000 ... 48000 (Hz) */ u8 data_format; /* HARMONY_DF_xx_BIT_xxx */ u8 sample_rate; /* HARMONY_SR_xx_KHZ */ u8 stereo_select; /* HARMONY_SS_MONO or HARMONY_SS_STEREO */ int format_initialized :1; int suspended_playing :1; int suspended_recording :1; int blocked_playing :1; int blocked_recording :1; int audio_open :1; int mixer_open :1; wait_queue_head_t wq_play, wq_record; int first_filled_play; /* first buffer containing data (next to play) */ int nb_filled_play; int play_offset; int first_filled_record; int nb_filled_record; int dsp_unit, mixer_unit;};static struct harmony_dev harmony;/* * Dynamic sound buffer allocation and DMA memory */struct harmony_buffer { unsigned char *addr; dma_addr_t dma_handle; int dma_coherent; /* Zero if dma_alloc_coherent() fails */ unsigned int len;};/* * Harmony memory buffers */static struct harmony_buffer played_buf, recorded_buf, silent, graveyard;#define CHECK_WBACK_INV_OFFSET(b,offset,len) \ do { if (!b.dma_coherent) \ dma_cache_wback_inv((unsigned long)b.addr+offset,len); \ } while (0) static int __init harmony_alloc_buffer(struct harmony_buffer *b, unsigned int buffer_count){ b->len = buffer_count * HARMONY_BUF_SIZE; b->addr = dma_alloc_coherent(&harmony.dev->dev, b->len, &b->dma_handle, GFP_KERNEL|GFP_DMA); if (b->addr && b->dma_handle) { b->dma_coherent = 1; DPRINTK(KERN_INFO PFX "coherent memory: 0x%lx, played_buf: 0x%lx\n", (unsigned long)b->dma_handle, (unsigned long)b->addr); } else { b->dma_coherent = 0; /* kmalloc()ed memory will HPMC on ccio machines ! */ b->addr = kmalloc(b->len, GFP_KERNEL); if (!b->addr) { printk(KERN_ERR PFX "couldn't allocate memory\n"); return -EBUSY; } b->dma_handle = __pa(b->addr); } return 0;}static void __exit harmony_free_buffer(struct harmony_buffer *b){ if (!b->addr) return; if (b->dma_coherent) dma_free_coherent(&harmony.dev->dev, b->len, b->addr, b->dma_handle); else kfree(b->addr); memset(b, 0, sizeof(*b));}/* * Low-Level sound-chip programming */static void __inline__ harmony_wait_CNTL(void){ /* Wait until we're out of control mode */ while (gsc_readl(&harmony.hpa->cntl) & CNTL_C) /* wait */ ;}static void harmony_update_control(void) { u32 default_cntl; /* Set CNTL */ default_cntl = (CNTL_C | /* The C bit */ (harmony.data_format << 6) | /* Set the data format */ (harmony.stereo_select << 5) | /* Stereo select */ (harmony.sample_rate)); /* Set sample rate */ harmony.format_initialized = 1; /* initialize CNTL */ gsc_writel(default_cntl, &harmony.hpa->cntl);}static void harmony_set_control(u8 data_format, u8 sample_rate, u8 stereo_select) { harmony.sample_rate = sample_rate; harmony.data_format = data_format; harmony.stereo_select = stereo_select; harmony_update_control();}static void harmony_set_rate(u8 data_rate) { harmony.sample_rate = data_rate; harmony_update_control();}static int harmony_detect_rate(int *freq){ int newrate; switch (*freq) { case 8000: newrate = HARMONY_SR_8KHZ; break; case 16000: newrate = HARMONY_SR_16KHZ; break; case 27428: newrate = HARMONY_SR_27KHZ; break; case 32000: newrate = HARMONY_SR_32KHZ; break; case 48000: newrate = HARMONY_SR_48KHZ; break; case 9600: newrate = HARMONY_SR_9KHZ; break; case 5125: newrate = HARMONY_SR_5KHZ; break; case 11025: newrate = HARMONY_SR_11KHZ; break; case 18900: newrate = HARMONY_SR_18KHZ; break; case 22050: newrate = HARMONY_SR_22KHZ; break; case 37800: newrate = HARMONY_SR_37KHZ; break; case 44100: newrate = HARMONY_SR_44KHZ; break; case 33075: newrate = HARMONY_SR_33KHZ; break; case 6615: newrate = HARMONY_SR_6KHZ; break; default: newrate = HARMONY_SR_8KHZ; *freq = 8000; break; } return newrate;}static void harmony_set_format(u8 data_format) { harmony.data_format = data_format; harmony_update_control();}static void harmony_set_stereo(u8 stereo_select) { harmony.stereo_select = stereo_select; harmony_update_control();}static void harmony_disable_interrupts(void) { harmony_wait_CNTL(); gsc_writel(0, &harmony.hpa->dstatus); }static void harmony_enable_interrupts(void) { harmony_wait_CNTL(); gsc_writel(DSTATUS_IE, &harmony.hpa->dstatus); }/* * harmony_silence() * * This subroutine fills in a buffer starting at location start and * silences for length bytes. This references the current * configuration of the audio format. * */static void harmony_silence(struct harmony_buffer *buffer, int start, int length) { u8 silence_char; /* Despite what you hear, silence is different in different audio formats. */ switch (harmony.data_format) { case HARMONY_DF_8BIT_ULAW: silence_char = 0x55; break; case HARMONY_DF_8BIT_ALAW: silence_char = 0xff; break; case HARMONY_DF_16BIT_LINEAR: /* fall through */ default: silence_char = 0; } memset(buffer->addr+start, silence_char, length);}static int harmony_audio_open(struct inode *inode, struct file *file){ if (harmony.audio_open) return -EBUSY; harmony.audio_open = 1; harmony.suspended_playing = harmony.suspended_recording = 1; harmony.blocked_playing = harmony.blocked_recording = 0; harmony.first_filled_play = harmony.first_filled_record = 0; harmony.nb_filled_play = harmony.nb_filled_record = 0; harmony.play_offset = 0; init_waitqueue_head(&harmony.wq_play); init_waitqueue_head(&harmony.wq_record); /* Start off in a balanced mode. */ harmony_set_control(HARMONY_DF_8BIT_ULAW, HARMONY_SR_8KHZ, HARMONY_SS_MONO); harmony_update_control(); harmony.format_initialized = 0; /* Clear out all the buffers and flush to cache */ harmony_silence(&played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS); CHECK_WBACK_INV_OFFSET(played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS); return 0;}/* * Release (close) the audio device. */static int harmony_audio_release(struct inode *inode, struct file *file){ if (!harmony.audio_open) return -EBUSY; harmony.audio_open = 0; return 0;}/* * Read recorded data off the audio device. */static ssize_t harmony_audio_read(struct file *file, char *buffer, size_t size_count, loff_t *ppos){ int total_count = (int) size_count; int count = 0; int buf_to_read; while (count<total_count) { /* Wait until we're out of control mode */ harmony_wait_CNTL(); /* Figure out which buffer to fill in */ if (harmony.nb_filled_record <= 2) { harmony.blocked_recording = 1; if (harmony.suspended_recording) { harmony.suspended_recording = 0; harmony_enable_interrupts(); } interruptible_sleep_on(&harmony.wq_record); harmony.blocked_recording = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -