2 * Helper functions for indirect PCM data transfer to a simple FIFO in
3 * hardware (small, no possibility to read "hardware io position",
4 * updating position done by interrupt, ...)
6 * Copyright (c) by 2007 Joachim Foerster <JOFT@gmx.de>
8 * Based on "pcm-indirect.h" (alsa-driver-1.0.13) by
10 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
11 * Jaroslav Kysela <perex@suse.cz>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sound/core.h>
30 /* struct snd_pcm_substream, struct snd_pcm_runtime, snd_pcm_uframes_t
31 * snd_pcm_period_elapsed() */
32 #include <sound/pcm.h>
34 #include "pcm-indirect2.h"
36 #ifdef SND_PCM_INDIRECT2_STAT
38 #include <linux/jiffies.h>
40 void snd_pcm_indirect2_stat(struct snd_pcm_substream *substream,
41 struct snd_pcm_indirect2 *rec)
43 struct snd_pcm_runtime *runtime = substream->runtime;
47 int seconds = (rec->lastbytetime - rec->firstbytetime) / HZ;
49 snd_printk(KERN_DEBUG "STAT: mul_elapsed: %u, mul_elapsed_real: %d, "
51 rec->mul_elapsed, rec->mul_elapsed_real, rec->irq_occured);
52 snd_printk(KERN_DEBUG "STAT: min_multiple: %d (irqs/period)\n",
54 snd_printk(KERN_DEBUG "STAT: firstbytetime: %lu, lastbytetime: %lu, "
55 "firstzerotime: %lu\n",
56 rec->firstbytetime, rec->lastbytetime, rec->firstzerotime);
57 snd_printk(KERN_DEBUG "STAT: bytes2hw: %u Bytes => (by runtime->rate) "
59 rec->bytes2hw, rec->bytes2hw / 2 / 2 / runtime->rate);
60 snd_printk(KERN_DEBUG "STAT: (by measurement) length: %d => "
61 "rate: %d Bytes/s = %d Frames/s|Hz\n",
62 seconds, rec->bytes2hw / seconds,
63 rec->bytes2hw / 2 / 2 / seconds);
65 "STAT: zeros2hw: %u = %d ms ~ %d * %d zero copies\n",
66 rec->zeros2hw, ((rec->zeros2hw / 2 / 2) * 1000) /
68 rec->zeros2hw / (rec->hw_buffer_size / 2),
69 (rec->hw_buffer_size / 2));
70 snd_printk(KERN_DEBUG "STAT: pointer_calls: %u, lastdifftime: %u\n",
71 rec->pointer_calls, rec->lastdifftime);
72 snd_printk(KERN_DEBUG "STAT: sw_io: %d, sw_data: %d\n", rec->sw_io,
74 snd_printk(KERN_DEBUG "STAT: byte_sizes[]:\n");
76 for (j = 0; j < 8; j++) {
77 for (i = j * 8; i < (j + 1) * 8; i++)
78 if (rec->byte_sizes[i] != 0) {
79 snd_printk(KERN_DEBUG "%u: %u",
80 i, rec->byte_sizes[i]);
83 if (((k % 8) == 0) && (k != 0)) {
84 snd_printk(KERN_DEBUG "\n");
88 snd_printk(KERN_DEBUG "\n");
89 snd_printk(KERN_DEBUG "STAT: zero_sizes[]:\n");
90 for (j = 0; j < 8; j++) {
92 for (i = j * 8; i < (j + 1) * 8; i++)
93 if (rec->zero_sizes[i] != 0)
94 snd_printk(KERN_DEBUG "%u: %u",
95 i, rec->zero_sizes[i]);
99 snd_printk(KERN_DEBUG "\n");
101 snd_printk(KERN_DEBUG "\n");
102 snd_printk(KERN_DEBUG "STAT: min_adds[]:\n");
103 for (j = 0; j < 8; j++) {
104 if (rec->min_adds[j] != 0)
105 snd_printk(KERN_DEBUG "%u: %u", j, rec->min_adds[j]);
107 snd_printk(KERN_DEBUG "\n");
108 snd_printk(KERN_DEBUG "STAT: mul_adds[]:\n");
109 for (j = 0; j < 8; j++) {
110 if (rec->mul_adds[j] != 0)
111 snd_printk(KERN_DEBUG "%u: %u", j, rec->mul_adds[j]);
113 snd_printk(KERN_DEBUG "\n");
114 snd_printk(KERN_DEBUG
115 "STAT: zero_times_saved: %d, zero_times_notsaved: %d\n",
116 rec->zero_times_saved, rec->zero_times_notsaved);
117 /* snd_printk(KERN_DEBUG "STAT: zero_times[]\n");
119 for (j = 0; j < 3750; j++) {
120 if (rec->zero_times[j] != 0) {
121 snd_printk(KERN_DEBUG "%u: %u", j, rec->zero_times[j]);
124 if (((i % 8) == 0) && (i != 0))
125 snd_printk(KERN_DEBUG "\n");
127 snd_printk(KERN_DEBUG "\n"); */
133 * _internal_ helper function for playback/capture transfer function
136 snd_pcm_indirect2_increase_min_periods(struct snd_pcm_substream *substream,
137 struct snd_pcm_indirect2 *rec,
138 int isplay, int iscopy,
141 if (rec->min_periods >= 0) {
144 if (rec->sw_io >= rec->sw_buffer_size)
145 rec->sw_io -= rec->sw_buffer_size;
147 /* If application does not write data in multiples of
148 * a period, move sw_data to the next correctly aligned
149 * position, so that sw_io can converge to it (in the
152 if (!rec->check_alignment) {
154 snd_pcm_lib_period_bytes(substream)) {
155 unsigned bytes2hw_aligned =
158 snd_pcm_lib_period_bytes
160 snd_pcm_lib_period_bytes
165 #ifdef SND_PCM_INDIRECT2_STAT
166 snd_printk(KERN_DEBUG
167 "STAT: @re-align: aligned "
168 "bytes2hw to next period "
173 snd_printk(KERN_DEBUG
174 "STAT: @re-align: sw_data "
179 rec->check_alignment = 1;
181 /* We are at the end and are copying zeros into the
183 * Now, we have to make sure that sw_io is increased
184 * until the position of sw_data: Filling the fifo with
185 * the first zeros means, the last bytes were played.
187 if (rec->sw_io != rec->sw_data) {
189 if (rec->sw_data > rec->sw_io)
190 diff = rec->sw_data - rec->sw_io;
192 diff = (rec->sw_buffer_size -
196 rec->sw_io = rec->sw_data;
199 if (rec->sw_io >= rec->sw_buffer_size)
205 rec->min_period_count += bytes;
206 if (rec->min_period_count >= (rec->hw_buffer_size / 2)) {
207 rec->min_periods += (rec->min_period_count /
208 (rec->hw_buffer_size / 2));
209 #ifdef SND_PCM_INDIRECT2_STAT
210 if ((rec->min_period_count /
211 (rec->hw_buffer_size / 2)) > 7)
212 snd_printk(KERN_DEBUG
213 "STAT: more than 7 (%d) min_adds "
214 "at once - too big to save!\n",
215 (rec->min_period_count /
216 (rec->hw_buffer_size / 2)));
218 rec->min_adds[(rec->min_period_count /
219 (rec->hw_buffer_size / 2))]++;
221 rec->min_period_count = (rec->min_period_count %
222 (rec->hw_buffer_size / 2));
224 } else if (isplay && iscopy)
225 rec->min_periods = 0;
229 * helper function for playback/capture pointer callback
232 snd_pcm_indirect2_pointer(struct snd_pcm_substream *substream,
233 struct snd_pcm_indirect2 *rec)
235 #ifdef SND_PCM_INDIRECT2_STAT
236 rec->pointer_calls++;
238 return bytes_to_frames(substream->runtime, rec->sw_io);
242 * _internal_ helper function for playback interrupt callback
245 snd_pcm_indirect2_playback_transfer(struct snd_pcm_substream *substream,
246 struct snd_pcm_indirect2 *rec,
247 snd_pcm_indirect2_copy_t copy,
248 snd_pcm_indirect2_zero_t zero)
250 struct snd_pcm_runtime *runtime = substream->runtime;
251 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
253 /* runtime->control->appl_ptr: position where ALSA will write next time
254 * rec->appl_ptr: position where ALSA was last time
255 * diff: obviously ALSA wrote that much bytes into the intermediate
256 * buffer since we checked last time
258 snd_pcm_sframes_t diff = appl_ptr - rec->appl_ptr;
261 #ifdef SND_PCM_INDIRECT2_STAT
262 rec->lastdifftime = jiffies;
264 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
265 diff += runtime->boundary;
266 /* number of bytes "added" by ALSA increases the number of
267 * bytes which are ready to "be transferred to HW"/"played"
268 * Then, set rec->appl_ptr to not count bytes twice next time.
270 rec->sw_ready += (int)frames_to_bytes(runtime, diff);
271 rec->appl_ptr = appl_ptr;
273 if (rec->hw_ready && (rec->sw_ready <= 0)) {
276 #ifdef SND_PCM_INDIRECT2_STAT
277 if (rec->firstzerotime == 0) {
278 rec->firstzerotime = jiffies;
279 snd_printk(KERN_DEBUG
280 "STAT: @firstzerotime: mul_elapsed: %d, "
281 "min_period_count: %d\n",
282 rec->mul_elapsed, rec->min_period_count);
283 snd_printk(KERN_DEBUG
284 "STAT: @firstzerotime: sw_io: %d, "
285 "sw_data: %d, appl_ptr: %u\n",
286 rec->sw_io, rec->sw_data,
287 (unsigned int)appl_ptr);
289 if ((jiffies - rec->firstzerotime) < 3750) {
290 rec->zero_times[(jiffies - rec->firstzerotime)]++;
291 rec->zero_times_saved++;
293 rec->zero_times_notsaved++;
295 bytes = zero(substream, rec);
297 #ifdef SND_PCM_INDIRECT2_STAT
298 rec->zeros2hw += bytes;
300 rec->zero_sizes[bytes]++;
302 snd_printk(KERN_DEBUG
303 "STAT: %d zero Bytes copied to hardware at "
304 "once - too big to save!\n",
307 snd_pcm_indirect2_increase_min_periods(substream, rec, 1, 0,
311 while (rec->hw_ready && (rec->sw_ready > 0)) {
312 /* sw_to_end: max. number of bytes that can be read/take from
313 * the current position (sw_data) in _one_ step
315 unsigned int sw_to_end = rec->sw_buffer_size - rec->sw_data;
317 /* bytes: number of bytes we have available (for reading) */
318 unsigned int bytes = rec->sw_ready;
320 if (sw_to_end < bytes)
325 #ifdef SND_PCM_INDIRECT2_STAT
326 if (rec->firstbytetime == 0)
327 rec->firstbytetime = jiffies;
328 rec->lastbytetime = jiffies;
330 /* copy bytes from intermediate buffer position sw_data to the
331 * HW and return number of bytes actually written
332 * Furthermore, set hw_ready to 0, if the fifo isn't empty
333 * now => more could be transferred to fifo
335 bytes = copy(substream, rec, bytes);
336 rec->bytes2hw += bytes;
338 #ifdef SND_PCM_INDIRECT2_STAT
340 rec->byte_sizes[bytes]++;
342 snd_printk(KERN_DEBUG
343 "STAT: %d Bytes copied to hardware at once "
344 "- too big to save!\n",
347 /* increase sw_data by the number of actually written bytes
348 * (= number of taken bytes from intermediate buffer)
350 rec->sw_data += bytes;
351 if (rec->sw_data == rec->sw_buffer_size)
353 /* now sw_data is the position where ALSA is going to write
354 * in the intermediate buffer next time = position we are going
355 * to read from next time
358 snd_pcm_indirect2_increase_min_periods(substream, rec, 1, 1,
361 /* we read bytes from intermediate buffer, so we need to say
362 * that the number of bytes ready for transfer are decreased
365 rec->sw_ready -= bytes;
371 * helper function for playback interrupt routine
374 snd_pcm_indirect2_playback_interrupt(struct snd_pcm_substream *substream,
375 struct snd_pcm_indirect2 *rec,
376 snd_pcm_indirect2_copy_t copy,
377 snd_pcm_indirect2_zero_t zero)
379 #ifdef SND_PCM_INDIRECT2_STAT
382 /* hardware played some bytes, so there is room again (in fifo) */
385 /* don't call ack() now, instead call transfer() function directly
386 * (normally called by ack() )
388 snd_pcm_indirect2_playback_transfer(substream, rec, copy, zero);
390 if (rec->min_periods >= rec->min_multiple) {
391 #ifdef SND_PCM_INDIRECT2_STAT
392 if ((rec->min_periods / rec->min_multiple) > 7)
393 snd_printk(KERN_DEBUG
394 "STAT: more than 7 (%d) mul_adds - too big "
396 (rec->min_periods / rec->min_multiple));
398 rec->mul_adds[(rec->min_periods /
399 rec->min_multiple)]++;
400 rec->mul_elapsed_real += (rec->min_periods /
404 rec->min_periods = (rec->min_periods % rec->min_multiple);
405 snd_pcm_period_elapsed(substream);
410 * _internal_ helper function for capture interrupt callback
413 snd_pcm_indirect2_capture_transfer(struct snd_pcm_substream *substream,
414 struct snd_pcm_indirect2 *rec,
415 snd_pcm_indirect2_copy_t copy,
416 snd_pcm_indirect2_zero_t null)
418 struct snd_pcm_runtime *runtime = substream->runtime;
419 snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
420 snd_pcm_sframes_t diff = appl_ptr - rec->appl_ptr;
423 #ifdef SND_PCM_INDIRECT2_STAT
424 rec->lastdifftime = jiffies;
426 if (diff < -(snd_pcm_sframes_t) (runtime->boundary / 2))
427 diff += runtime->boundary;
428 rec->sw_ready -= frames_to_bytes(runtime, diff);
429 rec->appl_ptr = appl_ptr;
431 /* if hardware has something, but the intermediate buffer is full
432 * => skip contents of buffer
434 if (rec->hw_ready && (rec->sw_ready >= (int)rec->sw_buffer_size)) {
437 #ifdef SND_PCM_INDIRECT2_STAT
438 if (rec->firstzerotime == 0) {
439 rec->firstzerotime = jiffies;
440 snd_printk(KERN_DEBUG "STAT: (capture) "
441 "@firstzerotime: mul_elapsed: %d, "
442 "min_period_count: %d\n",
443 rec->mul_elapsed, rec->min_period_count);
444 snd_printk(KERN_DEBUG "STAT: (capture) "
445 "@firstzerotime: sw_io: %d, sw_data: %d, "
447 rec->sw_io, rec->sw_data,
448 (unsigned int)appl_ptr);
450 if ((jiffies - rec->firstzerotime) < 3750) {
451 rec->zero_times[(jiffies - rec->firstzerotime)]++;
452 rec->zero_times_saved++;
454 rec->zero_times_notsaved++;
456 bytes = null(substream, rec);
458 #ifdef SND_PCM_INDIRECT2_STAT
459 rec->zeros2hw += bytes;
461 rec->zero_sizes[bytes]++;
463 snd_printk(KERN_DEBUG
464 "STAT: (capture) %d zero Bytes copied to "
465 "hardware at once - too big to save!\n",
468 snd_pcm_indirect2_increase_min_periods(substream, rec, 0, 0,
470 /* report an overrun */
471 rec->sw_io = SNDRV_PCM_POS_XRUN;
474 while (rec->hw_ready && (rec->sw_ready < (int)rec->sw_buffer_size)) {
475 /* sw_to_end: max. number of bytes that we can write to the
476 * intermediate buffer (until it's end)
478 size_t sw_to_end = rec->sw_buffer_size - rec->sw_data;
480 /* bytes: max. number of bytes, which may be copied to the
481 * intermediate buffer without overflow (in _one_ step)
483 size_t bytes = rec->sw_buffer_size - rec->sw_ready;
485 /* limit number of bytes (for transfer) by available room in
486 * the intermediate buffer
488 if (sw_to_end < bytes)
493 #ifdef SND_PCM_INDIRECT2_STAT
494 if (rec->firstbytetime == 0)
495 rec->firstbytetime = jiffies;
496 rec->lastbytetime = jiffies;
498 /* copy bytes from the intermediate buffer (position sw_data)
499 * to the HW at most and return number of bytes actually copied
501 * Furthermore, set hw_ready to 0, if the fifo is empty now.
503 bytes = copy(substream, rec, bytes);
504 rec->bytes2hw += bytes;
506 #ifdef SND_PCM_INDIRECT2_STAT
508 rec->byte_sizes[bytes]++;
510 snd_printk(KERN_DEBUG
511 "STAT: (capture) %d Bytes copied to "
512 "hardware at once - too big to save!\n",
515 /* increase sw_data by the number of actually copied bytes from
518 rec->sw_data += bytes;
519 if (rec->sw_data == rec->sw_buffer_size)
522 snd_pcm_indirect2_increase_min_periods(substream, rec, 0, 1,
525 /* number of bytes in the intermediate buffer, which haven't
526 * been fetched by ALSA yet.
528 rec->sw_ready += bytes;
534 * helper function for capture interrupt routine
537 snd_pcm_indirect2_capture_interrupt(struct snd_pcm_substream *substream,
538 struct snd_pcm_indirect2 *rec,
539 snd_pcm_indirect2_copy_t copy,
540 snd_pcm_indirect2_zero_t null)
542 #ifdef SND_PCM_INDIRECT2_STAT
545 /* hardware recorded some bytes, so there is something to read from the
550 /* don't call ack() now, instead call transfer() function directly
551 * (normally called by ack() )
553 snd_pcm_indirect2_capture_transfer(substream, rec, copy, null);
555 if (rec->min_periods >= rec->min_multiple) {
557 #ifdef SND_PCM_INDIRECT2_STAT
558 if ((rec->min_periods / rec->min_multiple) > 7)
559 snd_printk(KERN_DEBUG
560 "STAT: more than 7 (%d) mul_adds - "
561 "too big to save!\n",
562 (rec->min_periods / rec->min_multiple));
564 rec->mul_adds[(rec->min_periods /
565 rec->min_multiple)]++;
566 rec->mul_elapsed_real += (rec->min_periods /
570 rec->min_periods = (rec->min_periods % rec->min_multiple);
571 snd_pcm_period_elapsed(substream);