capture.c 6.7 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line 6 Linux USB driver
M
Markus Grabner 已提交
3
 *
4
 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
M
Markus Grabner 已提交
5 6 7 8 9 10 11
 *
 *	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.
 *
 */

12
#include <linux/slab.h>
M
Markus Grabner 已提交
13 14 15 16
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>

17 18
#include "capture.h"
#include "driver.h"
M
Markus Grabner 已提交
19
#include "pcm.h"
20

M
Markus Grabner 已提交
21 22
/*
	Find a free URB and submit it.
23
	must be called in line6pcm->in.lock context
M
Markus Grabner 已提交
24
*/
25
static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
26
{
27
	int index;
M
Markus Grabner 已提交
28
	int i, urb_size;
29
	int ret;
M
Markus Grabner 已提交
30 31
	struct urb *urb_in;

32
	index =
33
	    find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
M
Markus Grabner 已提交
34

35 36
	if (index < 0 || index >= LINE6_ISO_BUFFERS) {
		dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
M
Markus Grabner 已提交
37 38 39
		return -EINVAL;
	}

40
	urb_in = line6pcm->in.urbs[index];
M
Markus Grabner 已提交
41 42
	urb_size = 0;

43
	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
44 45
		struct usb_iso_packet_descriptor *fin =
		    &urb_in->iso_frame_desc[i];
M
Markus Grabner 已提交
46 47 48 49 50
		fin->offset = urb_size;
		fin->length = line6pcm->max_packet_size;
		urb_size += line6pcm->max_packet_size;
	}

51
	urb_in->transfer_buffer =
52
	    line6pcm->in.buffer +
53
	    index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
M
Markus Grabner 已提交
54
	urb_in->transfer_buffer_length = urb_size;
55
	urb_in->context = line6pcm;
M
Markus Grabner 已提交
56

57 58 59
	ret = usb_submit_urb(urb_in, GFP_ATOMIC);

	if (ret == 0)
60
		set_bit(index, &line6pcm->in.active_urbs);
M
Markus Grabner 已提交
61
	else
62
		dev_err(line6pcm->line6->ifcdev,
63
			"URB in #%d submission failed (%d)\n", index, ret);
M
Markus Grabner 已提交
64 65 66 67 68 69

	return 0;
}

/*
	Submit all currently available capture URBs.
70
	must be called in line6pcm->in.lock context
M
Markus Grabner 已提交
71
*/
72
int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
73
{
74
	int ret = 0, i;
M
Markus Grabner 已提交
75

76
	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
77
		ret = submit_audio_in_urb(line6pcm);
78
		if (ret < 0)
79
			break;
80
	}
M
Markus Grabner 已提交
81

82
	return ret;
M
Markus Grabner 已提交
83 84 85
}

/*
86 87 88 89 90 91 92 93 94 95
	Copy data into ALSA capture buffer.
*/
void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
{
	struct snd_pcm_substream *substream =
	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
	struct snd_pcm_runtime *runtime = substream->runtime;
	const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
	int frames = fsize / bytes_per_frame;

96
	if (runtime == NULL)
97 98
		return;

99
	if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
100
		/*
101 102 103
		   The transferred area goes over buffer boundary,
		   copy two separate chunks.
		 */
104
		int len;
105

106
		len = runtime->buffer_size - line6pcm->in.pos_done;
107 108 109

		if (len > 0) {
			memcpy(runtime->dma_area +
110
			       line6pcm->in.pos_done * bytes_per_frame, fbuf,
111 112 113
			       len * bytes_per_frame);
			memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
			       (frames - len) * bytes_per_frame);
114 115 116 117 118
		} else {
			/* this is somewhat paranoid */
			dev_err(line6pcm->line6->ifcdev,
				"driver bug: len = %d\n", len);
		}
119 120 121
	} else {
		/* copy single chunk */
		memcpy(runtime->dma_area +
122
		       line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
123 124
	}

125 126 127
	line6pcm->in.pos_done += frames;
	if (line6pcm->in.pos_done >= runtime->buffer_size)
		line6pcm->in.pos_done -= runtime->buffer_size;
128 129 130 131 132 133 134
}

void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
{
	struct snd_pcm_substream *substream =
	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);

135 136 137
	line6pcm->in.bytes += length;
	if (line6pcm->in.bytes >= line6pcm->in.period) {
		line6pcm->in.bytes %= line6pcm->in.period;
138
		spin_unlock(&line6pcm->in.lock);
139
		snd_pcm_period_elapsed(substream);
140
		spin_lock(&line6pcm->in.lock);
141 142 143 144
	}
}

/*
145 146
 * Callback for completed capture URB.
 */
147
static void audio_in_callback(struct urb *urb)
M
Markus Grabner 已提交
148 149 150 151
{
	int i, index, length = 0, shutdown = 0;
	unsigned long flags;

152 153
	struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;

154
	line6pcm->in.last_frame = urb->start_frame;
M
Markus Grabner 已提交
155 156

	/* find index of URB */
157
	for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
158
		if (urb == line6pcm->in.urbs[index])
M
Markus Grabner 已提交
159 160
			break;

161
	spin_lock_irqsave(&line6pcm->in.lock, flags);
M
Markus Grabner 已提交
162

163
	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
M
Markus Grabner 已提交
164 165 166 167
		char *fbuf;
		int fsize;
		struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];

168
		if (fin->status == -EXDEV) {
M
Markus Grabner 已提交
169 170 171 172 173 174
			shutdown = 1;
			break;
		}

		fbuf = urb->transfer_buffer + fin->offset;
		fsize = fin->actual_length;
175 176 177 178 179 180 181

		if (fsize > line6pcm->max_packet_size) {
			dev_err(line6pcm->line6->ifcdev,
				"driver and/or device bug: packet too large (%d > %d)\n",
				fsize, line6pcm->max_packet_size);
		}

M
Markus Grabner 已提交
182 183
		length += fsize;

184 185 186
		/* the following assumes LINE6_ISO_PACKETS == 1: */
		line6pcm->prev_fbuf = fbuf;
		line6pcm->prev_fsize = fsize;
M
Markus Grabner 已提交
187

188 189 190 191
		if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
		    test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) &&
		    fsize > 0)
			line6_capture_copy(line6pcm, fbuf, fsize);
M
Markus Grabner 已提交
192 193
	}

194
	clear_bit(index, &line6pcm->in.active_urbs);
M
Markus Grabner 已提交
195

196
	if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
M
Markus Grabner 已提交
197 198
		shutdown = 1;

199
	if (!shutdown) {
200
		submit_audio_in_urb(line6pcm);
M
Markus Grabner 已提交
201

202 203 204
		if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
		    test_bit(LINE6_STREAM_PCM, &line6pcm->in.running))
			line6_capture_check_period(line6pcm, length);
M
Markus Grabner 已提交
205
	}
206 207

	spin_unlock_irqrestore(&line6pcm->in.lock, flags);
M
Markus Grabner 已提交
208 209 210 211 212 213 214 215 216
}

/* open capture callback */
static int snd_line6_capture_open(struct snd_pcm_substream *substream)
{
	int err;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);

217 218
	err = snd_pcm_hw_constraint_ratdens(runtime, 0,
					    SNDRV_PCM_HW_PARAM_RATE,
219
					    &line6pcm->properties->rates);
220
	if (err < 0)
M
Markus Grabner 已提交
221 222
		return err;

223
	runtime->hw = line6pcm->properties->capture_hw;
M
Markus Grabner 已提交
224 225 226 227 228 229 230 231 232 233 234
	return 0;
}

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

/* capture operators */
struct snd_pcm_ops snd_line6_capture_ops = {
235 236 237
	.open = snd_line6_capture_open,
	.close = snd_line6_capture_close,
	.ioctl = snd_pcm_lib_ioctl,
238 239
	.hw_params = snd_line6_hw_params,
	.hw_free = snd_line6_hw_free,
240 241
	.prepare = snd_line6_prepare,
	.trigger = snd_line6_trigger,
242
	.pointer = snd_line6_pointer,
M
Markus Grabner 已提交
243 244
};

245
int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
M
Markus Grabner 已提交
246
{
247
	struct usb_line6 *line6 = line6pcm->line6;
M
Markus Grabner 已提交
248 249 250
	int i;

	/* create audio URBs and fill in constant values: */
251
	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
M
Markus Grabner 已提交
252 253 254
		struct urb *urb;

		/* URB for audio in: */
255
		urb = line6pcm->in.urbs[i] =
256
		    usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
M
Markus Grabner 已提交
257

258
		if (urb == NULL)
M
Markus Grabner 已提交
259 260
			return -ENOMEM;

261
		urb->dev = line6->usbdev;
262
		urb->pipe =
263 264
		    usb_rcvisocpipe(line6->usbdev,
				    line6->properties->ep_audio_r &
265
				    USB_ENDPOINT_NUMBER_MASK);
M
Markus Grabner 已提交
266 267 268 269 270 271 272 273 274 275
		urb->transfer_flags = URB_ISO_ASAP;
		urb->start_frame = -1;
		urb->number_of_packets = LINE6_ISO_PACKETS;
		urb->interval = LINE6_ISO_INTERVAL;
		urb->error_count = 0;
		urb->complete = audio_in_callback;
	}

	return 0;
}