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 #include "pod.h"
22 
23 /*
24 	Find a free URB and submit it.
25 */
submit_audio_in_urb(struct snd_line6_pcm * line6pcm)26 static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
27 {
28 	int index;
29 	unsigned long flags;
30 	int i, urb_size;
31 	int ret;
32 	struct urb *urb_in;
33 
34 	spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
35 	index =
36 	    find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
37 
38 	if (index < 0 || index >= LINE6_ISO_BUFFERS) {
39 		spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
40 		dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
41 		return -EINVAL;
42 	}
43 
44 	urb_in = line6pcm->urb_audio_in[index];
45 	urb_size = 0;
46 
47 	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
48 		struct usb_iso_packet_descriptor *fin =
49 		    &urb_in->iso_frame_desc[i];
50 		fin->offset = urb_size;
51 		fin->length = line6pcm->max_packet_size;
52 		urb_size += line6pcm->max_packet_size;
53 	}
54 
55 	urb_in->transfer_buffer =
56 	    line6pcm->buffer_in +
57 	    index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
58 	urb_in->transfer_buffer_length = urb_size;
59 	urb_in->context = line6pcm;
60 
61 	ret = usb_submit_urb(urb_in, GFP_ATOMIC);
62 
63 	if (ret == 0)
64 		set_bit(index, &line6pcm->active_urb_in);
65 	else
66 		dev_err(line6pcm->line6->ifcdev,
67 			"URB in #%d submission failed (%d)\n", index, ret);
68 
69 	spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
70 	return 0;
71 }
72 
73 /*
74 	Submit all currently available capture URBs.
75 */
line6_submit_audio_in_all_urbs(struct snd_line6_pcm * line6pcm)76 int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
77 {
78 	int ret, i;
79 
80 	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
81 		ret = submit_audio_in_urb(line6pcm);
82 		if (ret < 0)
83 			return ret;
84 	}
85 
86 	return 0;
87 }
88 
89 /*
90 	Unlink all currently active capture URBs.
91 */
line6_unlink_audio_in_urbs(struct snd_line6_pcm * line6pcm)92 void line6_unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
93 {
94 	unsigned int i;
95 
96 	for (i = LINE6_ISO_BUFFERS; i--;) {
97 		if (test_bit(i, &line6pcm->active_urb_in)) {
98 			if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) {
99 				struct urb *u = line6pcm->urb_audio_in[i];
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 */
wait_clear_audio_in_urbs(struct snd_line6_pcm * line6pcm)110 static void 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 */
line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm * line6pcm)134 void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
135 {
136 	line6_unlink_audio_in_urbs(line6pcm);
137 	wait_clear_audio_in_urbs(line6pcm);
138 }
139 
140 /*
141 	Copy data into ALSA capture buffer.
142 */
line6_capture_copy(struct snd_line6_pcm * line6pcm,char * fbuf,int fsize)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 		len = runtime->buffer_size - line6pcm->pos_in_done;
161 
162 		if (len > 0) {
163 			memcpy(runtime->dma_area +
164 			       line6pcm->pos_in_done * bytes_per_frame, fbuf,
165 			       len * bytes_per_frame);
166 			memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
167 			       (frames - len) * bytes_per_frame);
168 		} else {
169 			/* this is somewhat paranoid */
170 			dev_err(line6pcm->line6->ifcdev,
171 				"driver bug: len = %d\n", len);
172 		}
173 	} else {
174 		/* copy single chunk */
175 		memcpy(runtime->dma_area +
176 		       line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize);
177 	}
178 
179 	line6pcm->pos_in_done += frames;
180 	if (line6pcm->pos_in_done >= runtime->buffer_size)
181 		line6pcm->pos_in_done -= runtime->buffer_size;
182 }
183 
line6_capture_check_period(struct snd_line6_pcm * line6pcm,int length)184 void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
185 {
186 	struct snd_pcm_substream *substream =
187 	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
188 
189 	line6pcm->bytes_in += length;
190 	if (line6pcm->bytes_in >= line6pcm->period_in) {
191 		line6pcm->bytes_in %= line6pcm->period_in;
192 		snd_pcm_period_elapsed(substream);
193 	}
194 }
195 
line6_alloc_capture_buffer(struct snd_line6_pcm * line6pcm)196 int line6_alloc_capture_buffer(struct snd_line6_pcm *line6pcm)
197 {
198 	/* We may be invoked multiple times in a row so allocate once only */
199 	if (line6pcm->buffer_in)
200 		return 0;
201 
202 	line6pcm->buffer_in =
203 		kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
204 			line6pcm->max_packet_size, GFP_KERNEL);
205 
206 	if (!line6pcm->buffer_in) {
207 		dev_err(line6pcm->line6->ifcdev,
208 			"cannot malloc capture buffer\n");
209 		return -ENOMEM;
210 	}
211 
212 	return 0;
213 }
214 
line6_free_capture_buffer(struct snd_line6_pcm * line6pcm)215 void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
216 {
217 	kfree(line6pcm->buffer_in);
218 	line6pcm->buffer_in = NULL;
219 }
220 
221 /*
222  * Callback for completed capture URB.
223  */
audio_in_callback(struct urb * urb)224 static void audio_in_callback(struct urb *urb)
225 {
226 	int i, index, length = 0, shutdown = 0;
227 	unsigned long flags;
228 
229 	struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
230 
231 	line6pcm->last_frame_in = urb->start_frame;
232 
233 	/* find index of URB */
234 	for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
235 		if (urb == line6pcm->urb_audio_in[index])
236 			break;
237 
238 #ifdef CONFIG_LINE6_USB_DUMP_PCM
239 	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
240 		struct usb_iso_packet_descriptor *fout =
241 		    &urb->iso_frame_desc[i];
242 		line6_write_hexdump(line6pcm->line6, 'C',
243 				    urb->transfer_buffer + fout->offset,
244 				    fout->length);
245 	}
246 #endif
247 
248 	spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
249 
250 	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
251 		char *fbuf;
252 		int fsize;
253 		struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
254 
255 		if (fin->status == -EXDEV) {
256 			shutdown = 1;
257 			break;
258 		}
259 
260 		fbuf = urb->transfer_buffer + fin->offset;
261 		fsize = fin->actual_length;
262 
263 		if (fsize > line6pcm->max_packet_size) {
264 			dev_err(line6pcm->line6->ifcdev,
265 				"driver and/or device bug: packet too large (%d > %d)\n",
266 				fsize, line6pcm->max_packet_size);
267 		}
268 
269 		length += fsize;
270 
271 		/* the following assumes LINE6_ISO_PACKETS == 1: */
272 		line6pcm->prev_fbuf = fbuf;
273 		line6pcm->prev_fsize = fsize;
274 
275 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
276 		if (!(line6pcm->flags & MASK_PCM_IMPULSE))
277 #endif
278 			if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags)
279 			    && (fsize > 0))
280 				line6_capture_copy(line6pcm, fbuf, fsize);
281 	}
282 
283 	clear_bit(index, &line6pcm->active_urb_in);
284 
285 	if (test_and_clear_bit(index, &line6pcm->unlink_urb_in))
286 		shutdown = 1;
287 
288 	spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
289 
290 	if (!shutdown) {
291 		submit_audio_in_urb(line6pcm);
292 
293 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
294 		if (!(line6pcm->flags & MASK_PCM_IMPULSE))
295 #endif
296 			if (test_bit(BIT_PCM_ALSA_CAPTURE, &line6pcm->flags))
297 				line6_capture_check_period(line6pcm, length);
298 	}
299 }
300 
301 /* open capture callback */
snd_line6_capture_open(struct snd_pcm_substream * substream)302 static int snd_line6_capture_open(struct snd_pcm_substream *substream)
303 {
304 	int err;
305 	struct snd_pcm_runtime *runtime = substream->runtime;
306 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
307 
308 	err = snd_pcm_hw_constraint_ratdens(runtime, 0,
309 					    SNDRV_PCM_HW_PARAM_RATE,
310 					    (&line6pcm->
311 					     properties->snd_line6_rates));
312 	if (err < 0)
313 		return err;
314 
315 	runtime->hw = line6pcm->properties->snd_line6_capture_hw;
316 	return 0;
317 }
318 
319 /* close capture callback */
snd_line6_capture_close(struct snd_pcm_substream * substream)320 static int snd_line6_capture_close(struct snd_pcm_substream *substream)
321 {
322 	return 0;
323 }
324 
325 /* hw_params capture callback */
snd_line6_capture_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)326 static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
327 				       struct snd_pcm_hw_params *hw_params)
328 {
329 	int ret;
330 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
331 
332 	/* -- Florian Demski [FD] */
333 	/* don't ask me why, but this fixes the bug on my machine */
334 	if (line6pcm == NULL) {
335 		if (substream->pcm == NULL)
336 			return -ENOMEM;
337 		if (substream->pcm->private_data == NULL)
338 			return -ENOMEM;
339 		substream->private_data = substream->pcm->private_data;
340 		line6pcm = snd_pcm_substream_chip(substream);
341 	}
342 	/* -- [FD] end */
343 
344 	if ((line6pcm->flags & MASK_CAPTURE) == 0) {
345 		ret = line6_alloc_capture_buffer(line6pcm);
346 
347 		if (ret < 0)
348 			return ret;
349 	}
350 
351 	ret = snd_pcm_lib_malloc_pages(substream,
352 				       params_buffer_bytes(hw_params));
353 	if (ret < 0)
354 		return ret;
355 
356 	line6pcm->period_in = params_period_bytes(hw_params);
357 	return 0;
358 }
359 
360 /* hw_free capture callback */
snd_line6_capture_hw_free(struct snd_pcm_substream * substream)361 static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
362 {
363 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
364 
365 	if ((line6pcm->flags & MASK_CAPTURE) == 0) {
366 		line6_unlink_wait_clear_audio_in_urbs(line6pcm);
367 		line6_free_capture_buffer(line6pcm);
368 	}
369 
370 	return snd_pcm_lib_free_pages(substream);
371 }
372 
373 /* trigger callback */
snd_line6_capture_trigger(struct snd_line6_pcm * line6pcm,int cmd)374 int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
375 {
376 	int err;
377 
378 	switch (cmd) {
379 	case SNDRV_PCM_TRIGGER_START:
380 #ifdef CONFIG_PM
381 	case SNDRV_PCM_TRIGGER_RESUME:
382 #endif
383 		err = line6_pcm_start(line6pcm, MASK_PCM_ALSA_CAPTURE);
384 
385 		if (err < 0)
386 			return err;
387 
388 		break;
389 
390 	case SNDRV_PCM_TRIGGER_STOP:
391 #ifdef CONFIG_PM
392 	case SNDRV_PCM_TRIGGER_SUSPEND:
393 #endif
394 		err = line6_pcm_stop(line6pcm, MASK_PCM_ALSA_CAPTURE);
395 
396 		if (err < 0)
397 			return err;
398 
399 		break;
400 
401 	default:
402 		return -EINVAL;
403 	}
404 
405 	return 0;
406 }
407 
408 /* capture pointer callback */
409 static snd_pcm_uframes_t
snd_line6_capture_pointer(struct snd_pcm_substream * substream)410 snd_line6_capture_pointer(struct snd_pcm_substream *substream)
411 {
412 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
413 	return line6pcm->pos_in_done;
414 }
415 
416 /* capture operators */
417 struct snd_pcm_ops snd_line6_capture_ops = {
418 	.open = snd_line6_capture_open,
419 	.close = snd_line6_capture_close,
420 	.ioctl = snd_pcm_lib_ioctl,
421 	.hw_params = snd_line6_capture_hw_params,
422 	.hw_free = snd_line6_capture_hw_free,
423 	.prepare = snd_line6_prepare,
424 	.trigger = snd_line6_trigger,
425 	.pointer = snd_line6_capture_pointer,
426 };
427 
line6_create_audio_in_urbs(struct snd_line6_pcm * line6pcm)428 int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
429 {
430 	int i;
431 
432 	/* create audio URBs and fill in constant values: */
433 	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
434 		struct urb *urb;
435 
436 		/* URB for audio in: */
437 		urb = line6pcm->urb_audio_in[i] =
438 		    usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
439 
440 		if (urb == NULL) {
441 			dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
442 			return -ENOMEM;
443 		}
444 
445 		urb->dev = line6pcm->line6->usbdev;
446 		urb->pipe =
447 		    usb_rcvisocpipe(line6pcm->line6->usbdev,
448 				    line6pcm->ep_audio_read &
449 				    USB_ENDPOINT_NUMBER_MASK);
450 		urb->transfer_flags = URB_ISO_ASAP;
451 		urb->start_frame = -1;
452 		urb->number_of_packets = LINE6_ISO_PACKETS;
453 		urb->interval = LINE6_ISO_INTERVAL;
454 		urb->error_count = 0;
455 		urb->complete = audio_in_callback;
456 	}
457 
458 	return 0;
459 }
460