File: coding-5.html

package info (click to toggle)
alsadriver 0.2.0-pre8-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,808 kB
  • ctags: 6,550
  • sloc: ansic: 43,490; sh: 916; makefile: 759; perl: 54
file content (379 lines) | stat: -rw-r--r-- 12,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<HTML>
<HEAD>
<TITLE>Advanced Linux Sound Architecture - Driver: PCM - Digital Audio</TITLE>
</HEAD>
<BODY>
<A HREF="coding-4.html">Previous</A>
Next
<A HREF="coding.html#toc5">Table of Contents</A>
<HR>
<H2><A NAME="s5">5. PCM - Digital Audio</A></H2>

<P>Digital Audio related structures and function is in <I>include/pcm.h</I>
header file.</P>

<H2><A NAME="ss5.1">5.1 Variables and functions</A></H2>

<P>Variables from <I>snd_pcm_t</I> structure which must be filled:</P>
<P>
<UL>
<LI><B>info_flags</B> look to <B>SND_PCM_INFO_*</B> </LI>
<LI><B>name</B> of pcm device (for example 'CS4231')</LI>
<LI><B>playback</B> variables for playback direction</LI>
<LI><B>record</B> variables for record direction</LI>
</UL>
</P>

<P>Variables from <I>snd_pcm_t</I> structure which should be filled:</P>
<P>
<UL>
<LI><B>private_data</B> should contains pointer to private data</LI>
<LI><B>private_free</B> should free private data</LI>
</UL>
</P>

<P>Variables from <I>struct snd_stru_pcm_hardware</I> which must be filled:</P>
<P>
<UL>
<LI><B>flags</B>
<UL>
<LI><B>SND_PCM_HW_BATCH</B> - hardware does double buffering</LI>
<LI><B>SND_PCM_HW_8BITONLY</B> - hardware supports only 8-bit data (be careful - look to sb16.c)</LI>
<LI><B>SND_PCM_HW_16BITONLY</B> - hardware supports only 16-bit data (be careful - look to sb16.c)</LI>
<LI><B>SND_PCM_HW_AUTODMA</B> - hardware supports auto init dma transfer (unterminated loop)</LI>
</UL>
</LI>
<LI><B>formats</B> list of supported formats <B>SND_PCM_FMT_*</B>
<UL>
<LI>if hardware doesn't support Mu-Law - it _must_ be emulated (converted), but it can't be listed here</LI>
</UL>
</LI>
<LI><B>align</B> - if transfer block must be aligned to some value - set this mask</LI>
<LI><B>min_fragment</B> - minimal fragment in 2^x</LI>
<LI><B>min_rate</B> - minimal rate in Hz</LI>
<LI><B>max_rate</B> - maximal rate in Hz</LI>
<LI><B>max_voices</B> - maximal number of voices</LI>
<LI><B>int (*open)( snd_pcm_t *pcm )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI>opens pcm direction and allocates dma buffer with <I>snd_pcm_dma_alloc</I></LI>
</UL>
</LI>
<LI><B>int (*close)( snd_pcm_t *pcm )</B>
<UL>
<LI>closes pcm direction and frees dma buffer with <I>snd_pcm_dma_free</I></LI>
</UL>
</LI>
<LI><B>void (*compute_rate)( snd_pcm_t *pcm )</B>
<UL>
<LI>computes <I>snd_pcm_channel_t -> real_rate</I> from <I>snd_pcm_channel_t -> rate</I></LI>
</UL>
</LI>
<LI><B>void (*prepare)( snd_pcm_t *pcm, unsigned char *buffer, unsigned int size, unsigned int offset, unsigned int count )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>buffer</B> pointer to dma buffer</LI>
<LI><B>size</B> used size of dma buffer</LI>
<LI><B>offset</B> offset in bytes to dma buffer
<UL>
<LI>this value is always zero if hardware supports auto init dma transfer mode</LI>
</UL>
</LI>
<LI><B>count</B> of bytes to transfer (block size)</LI>
<LI>prepares pcm direction to output/input</LI>
</UL>
</LI>
<LI><B>void (*trigger)( snd_pcm_t *pcm, int up )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>up</B> if zero - trigger down (turn off), if nonzero - trigger up (turn on)</LI>
<LI>starts or stops pcm direction</LI>
</UL>
</LI>
<LI><B>unsigned int (*pointer)( snd_pcm_t *pcm, unsigned int used_size )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>used_size</B> - used size of dma buffer</LI>
<LI>returns actual byte pointer from pcm direction</LI>
</UL>
</LI>
<LI><B>void (*dma)( snd_pcm_t *pcm, unsigned char *buffer, unsigned int offset, unsigned char *user, unsigned int count )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>buffer</B> pointer to dma buffer</LI>
<LI><B>offset</B> destonation/source offset in bytes to/from dma buffer</LI>
<LI><B>user</B> pointer to buffer in user space</LI>
<LI><B>count</B> transfer count in bytes</LI>
<LI>copies data from or to user space to or from kernel dma buffer</LI>
</UL>
</LI>
<LI><B>void (*dma_move)( snd_pcm_t *pcm, unsigned char *buffer, unsigned int dest_offset, unsigned int src_offset, unsigned int count )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>buffer</B> pointer to dma buffer</LI>
<LI><B>dest_offset</B> destonation offset in bytes to dma buffer</LI>
<LI><B>src_offset</B> source offset in bytes from dma buffer</LI>
<LI><B>count</B> transfer count</LI>
<LI>transfer areas can never be overlapped</LI>
</UL>
</LI>
<LI><B>void (*dma_neutral)( snd_pcm_t *pcm, unsigned char *buffer, unsigned offset, unsigned int count, unsigned char neutral_byte )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>buffer</B> pointer to dma buffer</LI>
<LI><B>offset</B> destonation offset in bytes to dma buffer</LI>
<LI><B>count</B> count in bytes</LI>
<LI><B>neutral_byte</B> - neutral byte</LI>
<LI>fills specified area of dma buffer with neutral byte</LI>
</UL>
</LI>
</UL>
</P>

<P>Variables from <I>struct snd_stru_pcm_hardware</I> which should be filled:</P>
<P>
<UL>
<LI><B>private_data</B> should contains pointer to private data for specified direction</LI>
<LI><B>private_free</B> should free private data</LI>
</UL>
</P>

<P>Functions list:</P>
<P>
<UL>
<LI><B>void snd_pcm_playback_dma( snd_pcm_t *pcm,
unsigned char *buffer, unsigned int offset,
unsigned char *user, unsigned int count )</B>
<UL>
<LI>standard function for playback snd_pcm_channel_t -> hw.dma</LI>
</UL>
</LI>
<LI><B>void snd_pcm_playback_dma_ulaw( snd_pcm_t *pcm,
unsigned char *buffer, unsigned int offset,
unsigned char *user, unsigned int count )</B>
<UL>
<LI>standard function for playback snd_pcm_channel_t -> hw.dma
if hardware doesn't supports Mu-Law compression</LI>
</UL>
</LI>
<LI><B>void snd_pcm_playback_dma_neutral( snd_pcm_t *pcm,
unsigned char *buffer, unsigned int of
unsigned int count,
unsigned char neutral_byte )</B>
<UL>
<LI>standard function for playback snd_pcm_channel_t -> hw.dma_neutral</LI>
</UL>
</LI>
<LI><B>void snd_pcm_record_dma( snd_pcm_t *pcm,
unsigned char *buffer, unsigned int offset,
unsigned char *user, unsigned int count )</B>
<UL>
<LI>standard function for record snd_pcm_channel_t -> hw.dma</LI>
</UL>
</LI>
<LI><B>void snd_pcm_record_dma_ulaw( snd_pcm_t *pcm,
unsigned char *buffer, unsigned int offset,
unsigned char *user, unsigned int count )</B>
<UL>
<LI>standard function for record snd_pcm_channel_t -> hw.dma
if hardware doesn't supports Mu-Law compression</LI>
</UL>
</LI>
<LI><B>void snd_pcm_dma_move( snd_pcm_t *pcm,
unsigned char *buffer,
unsigned int dest_offset, unsigned int src_offset,
unsigned int count )</B>
<UL>
<LI>standard function for snd_pcm_channel_t -> hw.dma_move</LI>
</UL>
</LI>
</UL>
</P>

<P>Functions for dma allocation:</P>
<P>
<UL>
<LI><B>int snd_pcm_dma_alloc( snd_pcm_t *pcm, int direction, int dmanum, char *ident )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>direction</B> pcm direction - <B>SND_PCM_PLAYBACK</B> or <B>SND_PCM_RECORD</B></LI>
<LI><B>dmanum</B> dma index</LI>
<LI><B>ident</B> identification for dma owner</LI>
<LI>allocates pcm dma buffer</LI>
</UL>
</LI>
<LI><B>int snd_pcm_dma_free( snd_pcm_t *pcm, int direction, int dmanum )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>direction</B> pcm direction</LI>
<LI><B>dmanum</B> dma index</LI>
<LI>frees pcm dma buffer</LI>
</UL>
</LI>
</UL>
</P>

<P>Functions for allocation and registering:</P>
<P>
<UL>
<LI><B>snd_pcm_t *snd_pcm_new_device( snd_card_t *card, char *id )</B>
<UL>
<LI><B>card</B> pointer to soundcard structure</LI>
<LI><B>id</B> identification for pcm device (for example 'AD1848')</LI>
</UL>
</LI>
<LI><B>int snd_pcm_free( snd_pcm_t *pcm )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI>frees pcm structure and associated private data</LI>
</UL>
</LI>
<LI><B>int snd_pcm_register( snd_pcm_t *pcm, int pcm_device )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI><B>pcm_device</B> number of pcm device (0-3)</LI>
<LI>registers pcm device</LI>
</UL>
</LI>
<LI><B>int snd_pcm_unregister( snd_pcm_t *pcm )</B>
<UL>
<LI><B>pcm</B> pointer to pcm structure</LI>
<LI>unregisters and frees pcm structure</LI>
</UL>
</LI>
</UL>
</P>


<H2><A NAME="ss5.2">5.2 Examples</A></H2>


<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
static struct snd_stru_pcm_hardware snd_es1688_playback = {
  NULL,                         /* private data */
  NULL,                         /* private_free */
  SND_PCM_HW_AUTODMA,           /* flags */
  SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,  /* formats */
  0,                            /* align value */
  6,                            /* minimal fragment */
  4000,                         /* min. rate */
  48000,                        /* max. rate */
  2,                            /* max. voices */
  snd_es1688_playback_open,
  snd_es1688_playback_close,
  snd_es1688_playback_compute_rate,
  snd_es1688_playback_prepare,
  snd_es1688_playback_trigger,
  snd_es1688_playback_pointer,
  snd_pcm_playback_dma_ulaw,
  snd_pcm_dma_move,
  snd_pcm_playback_dma_neutral
};

static struct snd_stru_pcm_hardware snd_es1688_record = {
  NULL,                         /* private data */
  NULL,                         /* private free */
  SND_PCM_HW_AUTODMA,           /* flags */
  SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,  /* formats */
  0,                            /* align value */
  6,                            /* minimal fragment */
  4000,                         /* min. rate */
  48000,                        /* max. rate */
  2,                            /* max. voices */
  snd_es1688_record_open,
  snd_es1688_record_close,
  snd_es1688_record_compute_rate,
  snd_es1688_record_prepare,
  snd_es1688_record_trigger,
  snd_es1688_record_pointer,
  snd_pcm_record_dma_ulaw,
  snd_pcm_dma_move,
  NULL
};

static void snd_es1688_free( void *private_data )
{
  snd_free( private_data, sizeof( es1688_t ) );
}

snd_pcm_t *snd_es1688_new_device( snd_card_t *card,
                                  unsigned short port,
                                  unsigned short mpu_port,
                                  unsigned short irqnum,
                                  unsigned short mpu_irqnum,
                                  unsigned short dma8num,
                                  unsigned short hardware )
{
  snd_pcm_t *pcm;
  es1688_t *codec;

  pcm = snd_pcm_new_device( card, &quot;ESx688&quot; );
  if ( !pcm ) return NULL;
  codec = (es1688_t *)snd_malloc( sizeof( es1688_t ) );
  if ( !codec ) return NULL;
  memset( codec, 0, sizeof( es1688_t ) );
  snd_spin_prepare( codec, reg );
  snd_spin_prepare( codec, mixer );
  codec -&gt; pcm = pcm;
  codec -&gt; card = pcm -&gt; card;
  codec -&gt; port = port;
  codec -&gt; mpu_port = mpu_port;
  codec -&gt; irqnum = irqnum;
  codec -&gt; irq = pcm -&gt; card -&gt; irqs[ irqnum ] -&gt; irq;
  codec -&gt; mpu_irqnum = mpu_irqnum;
  if ( mpu_irqnum != SND_IRQ_DISABLE )
    codec -&gt; mpu_irq = pcm -&gt; card -&gt; irqs[ mpu_irqnum ] -&gt; irq;
  codec -&gt; dma8num = dma8num;
  codec -&gt; dma8 = pcm -&gt; card -&gt; dmas[ dma8num ] -&gt; dma;
  codec -&gt; hardware = hardware;
  memcpy( &amp;pcm -&gt; playback.hw, &amp;snd_es1688_playback, sizeof( snd_es1688_playback ) );
  memcpy( &amp;pcm -&gt; record.hw, &amp;snd_es1688_record, sizeof( snd_es1688_record ) );
  pcm -&gt; private_data = codec;
  pcm -&gt; private_free = snd_es1688_free;
  pcm -&gt; info_flags = SND_PCM_INFO_CODEC | SND_PCM_INFO_MMAP |
                      SND_PCM_INFO_PLAYBACK | SND_PCM_INFO_RECORD;
  sprintf( pcm -&gt; name, &quot;ES%s688 rev %i&quot;, codec -&gt; hardware == ES1688_HW_688 ?&quot;&quot; : &quot;1&quot;, codec -&gt; version & 0x0f );
  if ( snd_es1688_probe( pcm ) &lt; 0 ) {
    snd_pcm_free( pcm );
    return NULL;
  }
  return pcm;
}
</PRE>
<HR>
</CODE></BLOCKQUOTE>
</P>
<P>PCM device registering:</P>
<P>
<BLOCKQUOTE><CODE>
<HR>
<PRE>
  snd_card_t *card;
  snd_pcm_t *pcm;

  pcm = snd_es1688_new_device( card, port, mpu_port, irqnum, mpu_irqnum, dma8num, ES1688_HW_AUTO );
  if ( !pcm ) return NULL;
  ...
  if ( snd_pcm_register( pcm, 0 ) ) {
    ... unregister already registered devices ...
    snd_pcm_free( pcm );
    snd_card_free( card );
    return -ENXIO;
  }
  ...
  snd_pcm_unregister( pcm );
</PRE>
<HR>
</CODE></BLOCKQUOTE>
</P>


<HR>
<A HREF="coding-4.html">Previous</A>
Next
<A HREF="coding.html#toc5">Table of Contents</A>
</BODY>
</HTML>