xref: /linux/sound/usb/line6/pcm.c (revision 122fe6e9)
1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
261864d84STakashi Iwai /*
3c078a4aaSChris Rorvick  * Line 6 Linux USB driver
461864d84STakashi Iwai  *
5*122fe6e9SMarkus Grabner  * Copyright (C) 2004-2010 Markus Grabner (line6@grabner-graz.at)
661864d84STakashi Iwai  */
761864d84STakashi Iwai 
861864d84STakashi Iwai #include <linux/slab.h>
9ccddbe4aSTakashi Iwai #include <linux/export.h>
1061864d84STakashi Iwai #include <sound/core.h>
1161864d84STakashi Iwai #include <sound/control.h>
1261864d84STakashi Iwai #include <sound/pcm.h>
1361864d84STakashi Iwai #include <sound/pcm_params.h>
1461864d84STakashi Iwai 
1561864d84STakashi Iwai #include "capture.h"
1661864d84STakashi Iwai #include "driver.h"
1761864d84STakashi Iwai #include "playback.h"
1861864d84STakashi Iwai 
19075587b7STakashi Iwai /* impulse response volume controls */
snd_line6_impulse_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)20075587b7STakashi Iwai static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
21075587b7STakashi Iwai 					 struct snd_ctl_elem_info *uinfo)
2261864d84STakashi Iwai {
23075587b7STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
24075587b7STakashi Iwai 	uinfo->count = 1;
25075587b7STakashi Iwai 	uinfo->value.integer.min = 0;
26075587b7STakashi Iwai 	uinfo->value.integer.max = 255;
27075587b7STakashi Iwai 	return 0;
2861864d84STakashi Iwai }
2961864d84STakashi Iwai 
snd_line6_impulse_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)30075587b7STakashi Iwai static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
31075587b7STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
3261864d84STakashi Iwai {
33075587b7STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
34075587b7STakashi Iwai 
35075587b7STakashi Iwai 	ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
36075587b7STakashi Iwai 	return 0;
3761864d84STakashi Iwai }
3861864d84STakashi Iwai 
snd_line6_impulse_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)39075587b7STakashi Iwai static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
40075587b7STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
4161864d84STakashi Iwai {
42075587b7STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
43075587b7STakashi Iwai 	int value = ucontrol->value.integer.value[0];
44247d95eeSTakashi Iwai 	int err;
4561864d84STakashi Iwai 
46075587b7STakashi Iwai 	if (line6pcm->impulse_volume == value)
47075587b7STakashi Iwai 		return 0;
4861864d84STakashi Iwai 
4961864d84STakashi Iwai 	line6pcm->impulse_volume = value;
50247d95eeSTakashi Iwai 	if (value > 0) {
51f56742ccSAndrej Krutak 		err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE, true);
52247d95eeSTakashi Iwai 		if (err < 0) {
53247d95eeSTakashi Iwai 			line6pcm->impulse_volume = 0;
54247d95eeSTakashi Iwai 			return err;
55247d95eeSTakashi Iwai 		}
56247d95eeSTakashi Iwai 	} else {
57247d95eeSTakashi Iwai 		line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE);
58247d95eeSTakashi Iwai 	}
59075587b7STakashi Iwai 	return 1;
6061864d84STakashi Iwai }
6161864d84STakashi Iwai 
62075587b7STakashi Iwai /* impulse response period controls */
snd_line6_impulse_period_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)63075587b7STakashi Iwai static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
64075587b7STakashi Iwai 					 struct snd_ctl_elem_info *uinfo)
6561864d84STakashi Iwai {
66075587b7STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
67075587b7STakashi Iwai 	uinfo->count = 1;
68075587b7STakashi Iwai 	uinfo->value.integer.min = 0;
69075587b7STakashi Iwai 	uinfo->value.integer.max = 2000;
70075587b7STakashi Iwai 	return 0;
7161864d84STakashi Iwai }
7261864d84STakashi Iwai 
snd_line6_impulse_period_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)73075587b7STakashi Iwai static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
74075587b7STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
7561864d84STakashi Iwai {
76075587b7STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
7761864d84STakashi Iwai 
78075587b7STakashi Iwai 	ucontrol->value.integer.value[0] = line6pcm->impulse_period;
79075587b7STakashi Iwai 	return 0;
8061864d84STakashi Iwai }
8161864d84STakashi Iwai 
snd_line6_impulse_period_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)82075587b7STakashi Iwai static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
83075587b7STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
84075587b7STakashi Iwai {
85075587b7STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
86075587b7STakashi Iwai 	int value = ucontrol->value.integer.value[0];
87075587b7STakashi Iwai 
88075587b7STakashi Iwai 	if (line6pcm->impulse_period == value)
89075587b7STakashi Iwai 		return 0;
90075587b7STakashi Iwai 
91075587b7STakashi Iwai 	line6pcm->impulse_period = value;
92075587b7STakashi Iwai 	return 1;
93075587b7STakashi Iwai }
9461864d84STakashi Iwai 
95d8131e67STakashi Iwai /*
96d8131e67STakashi Iwai 	Unlink all currently active URBs.
97d8131e67STakashi Iwai */
line6_unlink_audio_urbs(struct snd_line6_pcm * line6pcm,struct line6_pcm_stream * pcms)98d8131e67STakashi Iwai static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
99d8131e67STakashi Iwai 				    struct line6_pcm_stream *pcms)
100d8131e67STakashi Iwai {
101d8131e67STakashi Iwai 	int i;
102d8131e67STakashi Iwai 
103b2233d97SAndrej Krutak 	for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
104d8131e67STakashi Iwai 		if (test_bit(i, &pcms->active_urbs)) {
105d8131e67STakashi Iwai 			if (!test_and_set_bit(i, &pcms->unlink_urbs))
106d8131e67STakashi Iwai 				usb_unlink_urb(pcms->urbs[i]);
107d8131e67STakashi Iwai 		}
108d8131e67STakashi Iwai 	}
109d8131e67STakashi Iwai }
110d8131e67STakashi Iwai 
111d8131e67STakashi Iwai /*
112d8131e67STakashi Iwai 	Wait until unlinking of all currently active URBs has been finished.
113d8131e67STakashi Iwai */
line6_wait_clear_audio_urbs(struct snd_line6_pcm * line6pcm,struct line6_pcm_stream * pcms)114d8131e67STakashi Iwai static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
115d8131e67STakashi Iwai 					struct line6_pcm_stream *pcms)
116d8131e67STakashi Iwai {
117d8131e67STakashi Iwai 	int timeout = HZ;
118d8131e67STakashi Iwai 	int i;
119d8131e67STakashi Iwai 	int alive;
120d8131e67STakashi Iwai 
121d8131e67STakashi Iwai 	do {
122d8131e67STakashi Iwai 		alive = 0;
123b2233d97SAndrej Krutak 		for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
124d8131e67STakashi Iwai 			if (test_bit(i, &pcms->active_urbs))
125d8131e67STakashi Iwai 				alive++;
126d8131e67STakashi Iwai 		}
127d8131e67STakashi Iwai 		if (!alive)
128d8131e67STakashi Iwai 			break;
129d8131e67STakashi Iwai 		set_current_state(TASK_UNINTERRUPTIBLE);
130d8131e67STakashi Iwai 		schedule_timeout(1);
131d8131e67STakashi Iwai 	} while (--timeout > 0);
132d8131e67STakashi Iwai 	if (alive)
133ccaac9edSTakashi Iwai 		dev_err(line6pcm->line6->ifcdev,
134ccaac9edSTakashi Iwai 			"timeout: still %d active urbs..\n", alive);
135d8131e67STakashi Iwai }
136d8131e67STakashi Iwai 
13763e20df1STakashi Iwai static inline struct line6_pcm_stream *
get_stream(struct snd_line6_pcm * line6pcm,int direction)13863e20df1STakashi Iwai get_stream(struct snd_line6_pcm *line6pcm, int direction)
13963e20df1STakashi Iwai {
14063e20df1STakashi Iwai 	return (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
14163e20df1STakashi Iwai 		&line6pcm->out : &line6pcm->in;
14263e20df1STakashi Iwai }
14363e20df1STakashi Iwai 
14463e20df1STakashi Iwai /* allocate a buffer if not opened yet;
1457a0f55aeSAndrej Krutak  * call this in line6pcm.state_mutex
14663e20df1STakashi Iwai  */
line6_buffer_acquire(struct snd_line6_pcm * line6pcm,struct line6_pcm_stream * pstr,int direction,int type)14763e20df1STakashi Iwai static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm,
1487a0f55aeSAndrej Krutak 				struct line6_pcm_stream *pstr, int direction, int type)
149e90576c5STakashi Iwai {
1507a0f55aeSAndrej Krutak 	const int pkt_size =
1517a0f55aeSAndrej Krutak 		(direction == SNDRV_PCM_STREAM_PLAYBACK) ?
1527a0f55aeSAndrej Krutak 			line6pcm->max_packet_size_out :
1537a0f55aeSAndrej Krutak 			line6pcm->max_packet_size_in;
1547a0f55aeSAndrej Krutak 
155e90576c5STakashi Iwai 	/* Invoked multiple times in a row so allocate once only */
15663e20df1STakashi Iwai 	if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) {
1576da2ec56SKees Cook 		pstr->buffer =
1586da2ec56SKees Cook 			kmalloc(array3_size(line6pcm->line6->iso_buffers,
1596da2ec56SKees Cook 					    LINE6_ISO_PACKETS, pkt_size),
1606da2ec56SKees Cook 				GFP_KERNEL);
16163e20df1STakashi Iwai 		if (!pstr->buffer)
162e90576c5STakashi Iwai 			return -ENOMEM;
16363e20df1STakashi Iwai 	}
164e90576c5STakashi Iwai 	return 0;
165e90576c5STakashi Iwai }
166e90576c5STakashi Iwai 
16763e20df1STakashi Iwai /* free a buffer if all streams are closed;
1687a0f55aeSAndrej Krutak  * call this in line6pcm.state_mutex
16961864d84STakashi Iwai  */
line6_buffer_release(struct snd_line6_pcm * line6pcm,struct line6_pcm_stream * pstr,int type)17063e20df1STakashi Iwai static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
17163e20df1STakashi Iwai 				 struct line6_pcm_stream *pstr, int type)
17261864d84STakashi Iwai {
17363e20df1STakashi Iwai 	clear_bit(type, &pstr->opened);
17463e20df1STakashi Iwai 	if (!pstr->opened) {
17563e20df1STakashi Iwai 		line6_wait_clear_audio_urbs(line6pcm, pstr);
17663e20df1STakashi Iwai 		kfree(pstr->buffer);
17763e20df1STakashi Iwai 		pstr->buffer = NULL;
17863e20df1STakashi Iwai 	}
17963e20df1STakashi Iwai }
18061864d84STakashi Iwai 
18163e20df1STakashi Iwai /* start a PCM stream */
line6_stream_start(struct snd_line6_pcm * line6pcm,int direction,int type)18263e20df1STakashi Iwai static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction,
18363e20df1STakashi Iwai 			      int type)
18463e20df1STakashi Iwai {
18563e20df1STakashi Iwai 	unsigned long flags;
18663e20df1STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
18763e20df1STakashi Iwai 	int ret = 0;
18863e20df1STakashi Iwai 
18963e20df1STakashi Iwai 	spin_lock_irqsave(&pstr->lock, flags);
1904d0e6775STakashi Iwai 	if (!test_and_set_bit(type, &pstr->running) &&
1914d0e6775STakashi Iwai 	    !(pstr->active_urbs || pstr->unlink_urbs)) {
19263e20df1STakashi Iwai 		pstr->count = 0;
19363e20df1STakashi Iwai 		/* Submit all currently available URBs */
19463e20df1STakashi Iwai 		if (direction == SNDRV_PCM_STREAM_PLAYBACK)
19563e20df1STakashi Iwai 			ret = line6_submit_audio_out_all_urbs(line6pcm);
19663e20df1STakashi Iwai 		else
19763e20df1STakashi Iwai 			ret = line6_submit_audio_in_all_urbs(line6pcm);
19863e20df1STakashi Iwai 	}
1997a0f55aeSAndrej Krutak 
20063e20df1STakashi Iwai 	if (ret < 0)
20163e20df1STakashi Iwai 		clear_bit(type, &pstr->running);
20263e20df1STakashi Iwai 	spin_unlock_irqrestore(&pstr->lock, flags);
20363e20df1STakashi Iwai 	return ret;
20463e20df1STakashi Iwai }
20563e20df1STakashi Iwai 
20663e20df1STakashi Iwai /* stop a PCM stream; this doesn't sync with the unlinked URBs */
line6_stream_stop(struct snd_line6_pcm * line6pcm,int direction,int type)20763e20df1STakashi Iwai static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
20863e20df1STakashi Iwai 			  int type)
20963e20df1STakashi Iwai {
21063e20df1STakashi Iwai 	unsigned long flags;
21163e20df1STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
21263e20df1STakashi Iwai 
21363e20df1STakashi Iwai 	spin_lock_irqsave(&pstr->lock, flags);
21463e20df1STakashi Iwai 	clear_bit(type, &pstr->running);
21563e20df1STakashi Iwai 	if (!pstr->running) {
216adc8a43aSAndrej Krutak 		spin_unlock_irqrestore(&pstr->lock, flags);
21763e20df1STakashi Iwai 		line6_unlink_audio_urbs(line6pcm, pstr);
218adc8a43aSAndrej Krutak 		spin_lock_irqsave(&pstr->lock, flags);
21963e20df1STakashi Iwai 		if (direction == SNDRV_PCM_STREAM_CAPTURE) {
220f2bb614bSTakashi Iwai 			line6pcm->prev_fbuf = NULL;
221f2bb614bSTakashi Iwai 			line6pcm->prev_fsize = 0;
222f2bb614bSTakashi Iwai 		}
22363e20df1STakashi Iwai 	}
22463e20df1STakashi Iwai 	spin_unlock_irqrestore(&pstr->lock, flags);
22561864d84STakashi Iwai }
22661864d84STakashi Iwai 
22763e20df1STakashi Iwai /* common PCM trigger callback */
snd_line6_trigger(struct snd_pcm_substream * substream,int cmd)22861864d84STakashi Iwai int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
22961864d84STakashi Iwai {
23061864d84STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
23161864d84STakashi Iwai 	struct snd_pcm_substream *s;
23263e20df1STakashi Iwai 	int err;
23361864d84STakashi Iwai 
23463e20df1STakashi Iwai 	clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
23561864d84STakashi Iwai 
23661864d84STakashi Iwai 	snd_pcm_group_for_each_entry(s, substream) {
2377d70c81cSTakashi Iwai 		if (s->pcm->card != substream->pcm->card)
2387d70c81cSTakashi Iwai 			continue;
23963e20df1STakashi Iwai 
24063e20df1STakashi Iwai 		switch (cmd) {
24163e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_START:
24263e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_RESUME:
243f56742ccSAndrej Krutak 			if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
244f56742ccSAndrej Krutak 				(line6pcm->line6->properties->capabilities &
245f56742ccSAndrej Krutak 					LINE6_CAP_IN_NEEDS_OUT)) {
246f56742ccSAndrej Krutak 				err = line6_stream_start(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
247f56742ccSAndrej Krutak 						 LINE6_STREAM_CAPTURE_HELPER);
248f56742ccSAndrej Krutak 				if (err < 0)
249f56742ccSAndrej Krutak 					return err;
250f56742ccSAndrej Krutak 			}
25163e20df1STakashi Iwai 			err = line6_stream_start(line6pcm, s->stream,
25263e20df1STakashi Iwai 						 LINE6_STREAM_PCM);
25363e20df1STakashi Iwai 			if (err < 0)
25463e20df1STakashi Iwai 				return err;
25561864d84STakashi Iwai 			break;
25661864d84STakashi Iwai 
25763e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_STOP:
25863e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_SUSPEND:
259f56742ccSAndrej Krutak 			if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
260f56742ccSAndrej Krutak 				(line6pcm->line6->properties->capabilities &
261f56742ccSAndrej Krutak 					LINE6_CAP_IN_NEEDS_OUT)) {
262f56742ccSAndrej Krutak 				line6_stream_stop(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
263f56742ccSAndrej Krutak 					  LINE6_STREAM_CAPTURE_HELPER);
264f56742ccSAndrej Krutak 			}
26563e20df1STakashi Iwai 			line6_stream_stop(line6pcm, s->stream,
26663e20df1STakashi Iwai 					  LINE6_STREAM_PCM);
26763e20df1STakashi Iwai 			break;
26863e20df1STakashi Iwai 
26963e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
27063e20df1STakashi Iwai 			if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
27163e20df1STakashi Iwai 				return -EINVAL;
27263e20df1STakashi Iwai 			set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
27363e20df1STakashi Iwai 			break;
27463e20df1STakashi Iwai 
27563e20df1STakashi Iwai 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
27663e20df1STakashi Iwai 			if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
27763e20df1STakashi Iwai 				return -EINVAL;
27863e20df1STakashi Iwai 			clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
27961864d84STakashi Iwai 			break;
28061864d84STakashi Iwai 
28161864d84STakashi Iwai 		default:
28263e20df1STakashi Iwai 			return -EINVAL;
28361864d84STakashi Iwai 		}
28461864d84STakashi Iwai 	}
28561864d84STakashi Iwai 
28663e20df1STakashi Iwai 	return 0;
28761864d84STakashi Iwai }
28861864d84STakashi Iwai 
2892954f914STakashi Iwai /* common PCM pointer callback */
snd_line6_pointer(struct snd_pcm_substream * substream)2902954f914STakashi Iwai snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
2912954f914STakashi Iwai {
2922954f914STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
2932954f914STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
2942954f914STakashi Iwai 
2952954f914STakashi Iwai 	return pstr->pos_done;
2962954f914STakashi Iwai }
2972954f914STakashi Iwai 
298f56742ccSAndrej Krutak /* Acquire and optionally start duplex streams:
29963e20df1STakashi Iwai  * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
30063e20df1STakashi Iwai  */
line6_pcm_acquire(struct snd_line6_pcm * line6pcm,int type,bool start)301f56742ccSAndrej Krutak int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
30263e20df1STakashi Iwai {
30363e20df1STakashi Iwai 	struct line6_pcm_stream *pstr;
30463e20df1STakashi Iwai 	int ret = 0, dir;
30563e20df1STakashi Iwai 
306f56742ccSAndrej Krutak 	/* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
30763e20df1STakashi Iwai 	mutex_lock(&line6pcm->state_mutex);
30863e20df1STakashi Iwai 	for (dir = 0; dir < 2; dir++) {
30963e20df1STakashi Iwai 		pstr = get_stream(line6pcm, dir);
3107a0f55aeSAndrej Krutak 		ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
31163e20df1STakashi Iwai 		if (ret < 0)
31263e20df1STakashi Iwai 			goto error;
31363e20df1STakashi Iwai 		if (!pstr->running)
31463e20df1STakashi Iwai 			line6_wait_clear_audio_urbs(line6pcm, pstr);
31563e20df1STakashi Iwai 	}
316f56742ccSAndrej Krutak 	if (start) {
31763e20df1STakashi Iwai 		for (dir = 0; dir < 2; dir++) {
31863e20df1STakashi Iwai 			ret = line6_stream_start(line6pcm, dir, type);
31963e20df1STakashi Iwai 			if (ret < 0)
32063e20df1STakashi Iwai 				goto error;
32163e20df1STakashi Iwai 		}
322f56742ccSAndrej Krutak 	}
32363e20df1STakashi Iwai  error:
32463e20df1STakashi Iwai 	mutex_unlock(&line6pcm->state_mutex);
32563e20df1STakashi Iwai 	if (ret < 0)
32663e20df1STakashi Iwai 		line6_pcm_release(line6pcm, type);
32763e20df1STakashi Iwai 	return ret;
32863e20df1STakashi Iwai }
32963e20df1STakashi Iwai EXPORT_SYMBOL_GPL(line6_pcm_acquire);
33063e20df1STakashi Iwai 
33163e20df1STakashi Iwai /* Stop and release duplex streams */
line6_pcm_release(struct snd_line6_pcm * line6pcm,int type)33263e20df1STakashi Iwai void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
33363e20df1STakashi Iwai {
33463e20df1STakashi Iwai 	struct line6_pcm_stream *pstr;
33563e20df1STakashi Iwai 	int dir;
33663e20df1STakashi Iwai 
33763e20df1STakashi Iwai 	mutex_lock(&line6pcm->state_mutex);
33863e20df1STakashi Iwai 	for (dir = 0; dir < 2; dir++)
33963e20df1STakashi Iwai 		line6_stream_stop(line6pcm, dir, type);
34063e20df1STakashi Iwai 	for (dir = 0; dir < 2; dir++) {
34163e20df1STakashi Iwai 		pstr = get_stream(line6pcm, dir);
34263e20df1STakashi Iwai 		line6_buffer_release(line6pcm, pstr, type);
34363e20df1STakashi Iwai 	}
34463e20df1STakashi Iwai 	mutex_unlock(&line6pcm->state_mutex);
34563e20df1STakashi Iwai }
34663e20df1STakashi Iwai EXPORT_SYMBOL_GPL(line6_pcm_release);
34763e20df1STakashi Iwai 
34863e20df1STakashi Iwai /* common PCM hw_params callback */
snd_line6_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)34963e20df1STakashi Iwai int snd_line6_hw_params(struct snd_pcm_substream *substream,
35063e20df1STakashi Iwai 			struct snd_pcm_hw_params *hw_params)
35163e20df1STakashi Iwai {
35263e20df1STakashi Iwai 	int ret;
35363e20df1STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
35463e20df1STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
35563e20df1STakashi Iwai 
35663e20df1STakashi Iwai 	mutex_lock(&line6pcm->state_mutex);
3577a0f55aeSAndrej Krutak 	ret = line6_buffer_acquire(line6pcm, pstr, substream->stream,
3587a0f55aeSAndrej Krutak 	                           LINE6_STREAM_PCM);
35963e20df1STakashi Iwai 	if (ret < 0)
36063e20df1STakashi Iwai 		goto error;
36163e20df1STakashi Iwai 
36263e20df1STakashi Iwai 	pstr->period = params_period_bytes(hw_params);
36363e20df1STakashi Iwai  error:
36463e20df1STakashi Iwai 	mutex_unlock(&line6pcm->state_mutex);
36563e20df1STakashi Iwai 	return ret;
36663e20df1STakashi Iwai }
36763e20df1STakashi Iwai 
36863e20df1STakashi Iwai /* common PCM hw_free callback */
snd_line6_hw_free(struct snd_pcm_substream * substream)36963e20df1STakashi Iwai int snd_line6_hw_free(struct snd_pcm_substream *substream)
37063e20df1STakashi Iwai {
37163e20df1STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
37263e20df1STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
37363e20df1STakashi Iwai 
37463e20df1STakashi Iwai 	mutex_lock(&line6pcm->state_mutex);
37563e20df1STakashi Iwai 	line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
37663e20df1STakashi Iwai 	mutex_unlock(&line6pcm->state_mutex);
377b9626bd6STakashi Iwai 	return 0;
37863e20df1STakashi Iwai }
37963e20df1STakashi Iwai 
38063e20df1STakashi Iwai 
38161864d84STakashi Iwai /* control info callback */
snd_line6_control_playback_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)38261864d84STakashi Iwai static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
38361864d84STakashi Iwai 					   struct snd_ctl_elem_info *uinfo)
38461864d84STakashi Iwai {
38561864d84STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
38661864d84STakashi Iwai 	uinfo->count = 2;
38761864d84STakashi Iwai 	uinfo->value.integer.min = 0;
38861864d84STakashi Iwai 	uinfo->value.integer.max = 256;
38961864d84STakashi Iwai 	return 0;
39061864d84STakashi Iwai }
39161864d84STakashi Iwai 
39261864d84STakashi Iwai /* control get callback */
snd_line6_control_playback_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)39361864d84STakashi Iwai static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
39461864d84STakashi Iwai 					  struct snd_ctl_elem_value *ucontrol)
39561864d84STakashi Iwai {
39661864d84STakashi Iwai 	int i;
39761864d84STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
39861864d84STakashi Iwai 
3999fb754b7STakashi Iwai 	for (i = 0; i < 2; i++)
40061864d84STakashi Iwai 		ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
40161864d84STakashi Iwai 
40261864d84STakashi Iwai 	return 0;
40361864d84STakashi Iwai }
40461864d84STakashi Iwai 
40561864d84STakashi Iwai /* control put callback */
snd_line6_control_playback_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)40661864d84STakashi Iwai static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
40761864d84STakashi Iwai 					  struct snd_ctl_elem_value *ucontrol)
40861864d84STakashi Iwai {
40961864d84STakashi Iwai 	int i, changed = 0;
41061864d84STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
41161864d84STakashi Iwai 
4129fb754b7STakashi Iwai 	for (i = 0; i < 2; i++)
41361864d84STakashi Iwai 		if (line6pcm->volume_playback[i] !=
41461864d84STakashi Iwai 		    ucontrol->value.integer.value[i]) {
41561864d84STakashi Iwai 			line6pcm->volume_playback[i] =
41661864d84STakashi Iwai 			    ucontrol->value.integer.value[i];
41761864d84STakashi Iwai 			changed = 1;
41861864d84STakashi Iwai 		}
41961864d84STakashi Iwai 
42061864d84STakashi Iwai 	return changed;
42161864d84STakashi Iwai }
42261864d84STakashi Iwai 
42361864d84STakashi Iwai /* control definition */
4241e0f8f68STakashi Sakamoto static const struct snd_kcontrol_new line6_controls[] = {
425075587b7STakashi Iwai 	{
42661864d84STakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
42761864d84STakashi Iwai 		.name = "PCM Playback Volume",
42861864d84STakashi Iwai 		.info = snd_line6_control_playback_info,
42961864d84STakashi Iwai 		.get = snd_line6_control_playback_get,
43061864d84STakashi Iwai 		.put = snd_line6_control_playback_put
431075587b7STakashi Iwai 	},
432075587b7STakashi Iwai 	{
433075587b7STakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
434075587b7STakashi Iwai 		.name = "Impulse Response Volume",
435075587b7STakashi Iwai 		.info = snd_line6_impulse_volume_info,
436075587b7STakashi Iwai 		.get = snd_line6_impulse_volume_get,
437075587b7STakashi Iwai 		.put = snd_line6_impulse_volume_put
438075587b7STakashi Iwai 	},
439075587b7STakashi Iwai 	{
440075587b7STakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
441075587b7STakashi Iwai 		.name = "Impulse Response Period",
442075587b7STakashi Iwai 		.info = snd_line6_impulse_period_info,
443075587b7STakashi Iwai 		.get = snd_line6_impulse_period_get,
444075587b7STakashi Iwai 		.put = snd_line6_impulse_period_put
445075587b7STakashi Iwai 	},
44661864d84STakashi Iwai };
44761864d84STakashi Iwai 
44861864d84STakashi Iwai /*
44961864d84STakashi Iwai 	Cleanup the PCM device.
45061864d84STakashi Iwai */
cleanup_urbs(struct line6_pcm_stream * pcms,int iso_buffers)451b2233d97SAndrej Krutak static void cleanup_urbs(struct line6_pcm_stream *pcms, int iso_buffers)
45261864d84STakashi Iwai {
45361864d84STakashi Iwai 	int i;
45461864d84STakashi Iwai 
455b2233d97SAndrej Krutak 	/* Most likely impossible in current code... */
456b2233d97SAndrej Krutak 	if (pcms->urbs == NULL)
457b2233d97SAndrej Krutak 		return;
458b2233d97SAndrej Krutak 
459b2233d97SAndrej Krutak 	for (i = 0; i < iso_buffers; i++) {
460d8131e67STakashi Iwai 		if (pcms->urbs[i]) {
461d8131e67STakashi Iwai 			usb_kill_urb(pcms->urbs[i]);
462d8131e67STakashi Iwai 			usb_free_urb(pcms->urbs[i]);
46361864d84STakashi Iwai 		}
46461864d84STakashi Iwai 	}
465b2233d97SAndrej Krutak 	kfree(pcms->urbs);
466b2233d97SAndrej Krutak 	pcms->urbs = NULL;
467d8131e67STakashi Iwai }
468d8131e67STakashi Iwai 
line6_cleanup_pcm(struct snd_pcm * pcm)469d8131e67STakashi Iwai static void line6_cleanup_pcm(struct snd_pcm *pcm)
470d8131e67STakashi Iwai {
471d8131e67STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
472d8131e67STakashi Iwai 
473b2233d97SAndrej Krutak 	cleanup_urbs(&line6pcm->out, line6pcm->line6->iso_buffers);
474b2233d97SAndrej Krutak 	cleanup_urbs(&line6pcm->in, line6pcm->line6->iso_buffers);
475b45a7c56STakashi Iwai 	kfree(line6pcm);
47661864d84STakashi Iwai }
47761864d84STakashi Iwai 
47861864d84STakashi Iwai /* create a PCM device */
snd_line6_new_pcm(struct usb_line6 * line6,struct snd_pcm ** pcm_ret)479b45a7c56STakashi Iwai static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
48061864d84STakashi Iwai {
48161864d84STakashi Iwai 	struct snd_pcm *pcm;
48261864d84STakashi Iwai 	int err;
48361864d84STakashi Iwai 
484b45a7c56STakashi Iwai 	err = snd_pcm_new(line6->card, (char *)line6->properties->name,
485b45a7c56STakashi Iwai 			  0, 1, 1, pcm_ret);
48661864d84STakashi Iwai 	if (err < 0)
48761864d84STakashi Iwai 		return err;
488b45a7c56STakashi Iwai 	pcm = *pcm_ret;
489b45a7c56STakashi Iwai 	strcpy(pcm->name, line6->properties->name);
49061864d84STakashi Iwai 
49161864d84STakashi Iwai 	/* set operators */
49261864d84STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
49361864d84STakashi Iwai 			&snd_line6_playback_ops);
49461864d84STakashi Iwai 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
49561864d84STakashi Iwai 
49661864d84STakashi Iwai 	/* pre-allocation of buffers */
497b9626bd6STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
4980af0a4feSTakashi Iwai 				       NULL, 64 * 1024, 128 * 1024);
49961864d84STakashi Iwai 	return 0;
50061864d84STakashi Iwai }
50161864d84STakashi Iwai 
50261864d84STakashi Iwai /*
5035a475311STakashi Iwai 	Sync with PCM stream stops.
50461864d84STakashi Iwai */
line6_pcm_disconnect(struct snd_line6_pcm * line6pcm)50561864d84STakashi Iwai void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
50661864d84STakashi Iwai {
507d8131e67STakashi Iwai 	line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
508d8131e67STakashi Iwai 	line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
509d8131e67STakashi Iwai 	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
510d8131e67STakashi Iwai 	line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
51161864d84STakashi Iwai }
51261864d84STakashi Iwai 
51361864d84STakashi Iwai /*
51461864d84STakashi Iwai 	Create and register the PCM device and mixer entries.
51561864d84STakashi Iwai 	Create URBs for playback and capture.
51661864d84STakashi Iwai */
line6_init_pcm(struct usb_line6 * line6,struct line6_pcm_properties * properties)51761864d84STakashi Iwai int line6_init_pcm(struct usb_line6 *line6,
51861864d84STakashi Iwai 		   struct line6_pcm_properties *properties)
51961864d84STakashi Iwai {
520075587b7STakashi Iwai 	int i, err;
52161864d84STakashi Iwai 	unsigned ep_read = line6->properties->ep_audio_r;
52261864d84STakashi Iwai 	unsigned ep_write = line6->properties->ep_audio_w;
523b45a7c56STakashi Iwai 	struct snd_pcm *pcm;
52461864d84STakashi Iwai 	struct snd_line6_pcm *line6pcm;
52561864d84STakashi Iwai 
52661864d84STakashi Iwai 	if (!(line6->properties->capabilities & LINE6_CAP_PCM))
52761864d84STakashi Iwai 		return 0;	/* skip PCM initialization and report success */
52861864d84STakashi Iwai 
529b45a7c56STakashi Iwai 	err = snd_line6_new_pcm(line6, &pcm);
530b45a7c56STakashi Iwai 	if (err < 0)
531b45a7c56STakashi Iwai 		return err;
53261864d84STakashi Iwai 
533b45a7c56STakashi Iwai 	line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
534b45a7c56STakashi Iwai 	if (!line6pcm)
53561864d84STakashi Iwai 		return -ENOMEM;
53661864d84STakashi Iwai 
53763e20df1STakashi Iwai 	mutex_init(&line6pcm->state_mutex);
538b45a7c56STakashi Iwai 	line6pcm->pcm = pcm;
539b45a7c56STakashi Iwai 	line6pcm->properties = properties;
54061864d84STakashi Iwai 	line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
54161864d84STakashi Iwai 	line6pcm->volume_monitor = 255;
54261864d84STakashi Iwai 	line6pcm->line6 = line6;
54361864d84STakashi Iwai 
5441bc8d18cSTakashi Iwai 	spin_lock_init(&line6pcm->out.lock);
5451bc8d18cSTakashi Iwai 	spin_lock_init(&line6pcm->in.lock);
5461bc8d18cSTakashi Iwai 	line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
5471bc8d18cSTakashi Iwai 
5481bc8d18cSTakashi Iwai 	line6->line6pcm = line6pcm;
5491bc8d18cSTakashi Iwai 
5501bc8d18cSTakashi Iwai 	pcm->private_data = line6pcm;
5511bc8d18cSTakashi Iwai 	pcm->private_free = line6_cleanup_pcm;
5521bc8d18cSTakashi Iwai 
5537a0f55aeSAndrej Krutak 	line6pcm->max_packet_size_in =
55461864d84STakashi Iwai 		usb_maxpacket(line6->usbdev,
55580b2b03bSVincent Mailhol 			usb_rcvisocpipe(line6->usbdev, ep_read));
5567a0f55aeSAndrej Krutak 	line6pcm->max_packet_size_out =
55761864d84STakashi Iwai 		usb_maxpacket(line6->usbdev,
55880b2b03bSVincent Mailhol 			usb_sndisocpipe(line6->usbdev, ep_write));
55934501219STakashi Iwai 	if (!line6pcm->max_packet_size_in || !line6pcm->max_packet_size_out) {
56034501219STakashi Iwai 		dev_err(line6pcm->line6->ifcdev,
56134501219STakashi Iwai 			"cannot get proper max packet size\n");
56234501219STakashi Iwai 		return -EINVAL;
56334501219STakashi Iwai 	}
56461864d84STakashi Iwai 
56561864d84STakashi Iwai 	err = line6_create_audio_out_urbs(line6pcm);
56661864d84STakashi Iwai 	if (err < 0)
56761864d84STakashi Iwai 		return err;
56861864d84STakashi Iwai 
56961864d84STakashi Iwai 	err = line6_create_audio_in_urbs(line6pcm);
57061864d84STakashi Iwai 	if (err < 0)
57161864d84STakashi Iwai 		return err;
57261864d84STakashi Iwai 
57361864d84STakashi Iwai 	/* mixer: */
574075587b7STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
575075587b7STakashi Iwai 		err = snd_ctl_add(line6->card,
576075587b7STakashi Iwai 				  snd_ctl_new1(&line6_controls[i], line6pcm));
57761864d84STakashi Iwai 		if (err < 0)
57861864d84STakashi Iwai 			return err;
579075587b7STakashi Iwai 	}
58061864d84STakashi Iwai 
58161864d84STakashi Iwai 	return 0;
58261864d84STakashi Iwai }
583ccddbe4aSTakashi Iwai EXPORT_SYMBOL_GPL(line6_init_pcm);
58461864d84STakashi Iwai 
58561864d84STakashi Iwai /* prepare pcm callback */
snd_line6_prepare(struct snd_pcm_substream * substream)58661864d84STakashi Iwai int snd_line6_prepare(struct snd_pcm_substream *substream)
58761864d84STakashi Iwai {
58861864d84STakashi Iwai 	struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
58963e20df1STakashi Iwai 	struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
59061864d84STakashi Iwai 
59163e20df1STakashi Iwai 	mutex_lock(&line6pcm->state_mutex);
59263e20df1STakashi Iwai 	if (!pstr->running)
59363e20df1STakashi Iwai 		line6_wait_clear_audio_urbs(line6pcm, pstr);
59461864d84STakashi Iwai 
59563e20df1STakashi Iwai 	if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
596ad0119abSTakashi Iwai 		line6pcm->out.count = 0;
597ad0119abSTakashi Iwai 		line6pcm->out.pos = 0;
598ad0119abSTakashi Iwai 		line6pcm->out.pos_done = 0;
599ad0119abSTakashi Iwai 		line6pcm->out.bytes = 0;
600ad0119abSTakashi Iwai 		line6pcm->in.count = 0;
601ad0119abSTakashi Iwai 		line6pcm->in.pos_done = 0;
602ad0119abSTakashi Iwai 		line6pcm->in.bytes = 0;
60361864d84STakashi Iwai 	}
60461864d84STakashi Iwai 
60563e20df1STakashi Iwai 	mutex_unlock(&line6pcm->state_mutex);
60661864d84STakashi Iwai 	return 0;
60761864d84STakashi Iwai }
608