-#ifdef DO_SOFT_AC3
-/*
- * special tricks for soft ac3 transfer:
- *
- * we compose an iec958 subframe from 16bit ac3 sample and
- * write the raw subframe via 32bit data mode.
- */
-
-# ifndef USE_AES_IEC958
-
-/* find parity for bit 4~30 */
-static unsigned int parity(unsigned int data)
-{
- unsigned int parity = 0;
- int counter = 4;
-
- data >>= 4; /* start from bit 4 */
- while (counter <= 30) {
- if (data & 1)
- parity++;
- data >>= 1;
- counter++;
- }
- return parity & 1;
-}
-
-/*
- * compose 32bit iec958 subframe with non-audio data.
- * bit 0-3 = preamble
- * 4-7 = aux (=0)
- * 8-27 = data (12-27 for 16bit)
- * 28 = validity (=0)
- * 29 = user data (=0)
- * 30 = channel status
- * 31 = parity
- *
- * channel status is assumed as consumer, non-audio
- * thus all 0 except bit 1
- */
-inline static u32 convert_ac3_32bit(cmipci_t *cm, u32 val)
-{
- u32 data = (u32)val << 12;
-
- if (cm->spdif_counter == 2 || cm->spdif_counter == 3) /* bit 1 */
- data |= 0x40000000; /* indicate AC-3 raw data */
- if (parity(data)) /* parity bit 4-30 */
- data |= 0x80000000;
- if (cm->spdif_counter == 0)
- data |= 3; /* preamble 'M' */
- else if (cm->spdif_counter & 1)
- data |= 5; /* odd, 'W' */
- else
- data |= 9; /* even, 'M' */
-
- cm->spdif_counter++;
- if (cm->spdif_counter == 384)
- cm->spdif_counter = 0;
-
- return data;
-}
-
-# else /* if USE_AES_IEC958 */
-
-/*
- * The bitstream handling
- */
-typedef struct iec958_stru_bitstream {
- u32 *data; /* Holds the current position */
- u32 left; /* Bits left in current 32bit frame */
- u32 word; /* The 32bit frame of the current position */
- u32 bits; /* All bits together */
- int err; /* Error condition */
-} iec958_bitstream_t ;
-
-static iec958_bitstream_t bs;
-
-/* Initialize ptr on the buffer */
-static void iec958_init_bitstream(u8 *buf, u32 size)
-{
- bs.data = (u32 *)buf; /* Set initial position */
- bs.word = *bs.data; /* The first 32bit frame */
- bs.left = 32; /* has exactly 32bits */
- bs.bits = size;
- bs.err = 0;
-}
-
-/* Remove ptr on the buffer */
-static void iec958_clear_bitstream(void)
-{
- bs.data = NULL;
- bs.left = 0;
- bs.err = 0;
-}
-
-/* Get bits from bitstream (max 32) */
-static inline u32 iec958_getbits(u32 bits)
-{
- u32 res;
-
- if (bs.bits < bits) {
- bits = bs.bits;
- bs.err = 1;
- }
- if (bits > 32) {
- bits = 32;
- bs.err = 1;
- }
- bs.bits -= bits;
-
-# ifdef WORDS_BIGENDIAN
- if (bits < bs.left) { /* Within 32bit frame */
- res = (bs.word << (32 - bs.left)) >> (32 - bits);
- bs.left -= bits;
- goto out;
- } /* We may cross the frame boundary */
- res = (bs.word << (32 - bs.left)) >> (32 - bs.left);
- bits -= bs.left;
-
- bs.word = *(++bs.data); /* Next 32bit frame */
-
- if (bits) /* Add remaining bits, if any */
- res = (res << bits) | (bs.word >> (32 - bits));
-
-# else /* not WORDS_BIGENDIAN */
-
- if (bits < bs.left) { /* Within 32bit frame */
- res = (bs.word << (32 - bits)) >> (32 - bits);
- bs.word >>= bits;
- bs.left -= bits;
- goto out;
- } /* We may cross the frame boundary */
- res = bs.word;
- bits -= bs.left;
-
- bs.word = *(++bs.data); /* Next 32bit frame */
-
- if (bits) { /* Add remaining bits, if any */
- res = res | (((bs.word << (32 - bits)) >> (32 - bits)) << bits);
- bs.word >>= bits;
- }
-# endif /* not WORDS_BIGENDIAN */
-
- bs.left = (32 - bits);
-out:
- return res;
-}
-
-static inline u32 iec958_bits_avail(void)
-{
- return bs.bits;
-}
-
-static inline int iec958_error(void)
-{
- return bs.err;
-}
-
-/*
- * Determine parity for time slots 4 upto 30
- * to be sure that bit 4 upt 31 will carry
- * an even number of ones and zeros.
- */
-static u32 iec958_parity(u32 data)
-{
- u32 parity = 0;
- int counter = 4;
-
- data >>= 4; /* start from bit 4 */
- while (counter++ <= 30) {
- if (data & 0x00000001)
- parity++;
- data >>= 1;
- }
- return (parity & 0x00000001);
-}
-
-/*
- * Compose 32bit iec958 subframe, two sub frames
- * build one frame with two channels.
- *
- * bit 0-3 = preamble
- * 4-7 = AUX (=0)
- * 8-27 = data (12-27 for 16bit, 8-27 for 20bit, and 24bit without AUX)
- * 28 = validity (0 for valid data, else 'in error')
- * 29 = user data (0)
- * 30 = channel status (24 bytes for 192 frames)
- * 31 = parity
- */
-
-static inline u32 iec958_subframe(cmipci_t *cm, snd_ctl_elem_value_t * ucontrol)
-{
- u32 data;
- u32 byte = cm->spdif_counter >> 4;
- u32 mask = 1 << ((cm->spdif_counter >> 1) - (byte << 3));
- u8 * status = ucontrol->value.iec958.status;
-
- if (status[2] & IEC958_AES2_PRO_SBITS_24) {
- /* Does this work for LE systems ??? */
- if (status[2] & IEC958_AES2_PRO_WORDLEN_24_20) {
- data = iec958_getbits(24);
- data <<= 4;
- } else {
- data = iec958_getbits(20);
- data <<= 8;
- }
- } else {
- if (status[2] & IEC958_AES2_PRO_WORDLEN_24_20) {
- /* Does this work for LE systems ??? */
- data = iec958_getbits(20);
- data <<= 8;
- } else {
- data = iec958_getbits(16);
- data <<= 12;
- }
- }
-
- /*
- * Set one of the 192 bits of the channel status (AES3 and higher)
- */
- if (status[byte] & mask)
- data |= 0x40000000;
-
- if (iec958_parity(data)) /* parity bit 4-30 */
- data |= 0x80000000;
-
- /* Preamble */
- if (!cm->spdif_counter)
- data |= 0x03; /* Block start, 'Z' */
- else if (cm->spdif_counter % 2)
- data |= 0x05; /* odd sub frame, 'Y' */
- else
- data |= 0x09; /* even sub frame, 'X' */
-
- /*
- * sub frame counter: 2 sub frame are one audio frame
- * and 192 frames are one block
- */
- cm->spdif_counter = (++cm->spdif_counter) % 384;
-
- return data;
-}
-# endif /* if USE_AES_IEC958 */
-
-static int snd_cmipci_ac3_copy(snd_pcm_substream_t *subs, int channel,
- snd_pcm_uframes_t pos, void *src,
- snd_pcm_uframes_t count)
-{
- cmipci_t *cm = snd_pcm_substream_chip(subs);
- u32 *dst;
- snd_pcm_uframes_t offset;
- snd_pcm_runtime_t *runtime = subs->runtime;
-#ifndef USE_AES_IEC958
- u16 *srcp = src, val;
-#else
- char buf[480]; /* bits can be divided by 20, 24, 16 */
- size_t bytes = frames_to_bytes(runtime, count);
-#endif
-
-
- if (!cm->channel[CM_CH_PLAY].ac3_shift) {
- if (copy_from_user(runtime->dma_area +
- frames_to_bytes(runtime, pos), src,
- frames_to_bytes(runtime, count)))
- return -EFAULT;
- return 0;
- }
-
- if (! access_ok(VERIFY_READ, src, count))
- return -EFAULT;
-
- /* frame = 16bit stereo */
- offset = (pos << 1) % (cm->channel[CM_CH_PLAY].dma_size << 2);
- dst = (u32*)(runtime->dma_area + offset);
-# ifndef USE_AES_IEC958
- count /= 2;
- while (count-- > 0) {
- get_user(val, srcp);
- srcp++;
- *dst++ = convert_ac3_32bit(cm, val);
- }
-# else
- while (bytes) {
- size_t c = bytes;
-
- if (c > sizeof(buf))
- c = sizeof(buf);
-
- if (copy_from_user(buf, src, c))
- return -EFAULT;
- bytes -= c;
- src += c;
-
- iec958_init_bitstream(buf, c*8);
- while (iec958_bits_avail()) {
- *(dst++) = iec958_subframe(cm, cm->spdif_channel);
- if (iec958_error())
- return -EINVAL;
- }
- iec958_clear_bitstream();
- }
-# endif
- return 0;
-}
-
-static int snd_cmipci_ac3_silence(snd_pcm_substream_t *subs, int channel,
- snd_pcm_uframes_t pos,
- snd_pcm_uframes_t count)
-{
- cmipci_t *cm = snd_pcm_substream_chip(subs);
- u32 *dst;
- snd_pcm_uframes_t offset;
- snd_pcm_runtime_t *runtime = subs->runtime;
-# ifdef USE_AES_IEC958
- char buf[480]; /* bits can be divided by 20, 24, 16 */
- size_t bytes = frames_to_bytes(runtime, count);
-# endif
- if (! cm->channel[CM_CH_PLAY].ac3_shift)
- return snd_pcm_format_set_silence(runtime->format,
- runtime->dma_area + frames_to_bytes(runtime, pos), count);
-
- /* frame = 16bit stereo */
- offset = (pos << 1) % (cm->channel[CM_CH_PLAY].dma_size << 2);
- dst = (u32*)(subs->runtime->dma_area + offset);
-# ifndef USE_AES_IEC958
- count /= 2;
- while (count-- > 0) {
- *dst++ = convert_ac3_32bit(cm, 0);
- }
-# else
- while (bytes) {
- size_t c = bytes;
-
- if (c > sizeof(buf))
- c = sizeof(buf);
-
- /* Q: Does this function know about 24bit silence? */
- if (snd_pcm_format_set_silence(runtime->format, buf, bytes_to_frames(runtime, c)))
- return -EINVAL;
-
- iec958_init_bitstream(buf, c*8);
- while (iec958_bits_avail()) {
- *(dst++) = iec958_subframe(cm, cm->spdif_channel);
- if (iec958_error())
- return -EINVAL;
- }
- iec958_clear_bitstream();
- }
-# endif
- return 0;
-}
-#endif /* DO_SOFT_AC3 */
-