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*/
26static 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*/
76int 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*/
92void 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
101				usb_unlink_urb(u);
102			}
103		}
104	}
105}
106
107/*
108	Wait until unlinking of all currently active capture URBs has been
109	finished.
110*/
111void line6_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
112{
113	int timeout = HZ;
114	unsigned int i;
115	int alive;
116
117	do {
118		alive = 0;
119		for (i = LINE6_ISO_BUFFERS; i--;) {
120			if (test_bit(i, &line6pcm->active_urb_in))
121				alive++;
122		}
123		if (!alive)
124			break;
125		set_current_state(TASK_UNINTERRUPTIBLE);
126		schedule_timeout(1);
127	} while (--timeout > 0);
128	if (alive)
129		snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
130}
131
132/*
133	Unlink all currently active capture URBs, and wait for finishing.
134*/
135void line6_unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
136{
137	line6_unlink_audio_in_urbs(line6pcm);
138	line6_wait_clear_audio_in_urbs(line6pcm);
139}
140
141/*
142	Copy data into ALSA capture buffer.
143*/
144void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
145{
146	struct snd_pcm_substream *substream =
147	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
148	struct snd_pcm_runtime *runtime = substream->runtime;
149	const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
150	int frames = fsize / bytes_per_frame;
151
152	if (runtime == NULL)
153		return;
154
155	if (line6pcm->pos_in_done + frames > runtime->buffer_size) {
156		/*
157		   The transferred area goes over buffer boundary,
158		   copy two separate chunks.
159		 */
160		int len;
161
162		len = runtime->buffer_size - line6pcm->pos_in_done;
163
164		if (len > 0) {
165			memcpy(runtime->dma_area +
166			       line6pcm->pos_in_done * bytes_per_frame, fbuf,
167			       len * bytes_per_frame);
168			memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
169			       (frames - len) * bytes_per_frame);
170		} else {
171			/* this is somewhat paranoid */
172			dev_err(line6pcm->line6->ifcdev,
173				"driver bug: len = %d\n", len);
174		}
175	} else {
176		/* copy single chunk */
177		memcpy(runtime->dma_area +
178		       line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize);
179	}
180
181	line6pcm->pos_in_done += frames;
182	if (line6pcm->pos_in_done >= runtime->buffer_size)
183		line6pcm->pos_in_done -= runtime->buffer_size;
184}
185
186void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
187{
188	struct snd_pcm_substream *substream =
189	    get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
190
191	line6pcm->bytes_in += length;
192	if (line6pcm->bytes_in >= line6pcm->period_in) {
193		line6pcm->bytes_in %= line6pcm->period_in;
194		snd_pcm_period_elapsed(substream);
195	}
196}
197
198void line6_free_capture_buffer(struct snd_line6_pcm *line6pcm)
199{
200	kfree(line6pcm->buffer_in);
201	line6pcm->buffer_in = NULL;
202}
203
204/*
205 * Callback for completed capture URB.
206 */
207static void audio_in_callback(struct urb *urb)
208{
209	int i, index, length = 0, shutdown = 0;
210	unsigned long flags;
211
212	struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
213
214	line6pcm->last_frame_in = urb->start_frame;
215
216	/* find index of URB */
217	for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
218		if (urb == line6pcm->urb_audio_in[index])
219			break;
220
221	spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
222
223	for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
224		char *fbuf;
225		int fsize;
226		struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
227
228		if (fin->status == -EXDEV) {
229			shutdown = 1;
230			break;
231		}
232
233		fbuf = urb->transfer_buffer + fin->offset;
234		fsize = fin->actual_length;
235
236		if (fsize > line6pcm->max_packet_size) {
237			dev_err(line6pcm->line6->ifcdev,
238				"driver and/or device bug: packet too large (%d > %d)\n",
239				fsize, line6pcm->max_packet_size);
240		}
241
242		length += fsize;
243
244		/* the following assumes LINE6_ISO_PACKETS == 1: */
245		line6pcm->prev_fbuf = fbuf;
246		line6pcm->prev_fsize = fsize;
247
248#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
249		if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
250#endif
251			if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
252				     &line6pcm->flags) && (fsize > 0))
253				line6_capture_copy(line6pcm, fbuf, fsize);
254	}
255
256	clear_bit(index, &line6pcm->active_urb_in);
257
258	if (test_and_clear_bit(index, &line6pcm->unlink_urb_in))
259		shutdown = 1;
260
261	spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
262
263	if (!shutdown) {
264		submit_audio_in_urb(line6pcm);
265
266#ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
267		if (!(line6pcm->flags & LINE6_BITS_PCM_IMPULSE))
268#endif
269			if (test_bit(LINE6_INDEX_PCM_ALSA_CAPTURE_STREAM,
270				     &line6pcm->flags))
271				line6_capture_check_period(line6pcm, length);
272	}
273}
274
275/* open capture callback */
276static int snd_line6_capture_open(struct snd_pcm_substream *substream)
277{
278	int err;
279	struct snd_pcm_runtime *runtime = substream->runtime;
280	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
281
282	err = snd_pcm_hw_constraint_ratdens(runtime, 0,
283					    SNDRV_PCM_HW_PARAM_RATE,
284					    (&line6pcm->
285					     properties->snd_line6_rates));
286	if (err < 0)
287		return err;
288
289	runtime->hw = line6pcm->properties->snd_line6_capture_hw;
290	return 0;
291}
292
293/* close capture callback */
294static int snd_line6_capture_close(struct snd_pcm_substream *substream)
295{
296	return 0;
297}
298
299/* hw_params capture callback */
300static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
301				       struct snd_pcm_hw_params *hw_params)
302{
303	int ret;
304	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
305
306	/* -- Florian Demski [FD] */
307	/* don't ask me why, but this fixes the bug on my machine */
308	if (line6pcm == NULL) {
309		if (substream->pcm == NULL)
310			return -ENOMEM;
311		if (substream->pcm->private_data == NULL)
312			return -ENOMEM;
313		substream->private_data = substream->pcm->private_data;
314		line6pcm = snd_pcm_substream_chip(substream);
315	}
316	/* -- [FD] end */
317
318	ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
319
320	if (ret < 0)
321		return ret;
322
323	ret = snd_pcm_lib_malloc_pages(substream,
324				       params_buffer_bytes(hw_params));
325	if (ret < 0) {
326		line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
327		return ret;
328	}
329
330	line6pcm->period_in = params_period_bytes(hw_params);
331	return 0;
332}
333
334/* hw_free capture callback */
335static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
336{
337	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
338
339	line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_CAPTURE_BUFFER);
340	return snd_pcm_lib_free_pages(substream);
341}
342
343/* trigger callback */
344int snd_line6_capture_trigger(struct snd_line6_pcm *line6pcm, int cmd)
345{
346	int err;
347
348	switch (cmd) {
349	case SNDRV_PCM_TRIGGER_START:
350#ifdef CONFIG_PM
351	case SNDRV_PCM_TRIGGER_RESUME:
352#endif
353		err = line6_pcm_acquire(line6pcm,
354					LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
355
356		if (err < 0)
357			return err;
358
359		break;
360
361	case SNDRV_PCM_TRIGGER_STOP:
362#ifdef CONFIG_PM
363	case SNDRV_PCM_TRIGGER_SUSPEND:
364#endif
365		err = line6_pcm_release(line6pcm,
366					LINE6_BIT_PCM_ALSA_CAPTURE_STREAM);
367
368		if (err < 0)
369			return err;
370
371		break;
372
373	default:
374		return -EINVAL;
375	}
376
377	return 0;
378}
379
380/* capture pointer callback */
381static snd_pcm_uframes_t
382snd_line6_capture_pointer(struct snd_pcm_substream *substream)
383{
384	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
385
386	return line6pcm->pos_in_done;
387}
388
389/* capture operators */
390struct snd_pcm_ops snd_line6_capture_ops = {
391	.open = snd_line6_capture_open,
392	.close = snd_line6_capture_close,
393	.ioctl = snd_pcm_lib_ioctl,
394	.hw_params = snd_line6_capture_hw_params,
395	.hw_free = snd_line6_capture_hw_free,
396	.prepare = snd_line6_prepare,
397	.trigger = snd_line6_trigger,
398	.pointer = snd_line6_capture_pointer,
399};
400
401int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
402{
403	int i;
404
405	/* create audio URBs and fill in constant values: */
406	for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
407		struct urb *urb;
408
409		/* URB for audio in: */
410		urb = line6pcm->urb_audio_in[i] =
411		    usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
412
413		if (urb == NULL) {
414			dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
415			return -ENOMEM;
416		}
417
418		urb->dev = line6pcm->line6->usbdev;
419		urb->pipe =
420		    usb_rcvisocpipe(line6pcm->line6->usbdev,
421				    line6pcm->ep_audio_read &
422				    USB_ENDPOINT_NUMBER_MASK);
423		urb->transfer_flags = URB_ISO_ASAP;
424		urb->start_frame = -1;
425		urb->number_of_packets = LINE6_ISO_PACKETS;
426		urb->interval = LINE6_ISO_INTERVAL;
427		urb->error_count = 0;
428		urb->complete = audio_in_callback;
429	}
430
431	return 0;
432}
433