File: capture.c

package info (click to toggle)
line6-usb 0.7.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 324 kB
  • ctags: 648
  • sloc: ansic: 4,215; sh: 120; makefile: 90; perl: 69
file content (379 lines) | stat: -rw-r--r-- 9,516 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
/*
 * Line6 Linux USB driver - 0.7.3
 *
 * Copyright (C) 2004-2008 Markus Grabner (grabner@icg.tugraz.at)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 */

#include "config.h"

#include <sound/driver.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>

#include "audio.h"
#include "driver.h"
#include "pcm.h"
#include "pod.h"


/* hardware definition (capture) */
static struct snd_pcm_hardware snd_pod_capture_hw = {
	.info = (SNDRV_PCM_INFO_MMAP |
					 SNDRV_PCM_INFO_INTERLEAVED |
					 SNDRV_PCM_INFO_BLOCK_TRANSFER |
					 SNDRV_PCM_INFO_MMAP_VALID |
					 SNDRV_PCM_INFO_SYNC_START),
	.formats =          SNDRV_PCM_FMTBIT_S24_3LE,
	.rates =            SNDRV_PCM_RATE_KNOT,
	.rate_min =         39063,
	.rate_max =         39063,
	.channels_min =     2,
	.channels_max =     2,
	.buffer_bytes_max = 60000,
	.period_bytes_min = POD_ISO_PACKET_SIZE_MAX * POD_BYTES_PER_FRAME,  /* at least one URB must fit into one period */
	.period_bytes_max = 8192,
	.periods_min =      1,
	.periods_max =      1024,
};


/*
	Find a free URB and submit it.
*/
static int submit_audio_in_urb(struct snd_pcm_substream *substream)
{
	int index;
	unsigned long flags;
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);
	int i, urb_size;
	struct urb *urb_in;

	spin_lock_irqsave(&chip->lock_audio_in, flags);
	index = find_first_zero_bit(&chip->active_urb_in, POD_ISO_BUFFERS);

	if(index < 0 || index >= POD_ISO_BUFFERS) {
		spin_unlock_irqrestore(&chip->lock_audio_in, flags);
		dev_err(s2m(substream), "no free URB found\n");
		return -EINVAL;
	}

	urb_in = chip->urb_audio_in[index];
	urb_size = 0;

	for(i = 0; i < POD_ISO_PACKETS; ++i) {
		struct usb_iso_packet_descriptor *fin = &urb_in->iso_frame_desc[i];
		fin->offset = urb_size;
		fin->length = POD_ISO_PACKET_SIZE_MAX;
		urb_size += POD_ISO_PACKET_SIZE_MAX;
	}

	urb_in->transfer_buffer = chip->buffer_in + index * POD_ISO_PACKETS * POD_ISO_PACKET_SIZE_MAX;
	urb_in->transfer_buffer_length = urb_size;
	urb_in->context = substream;

	if(usb_submit_urb(urb_in, GFP_ATOMIC) == 0)
		set_bit(index, &chip->active_urb_in);
	else
		dev_err(s2m(substream), "URB in #%d submission failed\n", index);

	spin_unlock_irqrestore(&chip->lock_audio_in, flags);
	return 0;
}

/*
	Submit all currently available capture URBs.
*/
static int submit_audio_in_all_urbs(struct snd_pcm_substream *substream)
{
	int ret, i;

	for(i = 0; i < POD_ISO_BUFFERS; ++i)
		if((ret = submit_audio_in_urb(substream)) < 0)
			return ret;

	return 0;
}

/*
	Unlink all currently active capture URBs.
*/
static void unlink_audio_in_urbs(struct snd_pod_pcm *chip)
{
	unsigned int i;

	for(i = POD_ISO_BUFFERS; i--;) {
		if(test_bit(i, &chip->active_urb_in)) {
			if(!test_and_set_bit(i, &chip->unlink_urb_in)) {
				struct urb *u = chip->urb_audio_in[i];
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14)
				u->transfer_flags |= URB_ASYNC_UNLINK;
#endif
				usb_unlink_urb(u);
			}
		}
	}
}

/*
	Wait until unlinking of all currently active capture URBs has been finished.
*/
static void wait_clear_audio_in_urbs(struct snd_pod_pcm *chip)
{
	int timeout = HZ;
	unsigned int i;
	int alive;

	do {
		alive = 0;
		for (i = POD_ISO_BUFFERS; i--;) {
			if (test_bit(i, &chip->active_urb_in))
				alive++;
		}
		if (! alive)
			break;
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(1);
	} while (--timeout > 0);
	if (alive)
		snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);

	chip->active_urb_in = 0;
	chip->unlink_urb_in = 0;
}

/*
	Unlink all currently active capture URBs, and wait for finishing.
*/
void unlink_wait_clear_audio_in_urbs(struct snd_pod_pcm *chip)
{
	unlink_audio_in_urbs(chip);
	wait_clear_audio_in_urbs(chip);
}

/*
	Callback for completed capture URB.
*/
static void audio_in_callback(struct urb *urb PT_REGS)
{
	int i, index, length = 0, shutdown = 0;
	int frames;
	unsigned long flags;

	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)urb->context;
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;

	/* find index of URB */
	for(index = 0; index < POD_ISO_BUFFERS; ++index)
		if(urb == chip->urb_audio_in[index])
			break;

	spin_lock_irqsave(&chip->lock_audio_in, flags);

	for(i = 0; i < POD_ISO_PACKETS; ++i) {
		char *fbuf;
		int fsize;
		struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];

		if(fin->status == -18) {
			shutdown = 1;
			break;
		}

		fbuf = urb->transfer_buffer + fin->offset;
		fsize = fin->actual_length;
		length += fsize;

		if(fsize > 0) {
			frames = fsize / POD_BYTES_PER_FRAME;

			if(chip->pos_in_done + frames > runtime->buffer_size) {
				/*
					The transferred area goes over buffer boundary,
					copy two separate chunks.
				*/
				int len;
				len = runtime->buffer_size - chip->pos_in_done;

				if(len > 0) {
					memcpy(runtime->dma_area + chip->pos_in_done * POD_BYTES_PER_FRAME, fbuf, len * POD_BYTES_PER_FRAME);
					memcpy(runtime->dma_area, fbuf + len * POD_BYTES_PER_FRAME, (frames - len) * POD_BYTES_PER_FRAME);
				}
				else
					dev_err(s2m(substream), "driver bug: len = %d\n", len);  /* this is somewhat paranoid */
			}
			else {
				/* copy single chunk */
				memcpy(runtime->dma_area + chip->pos_in_done * POD_BYTES_PER_FRAME, fbuf, fsize * POD_BYTES_PER_FRAME);
			}

			if((chip->pos_in_done += frames) >= runtime->buffer_size)
				chip->pos_in_done -= runtime->buffer_size;
		}
	}

	clear_bit(index, &chip->active_urb_in);

	if(test_bit(index, &chip->unlink_urb_in))
		shutdown = 1;

	spin_unlock_irqrestore(&chip->lock_audio_in, flags);

	if(!shutdown) {
		submit_audio_in_urb(substream);

		if((chip->bytes_in += length) >= chip->period_in) {
			chip->bytes_in -= chip->period_in;
			snd_pcm_period_elapsed(substream);
		}
	}
}

/* open capture callback */
static int snd_pod_capture_open(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	runtime->hw = snd_pod_capture_hw;
	return 0;
}

/* close capture callback */
static int snd_pod_capture_close(struct snd_pcm_substream *substream)
{
	return 0;
}

/* hw_params capture callback */
static int snd_pod_capture_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params)
{
	int ret;
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);

	/* -- Florian Demski [FD] */
	/* don't ask me why, but this fixes the bug on my machine */
	if ( chip == NULL ) {
		if ( substream->pcm == NULL )
			return -ENOMEM;
		if ( substream->pcm->private_data == NULL )
			return -ENOMEM;
		substream->private_data = substream->pcm->private_data;
		chip = snd_pcm_substream_chip( substream );
	}
	/* -- [FD] end */

	if((ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
		return ret;

	chip->period_in = params_period_bytes(hw_params);
	chip->buffer_in = kmalloc(POD_ISO_BUFFERS * POD_ISO_PACKETS * POD_ISO_PACKET_SIZE_MAX, GFP_KERNEL);

	if(!chip->buffer_in) {
		dev_err(s2m(substream), "cannot malloc buffer_in\n");
		return -ENOMEM;
	}

	return 0;
}

/* hw_free capture callback */
static int snd_pod_capture_hw_free(struct snd_pcm_substream *substream)
{
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);
	unlink_wait_clear_audio_in_urbs(chip);

	if(chip->buffer_in) {
		kfree(chip->buffer_in);
		chip->buffer_in = 0;
	}

	return snd_pcm_lib_free_pages(substream);
}

/* trigger callback */
int snd_pod_capture_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);
	int err;
	chip->count_in = 0;

	switch(cmd) {
	case SNDRV_PCM_TRIGGER_START:
		if(!test_and_set_bit(BIT_RUNNING_CAPTURE, &chip->flags)) {
			err = submit_audio_in_all_urbs(substream);

			if(err < 0) {
				clear_bit(BIT_RUNNING_CAPTURE, &chip->flags);
				return err;
			}
		}

		break;

	case SNDRV_PCM_TRIGGER_STOP:
		if(test_and_clear_bit(BIT_RUNNING_CAPTURE, &chip->flags))
			unlink_audio_in_urbs(chip);

		break;

	default:
		return -EINVAL;
	}

	return 0;
}

/* capture pointer callback */
static snd_pcm_uframes_t
snd_pod_capture_pointer(struct snd_pcm_substream *substream)
{
	struct snd_pod_pcm *chip = snd_pcm_substream_chip(substream);
	return chip->pos_in_done;
}

/* capture operators */
struct snd_pcm_ops snd_pod_capture_ops = {
	.open =        snd_pod_capture_open,
	.close =       snd_pod_capture_close,
	.ioctl =       snd_pcm_lib_ioctl,
	.hw_params =   snd_pod_capture_hw_params,
	.hw_free =     snd_pod_capture_hw_free,
	.prepare =     snd_pod_prepare,
	.trigger =     snd_pod_trigger,
	.pointer =     snd_pod_capture_pointer,
};

int create_audio_in_urbs(struct snd_pod_pcm *chip)
{
	int i;
	struct usb_line6_pod *pod = chip->pod;

	/* create audio URBs and fill in constant values: */
	for(i = 0; i < POD_ISO_BUFFERS; ++i) {
		struct urb *urb;

		/* URB for audio in: */
		urb = chip->urb_audio_in[i] = usb_alloc_urb(POD_ISO_PACKETS, GFP_KERNEL);

		if(urb == NULL) {
			dev_err(pod->line6.ifcdev, "Out of memory\n");
			return -ENOMEM;
		}

		urb->dev = pod->line6.usbdev;
		urb->pipe = usb_rcvisocpipe(pod->line6.usbdev, POD_EP_READ_AUDIO & USB_ENDPOINT_NUMBER_MASK);
		urb->transfer_flags = URB_ISO_ASAP;
		urb->start_frame = -1;
		urb->number_of_packets = POD_ISO_PACKETS;
		urb->interval = POD_ISO_INTERVAL;
		urb->error_count = 0;
		urb->complete = audio_in_callback;
	}

	return 0;
}