xref: /openbmc/linux/sound/usb/line6/capture.c (revision a019f5e8c522f5b5f8b3419a1e56d142ea4c7621)
1 /*
2  * Line6 Linux USB driver - 0.9.1beta
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  */
11 
12 #include <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 
17 #include "audio.h"
18 #include "capture.h"
19 #include "driver.h"
20 #include "pcm.h"
21 
22 /*
23 	Find a free URB and submit it.
24 */
25 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
26 {
27 	int index;
28 	unsigned long flags;
29 	int i, urb_size;
30 	int ret;
31 	struct urb *urb_in;
32 
33 	spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
34 	index =
35 	    find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
36 
37 	if (index < 0 || index >= LINE6_ISO_BUFFERS) {
38 		spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
39 		dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
40 		return -EINVAL;
41 	}
42 
43 	urb_in = line6pcm->urb_audio_in[index];
44 	urb_size = 0;
45 
46 	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
47 		struct usb_iso_packet_descriptor *fin =
48 		    &urb_in->iso_frame_desc[i];
49 		fin->offset = urb_size;
50 		fin->length = line6pcm->max_packet_size;
51 		urb_size += line6pcm->max_packet_size;
52 	}
53 
54 	urb_in->transfer_buffer =
55 	    line6pcm->buffer_in +
56 	    index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
57 	urb_in->transfer_buffer_length = urb_size;
58 	urb_in->context = line6pcm;
59 
60 	ret = usb_submit_urb(urb_in, GFP_ATOMIC);
61 
62 	if (ret == 0)
63 		set_bit(index, &line6pcm->active_urb_in);
64 	else
65 		dev_err(line6pcm->line6->ifcdev,
66 			"URB in #%d submission failed (%d)\n", index, ret);
67 
68 	spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
69 	return 0;
70 }
71 
72 /*
73 	Submit all currently available capture URBs.
74 */
75 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
76 {
77 	int ret, i;
78 
79 	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
80 		ret = submit_audio_in_urb(line6pcm);
81 		if (ret < 0)
82 			return ret;
83 	}
84 
85 	return 0;
86 }
87 
88 /*
89 	Unlink all currently active capture URBs.
90 */
91 void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
92 {
93 	unsigned int i;
94 
95 	for (i = LINE6_ISO_BUFFERS; i--;) {
96 		if (test_bit(i, &line6pcm->active_urb_in)) {
97 			if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) {
98 				struct urb *u = line6pcm->urb_audio_in[i];
99 
100 				usb_unlink_urb(u);
101 			}
102 		}
103 	}
104 }
105 
106 /*
107 	Wait until unlinking of all currently active capture URBs has been
108 	finished.
109 */
110 void line6_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
111 {
112 	int timeout = HZ;
113 	unsigned int i;
114 	int alive;
115 
116 	do {
117 		alive = 0;
118 		for (i = LINE6_ISO_BUFFERS; i--;) {
119 			if (test_bit(i, &line6pcm->active_urb_in))
120 				alive++;
121 		}
122 		if (!alive)
123 			break;
124 		set_current_state(TASK_UNINTERRUPTIBLE);
125 		schedule_timeout(1);
126 	} while (--timeout > 0);
127 	if (alive)
128 		snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
129 }
130 
131 /*
132 	Unlink all currently active capture URBs, and wait for finishing.
133 */
134 void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
135 {
136 	line6_unlink_audio_in_urbs(line6pcm);
137 	line6_wait_clear_audio_in_urbs(line6pcm);
138 }
139 
140 /*
141 	Copy data into ALSA capture buffer.
142 */
143 void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
144 {
145 	struct snd_pcm_substream *substream =
146 	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
147 	struct snd_pcm_runtime *runtime = substream->runtime;
148 	const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
149 	int frames = fsize / bytes_per_frame;
150 
151 	if (runtime == NULL)
152 		return;
153 
154 	if (line6pcm->pos_in_done + frames > runtime->buffer_size) {
155 		/*
156 		   The transferred area goes over buffer boundary,
157 		   copy two separate chunks.
158 		 */
159 		int len;
160 
161 		len = runtime->buffer_size - line6pcm->pos_in_done;
162 
163 		if (len > 0) {
164 			memcpy(runtime->dma_area +
165 			       line6pcm->pos_in_done * bytes_per_frame, fbuf,
166 			       len * bytes_per_frame);
167 			memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
168 			       (frames - len) * bytes_per_frame);
169 		} else {
170 			/* this is somewhat paranoid */
171 			dev_err(line6pcm->line6->ifcdev,
172 				"driver bug: len = %d\n", len);
173 		}
174 	} else {
175 		/* copy single chunk */
176 		memcpy(runtime->dma_area +
177 		       line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize);
178 	}
179 
180 	line6pcm->pos_in_done += frames;
181 	if (line6pcm->pos_in_done >= runtime->buffer_size)
182 		line6pcm->pos_in_done -= runtime->buffer_size;
183 }
184 
185 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
186 {
187 	struct snd_pcm_substream *substream =
188 	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
189 
190 	line6pcm->bytes_in += length;
191 	if (line6pcm->bytes_in >= line6pcm->period_in) {
192 		line6pcm->bytes_in %= line6pcm->period_in;
193 		snd_pcm_period_elapsed(substream);
194 	}
195 }
196 
197 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
198 {
199 	kfree(line6pcm->buffer_in);
200 	line6pcm->buffer_in = NULL;
201 }
202 
203 /*
204  * Callback for completed capture URB.
205  */
206 static void audio_in_callback(struct urb *urb)
207 {
208 	int i, index, length = 0, shutdown = 0;
209 	unsigned long flags;
210 
211 	struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
212 
213 	line6pcm->last_frame_in = urb->start_frame;
214 
215 	/* find index of URB */
216 	for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
217 		if (urb == line6pcm->urb_audio_in[index])
218 			break;
219 
220 	spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
221 
222 	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
223 		char *fbuf;
224 		int fsize;
225 		struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
226 
227 		if (fin->status == -EXDEV) {
228 			shutdown = 1;
229 			break;
230 		}
231 
232 		fbuf = urb->transfer_buffer + fin->offset;
233 		fsize = fin->actual_length;
234 
235 		if (fsize > line6pcm->max_packet_size) {
236 			dev_err(line6pcm->line6->ifcdev,
237 				"driver and/or device bug: packet too large (%d > %d)\n",
238 				fsize, line6pcm->max_packet_size);
239 		}
240 
241 		length += fsize;
242 
243 		/* the following assumes LINE6_ISO_PACKETS == 1: */
244 		line6pcm->prev_fbuf = fbuf;
245 		line6pcm->prev_fsize = fsize;
246 
247 		if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
248 			if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
249 				     &line6pcm->flags) && (fsize > 0))
250 				line6_capture_copy(line6pcm, fbuf, fsize);
251 	}
252 
253 	clear_bit(index, &line6pcm->active_urb_in);
254 
255 	if (test_and_clear_bit(index, &line6pcm->unlink_urb_in))
256 		shutdown = 1;
257 
258 	spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
259 
260 	if (!shutdown) {
261 		submit_audio_in_urb(line6pcm);
262 
263 		if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
264 			if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
265 				     &line6pcm->flags))
266 				line6_capture_check_period(line6pcm, length);
267 	}
268 }
269 
270 /* open capture callback */
271 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
272 {
273 	int err;
274 	struct snd_pcm_runtime *runtime = substream->runtime;
275 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
276 
277 	err = snd_pcm_hw_constraint_ratdens(runtime, 0,
278 					    SNDRV_PCM_HW_PARAM_RATE,
279 					    (&line6pcm->
280 					     properties->snd_line6_rates));
281 	if (err < 0)
282 		return err;
283 
284 	runtime->hw = line6pcm->properties->snd_line6_capture_hw;
285 	return 0;
286 }
287 
288 /* close capture callback */
289 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
290 {
291 	return 0;
292 }
293 
294 /* hw_params capture callback */
295 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
296 				       struct snd_pcm_hw_params *hw_params)
297 {
298 	int ret;
299 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
300 
301 	/* -- Florian Demski [FD] */
302 	/* don't ask me why, but this fixes the bug on my machine */
303 	if (line6pcm == NULL) {
304 		if (substream->pcm == NULL)
305 			return -ENOMEM;
306 		if (substream->pcm->private_data == NULL)
307 			return -ENOMEM;
308 		substream->private_data = substream->pcm->private_data;
309 		line6pcm = snd_pcm_substream_chip(substream);
310 	}
311 	/* -- [FD] end */
312 
313 	ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
314 
315 	if (ret < 0)
316 		return ret;
317 
318 	ret = snd_pcm_lib_malloc_pages(substream,
319 				       params_buffer_bytes(hw_params));
320 	if (ret < 0) {
321 		line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
322 		return ret;
323 	}
324 
325 	line6pcm->period_in = params_period_bytes(hw_params);
326 	return 0;
327 }
328 
329 /* hw_free capture callback */
330 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
331 {
332 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
333 
334 	line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
335 	return snd_pcm_lib_free_pages(substream);
336 }
337 
338 /* trigger callback */
339 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
340 {
341 	int err;
342 
343 	switch (cmd) {
344 	case SNDRV_PCM_TRIGGER_START:
345 	case SNDRV_PCM_TRIGGER_RESUME:
346 		err = line6_pcm_acquire(line6pcm,
347 					LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
348 
349 		if (err < 0)
350 			return err;
351 
352 		break;
353 
354 	case SNDRV_PCM_TRIGGER_STOP:
355 	case SNDRV_PCM_TRIGGER_SUSPEND:
356 		err = line6_pcm_release(line6pcm,
357 					LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
358 
359 		if (err < 0)
360 			return err;
361 
362 		break;
363 
364 	default:
365 		return -EINVAL;
366 	}
367 
368 	return 0;
369 }
370 
371 /* capture pointer callback */
372 static snd_pcm_uframes_t
373 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
374 {
375 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
376 
377 	return line6pcm->pos_in_done;
378 }
379 
380 /* capture operators */
381 struct snd_pcm_ops snd_line6_capture_ops = {
382 	.open = snd_line6_capture_open,
383 	.close = snd_line6_capture_close,
384 	.ioctl = snd_pcm_lib_ioctl,
385 	.hw_params = snd_line6_capture_hw_params,
386 	.hw_free = snd_line6_capture_hw_free,
387 	.prepare = snd_line6_prepare,
388 	.trigger = snd_line6_trigger,
389 	.pointer = snd_line6_capture_pointer,
390 };
391 
392 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
393 {
394 	struct usb_line6 *line6 = line6pcm->line6;
395 	int i;
396 
397 	/* create audio URBs and fill in constant values: */
398 	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
399 		struct urb *urb;
400 
401 		/* URB for audio in: */
402 		urb = line6pcm->urb_audio_in[i] =
403 		    usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
404 
405 		if (urb == NULL)
406 			return -ENOMEM;
407 
408 		urb->dev = line6->usbdev;
409 		urb->pipe =
410 		    usb_rcvisocpipe(line6->usbdev,
411 				    line6->properties->ep_audio_r &
412 				    USB_ENDPOINT_NUMBER_MASK);
413 		urb->transfer_flags = URB_ISO_ASAP;
414 		urb->start_frame = -1;
415 		urb->number_of_packets = LINE6_ISO_PACKETS;
416 		urb->interval = LINE6_ISO_INTERVAL;
417 		urb->error_count = 0;
418 		urb->complete = audio_in_callback;
419 	}
420 
421 	return 0;
422 }
423