cs4281m.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:133k
- return -EFAULT;
- return 0;
- }
- if (cmd == OSS_GETVERSION)
- return put_user(SOUND_VERSION, (int *) arg);
- if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
- return -EINVAL;
- // If ioctl has only the SIOC_READ bit(bit 31)
- // on, process the only-read commands.
- if (_SIOC_DIR(cmd) == _SIOC_READ) {
- switch (_IOC_NR(cmd)) {
- case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
- cs4281_read_ac97(s, BA0_AC97_RECORD_SELECT,
- &temp1);
- return put_user(mixer_src[temp1 & 7], (int *) arg);
- case SOUND_MIXER_DEVMASK: // Arg contains a bit for each supported device
- return put_user(SOUND_MASK_PCM | SOUND_MASK_SYNTH |
- SOUND_MASK_CD | SOUND_MASK_LINE |
- SOUND_MASK_LINE1 | SOUND_MASK_MIC |
- SOUND_MASK_VOLUME |
- SOUND_MASK_RECLEV |
- SOUND_MASK_SPEAKER, (int *) arg);
- case SOUND_MIXER_RECMASK: // Arg contains a bit for each supported recording source
- return_mask = (((SOUND_MASK_LINE | SOUND_MASK_MIC |
- SOUND_MASK_CD | SOUND_MASK_VOLUME |
- SOUND_MASK_LINE1) ) & ~recsrc_invalid);
- CS_DBGOUT(CS_PARMS, 6, printk(KERN_INFO
- "cs4281: mixer_ioctl(): return_mask=0x%x recsrc_invalid=0x%xn",
- (unsigned)return_mask,(unsigned)recsrc_invalid));
- return put_user(return_mask, (int *) arg);
- case SOUND_MIXER_STEREODEVS: // Mixer channels supporting stereo
- return put_user(SOUND_MASK_PCM | SOUND_MASK_SYNTH |
- SOUND_MASK_CD | SOUND_MASK_LINE |
- SOUND_MASK_LINE1 | SOUND_MASK_MIC |
- SOUND_MASK_VOLUME |
- SOUND_MASK_RECLEV, (int *) arg);
- case SOUND_MIXER_CAPS:
- return put_user(SOUND_CAP_EXCL_INPUT, (int *) arg);
- default:
- i = _IOC_NR(cmd);
- if (i >= SOUND_MIXER_NRDEVICES
- || !(vidx = mixtable1[i]))
- return -EINVAL;
- return put_user(s->mix.vol[vidx - 1], (int *) arg);
- }
- }
- // If ioctl doesn't have both the SIOC_READ and
- // the SIOC_WRITE bit set, return invalid.
- if (_SIOC_DIR(cmd) != (_SIOC_READ | _SIOC_WRITE))
- return -EINVAL;
- // Increment the count of volume writes.
- s->mix.modcnt++;
- // Isolate the command; it must be a write.
- switch (_IOC_NR(cmd)) {
- case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
- if (get_user(val, (int *) arg))
- return -EFAULT;
- i = hweight32(val); // i = # bits on in val.
- if (i != 1) // One & only 1 bit must be on.
- return 0;
- if(val & recsrc_invalid)
- {
- CS_DBGOUT(CS_ERROR, 2, printk(KERN_INFO
- "cs4281: mixer_ioctl(): REC SOURCE select error - record source invalid on this system (0x%x)n",
- val));
- return -EINVAL;
- }
- for (i = 0; i < sizeof(mixer_src) / sizeof(int); i++) {
- if (val == mixer_src[i]) {
- temp1 = (i << 8) | i;
- cs4281_write_ac97(s,
- BA0_AC97_RECORD_SELECT,
- temp1);
- return 0;
- }
- }
- return 0;
- case SOUND_MIXER_VOLUME:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100; // Max soundcard.h vol is 100.
- if (l < 6) {
- rl = 63;
- l = 0;
- } else
- rl = attentbl[(10 * l) / 100]; // Convert 0-100 vol to 63-0 atten.
- r = (val >> 8) & 0xff;
- if (r > 100)
- r = 100; // Max right volume is 100, too
- if (r < 6) {
- rr = 63;
- r = 0;
- } else
- rr = attentbl[(10 * r) / 100]; // Convert volume to attenuation.
- if ((rl > 60) && (rr > 60)) // If both l & r are 'low',
- temp1 = 0x8000; // turn on the mute bit.
- else
- temp1 = 0;
- temp1 |= (rl << 8) | rr;
- cs4281_write_ac97(s, BA0_AC97_MASTER_VOLUME, temp1);
- cs4281_write_ac97(s, BA0_AC97_HEADPHONE_VOLUME, temp1);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[8] = ((unsigned int) r << 8) | l;
- #else
- s->mix.vol[8] = val;
- #endif
- return put_user(s->mix.vol[8], (int *) arg);
- case SOUND_MIXER_SPEAKER:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100;
- if (l < 3) {
- rl = 0;
- l = 0;
- } else {
- rl = (l * 2 - 5) / 13; // Convert 0-100 range to 0-15.
- l = (rl * 13 + 5) / 2;
- }
- if (rl < 3) {
- temp1 = 0x8000;
- rl = 0;
- } else
- temp1 = 0;
- rl = 15 - rl; // Convert volume to attenuation.
- temp1 |= rl << 1;
- cs4281_write_ac97(s, BA0_AC97_PC_BEEP_VOLUME, temp1);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[6] = l << 8;
- #else
- s->mix.vol[6] = val;
- #endif
- return put_user(s->mix.vol[6], (int *) arg);
- case SOUND_MIXER_RECLEV:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100;
- r = (val >> 8) & 0xff;
- if (r > 100)
- r = 100;
- rl = (l * 2 - 5) / 13; // Convert 0-100 scale to 0-15.
- rr = (r * 2 - 5) / 13;
- if (rl < 3 && rr < 3)
- temp1 = 0x8000;
- else
- temp1 = 0;
- temp1 = temp1 | (rl << 8) | rr;
- cs4281_write_ac97(s, BA0_AC97_RECORD_GAIN, temp1);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[7] = ((unsigned int) r << 8) | l;
- #else
- s->mix.vol[7] = val;
- #endif
- return put_user(s->mix.vol[7], (int *) arg);
- case SOUND_MIXER_MIC:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100;
- if (l < 1) {
- l = 0;
- rl = 0;
- } else {
- rl = ((unsigned) l * 5 - 4) / 16; // Convert 0-100 range to 0-31.
- l = (rl * 16 + 4) / 5;
- }
- cs4281_read_ac97(s, BA0_AC97_MIC_VOLUME, &temp1);
- temp1 &= 0x40; // Isolate 20db gain bit.
- if (rl < 3) {
- temp1 |= 0x8000;
- rl = 0;
- }
- rl = 31 - rl; // Convert volume to attenuation.
- temp1 |= rl;
- cs4281_write_ac97(s, BA0_AC97_MIC_VOLUME, temp1);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[5] = val << 8;
- #else
- s->mix.vol[5] = val;
- #endif
- return put_user(s->mix.vol[5], (int *) arg);
- case SOUND_MIXER_SYNTH:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100;
- if (get_user(val, (int *) arg))
- return -EFAULT;
- r = (val >> 8) & 0xff;
- if (r > 100)
- r = 100;
- rl = (l * 2 - 11) / 3; // Convert 0-100 range to 0-63.
- rr = (r * 2 - 11) / 3;
- if (rl < 3) // If l is low, turn on
- temp1 = 0x0080; // the mute bit.
- else
- temp1 = 0;
- rl = 63 - rl; // Convert vol to attenuation.
- writel(temp1 | rl, s->pBA0 + BA0_FMLVC);
- if (rr < 3) // If rr is low, turn on
- temp1 = 0x0080; // the mute bit.
- else
- temp1 = 0;
- rr = 63 - rr; // Convert vol to attenuation.
- writel(temp1 | rr, s->pBA0 + BA0_FMRVC);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[4] = (r << 8) | l;
- #else
- s->mix.vol[4] = val;
- #endif
- return put_user(s->mix.vol[4], (int *) arg);
- default:
- CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
- "cs4281: mixer_ioctl(): defaultn"));
- i = _IOC_NR(cmd);
- if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
- return -EINVAL;
- if (get_user(val, (int *) arg))
- return -EFAULT;
- l = val & 0xff;
- if (l > 100)
- l = 100;
- if (l < 1) {
- l = 0;
- rl = 31;
- } else
- rl = (attentbl[(l * 10) / 100]) >> 1;
- r = (val >> 8) & 0xff;
- if (r > 100)
- r = 100;
- if (r < 1) {
- r = 0;
- rr = 31;
- } else
- rr = (attentbl[(r * 10) / 100]) >> 1;
- if ((rl > 30) && (rr > 30))
- temp1 = 0x8000;
- else
- temp1 = 0;
- temp1 = temp1 | (rl << 8) | rr;
- cs4281_write_ac97(s, mixreg[vidx - 1], temp1);
- #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
- s->mix.vol[vidx - 1] = ((unsigned int) r << 8) | l;
- #else
- s->mix.vol[vidx - 1] = val;
- #endif
- #ifndef NOT_CS4281_PM
- CS_DBGOUT(CS_PM, 9, printk(KERN_INFO
- "write ac97 mixreg[%d]=0x%x mix.vol[]=0x%xn",
- vidx-1,temp1,s->mix.vol[vidx-1]));
- #endif
- return put_user(s->mix.vol[vidx - 1], (int *) arg);
- }
- }
- // ---------------------------------------------------------------------
- static loff_t cs4281_llseek(struct file *file, loff_t offset, int origin)
- {
- return -ESPIPE;
- }
- // ---------------------------------------------------------------------
- static int cs4281_open_mixdev(struct inode *inode, struct file *file)
- {
- int minor = MINOR(inode->i_rdev);
- struct cs4281_state *s=NULL;
- struct list_head *entry;
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
- printk(KERN_INFO "cs4281: cs4281_open_mixdev()+n"));
- list_for_each(entry, &cs4281_devs)
- {
- s = list_entry(entry, struct cs4281_state, list);
- if(s->dev_mixer == minor)
- break;
- }
- if (!s)
- {
- CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
- printk(KERN_INFO "cs4281: cs4281_open_mixdev()- -ENODEVn"));
- return -ENODEV;
- }
- VALIDATE_STATE(s);
- file->private_data = s;
- MOD_INC_USE_COUNT;
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
- printk(KERN_INFO "cs4281: cs4281_open_mixdev()- 0n"));
- return 0;
- }
- static int cs4281_release_mixdev(struct inode *inode, struct file *file)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- VALIDATE_STATE(s);
- MOD_DEC_USE_COUNT;
- return 0;
- }
- static int cs4281_ioctl_mixdev(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
- {
- return mixer_ioctl((struct cs4281_state *) file->private_data, cmd,
- arg);
- }
- // ******************************************************************************************
- // Mixer file operations struct.
- // ******************************************************************************************
- static /*const */ struct file_operations cs4281_mixer_fops = {
- llseek:cs4281_llseek,
- ioctl:cs4281_ioctl_mixdev,
- open:cs4281_open_mixdev,
- release:cs4281_release_mixdev,
- };
- // ---------------------------------------------------------------------
- static int drain_adc(struct cs4281_state *s, int nonblock)
- {
- DECLARE_WAITQUEUE(wait, current);
- unsigned long flags;
- int count;
- unsigned tmo;
- if (s->dma_adc.mapped)
- return 0;
- add_wait_queue(&s->dma_adc.wait, &wait);
- for (;;) {
- set_current_state(TASK_INTERRUPTIBLE);
- spin_lock_irqsave(&s->lock, flags);
- count = s->dma_adc.count;
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_adc() %dn", count));
- spin_unlock_irqrestore(&s->lock, flags);
- if (count <= 0) {
- CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: drain_adc() count<0n"));
- break;
- }
- if (signal_pending(current))
- break;
- if (nonblock) {
- remove_wait_queue(&s->dma_adc.wait, &wait);
- current->state = TASK_RUNNING;
- return -EBUSY;
- }
- tmo =
- 3 * HZ * (count +
- s->dma_adc.fragsize) / 2 / s->prop_adc.rate;
- if (s->prop_adc.fmt & (AFMT_S16_LE | AFMT_U16_LE))
- tmo >>= 1;
- if (s->prop_adc.channels > 1)
- tmo >>= 1;
- if (!schedule_timeout(tmo + 1))
- printk(KERN_DEBUG "cs4281: dma timed out??n");
- }
- remove_wait_queue(&s->dma_adc.wait, &wait);
- current->state = TASK_RUNNING;
- if (signal_pending(current))
- return -ERESTARTSYS;
- return 0;
- }
- static int drain_dac(struct cs4281_state *s, int nonblock)
- {
- DECLARE_WAITQUEUE(wait, current);
- unsigned long flags;
- int count;
- unsigned tmo;
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_dac()+n"));
- if (s->dma_dac.mapped)
- {
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_dac()- (mmap) 0n"));
- return 0;
- }
- if (s->ena & FMODE_WRITE)
- add_wait_queue(&s->dma_dac.wait, &wait);
- else
- return 0;
- for (;;) {
- set_current_state(TASK_INTERRUPTIBLE);
- spin_lock_irqsave(&s->lock, flags);
- count = s->dma_dac.count;
- spin_unlock_irqrestore(&s->lock, flags);
- if (count <= 0)
- break;
- if (signal_pending(current))
- break;
- if (nonblock) {
- remove_wait_queue(&s->dma_dac.wait, &wait);
- current->state = TASK_RUNNING;
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_dac()- -EBUSYn"));
- return -EBUSY;
- }
- tmo =
- 3 * HZ * (count +
- s->dma_dac.fragsize) / 2 / s->prop_dac.rate;
- if (s->prop_dac.fmt & (AFMT_S16_LE | AFMT_U16_LE))
- tmo >>= 1;
- if (s->prop_dac.channels > 1)
- tmo >>= 1;
- if (!schedule_timeout(tmo + 1))
- printk(KERN_DEBUG "cs4281: dma timed out??n");
- }
- remove_wait_queue(&s->dma_dac.wait, &wait);
- current->state = TASK_RUNNING;
- if (signal_pending(current))
- {
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_dac()- -ERESTARTSYSn"));
- return -ERESTARTSYS;
- }
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: drain_dac()- 0n"));
- return 0;
- }
- //****************************************************************************
- //
- // CopySamples copies 16-bit stereo samples from the source to the
- // destination, possibly converting down to either 8-bit or mono or both.
- // count specifies the number of output bytes to write.
- //
- // Arguments:
- //
- // dst - Pointer to a destination buffer.
- // src - Pointer to a source buffer
- // count - The number of bytes to copy into the destination buffer.
- // iChannels - Stereo - 2
- // Mono - 1
- // fmt - AFMT_xxx (soundcard.h formats)
- //
- // NOTES: only call this routine for conversion to 8bit from 16bit
- //
- //****************************************************************************
- static void CopySamples(char *dst, char *src, int count, int iChannels,
- unsigned fmt)
- {
- unsigned short *psSrc;
- long lAudioSample;
- CS_DBGOUT(CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: CopySamples()+ "));
- CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
- " dst=0x%x src=0x%x count=%d iChannels=%d fmt=0x%xn",
- (unsigned) dst, (unsigned) src, (unsigned) count,
- (unsigned) iChannels, (unsigned) fmt));
- // Gershwin does format conversion in hardware so normally
- // we don't do any host based coversion. The data formatter
- // truncates 16 bit data to 8 bit and that causes some hiss.
- // We have already forced the HW to do 16 bit sampling and
- // 2 channel so that we can use software to round instead
- // of truncate
- //
- // See if the data should be output as 8-bit unsigned stereo.
- // or if the data should be output at 8-bit unsigned mono.
- //
- if ( ((iChannels == 2) && (fmt & AFMT_U8)) ||
- ((iChannels == 1) && (fmt & AFMT_U8)) ) {
- //
- // Convert each 16-bit unsigned stereo sample to 8-bit unsigned
- // stereo using rounding.
- //
- psSrc = (unsigned short *) src;
- count = count / 2;
- while (count--) {
- lAudioSample = (long) psSrc[count] + (long) 0x80;
- if (lAudioSample > 0xffff) {
- lAudioSample = 0xffff;
- }
- dst[count] = (char) (lAudioSample >> 8);
- }
- }
- //
- // check for 8-bit signed stereo.
- //
- else if ((iChannels == 2) && (fmt & AFMT_S8)) {
- //
- // Convert each 16-bit stereo sample to 8-bit stereo using rounding.
- //
- psSrc = (short *) src;
- while (count--) {
- lAudioSample =
- (((long) psSrc[0] + (long) psSrc[1]) / 2);
- psSrc += 2;
- *dst++ = (char) ((short) lAudioSample >> 8);
- }
- }
- //
- // Otherwise, the data should be output as 8-bit signed mono.
- //
- else if ((iChannels == 1) && (fmt & AFMT_S8)) {
- //
- // Convert each 16-bit signed mono sample to 8-bit signed mono
- // using rounding.
- //
- psSrc = (short *) src;
- count = count / 2;
- while (count--) {
- lAudioSample =
- (((long) psSrc[0] + (long) psSrc[1]) / 2);
- if (lAudioSample > 0x7fff) {
- lAudioSample = 0x7fff;
- }
- psSrc += 2;
- *dst++ = (char) ((short) lAudioSample >> 8);
- }
- }
- }
- //
- // cs_copy_to_user()
- // replacement for the standard copy_to_user, to allow for a conversion from
- // 16 bit to 8 bit if the record conversion is active. the cs4281 has some
- // issues with 8 bit capture, so the driver always captures data in 16 bit
- // and then if the user requested 8 bit, converts from 16 to 8 bit.
- //
- static unsigned cs_copy_to_user(struct cs4281_state *s, void *dest,
- unsigned *hwsrc, unsigned cnt,
- unsigned *copied)
- {
- void *src = hwsrc; //default to the standard destination buffer addr
- CS_DBGOUT(CS_FUNCTION, 6, printk(KERN_INFO
- "cs_copy_to_user()+ fmt=0x%x fmt_o=0x%x cnt=%d dest=0x%.8xn",
- s->prop_adc.fmt, s->prop_adc.fmt_original,
- (unsigned) cnt, (unsigned) dest));
- if (cnt > s->dma_adc.dmasize) {
- cnt = s->dma_adc.dmasize;
- }
- if (!cnt) {
- *copied = 0;
- return 0;
- }
- if (s->conversion) {
- if (!s->tmpbuff) {
- *copied = cnt / 2;
- return 0;
- }
- CopySamples(s->tmpbuff, (void *) hwsrc, cnt,
- (unsigned) s->prop_adc.channels,
- s->prop_adc.fmt_original);
- src = s->tmpbuff;
- cnt = cnt / 2;
- }
- if (copy_to_user(dest, src, cnt)) {
- *copied = 0;
- return -EFAULT;
- }
- *copied = cnt;
- CS_DBGOUT(CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: cs_copy_to_user()- copied bytes is %d n", cnt));
- return 0;
- }
- // ---------------------------------------------------------------------
- static ssize_t cs4281_read(struct file *file, char *buffer, size_t count,
- loff_t * ppos)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- ssize_t ret;
- unsigned long flags;
- unsigned swptr;
- int cnt;
- unsigned copied = 0;
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
- printk(KERN_INFO "cs4281: cs4281_read()+ %d n", count));
- VALIDATE_STATE(s);
- if (ppos != &file->f_pos)
- return -ESPIPE;
- if (s->dma_adc.mapped)
- return -ENXIO;
- if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
- return ret;
- if (!access_ok(VERIFY_WRITE, buffer, count))
- return -EFAULT;
- ret = 0;
- //
- // "count" is the amount of bytes to read (from app), is decremented each loop
- // by the amount of bytes that have been returned to the user buffer.
- // "cnt" is the running total of each read from the buffer (changes each loop)
- // "buffer" points to the app's buffer
- // "ret" keeps a running total of the amount of bytes that have been copied
- // to the user buffer.
- // "copied" is the total bytes copied into the user buffer for each loop.
- //
- while (count > 0) {
- CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
- "_read() count>0 count=%d .count=%d .swptr=%d .hwptr=%d n",
- count, s->dma_adc.count,
- s->dma_adc.swptr, s->dma_adc.hwptr));
- spin_lock_irqsave(&s->lock, flags);
- // get the current copy point of the sw buffer
- swptr = s->dma_adc.swptr;
- // cnt is the amount of unread bytes from the end of the
- // hw buffer to the current sw pointer
- cnt = s->dma_adc.dmasize - swptr;
- // dma_adc.count is the current total bytes that have not been read.
- // if the amount of unread bytes from the current sw pointer to the
- // end of the buffer is greater than the current total bytes that
- // have not been read, then set the "cnt" (unread bytes) to the
- // amount of unread bytes.
- if (s->dma_adc.count < cnt)
- cnt = s->dma_adc.count;
- spin_unlock_irqrestore(&s->lock, flags);
- //
- // if we are converting from 8/16 then we need to copy
- // twice the number of 16 bit bytes then 8 bit bytes.
- //
- if (s->conversion) {
- if (cnt > (count * 2))
- cnt = (count * 2);
- } else {
- if (cnt > count)
- cnt = count;
- }
- //
- // "cnt" NOW is the smaller of the amount that will be read,
- // and the amount that is requested in this read (or partial).
- // if there are no bytes in the buffer to read, then start the
- // ADC and wait for the interrupt handler to wake us up.
- //
- if (cnt <= 0) {
- // start up the dma engine and then continue back to the top of
- // the loop when wake up occurs.
- start_adc(s);
- if (file->f_flags & O_NONBLOCK)
- return ret ? ret : -EAGAIN;
- interruptible_sleep_on(&s->dma_adc.wait);
- if (signal_pending(current))
- return ret ? ret : -ERESTARTSYS;
- continue;
- }
- // there are bytes in the buffer to read.
- // copy from the hw buffer over to the user buffer.
- // user buffer is designated by "buffer"
- // virtual address to copy from is rawbuf+swptr
- // the "cnt" is the number of bytes to read.
- CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
- "_read() copy_to cnt=%d count=%d ", cnt, count));
- CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
- " .dmasize=%d .count=%d buffer=0x%.8x ret=%dn",
- s->dma_adc.dmasize, s->dma_adc.count,
- (unsigned) buffer, ret));
- if (cs_copy_to_user
- (s, buffer, s->dma_adc.rawbuf + swptr, cnt, &copied))
- return ret ? ret : -EFAULT;
- swptr = (swptr + cnt) % s->dma_adc.dmasize;
- spin_lock_irqsave(&s->lock, flags);
- s->dma_adc.swptr = swptr;
- s->dma_adc.count -= cnt;
- spin_unlock_irqrestore(&s->lock, flags);
- count -= copied;
- buffer += copied;
- ret += copied;
- start_adc(s);
- }
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
- printk(KERN_INFO "cs4281: cs4281_read()- %dn", ret));
- return ret;
- }
- static ssize_t cs4281_write(struct file *file, const char *buffer,
- size_t count, loff_t * ppos)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- ssize_t ret;
- unsigned long flags;
- unsigned swptr, hwptr, busaddr;
- int cnt;
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
- printk(KERN_INFO "cs4281: cs4281_write()+ count=%dn",
- count));
- VALIDATE_STATE(s);
- if (ppos != &file->f_pos)
- return -ESPIPE;
- if (s->dma_dac.mapped)
- return -ENXIO;
- if (!s->dma_dac.ready && (ret = prog_dmabuf_dac(s)))
- return ret;
- if (!access_ok(VERIFY_READ, buffer, count))
- return -EFAULT;
- ret = 0;
- while (count > 0) {
- spin_lock_irqsave(&s->lock, flags);
- if (s->dma_dac.count < 0) {
- s->dma_dac.count = 0;
- s->dma_dac.swptr = s->dma_dac.hwptr;
- }
- if (s->dma_dac.underrun) {
- s->dma_dac.underrun = 0;
- hwptr = readl(s->pBA0 + BA0_DCA0);
- busaddr = virt_to_bus(s->dma_dac.rawbuf);
- hwptr -= (unsigned) busaddr;
- s->dma_dac.swptr = s->dma_dac.hwptr = hwptr;
- }
- swptr = s->dma_dac.swptr;
- cnt = s->dma_dac.dmasize - swptr;
- if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
- cnt = s->dma_dac.dmasize - s->dma_dac.count;
- spin_unlock_irqrestore(&s->lock, flags);
- if (cnt > count)
- cnt = count;
- if (cnt <= 0) {
- start_dac(s);
- if (file->f_flags & O_NONBLOCK)
- return ret ? ret : -EAGAIN;
- interruptible_sleep_on(&s->dma_dac.wait);
- if (signal_pending(current))
- return ret ? ret : -ERESTARTSYS;
- continue;
- }
- if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt))
- return ret ? ret : -EFAULT;
- swptr = (swptr + cnt) % s->dma_dac.dmasize;
- spin_lock_irqsave(&s->lock, flags);
- s->dma_dac.swptr = swptr;
- s->dma_dac.count += cnt;
- s->dma_dac.endcleared = 0;
- spin_unlock_irqrestore(&s->lock, flags);
- count -= cnt;
- buffer += cnt;
- ret += cnt;
- start_dac(s);
- }
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
- printk(KERN_INFO "cs4281: cs4281_write()- %dn", ret));
- return ret;
- }
- /*
- * cs4281_poll(struct file *file, struct poll_table_struct *wait)
- */
- static unsigned int cs4281_poll(struct file *file,
- struct poll_table_struct *wait)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- unsigned long flags;
- unsigned int mask = 0;
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
- printk(KERN_INFO "cs4281: cs4281_poll()+ wait=0x%xn", (unsigned)wait));
- VALIDATE_STATE(s);
- if (file->f_mode & FMODE_WRITE) {
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
- printk(KERN_INFO
- "cs4281: cs4281_poll() wait on FMODE_WRITEn"));
- if(!s->dma_dac.ready && prog_dmabuf_dac(s))
- return 0;
- poll_wait(file, &s->dma_dac.wait, wait);
- } else if (file->f_mode & FMODE_READ) {
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
- printk(KERN_INFO
- "cs4281: cs4281_poll() wait on FMODE_READn"));
- if(!s->dma_adc.ready && prog_dmabuf_adc(s))
- return 0;
- poll_wait(file, &s->dma_adc.wait, wait);
- }
- spin_lock_irqsave(&s->lock, flags);
- if (file->f_mode & FMODE_WRITE) {
- if (s->dma_dac.mapped) {
- if (s->dma_dac.count >=
- (signed) s->dma_dac.fragsize) {
- if (s->dma_dac.wakeup)
- mask |= POLLOUT | POLLWRNORM;
- else
- mask = 0;
- s->dma_dac.wakeup = 0;
- }
- } else {
- if ((signed) (s->dma_dac.dmasize/2) >= s->dma_dac.count)
- mask |= POLLOUT | POLLWRNORM;
- }
- } else if (file->f_mode & FMODE_READ) {
- if (s->dma_adc.mapped) {
- if (s->dma_adc.count >= (signed) s->dma_adc.fragsize)
- mask |= POLLIN | POLLRDNORM;
- } else {
- if (s->dma_adc.count > 0)
- mask |= POLLIN | POLLRDNORM;
- }
- }
- spin_unlock_irqrestore(&s->lock, flags);
- CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
- printk(KERN_INFO "cs4281: cs4281_poll()- 0x%.8xn",
- mask));
- return mask;
- }
- static int cs4281_mmap(struct file *file, struct vm_area_struct *vma)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- struct dmabuf *db;
- int ret;
- unsigned long size;
- CS_DBGOUT(CS_FUNCTION | CS_PARMS | CS_OPEN, 4,
- printk(KERN_INFO "cs4281: cs4281_mmap()+n"));
- VALIDATE_STATE(s);
- if (vma->vm_flags & VM_WRITE) {
- if ((ret = prog_dmabuf_dac(s)) != 0)
- return ret;
- db = &s->dma_dac;
- } else if (vma->vm_flags & VM_READ) {
- if ((ret = prog_dmabuf_adc(s)) != 0)
- return ret;
- db = &s->dma_adc;
- } else
- return -EINVAL;
- //
- // only support PLAYBACK for now
- //
- db = &s->dma_dac;
- if ((vma->vm_pgoff) != 0)
- return -EINVAL;
- size = vma->vm_end - vma->vm_start;
- if (size > (PAGE_SIZE << db->buforder))
- return -EINVAL;
- if (remap_page_range
- (vma->vm_start, virt_to_phys(db->rawbuf), size,
- vma->vm_page_prot)) return -EAGAIN;
- db->mapped = 1;
- CS_DBGOUT(CS_FUNCTION | CS_PARMS | CS_OPEN, 4,
- printk(KERN_INFO "cs4281: cs4281_mmap()- 0 size=%dn",
- (unsigned) size));
- return 0;
- }
- static int cs4281_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- unsigned long flags;
- audio_buf_info abinfo;
- count_info cinfo;
- int val, mapped, ret;
- CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): file=0x%.8x cmd=0x%.8xn",
- (unsigned) file, cmd));
- #if CSDEBUG
- cs_printioctl(cmd);
- #endif
- VALIDATE_STATE(s);
- mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
- ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
- switch (cmd) {
- case OSS_GETVERSION:
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): SOUND_VERSION=0x%.8xn",
- SOUND_VERSION));
- return put_user(SOUND_VERSION, (int *) arg);
- case SNDCTL_DSP_SYNC:
- CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_SYNCn"));
- if (file->f_mode & FMODE_WRITE)
- return drain_dac(s,
- 0 /*file->f_flags & O_NONBLOCK */
- );
- return 0;
- case SNDCTL_DSP_SETDUPLEX:
- return 0;
- case SNDCTL_DSP_GETCAPS:
- return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
- DSP_CAP_TRIGGER | DSP_CAP_MMAP,
- (int *) arg);
- case SNDCTL_DSP_RESET:
- CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_RESETn"));
- if (file->f_mode & FMODE_WRITE) {
- stop_dac(s);
- synchronize_irq();
- s->dma_dac.swptr = s->dma_dac.hwptr =
- s->dma_dac.count = s->dma_dac.total_bytes =
- s->dma_dac.blocks = s->dma_dac.wakeup = 0;
- prog_codec(s, CS_TYPE_DAC);
- }
- if (file->f_mode & FMODE_READ) {
- stop_adc(s);
- synchronize_irq();
- s->dma_adc.swptr = s->dma_adc.hwptr =
- s->dma_adc.count = s->dma_adc.total_bytes =
- s->dma_adc.blocks = s->dma_dac.wakeup = 0;
- prog_codec(s, CS_TYPE_ADC);
- }
- return 0;
- case SNDCTL_DSP_SPEED:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_SPEED val=%dn", val));
- //
- // support independent capture and playback channels
- // assume that the file mode bit determines the
- // direction of the data flow.
- //
- if (file->f_mode & FMODE_READ) {
- if (val >= 0) {
- stop_adc(s);
- s->dma_adc.ready = 0;
- // program sampling rates
- if (val > 48000)
- val = 48000;
- if (val < 6300)
- val = 6300;
- s->prop_adc.rate = val;
- prog_codec(s, CS_TYPE_ADC);
- }
- }
- if (file->f_mode & FMODE_WRITE) {
- if (val >= 0) {
- stop_dac(s);
- s->dma_dac.ready = 0;
- // program sampling rates
- if (val > 48000)
- val = 48000;
- if (val < 6300)
- val = 6300;
- s->prop_dac.rate = val;
- prog_codec(s, CS_TYPE_DAC);
- }
- }
- if (file->f_mode & FMODE_WRITE)
- val = s->prop_dac.rate;
- else if (file->f_mode & FMODE_READ)
- val = s->prop_adc.rate;
- return put_user(val, (int *) arg);
- case SNDCTL_DSP_STEREO:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_STEREO val=%dn", val));
- if (file->f_mode & FMODE_READ) {
- stop_adc(s);
- s->dma_adc.ready = 0;
- s->prop_adc.channels = val ? 2 : 1;
- prog_codec(s, CS_TYPE_ADC);
- }
- if (file->f_mode & FMODE_WRITE) {
- stop_dac(s);
- s->dma_dac.ready = 0;
- s->prop_dac.channels = val ? 2 : 1;
- prog_codec(s, CS_TYPE_DAC);
- }
- return 0;
- case SNDCTL_DSP_CHANNELS:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_CHANNELS val=%dn",
- val));
- if (val != 0) {
- if (file->f_mode & FMODE_READ) {
- stop_adc(s);
- s->dma_adc.ready = 0;
- if (val >= 2)
- s->prop_adc.channels = 2;
- else
- s->prop_adc.channels = 1;
- prog_codec(s, CS_TYPE_ADC);
- }
- if (file->f_mode & FMODE_WRITE) {
- stop_dac(s);
- s->dma_dac.ready = 0;
- if (val >= 2)
- s->prop_dac.channels = 2;
- else
- s->prop_dac.channels = 1;
- prog_codec(s, CS_TYPE_DAC);
- }
- }
- if (file->f_mode & FMODE_WRITE)
- val = s->prop_dac.channels;
- else if (file->f_mode & FMODE_READ)
- val = s->prop_adc.channels;
- return put_user(val, (int *) arg);
- case SNDCTL_DSP_GETFMTS: // Returns a mask
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_GETFMT val=0x%.8xn",
- AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
- AFMT_U8));
- return put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
- AFMT_U8, (int *) arg);
- case SNDCTL_DSP_SETFMT:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_SETFMT val=0x%.8xn",
- val));
- if (val != AFMT_QUERY) {
- if (file->f_mode & FMODE_READ) {
- stop_adc(s);
- s->dma_adc.ready = 0;
- if (val != AFMT_S16_LE
- && val != AFMT_U16_LE && val != AFMT_S8
- && val != AFMT_U8)
- val = AFMT_U8;
- s->prop_adc.fmt = val;
- s->prop_adc.fmt_original = s->prop_adc.fmt;
- prog_codec(s, CS_TYPE_ADC);
- }
- if (file->f_mode & FMODE_WRITE) {
- stop_dac(s);
- s->dma_dac.ready = 0;
- if (val != AFMT_S16_LE
- && val != AFMT_U16_LE && val != AFMT_S8
- && val != AFMT_U8)
- val = AFMT_U8;
- s->prop_dac.fmt = val;
- s->prop_dac.fmt_original = s->prop_dac.fmt;
- prog_codec(s, CS_TYPE_DAC);
- }
- } else {
- if (file->f_mode & FMODE_WRITE)
- val = s->prop_dac.fmt_original;
- else if (file->f_mode & FMODE_READ)
- val = s->prop_adc.fmt_original;
- }
- CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_SETFMT return val=0x%.8xn",
- val));
- return put_user(val, (int *) arg);
- case SNDCTL_DSP_POST:
- CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): DSP_POSTn"));
- return 0;
- case SNDCTL_DSP_GETTRIGGER:
- val = 0;
- if (file->f_mode & s->ena & FMODE_READ)
- val |= PCM_ENABLE_INPUT;
- if (file->f_mode & s->ena & FMODE_WRITE)
- val |= PCM_ENABLE_OUTPUT;
- return put_user(val, (int *) arg);
- case SNDCTL_DSP_SETTRIGGER:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- if (file->f_mode & FMODE_READ) {
- if (val & PCM_ENABLE_INPUT) {
- if (!s->dma_adc.ready
- && (ret = prog_dmabuf_adc(s)))
- return ret;
- start_adc(s);
- } else
- stop_adc(s);
- }
- if (file->f_mode & FMODE_WRITE) {
- if (val & PCM_ENABLE_OUTPUT) {
- if (!s->dma_dac.ready
- && (ret = prog_dmabuf_dac(s)))
- return ret;
- start_dac(s);
- } else
- stop_dac(s);
- }
- return 0;
- case SNDCTL_DSP_GETOSPACE:
- if (!(file->f_mode & FMODE_WRITE))
- return -EINVAL;
- if (!s->dma_dac.ready && (val = prog_dmabuf_dac(s)))
- return val;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_update_ptr(s,CS_TRUE);
- abinfo.fragsize = s->dma_dac.fragsize;
- if (s->dma_dac.mapped)
- abinfo.bytes = s->dma_dac.dmasize;
- else
- abinfo.bytes =
- s->dma_dac.dmasize - s->dma_dac.count;
- abinfo.fragstotal = s->dma_dac.numfrag;
- abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
- CS_DBGOUT(CS_FUNCTION | CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): GETOSPACE .fragsize=%d .bytes=%d .fragstotal=%d .fragments=%dn",
- abinfo.fragsize,abinfo.bytes,abinfo.fragstotal,
- abinfo.fragments));
- spin_unlock_irqrestore(&s->lock, flags);
- return copy_to_user((void *) arg, &abinfo,
- sizeof(abinfo)) ? -EFAULT : 0;
- case SNDCTL_DSP_GETISPACE:
- if (!(file->f_mode & FMODE_READ))
- return -EINVAL;
- if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)))
- return val;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_update_ptr(s,CS_TRUE);
- if (s->conversion) {
- abinfo.fragsize = s->dma_adc.fragsize / 2;
- abinfo.bytes = s->dma_adc.count / 2;
- abinfo.fragstotal = s->dma_adc.numfrag;
- abinfo.fragments =
- abinfo.bytes >> (s->dma_adc.fragshift - 1);
- } else {
- abinfo.fragsize = s->dma_adc.fragsize;
- abinfo.bytes = s->dma_adc.count;
- abinfo.fragstotal = s->dma_adc.numfrag;
- abinfo.fragments =
- abinfo.bytes >> s->dma_adc.fragshift;
- }
- spin_unlock_irqrestore(&s->lock, flags);
- return copy_to_user((void *) arg, &abinfo,
- sizeof(abinfo)) ? -EFAULT : 0;
- case SNDCTL_DSP_NONBLOCK:
- file->f_flags |= O_NONBLOCK;
- return 0;
- case SNDCTL_DSP_GETODELAY:
- if (!(file->f_mode & FMODE_WRITE))
- return -EINVAL;
- if(!s->dma_dac.ready && prog_dmabuf_dac(s))
- return 0;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_update_ptr(s,CS_TRUE);
- val = s->dma_dac.count;
- spin_unlock_irqrestore(&s->lock, flags);
- return put_user(val, (int *) arg);
- case SNDCTL_DSP_GETIPTR:
- if (!(file->f_mode & FMODE_READ))
- return -EINVAL;
- if(!s->dma_adc.ready && prog_dmabuf_adc(s))
- return 0;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_update_ptr(s,CS_TRUE);
- cinfo.bytes = s->dma_adc.total_bytes;
- if (s->dma_adc.mapped) {
- cinfo.blocks =
- (cinfo.bytes >> s->dma_adc.fragshift) -
- s->dma_adc.blocks;
- s->dma_adc.blocks =
- cinfo.bytes >> s->dma_adc.fragshift;
- } else {
- if (s->conversion) {
- cinfo.blocks =
- s->dma_adc.count /
- 2 >> (s->dma_adc.fragshift - 1);
- } else
- cinfo.blocks =
- s->dma_adc.count >> s->dma_adc.
- fragshift;
- }
- if (s->conversion)
- cinfo.ptr = s->dma_adc.hwptr / 2;
- else
- cinfo.ptr = s->dma_adc.hwptr;
- if (s->dma_adc.mapped)
- s->dma_adc.count &= s->dma_adc.fragsize - 1;
- spin_unlock_irqrestore(&s->lock, flags);
- return copy_to_user((void *) arg, &cinfo, sizeof(cinfo));
- case SNDCTL_DSP_GETOPTR:
- if (!(file->f_mode & FMODE_WRITE))
- return -EINVAL;
- if(!s->dma_dac.ready && prog_dmabuf_dac(s))
- return 0;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_update_ptr(s,CS_TRUE);
- cinfo.bytes = s->dma_dac.total_bytes;
- if (s->dma_dac.mapped) {
- cinfo.blocks =
- (cinfo.bytes >> s->dma_dac.fragshift) -
- s->dma_dac.blocks;
- s->dma_dac.blocks =
- cinfo.bytes >> s->dma_dac.fragshift;
- } else {
- cinfo.blocks =
- s->dma_dac.count >> s->dma_dac.fragshift;
- }
- cinfo.ptr = s->dma_dac.hwptr;
- if (s->dma_dac.mapped)
- s->dma_dac.count &= s->dma_dac.fragsize - 1;
- spin_unlock_irqrestore(&s->lock, flags);
- return copy_to_user((void *) arg, &cinfo, sizeof(cinfo));
- case SNDCTL_DSP_GETBLKSIZE:
- if (file->f_mode & FMODE_WRITE) {
- if ((val = prog_dmabuf_dac(s)))
- return val;
- return put_user(s->dma_dac.fragsize, (int *) arg);
- }
- if ((val = prog_dmabuf_adc(s)))
- return val;
- if (s->conversion)
- return put_user(s->dma_adc.fragsize / 2,
- (int *) arg);
- else
- return put_user(s->dma_adc.fragsize, (int *) arg);
- case SNDCTL_DSP_SETFRAGMENT:
- if (get_user(val, (int *) arg))
- return -EFAULT;
- CS_DBGOUT(CS_PARMS, 4, printk(KERN_INFO
- "cs4281: cs4281_ioctl(): Attempt to set fragsize=%d fragnum=%dn",
- 1 << (val & 0xffff), (val >> 16) & 0xffff ));
- return 0; // Say OK, but do nothing.
- case SNDCTL_DSP_SUBDIVIDE:
- if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision)
- || (file->f_mode & FMODE_WRITE
- && s->dma_dac.subdivision)) return -EINVAL;
- if (get_user(val, (int *) arg))
- return -EFAULT;
- if (val != 1 && val != 2 && val != 4)
- return -EINVAL;
- if (file->f_mode & FMODE_READ)
- s->dma_adc.subdivision = val;
- else if (file->f_mode & FMODE_WRITE)
- s->dma_dac.subdivision = val;
- return 0;
- case SOUND_PCM_READ_RATE:
- if (file->f_mode & FMODE_READ)
- return put_user(s->prop_adc.rate, (int *) arg);
- else if (file->f_mode & FMODE_WRITE)
- return put_user(s->prop_dac.rate, (int *) arg);
- case SOUND_PCM_READ_CHANNELS:
- if (file->f_mode & FMODE_READ)
- return put_user(s->prop_adc.channels, (int *) arg);
- else if (file->f_mode & FMODE_WRITE)
- return put_user(s->prop_dac.channels, (int *) arg);
- case SOUND_PCM_READ_BITS:
- if (file->f_mode & FMODE_READ)
- return
- put_user(
- (s->prop_adc.
- fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
- (int *) arg);
- else if (file->f_mode & FMODE_WRITE)
- return
- put_user(
- (s->prop_dac.
- fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
- (int *) arg);
- case SOUND_PCM_WRITE_FILTER:
- case SNDCTL_DSP_SETSYNCRO:
- case SOUND_PCM_READ_FILTER:
- return -EINVAL;
- }
- return mixer_ioctl(s, cmd, arg);
- }
- static int cs4281_release(struct inode *inode, struct file *file)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk(KERN_INFO
- "cs4281: cs4281_release(): inode=0x%.8x file=0x%.8x f_mode=%dn",
- (unsigned) inode, (unsigned) file, file->f_mode));
- VALIDATE_STATE(s);
- if (file->f_mode & FMODE_WRITE) {
- drain_dac(s, file->f_flags & O_NONBLOCK);
- down(&s->open_sem_dac);
- stop_dac(s);
- dealloc_dmabuf(s, &s->dma_dac);
- s->open_mode &= ~FMODE_WRITE;
- up(&s->open_sem_dac);
- wake_up(&s->open_wait_dac);
- MOD_DEC_USE_COUNT;
- }
- if (file->f_mode & FMODE_READ) {
- drain_adc(s, file->f_flags & O_NONBLOCK);
- down(&s->open_sem_adc);
- stop_adc(s);
- dealloc_dmabuf(s, &s->dma_adc);
- s->open_mode &= ~FMODE_READ;
- up(&s->open_sem_adc);
- wake_up(&s->open_wait_adc);
- MOD_DEC_USE_COUNT;
- }
- return 0;
- }
- static int cs4281_open(struct inode *inode, struct file *file)
- {
- int minor = MINOR(inode->i_rdev);
- struct cs4281_state *s=NULL;
- struct list_head *entry;
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
- "cs4281: cs4281_open(): inode=0x%.8x file=0x%.8x f_mode=0x%xn",
- (unsigned) inode, (unsigned) file, file->f_mode));
- list_for_each(entry, &cs4281_devs)
- {
- s = list_entry(entry, struct cs4281_state, list);
- if (!((s->dev_audio ^ minor) & ~0xf))
- break;
- }
- if (entry == &cs4281_devs)
- return -ENODEV;
- if (!s) {
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
- "cs4281: cs4281_open(): Error - unable to find audio state structn"));
- return -ENODEV;
- }
- VALIDATE_STATE(s);
- file->private_data = s;
- // wait for device to become free
- if (!(file->f_mode & (FMODE_WRITE | FMODE_READ))) {
- CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2, printk(KERN_INFO
- "cs4281: cs4281_open(): Error - must open READ and/or WRITEn"));
- return -ENODEV;
- }
- if (file->f_mode & FMODE_WRITE) {
- down(&s->open_sem_dac);
- while (s->open_mode & FMODE_WRITE) {
- if (file->f_flags & O_NONBLOCK) {
- up(&s->open_sem_dac);
- return -EBUSY;
- }
- up(&s->open_sem_dac);
- interruptible_sleep_on(&s->open_wait_dac);
- if (signal_pending(current))
- return -ERESTARTSYS;
- down(&s->open_sem_dac);
- }
- }
- if (file->f_mode & FMODE_READ) {
- down(&s->open_sem_adc);
- while (s->open_mode & FMODE_READ) {
- if (file->f_flags & O_NONBLOCK) {
- up(&s->open_sem_adc);
- return -EBUSY;
- }
- up(&s->open_sem_adc);
- interruptible_sleep_on(&s->open_wait_adc);
- if (signal_pending(current))
- return -ERESTARTSYS;
- down(&s->open_sem_adc);
- }
- }
- s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
- if (file->f_mode & FMODE_READ) {
- s->prop_adc.fmt = AFMT_U8;
- s->prop_adc.fmt_original = s->prop_adc.fmt;
- s->prop_adc.channels = 1;
- s->prop_adc.rate = 8000;
- s->prop_adc.clkdiv = 96 | 0x80;
- s->conversion = 0;
- s->ena &= ~FMODE_READ;
- s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
- s->dma_adc.subdivision = 0;
- up(&s->open_sem_adc);
- MOD_INC_USE_COUNT;
- if (prog_dmabuf_adc(s)) {
- CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
- "cs4281: adc Program dmabufs failed.n"));
- cs4281_release(inode, file);
- return -ENOMEM;
- }
- prog_codec(s, CS_TYPE_ADC);
- }
- if (file->f_mode & FMODE_WRITE) {
- s->prop_dac.fmt = AFMT_U8;
- s->prop_dac.fmt_original = s->prop_dac.fmt;
- s->prop_dac.channels = 1;
- s->prop_dac.rate = 8000;
- s->prop_dac.clkdiv = 96 | 0x80;
- s->conversion = 0;
- s->ena &= ~FMODE_WRITE;
- s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
- s->dma_dac.subdivision = 0;
- up(&s->open_sem_dac);
- MOD_INC_USE_COUNT;
- if (prog_dmabuf_dac(s)) {
- CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
- "cs4281: dac Program dmabufs failed.n"));
- cs4281_release(inode, file);
- return -ENOMEM;
- }
- prog_codec(s, CS_TYPE_DAC);
- }
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2,
- printk(KERN_INFO "cs4281: cs4281_open()- 0n"));
- return 0;
- }
- // ******************************************************************************************
- // Wave (audio) file operations struct.
- // ******************************************************************************************
- static /*const */ struct file_operations cs4281_audio_fops = {
- llseek:cs4281_llseek,
- read:cs4281_read,
- write:cs4281_write,
- poll:cs4281_poll,
- ioctl:cs4281_ioctl,
- mmap:cs4281_mmap,
- open:cs4281_open,
- release:cs4281_release,
- };
- // ---------------------------------------------------------------------
- // hold spinlock for the following!
- static void cs4281_handle_midi(struct cs4281_state *s)
- {
- unsigned char ch;
- int wake;
- unsigned temp1;
- wake = 0;
- while (!(readl(s->pBA0 + BA0_MIDSR) & 0x80)) {
- ch = readl(s->pBA0 + BA0_MIDRP);
- if (s->midi.icnt < MIDIINBUF) {
- s->midi.ibuf[s->midi.iwr] = ch;
- s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
- s->midi.icnt++;
- }
- wake = 1;
- }
- if (wake)
- wake_up(&s->midi.iwait);
- wake = 0;
- while (!(readl(s->pBA0 + BA0_MIDSR) & 0x40) && s->midi.ocnt > 0) {
- temp1 = (s->midi.obuf[s->midi.ord]) & 0x000000ff;
- writel(temp1, s->pBA0 + BA0_MIDWP);
- s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
- s->midi.ocnt--;
- if (s->midi.ocnt < MIDIOUTBUF - 16)
- wake = 1;
- }
- if (wake)
- wake_up(&s->midi.owait);
- }
- static void cs4281_interrupt(int irq, void *dev_id, struct pt_regs *regs)
- {
- struct cs4281_state *s = (struct cs4281_state *) dev_id;
- unsigned int temp1;
- // fastpath out, to ease interrupt sharing
- temp1 = readl(s->pBA0 + BA0_HISR); // Get Int Status reg.
- CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
- "cs4281: cs4281_interrupt() BA0_HISR=0x%.8xn", temp1));
- /*
- * If not DMA or MIDI interrupt, then just return.
- */
- if (!(temp1 & (HISR_DMA0 | HISR_DMA1 | HISR_MIDI))) {
- writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR);
- CS_DBGOUT(CS_INTERRUPT, 9, printk(KERN_INFO
- "cs4281: cs4281_interrupt(): returning not cs4281 interrupt.n"));
- return;
- }
- if (temp1 & HISR_DMA0) // If play interrupt,
- readl(s->pBA0 + BA0_HDSR0); // clear the source.
- if (temp1 & HISR_DMA1) // Same for play.
- readl(s->pBA0 + BA0_HDSR1);
- writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Local EOI
- spin_lock(&s->lock);
- cs4281_update_ptr(s,CS_TRUE);
- cs4281_handle_midi(s);
- spin_unlock(&s->lock);
- }
- // **************************************************************************
- static void cs4281_midi_timer(unsigned long data)
- {
- struct cs4281_state *s = (struct cs4281_state *) data;
- unsigned long flags;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_handle_midi(s);
- spin_unlock_irqrestore(&s->lock, flags);
- s->midi.timer.expires = jiffies + 1;
- add_timer(&s->midi.timer);
- }
- // ---------------------------------------------------------------------
- static ssize_t cs4281_midi_read(struct file *file, char *buffer,
- size_t count, loff_t * ppos)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- ssize_t ret;
- unsigned long flags;
- unsigned ptr;
- int cnt;
- VALIDATE_STATE(s);
- if (ppos != &file->f_pos)
- return -ESPIPE;
- if (!access_ok(VERIFY_WRITE, buffer, count))
- return -EFAULT;
- ret = 0;
- while (count > 0) {
- spin_lock_irqsave(&s->lock, flags);
- ptr = s->midi.ird;
- cnt = MIDIINBUF - ptr;
- if (s->midi.icnt < cnt)
- cnt = s->midi.icnt;
- spin_unlock_irqrestore(&s->lock, flags);
- if (cnt > count)
- cnt = count;
- if (cnt <= 0) {
- if (file->f_flags & O_NONBLOCK)
- return ret ? ret : -EAGAIN;
- interruptible_sleep_on(&s->midi.iwait);
- if (signal_pending(current))
- return ret ? ret : -ERESTARTSYS;
- continue;
- }
- if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt))
- return ret ? ret : -EFAULT;
- ptr = (ptr + cnt) % MIDIINBUF;
- spin_lock_irqsave(&s->lock, flags);
- s->midi.ird = ptr;
- s->midi.icnt -= cnt;
- spin_unlock_irqrestore(&s->lock, flags);
- count -= cnt;
- buffer += cnt;
- ret += cnt;
- }
- return ret;
- }
- static ssize_t cs4281_midi_write(struct file *file, const char *buffer,
- size_t count, loff_t * ppos)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- ssize_t ret;
- unsigned long flags;
- unsigned ptr;
- int cnt;
- VALIDATE_STATE(s);
- if (ppos != &file->f_pos)
- return -ESPIPE;
- if (!access_ok(VERIFY_READ, buffer, count))
- return -EFAULT;
- ret = 0;
- while (count > 0) {
- spin_lock_irqsave(&s->lock, flags);
- ptr = s->midi.owr;
- cnt = MIDIOUTBUF - ptr;
- if (s->midi.ocnt + cnt > MIDIOUTBUF)
- cnt = MIDIOUTBUF - s->midi.ocnt;
- if (cnt <= 0)
- cs4281_handle_midi(s);
- spin_unlock_irqrestore(&s->lock, flags);
- if (cnt > count)
- cnt = count;
- if (cnt <= 0) {
- if (file->f_flags & O_NONBLOCK)
- return ret ? ret : -EAGAIN;
- interruptible_sleep_on(&s->midi.owait);
- if (signal_pending(current))
- return ret ? ret : -ERESTARTSYS;
- continue;
- }
- if (copy_from_user(s->midi.obuf + ptr, buffer, cnt))
- return ret ? ret : -EFAULT;
- ptr = (ptr + cnt) % MIDIOUTBUF;
- spin_lock_irqsave(&s->lock, flags);
- s->midi.owr = ptr;
- s->midi.ocnt += cnt;
- spin_unlock_irqrestore(&s->lock, flags);
- count -= cnt;
- buffer += cnt;
- ret += cnt;
- spin_lock_irqsave(&s->lock, flags);
- cs4281_handle_midi(s);
- spin_unlock_irqrestore(&s->lock, flags);
- }
- return ret;
- }
- static unsigned int cs4281_midi_poll(struct file *file,
- struct poll_table_struct *wait)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- unsigned long flags;
- unsigned int mask = 0;
- VALIDATE_STATE(s);
- if (file->f_flags & FMODE_WRITE)
- poll_wait(file, &s->midi.owait, wait);
- if (file->f_flags & FMODE_READ)
- poll_wait(file, &s->midi.iwait, wait);
- spin_lock_irqsave(&s->lock, flags);
- if (file->f_flags & FMODE_READ) {
- if (s->midi.icnt > 0)
- mask |= POLLIN | POLLRDNORM;
- }
- if (file->f_flags & FMODE_WRITE) {
- if (s->midi.ocnt < MIDIOUTBUF)
- mask |= POLLOUT | POLLWRNORM;
- }
- spin_unlock_irqrestore(&s->lock, flags);
- return mask;
- }
- static int cs4281_midi_open(struct inode *inode, struct file *file)
- {
- unsigned long flags, temp1;
- int minor = MINOR(inode->i_rdev);
- struct cs4281_state *s=NULL;
- struct list_head *entry;
- list_for_each(entry, &cs4281_devs)
- {
- s = list_entry(entry, struct cs4281_state, list);
- if (s->dev_midi == minor)
- break;
- }
- if (entry == &cs4281_devs)
- return -ENODEV;
- if (!s)
- {
- CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
- "cs4281: cs4281_open(): Error - unable to find audio state structn"));
- return -ENODEV;
- }
- VALIDATE_STATE(s);
- file->private_data = s;
- // wait for device to become free
- down(&s->open_sem);
- while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
- if (file->f_flags & O_NONBLOCK) {
- up(&s->open_sem);
- return -EBUSY;
- }
- up(&s->open_sem);
- interruptible_sleep_on(&s->open_wait);
- if (signal_pending(current))
- return -ERESTARTSYS;
- down(&s->open_sem);
- }
- spin_lock_irqsave(&s->lock, flags);
- if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
- s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
- s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
- writel(1, s->pBA0 + BA0_MIDCR); // Reset the interface.
- writel(0, s->pBA0 + BA0_MIDCR); // Return to normal mode.
- s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
- writel(0x0000000f, s->pBA0 + BA0_MIDCR); // Enable transmit, record, ints.
- temp1 = readl(s->pBA0 + BA0_HIMR);
- writel(temp1 & 0xffbfffff, s->pBA0 + BA0_HIMR); // Enable midi int. recognition.
- writel(HICR_IEV | HICR_CHGM, s->pBA0 + BA0_HICR); // Enable interrupts
- init_timer(&s->midi.timer);
- s->midi.timer.expires = jiffies + 1;
- s->midi.timer.data = (unsigned long) s;
- s->midi.timer.function = cs4281_midi_timer;
- add_timer(&s->midi.timer);
- }
- if (file->f_mode & FMODE_READ) {
- s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
- }
- if (file->f_mode & FMODE_WRITE) {
- s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
- }
- spin_unlock_irqrestore(&s->lock, flags);
- s->open_mode |=
- (file->
- f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ |
- FMODE_MIDI_WRITE);
- up(&s->open_sem);
- MOD_INC_USE_COUNT;
- return 0;
- }
- static int cs4281_midi_release(struct inode *inode, struct file *file)
- {
- struct cs4281_state *s =
- (struct cs4281_state *) file->private_data;
- DECLARE_WAITQUEUE(wait, current);
- unsigned long flags;
- unsigned count, tmo;
- VALIDATE_STATE(s);
- if (file->f_mode & FMODE_WRITE) {
- add_wait_queue(&s->midi.owait, &wait);
- for (;;) {
- set_current_state(TASK_INTERRUPTIBLE);
- spin_lock_irqsave(&s->lock, flags);
- count = s->midi.ocnt;
- spin_unlock_irqrestore(&s->lock, flags);
- if (count <= 0)
- break;
- if (signal_pending(current))
- break;
- if (file->f_flags & O_NONBLOCK) {
- remove_wait_queue(&s->midi.owait, &wait);
- current->state = TASK_RUNNING;
- return -EBUSY;
- }
- tmo = (count * HZ) / 3100;
- if (!schedule_timeout(tmo ? : 1) && tmo)
- printk(KERN_DEBUG
- "cs4281: midi timed out??n");
- }
- remove_wait_queue(&s->midi.owait, &wait);
- current->state = TASK_RUNNING;
- }
- down(&s->open_sem);
- s->open_mode &=
- (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ |
- FMODE_MIDI_WRITE);
- spin_lock_irqsave(&s->lock, flags);
- if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
- writel(0, s->pBA0 + BA0_MIDCR); // Disable Midi interrupts.
- del_timer(&s->midi.timer);
- }
- spin_unlock_irqrestore(&s->lock, flags);
- up(&s->open_sem);
- wake_up(&s->open_wait);
- MOD_DEC_USE_COUNT;
- return 0;
- }
- // ******************************************************************************************
- // Midi file operations struct.
- // ******************************************************************************************
- static /*const */ struct file_operations cs4281_midi_fops = {
- llseek:cs4281_llseek,
- read:cs4281_midi_read,
- write:cs4281_midi_write,
- poll:cs4281_midi_poll,
- open:cs4281_midi_open,
- release:cs4281_midi_release,
- };
- // ---------------------------------------------------------------------
- // maximum number of devices
- #define NR_DEVICE 8 // Only eight devices supported currently.
- // ---------------------------------------------------------------------
- static struct initvol {
- int mixch;
- int vol;
- } initvol[] __initdata = {
- {
- SOUND_MIXER_WRITE_VOLUME, 0x4040}, {
- SOUND_MIXER_WRITE_PCM, 0x4040}, {
- SOUND_MIXER_WRITE_SYNTH, 0x4040}, {
- SOUND_MIXER_WRITE_CD, 0x4040}, {
- SOUND_MIXER_WRITE_LINE, 0x4040}, {
- SOUND_MIXER_WRITE_LINE1, 0x4040}, {
- SOUND_MIXER_WRITE_RECLEV, 0x0000}, {
- SOUND_MIXER_WRITE_SPEAKER, 0x4040}, {
- SOUND_MIXER_WRITE_MIC, 0x0000}
- };
- #ifndef NOT_CS4281_PM
- void __devinit cs4281_BuildFIFO(
- struct cs4281_pipeline *p,
- struct cs4281_state *s)
- {
- switch(p->number)
- {
- case 0: /* playback */
- {
- p->u32FCRnAddress = BA0_FCR0;
- p->u32FSICnAddress = BA0_FSIC0;
- p->u32FPDRnAddress = BA0_FPDR0;
- break;
- }
- case 1: /* capture */
- {
- p->u32FCRnAddress = BA0_FCR1;
- p->u32FSICnAddress = BA0_FSIC1;
- p->u32FPDRnAddress = BA0_FPDR1;
- break;
- }
- case 2:
- {
- p->u32FCRnAddress = BA0_FCR2;
- p->u32FSICnAddress = BA0_FSIC2;
- p->u32FPDRnAddress = BA0_FPDR2;
- break;
- }
- case 3:
- {
- p->u32FCRnAddress = BA0_FCR3;
- p->u32FSICnAddress = BA0_FSIC3;
- p->u32FPDRnAddress = BA0_FPDR3;
- break;
- }
- default:
- break;
- }
- //
- // first read the hardware to initialize the member variables
- //
- p->u32FCRnValue = readl(s->pBA0 + p->u32FCRnAddress);
- p->u32FSICnValue = readl(s->pBA0 + p->u32FSICnAddress);
- p->u32FPDRnValue = readl(s->pBA0 + p->u32FPDRnAddress);
- }
- void __devinit cs4281_BuildDMAengine(
- struct cs4281_pipeline *p,
- struct cs4281_state *s)
- {
- /*
- * initialize all the addresses of this pipeline dma info.
- */
- switch(p->number)
- {
- case 0: /* playback */
- {
- p->u32DBAnAddress = BA0_DBA0;
- p->u32DCAnAddress = BA0_DCA0;
- p->u32DBCnAddress = BA0_DBC0;
- p->u32DCCnAddress = BA0_DCC0;
- p->u32DMRnAddress = BA0_DMR0;
- p->u32DCRnAddress = BA0_DCR0;
- p->u32HDSRnAddress = BA0_HDSR0;
- break;
- }
- case 1: /* capture */
- {
- p->u32DBAnAddress = BA0_DBA1;
- p->u32DCAnAddress = BA0_DCA1;
- p->u32DBCnAddress = BA0_DBC1;
- p->u32DCCnAddress = BA0_DCC1;
- p->u32DMRnAddress = BA0_DMR1;
- p->u32DCRnAddress = BA0_DCR1;
- p->u32HDSRnAddress = BA0_HDSR1;
- break;
- }
- case 2:
- {
- p->u32DBAnAddress = BA0_DBA2;
- p->u32DCAnAddress = BA0_DCA2;
- p->u32DBCnAddress = BA0_DBC2;
- p->u32DCCnAddress = BA0_DCC2;
- p->u32DMRnAddress = BA0_DMR2;
- p->u32DCRnAddress = BA0_DCR2;
- p->u32HDSRnAddress = BA0_HDSR2;
- break;
- }
- case 3:
- {
- p->u32DBAnAddress = BA0_DBA3;
- p->u32DCAnAddress = BA0_DCA3;
- p->u32DBCnAddress = BA0_DBC3;
- p->u32DCCnAddress = BA0_DCC3;
- p->u32DMRnAddress = BA0_DMR3;
- p->u32DCRnAddress = BA0_DCR3;
- p->u32HDSRnAddress = BA0_HDSR3;
- break;
- }
- default:
- break;
- }
- //
- // Initialize the dma values for this pipeline
- //
- p->u32DBAnValue = readl(s->pBA0 + p->u32DBAnAddress);
- p->u32DBCnValue = readl(s->pBA0 + p->u32DBCnAddress);
- p->u32DMRnValue = readl(s->pBA0 + p->u32DMRnAddress);
- p->u32DCRnValue = readl(s->pBA0 + p->u32DCRnAddress);
- }
- void __devinit cs4281_InitPM(struct cs4281_state *s)
- {
- int i;
- struct cs4281_pipeline *p;
- for(i=0;i<CS4281_NUMBER_OF_PIPELINES;i++)
- {
- p = &s->pl[i];
- p->number = i;
- cs4281_BuildDMAengine(p,s);
- cs4281_BuildFIFO(p,s);
- /*
- * currently only 2 pipelines are used
- * so, only set the valid bit on the playback and capture.
- */
- if( (i == CS4281_PLAYBACK_PIPELINE_NUMBER) ||
- (i == CS4281_CAPTURE_PIPELINE_NUMBER))
- p->flags |= CS4281_PIPELINE_VALID;
- }
- s->pm.u32SSPM_BITS = 0x7e; /* rev c, use 0x7c for rev a or b */
- }
- #endif
- /*
- * ss_vendor and ss_id must be setup prior to calling this routine.
- * setup the invalid recording source bitmask,
- * and also return a valid default initialization value as
- * the return value;
- */
- static int cs4281_setup_record_src(struct cs4281_state *s)
- {
- if(s->ss_vendor == PCI_VENDOR_ID_TOSHIBA)
- {
- if(s->ss_id == SS_ID_TOSHIBA_1640CDT)
- {
- CS_DBGOUT(CS_PARMS, 2, printk(KERN_INFO
- "cs4281: cs4281_setup_record_src(): setting LINE invalidn"));
- recsrc_invalid |= SOUND_MASK_LINE;
- }
- }
- /*
- * only return a valid recsrc value here, default to something useful.
- */
- if(!(recsrc_invalid & SOUND_MASK_MIC))
- return(SOUND_MASK_MIC);
- else if(!(recsrc_invalid & SOUND_MASK_LINE))
- return(SOUND_MASK_LINE);
- else if(!(recsrc_invalid & SOUND_MASK_LINE1))
- return(SOUND_MASK_LINE1);
- return 0;
- }
- static int __devinit cs4281_probe(struct pci_dev *pcidev,
- const struct pci_device_id *pciid)
- {
- #ifndef NOT_CS4281_PM
- struct pm_dev *pmdev;
- #endif
- struct cs4281_state *s;
- dma_addr_t dma_mask;
- mm_segment_t fs;
- int i, val;
- unsigned int temp1, temp2;
- CS_DBGOUT(CS_FUNCTION | CS_INIT, 2,
- printk(KERN_INFO "cs4281: probe()+n"));
- if (pci_enable_device(pcidev)) {
- CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
- "cs4281: pci_enable_device() failedn"));
- return -1;
- }
- if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM) ||
- !(pci_resource_flags(pcidev, 1) & IORESOURCE_MEM)) {
- CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe()- Memory region not assignedn"));
- return -ENODEV;
- }
- if (pcidev->irq == 0) {
- CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() IRQ not assignedn"));
- return -ENODEV;
- }
- dma_mask = 0xffffffff; /* this enables playback and recording */
- i = pci_set_dma_mask(pcidev, dma_mask);
- if (i) {
- CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() architecture does not support 32bit PCI busmaster DMAn"));
- return i;
- }
- if (!(s = kmalloc(sizeof(struct cs4281_state), GFP_KERNEL))) {
- CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() no memory for state struct.n"));
- return -1;
- }
- memset(s, 0, sizeof(struct cs4281_state));
- init_waitqueue_head(&s->dma_adc.wait);
- init_waitqueue_head(&s->dma_dac.wait);
- init_waitqueue_head(&s->open_wait);
- init_waitqueue_head(&s->open_wait_adc);
- init_waitqueue_head(&s->open_wait_dac);
- init_waitqueue_head(&s->midi.iwait);
- init_waitqueue_head(&s->midi.owait);
- init_MUTEX(&s->open_sem);
- init_MUTEX(&s->open_sem_adc);
- init_MUTEX(&s->open_sem_dac);
- spin_lock_init(&s->lock);
- s->pBA0phys = pci_resource_start(pcidev, 0);
- s->pBA1phys = pci_resource_start(pcidev, 1);
- /* Convert phys to linear. */
- s->pBA0 = ioremap_nocache(s->pBA0phys, 4096);
- if (!s->pBA0) {
- CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
- "cs4281: BA0 I/O mapping failed. Skipping part.n"));
- goto err_free;
- }
- s->pBA1 = ioremap_nocache(s->pBA1phys, 65536);
- if (!s->pBA1) {
- CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
- "cs4281: BA1 I/O mapping failed. Skipping part.n"));
- goto err_unmap;
- }
- temp1 = readl(s->pBA0 + BA0_PCICFG00);
- temp2 = readl(s->pBA0 + BA0_PCICFG04);
- CS_DBGOUT(CS_INIT, 2,
- printk(KERN_INFO
- "cs4281: probe() BA0=0x%.8x BA1=0x%.8x pBA0=0x%.8x pBA1=0x%.8x n",
- (unsigned) temp1, (unsigned) temp2,
- (unsigned) s->pBA0, (unsigned) s->pBA1));
- CS_DBGOUT(CS_INIT, 2,
- printk(KERN_INFO
- "cs4281: probe() pBA0phys=0x%.8x pBA1phys=0x%.8xn",
- (unsigned) s->pBA0phys, (unsigned) s->pBA1phys));
- #ifndef NOT_CS4281_PM
- s->pm.flags = CS4281_PM_IDLE;
- #endif
- temp1 = cs4281_hw_init(s);
- if (temp1) {
- CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(KERN_ERR
- "cs4281: cs4281_hw_init() failed. Skipping part.n"));
- goto err_irq;
- }
- s->magic = CS4281_MAGIC;
- s->pcidev = pcidev;
- s->irq = pcidev->irq;
- if (request_irq
- (s->irq, cs4281_interrupt, SA_SHIRQ, "Crystal CS4281", s)) {
- CS_DBGOUT(CS_INIT | CS_ERROR, 1,
- printk(KERN_ERR "cs4281: irq %u in usen", s->irq));
- goto err_irq;
- }
- if ((s->dev_audio = register_sound_dsp(&cs4281_audio_fops, -1)) <
- 0) {
- CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() register_sound_dsp() failed.n"));
- goto err_dev1;
- }
- if ((s->dev_mixer = register_sound_mixer(&cs4281_mixer_fops, -1)) <
- 0) {
- CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() register_sound_mixer() failed.n"));
- goto err_dev2;
- }
- if ((s->dev_midi = register_sound_midi(&cs4281_midi_fops, -1)) < 0) {
- CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
- "cs4281: probe() register_sound_midi() failed.n"));
- goto err_dev3;
- }
- #ifndef NOT_CS4281_PM
- cs4281_InitPM(s);
- pmdev = cs_pm_register(PM_PCI_DEV, PM_PCI_ID(pcidev), cs4281_pm_callback);
- if (pmdev)
- {
- CS_DBGOUT(CS_INIT | CS_PM, 4, printk(KERN_INFO
- "cs4281: probe() pm_register() succeeded (0x%x).n",
- (unsigned)pmdev));
- pmdev->data = s;
- }
- else
- {
- CS_DBGOUT(CS_INIT | CS_PM | CS_ERROR, 0, printk(KERN_INFO
- "cs4281: probe() pm_register() failed (0x%x).n",
- (unsigned)pmdev));
- s->pm.flags |= CS4281_PM_NOT_REGISTERED;
- }
- #endif
- pci_set_master(pcidev); // enable bus mastering
- pci_read_config_word(pcidev, PCI_SUBSYSTEM_VENDOR_ID, &s->ss_vendor);
- pci_read_config_word(pcidev, PCI_SUBSYSTEM_ID, &s->ss_id);
- printk(KERN_INFO "cs4281: Subsystem vendor/id (%04X:%04X) IRQ %dn",
- s->ss_vendor, s->ss_id, s->irq);
-
- if(!recsrc_invalid)
- val = cs4281_setup_record_src(s);
- else
- val = SOUND_MASK_MIC;
- fs = get_fs();
- set_fs(KERNEL_DS);
- mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long) &val);
- for (i = 0; i < sizeof(initvol) / sizeof(initvol[0]); i++) {
- val = initvol[i].vol;
- mixer_ioctl(s, initvol[i].mixch, (unsigned long) &val);
- }
- val = 1; // enable mic preamp
- mixer_ioctl(s, SOUND_MIXER_PRIVATE1, (unsigned long) &val);
- set_fs(fs);
- pci_set_drvdata(pcidev, s);
- list_add(&s->list, &cs4281_devs);
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: probe()- device allocated successfullyn"));
- return 0;
- err_dev3:
- unregister_sound_mixer(s->dev_mixer);
- err_dev2:
- unregister_sound_dsp(s->dev_audio);
- err_dev1:
- free_irq(s->irq, s);
- err_irq:
- iounmap(s->pBA1);
- err_unmap:
- iounmap(s->pBA0);
- err_free:
- kfree(s);
- CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_INFO
- "cs4281: probe()- no device allocatedn"));
- return -ENODEV;
- } // probe_cs4281
- // ---------------------------------------------------------------------
- static void __devinit cs4281_remove(struct pci_dev *pci_dev)
- {
- struct cs4281_state *s = pci_get_drvdata(pci_dev);
- // stop DMA controller
- synchronize_irq();
- free_irq(s->irq, s);
- unregister_sound_dsp(s->dev_audio);
- unregister_sound_mixer(s->dev_mixer);
- unregister_sound_midi(s->dev_midi);
- iounmap(s->pBA1);
- iounmap(s->pBA0);
- pci_set_drvdata(pci_dev, s);
- list_del(&s->list);
- kfree(s);
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: cs4281_remove()-: remove successfuln"));
- }
- static struct pci_device_id cs4281_pci_tbl[] __devinitdata = {
- {PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CRYSTAL_CS4281,
- PCI_ANY_ID, PCI_ANY_ID, 0, 0},
- {0,}
- };
- MODULE_DEVICE_TABLE(pci, cs4281_pci_tbl);
- struct pci_driver cs4281_pci_driver = {
- name:"cs4281",
- id_table:cs4281_pci_tbl,
- probe:cs4281_probe,
- remove:cs4281_remove,
- suspend:CS4281_SUSPEND_TBL,
- resume:CS4281_RESUME_TBL,
- };
- int __init cs4281_init_module(void)
- {
- int rtn = 0;
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: cs4281_init_module()+ n"));
- if (!pci_present()) { /* No PCI bus in this machine! */
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
- "cs4281: cs4281_init_module()- no pci bus foundn"));
- return -ENODEV;
- }
- printk(KERN_INFO "cs4281: version v%d.%02d.%d time " __TIME__ " "
- __DATE__ "n", CS4281_MAJOR_VERSION, CS4281_MINOR_VERSION,
- CS4281_ARCH);
- rtn = pci_module_init(&cs4281_pci_driver);
- if(rtn == -ENODEV)
- {
- CS_DBGOUT(CS_ERROR | CS_INIT, 1, printk(
- "cs4281: Unable to locate any cs4281 device with valid IDs 0x%x-0x%xn",
- PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CRYSTAL_CS4281));
- }
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: cs4281_init_module()- (%d)n",rtn));
- return rtn;
- }
- void __exit cs4281_cleanup_module(void)
- {
- pci_unregister_driver(&cs4281_pci_driver);
- #ifndef NOT_CS4281_PM
- cs_pm_unregister_all(cs4281_pm_callback);
- #endif
- CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
- printk(KERN_INFO "cs4281: cleanup_cs4281() finishedn"));
- }
- // ---------------------------------------------------------------------
- MODULE_AUTHOR("gw boynton, pcaudio@crystal.cirrus.com");
- MODULE_DESCRIPTION("Cirrus Logic CS4281 Driver");
- MODULE_LICENSE("GPL");
- // ---------------------------------------------------------------------
- module_init(cs4281_init_module);
- module_exit(cs4281_cleanup_module);
- #ifndef MODULE
- int __init init_cs4281(void)
- {
- return cs4281_init_module();
- }
- #endif
- #include "cs4281pm-24.c"