Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[pandora-kernel.git] / sound / core / pcm_misc.c
1 /*
2  *  PCM Interface - misc routines
3  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 of
9  *   the License, or (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21   
22 #include <linux/time.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #define SND_PCM_FORMAT_UNKNOWN (-1)
26
27 /* NOTE: "signed" prefix must be given below since the default char is
28  *       unsigned on some architectures!
29  */
30 struct pcm_format_data {
31         unsigned char width;    /* bit width */
32         unsigned char phys;     /* physical bit width */
33         signed char le; /* 0 = big-endian, 1 = little-endian, -1 = others */
34         signed char signd;      /* 0 = unsigned, 1 = signed, -1 = others */
35         unsigned char silence[8];       /* silence data to fill */
36 };
37
38 static struct pcm_format_data pcm_formats[SNDRV_PCM_FORMAT_LAST+1] = {
39         [SNDRV_PCM_FORMAT_S8] = {
40                 .width = 8, .phys = 8, .le = -1, .signd = 1,
41                 .silence = {},
42         },
43         [SNDRV_PCM_FORMAT_U8] = {
44                 .width = 8, .phys = 8, .le = -1, .signd = 0,
45                 .silence = { 0x80 },
46         },
47         [SNDRV_PCM_FORMAT_S16_LE] = {
48                 .width = 16, .phys = 16, .le = 1, .signd = 1,
49                 .silence = {},
50         },
51         [SNDRV_PCM_FORMAT_S16_BE] = {
52                 .width = 16, .phys = 16, .le = 0, .signd = 1,
53                 .silence = {},
54         },
55         [SNDRV_PCM_FORMAT_U16_LE] = {
56                 .width = 16, .phys = 16, .le = 1, .signd = 0,
57                 .silence = { 0x00, 0x80 },
58         },
59         [SNDRV_PCM_FORMAT_U16_BE] = {
60                 .width = 16, .phys = 16, .le = 0, .signd = 0,
61                 .silence = { 0x80, 0x00 },
62         },
63         [SNDRV_PCM_FORMAT_S24_LE] = {
64                 .width = 24, .phys = 32, .le = 1, .signd = 1,
65                 .silence = {},
66         },
67         [SNDRV_PCM_FORMAT_S24_BE] = {
68                 .width = 24, .phys = 32, .le = 0, .signd = 1,
69                 .silence = {},
70         },
71         [SNDRV_PCM_FORMAT_U24_LE] = {
72                 .width = 24, .phys = 32, .le = 1, .signd = 0,
73                 .silence = { 0x00, 0x00, 0x80 },
74         },
75         [SNDRV_PCM_FORMAT_U24_BE] = {
76                 .width = 24, .phys = 32, .le = 0, .signd = 0,
77                 .silence = { 0x00, 0x80, 0x00, 0x00 },
78         },
79         [SNDRV_PCM_FORMAT_S32_LE] = {
80                 .width = 32, .phys = 32, .le = 1, .signd = 1,
81                 .silence = {},
82         },
83         [SNDRV_PCM_FORMAT_S32_BE] = {
84                 .width = 32, .phys = 32, .le = 0, .signd = 1,
85                 .silence = {},
86         },
87         [SNDRV_PCM_FORMAT_U32_LE] = {
88                 .width = 32, .phys = 32, .le = 1, .signd = 0,
89                 .silence = { 0x00, 0x00, 0x00, 0x80 },
90         },
91         [SNDRV_PCM_FORMAT_U32_BE] = {
92                 .width = 32, .phys = 32, .le = 0, .signd = 0,
93                 .silence = { 0x80, 0x00, 0x00, 0x00 },
94         },
95         [SNDRV_PCM_FORMAT_FLOAT_LE] = {
96                 .width = 32, .phys = 32, .le = 1, .signd = -1,
97                 .silence = {},
98         },
99         [SNDRV_PCM_FORMAT_FLOAT_BE] = {
100                 .width = 32, .phys = 32, .le = 0, .signd = -1,
101                 .silence = {},
102         },
103         [SNDRV_PCM_FORMAT_FLOAT64_LE] = {
104                 .width = 64, .phys = 64, .le = 1, .signd = -1,
105                 .silence = {},
106         },
107         [SNDRV_PCM_FORMAT_FLOAT64_BE] = {
108                 .width = 64, .phys = 64, .le = 0, .signd = -1,
109                 .silence = {},
110         },
111         [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE] = {
112                 .width = 32, .phys = 32, .le = 1, .signd = -1,
113                 .silence = {},
114         },
115         [SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE] = {
116                 .width = 32, .phys = 32, .le = 0, .signd = -1,
117                 .silence = {},
118         },
119         [SNDRV_PCM_FORMAT_MU_LAW] = {
120                 .width = 8, .phys = 8, .le = -1, .signd = -1,
121                 .silence = { 0x7f },
122         },
123         [SNDRV_PCM_FORMAT_A_LAW] = {
124                 .width = 8, .phys = 8, .le = -1, .signd = -1,
125                 .silence = { 0x55 },
126         },
127         [SNDRV_PCM_FORMAT_IMA_ADPCM] = {
128                 .width = 4, .phys = 4, .le = -1, .signd = -1,
129                 .silence = {},
130         },
131         [SNDRV_PCM_FORMAT_G723_24] = {
132                 .width = 3, .phys = 3, .le = -1, .signd = -1,
133                 .silence = {},
134         },
135         [SNDRV_PCM_FORMAT_G723_40] = {
136                 .width = 5, .phys = 5, .le = -1, .signd = -1,
137                 .silence = {},
138         },
139         /* FIXME: the following three formats are not defined properly yet */
140         [SNDRV_PCM_FORMAT_MPEG] = {
141                 .le = -1, .signd = -1,
142         },
143         [SNDRV_PCM_FORMAT_GSM] = {
144                 .le = -1, .signd = -1,
145         },
146         [SNDRV_PCM_FORMAT_SPECIAL] = {
147                 .le = -1, .signd = -1,
148         },
149         [SNDRV_PCM_FORMAT_S24_3LE] = {
150                 .width = 24, .phys = 24, .le = 1, .signd = 1,
151                 .silence = {},
152         },
153         [SNDRV_PCM_FORMAT_S24_3BE] = {
154                 .width = 24, .phys = 24, .le = 0, .signd = 1,
155                 .silence = {},
156         },
157         [SNDRV_PCM_FORMAT_U24_3LE] = {
158                 .width = 24, .phys = 24, .le = 1, .signd = 0,
159                 .silence = { 0x00, 0x00, 0x80 },
160         },
161         [SNDRV_PCM_FORMAT_U24_3BE] = {
162                 .width = 24, .phys = 24, .le = 0, .signd = 0,
163                 .silence = { 0x80, 0x00, 0x00 },
164         },
165         [SNDRV_PCM_FORMAT_S20_3LE] = {
166                 .width = 20, .phys = 24, .le = 1, .signd = 1,
167                 .silence = {},
168         },
169         [SNDRV_PCM_FORMAT_S20_3BE] = {
170                 .width = 20, .phys = 24, .le = 0, .signd = 1,
171                 .silence = {},
172         },
173         [SNDRV_PCM_FORMAT_U20_3LE] = {
174                 .width = 20, .phys = 24, .le = 1, .signd = 0,
175                 .silence = { 0x00, 0x00, 0x08 },
176         },
177         [SNDRV_PCM_FORMAT_U20_3BE] = {
178                 .width = 20, .phys = 24, .le = 0, .signd = 0,
179                 .silence = { 0x08, 0x00, 0x00 },
180         },
181         [SNDRV_PCM_FORMAT_S18_3LE] = {
182                 .width = 18, .phys = 24, .le = 1, .signd = 1,
183                 .silence = {},
184         },
185         [SNDRV_PCM_FORMAT_S18_3BE] = {
186                 .width = 18, .phys = 24, .le = 0, .signd = 1,
187                 .silence = {},
188         },
189         [SNDRV_PCM_FORMAT_U18_3LE] = {
190                 .width = 18, .phys = 24, .le = 1, .signd = 0,
191                 .silence = { 0x00, 0x00, 0x02 },
192         },
193         [SNDRV_PCM_FORMAT_U18_3BE] = {
194                 .width = 18, .phys = 24, .le = 0, .signd = 0,
195                 .silence = { 0x02, 0x00, 0x00 },
196         },
197         [SNDRV_PCM_FORMAT_G723_24_1B] = {
198                 .width = 3, .phys = 8, .le = -1, .signd = -1,
199                 .silence = {},
200         },
201         [SNDRV_PCM_FORMAT_G723_40_1B] = {
202                 .width = 5, .phys = 8, .le = -1, .signd = -1,
203                 .silence = {},
204         },
205 };
206
207
208 /**
209  * snd_pcm_format_signed - Check the PCM format is signed linear
210  * @format: the format to check
211  *
212  * Returns 1 if the given PCM format is signed linear, 0 if unsigned
213  * linear, and a negative error code for non-linear formats.
214  */
215 int snd_pcm_format_signed(snd_pcm_format_t format)
216 {
217         int val;
218         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
219                 return -EINVAL;
220         if ((val = pcm_formats[format].signd) < 0)
221                 return -EINVAL;
222         return val;
223 }
224
225 EXPORT_SYMBOL(snd_pcm_format_signed);
226
227 /**
228  * snd_pcm_format_unsigned - Check the PCM format is unsigned linear
229  * @format: the format to check
230  *
231  * Returns 1 if the given PCM format is unsigned linear, 0 if signed
232  * linear, and a negative error code for non-linear formats.
233  */
234 int snd_pcm_format_unsigned(snd_pcm_format_t format)
235 {
236         int val;
237
238         val = snd_pcm_format_signed(format);
239         if (val < 0)
240                 return val;
241         return !val;
242 }
243
244 EXPORT_SYMBOL(snd_pcm_format_unsigned);
245
246 /**
247  * snd_pcm_format_linear - Check the PCM format is linear
248  * @format: the format to check
249  *
250  * Returns 1 if the given PCM format is linear, 0 if not.
251  */
252 int snd_pcm_format_linear(snd_pcm_format_t format)
253 {
254         return snd_pcm_format_signed(format) >= 0;
255 }
256
257 EXPORT_SYMBOL(snd_pcm_format_linear);
258
259 /**
260  * snd_pcm_format_little_endian - Check the PCM format is little-endian
261  * @format: the format to check
262  *
263  * Returns 1 if the given PCM format is little-endian, 0 if
264  * big-endian, or a negative error code if endian not specified.
265  */
266 int snd_pcm_format_little_endian(snd_pcm_format_t format)
267 {
268         int val;
269         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
270                 return -EINVAL;
271         if ((val = pcm_formats[format].le) < 0)
272                 return -EINVAL;
273         return val;
274 }
275
276 EXPORT_SYMBOL(snd_pcm_format_little_endian);
277
278 /**
279  * snd_pcm_format_big_endian - Check the PCM format is big-endian
280  * @format: the format to check
281  *
282  * Returns 1 if the given PCM format is big-endian, 0 if
283  * little-endian, or a negative error code if endian not specified.
284  */
285 int snd_pcm_format_big_endian(snd_pcm_format_t format)
286 {
287         int val;
288
289         val = snd_pcm_format_little_endian(format);
290         if (val < 0)
291                 return val;
292         return !val;
293 }
294
295 EXPORT_SYMBOL(snd_pcm_format_big_endian);
296
297 /**
298  * snd_pcm_format_width - return the bit-width of the format
299  * @format: the format to check
300  *
301  * Returns the bit-width of the format, or a negative error code
302  * if unknown format.
303  */
304 int snd_pcm_format_width(snd_pcm_format_t format)
305 {
306         int val;
307         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
308                 return -EINVAL;
309         if ((val = pcm_formats[format].width) == 0)
310                 return -EINVAL;
311         return val;
312 }
313
314 EXPORT_SYMBOL(snd_pcm_format_width);
315
316 /**
317  * snd_pcm_format_physical_width - return the physical bit-width of the format
318  * @format: the format to check
319  *
320  * Returns the physical bit-width of the format, or a negative error code
321  * if unknown format.
322  */
323 int snd_pcm_format_physical_width(snd_pcm_format_t format)
324 {
325         int val;
326         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
327                 return -EINVAL;
328         if ((val = pcm_formats[format].phys) == 0)
329                 return -EINVAL;
330         return val;
331 }
332
333 EXPORT_SYMBOL(snd_pcm_format_physical_width);
334
335 /**
336  * snd_pcm_format_size - return the byte size of samples on the given format
337  * @format: the format to check
338  * @samples: sampling rate
339  *
340  * Returns the byte size of the given samples for the format, or a
341  * negative error code if unknown format.
342  */
343 ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
344 {
345         int phys_width = snd_pcm_format_physical_width(format);
346         if (phys_width < 0)
347                 return -EINVAL;
348         return samples * phys_width / 8;
349 }
350
351 EXPORT_SYMBOL(snd_pcm_format_size);
352
353 /**
354  * snd_pcm_format_silence_64 - return the silent data in 8 bytes array
355  * @format: the format to check
356  *
357  * Returns the format pattern to fill or NULL if error.
358  */
359 const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format)
360 {
361         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
362                 return NULL;
363         if (! pcm_formats[format].phys)
364                 return NULL;
365         return pcm_formats[format].silence;
366 }
367
368 EXPORT_SYMBOL(snd_pcm_format_silence_64);
369
370 /**
371  * snd_pcm_format_set_silence - set the silence data on the buffer
372  * @format: the PCM format
373  * @data: the buffer pointer
374  * @samples: the number of samples to set silence
375  *
376  * Sets the silence data on the buffer for the given samples.
377  *
378  * Returns zero if successful, or a negative error code on failure.
379  */
380 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
381 {
382         int width;
383         unsigned char *dst, *pat;
384
385         if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
386                 return -EINVAL;
387         if (samples == 0)
388                 return 0;
389         width = pcm_formats[format].phys; /* physical width */
390         pat = pcm_formats[format].silence;
391         if (! width)
392                 return -EINVAL;
393         /* signed or 1 byte data */
394         if (pcm_formats[format].signd == 1 || width <= 8) {
395                 unsigned int bytes = samples * width / 8;
396                 memset(data, *pat, bytes);
397                 return 0;
398         }
399         /* non-zero samples, fill using a loop */
400         width /= 8;
401         dst = data;
402 #if 0
403         while (samples--) {
404                 memcpy(dst, pat, width);
405                 dst += width;
406         }
407 #else
408         /* a bit optimization for constant width */
409         switch (width) {
410         case 2:
411                 while (samples--) {
412                         memcpy(dst, pat, 2);
413                         dst += 2;
414                 }
415                 break;
416         case 3:
417                 while (samples--) {
418                         memcpy(dst, pat, 3);
419                         dst += 3;
420                 }
421                 break;
422         case 4:
423                 while (samples--) {
424                         memcpy(dst, pat, 4);
425                         dst += 4;
426                 }
427                 break;
428         case 8:
429                 while (samples--) {
430                         memcpy(dst, pat, 8);
431                         dst += 8;
432                 }
433                 break;
434         }
435 #endif
436         return 0;
437 }
438
439 EXPORT_SYMBOL(snd_pcm_format_set_silence);
440
441 /**
442  * snd_pcm_limit_hw_rates - determine rate_min/rate_max fields
443  * @runtime: the runtime instance
444  *
445  * Determines the rate_min and rate_max fields from the rates bits of
446  * the given runtime->hw.
447  *
448  * Returns zero if successful.
449  */
450 int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
451 {
452         int i;
453         for (i = 0; i < (int)snd_pcm_known_rates.count; i++) {
454                 if (runtime->hw.rates & (1 << i)) {
455                         runtime->hw.rate_min = snd_pcm_known_rates.list[i];
456                         break;
457                 }
458         }
459         for (i = (int)snd_pcm_known_rates.count - 1; i >= 0; i--) {
460                 if (runtime->hw.rates & (1 << i)) {
461                         runtime->hw.rate_max = snd_pcm_known_rates.list[i];
462                         break;
463                 }
464         }
465         return 0;
466 }
467
468 EXPORT_SYMBOL(snd_pcm_limit_hw_rates);
469
470 /**
471  * snd_pcm_rate_to_rate_bit - converts sample rate to SNDRV_PCM_RATE_xxx bit
472  * @rate: the sample rate to convert
473  *
474  * Returns the SNDRV_PCM_RATE_xxx flag that corresponds to the given rate, or
475  * SNDRV_PCM_RATE_KNOT for an unknown rate.
476  */
477 unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate)
478 {
479         unsigned int i;
480
481         for (i = 0; i < snd_pcm_known_rates.count; i++)
482                 if (snd_pcm_known_rates.list[i] == rate)
483                         return 1u << i;
484         return SNDRV_PCM_RATE_KNOT;
485 }
486 EXPORT_SYMBOL(snd_pcm_rate_to_rate_bit);