blob: 74b399372e0b4a04748b56996c040afa7f1966bc [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Chris Rorvickc078a4a2015-01-20 02:20:50 -06002 * Line 6 Linux USB driver
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f4762010-08-12 01:35:30 +02004 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08005 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Takashi Iwaiccddbe42015-01-15 08:22:31 +010013#include <linux/export.h>
Markus Grabner705ecec2009-02-27 19:43:04 -080014#include <sound/core.h>
15#include <sound/control.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18
Markus Grabner705ecec2009-02-27 19:43:04 -080019#include "capture.h"
Markus Grabner1027f4762010-08-12 01:35:30 +020020#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080021#include "playback.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080022
Takashi Iwai075587b2015-01-19 14:28:25 +010023/* impulse response volume controls */
24static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
25 struct snd_ctl_elem_info *uinfo)
Markus Grabner1027f4762010-08-12 01:35:30 +020026{
Takashi Iwai075587b2015-01-19 14:28:25 +010027 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
28 uinfo->count = 1;
29 uinfo->value.integer.min = 0;
30 uinfo->value.integer.max = 255;
31 return 0;
Markus Grabner1027f4762010-08-12 01:35:30 +020032}
33
Takashi Iwai075587b2015-01-19 14:28:25 +010034static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
35 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f4762010-08-12 01:35:30 +020036{
Takashi Iwai075587b2015-01-19 14:28:25 +010037 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
38
39 ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
40 return 0;
Markus Grabner1027f4762010-08-12 01:35:30 +020041}
42
Takashi Iwai075587b2015-01-19 14:28:25 +010043static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
44 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f4762010-08-12 01:35:30 +020045{
Takashi Iwai075587b2015-01-19 14:28:25 +010046 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
47 int value = ucontrol->value.integer.value[0];
Takashi Iwai247d95e2015-01-27 16:42:14 +010048 int err;
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020049
Takashi Iwai075587b2015-01-19 14:28:25 +010050 if (line6pcm->impulse_volume == value)
51 return 0;
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020052
Markus Grabner1027f4762010-08-12 01:35:30 +020053 line6pcm->impulse_volume = value;
Takashi Iwai247d95e2015-01-27 16:42:14 +010054 if (value > 0) {
Andrej Krutakf56742c2016-09-18 20:59:25 +020055 err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE, true);
Takashi Iwai247d95e2015-01-27 16:42:14 +010056 if (err < 0) {
57 line6pcm->impulse_volume = 0;
Takashi Iwai247d95e2015-01-27 16:42:14 +010058 return err;
59 }
60 } else {
Takashi Iwai63e20df2015-01-27 15:24:09 +010061 line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE);
Takashi Iwai247d95e2015-01-27 16:42:14 +010062 }
Takashi Iwai075587b2015-01-19 14:28:25 +010063 return 1;
Markus Grabner1027f4762010-08-12 01:35:30 +020064}
65
Takashi Iwai075587b2015-01-19 14:28:25 +010066/* impulse response period controls */
67static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
68 struct snd_ctl_elem_info *uinfo)
Markus Grabner1027f4762010-08-12 01:35:30 +020069{
Takashi Iwai075587b2015-01-19 14:28:25 +010070 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
71 uinfo->count = 1;
72 uinfo->value.integer.min = 0;
73 uinfo->value.integer.max = 2000;
74 return 0;
Markus Grabner1027f4762010-08-12 01:35:30 +020075}
76
Takashi Iwai075587b2015-01-19 14:28:25 +010077static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
78 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f4762010-08-12 01:35:30 +020079{
Takashi Iwai075587b2015-01-19 14:28:25 +010080 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
Laurent Naveta3762902012-11-30 11:57:32 +010081
Takashi Iwai075587b2015-01-19 14:28:25 +010082 ucontrol->value.integer.value[0] = line6pcm->impulse_period;
83 return 0;
Markus Grabner1027f4762010-08-12 01:35:30 +020084}
Markus Grabner1027f4762010-08-12 01:35:30 +020085
Takashi Iwai075587b2015-01-19 14:28:25 +010086static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_value *ucontrol)
88{
89 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
90 int value = ucontrol->value.integer.value[0];
91
92 if (line6pcm->impulse_period == value)
93 return 0;
94
95 line6pcm->impulse_period = value;
96 return 1;
97}
Markus Grabner1027f4762010-08-12 01:35:30 +020098
Takashi Iwaid8131e62015-01-23 16:18:42 +010099/*
100 Unlink all currently active URBs.
101*/
102static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
103 struct line6_pcm_stream *pcms)
104{
105 int i;
106
Andrej Krutakb2233d92016-09-18 20:59:21 +0200107 for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100108 if (test_bit(i, &pcms->active_urbs)) {
109 if (!test_and_set_bit(i, &pcms->unlink_urbs))
110 usb_unlink_urb(pcms->urbs[i]);
111 }
112 }
113}
114
115/*
116 Wait until unlinking of all currently active URBs has been finished.
117*/
118static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
119 struct line6_pcm_stream *pcms)
120{
121 int timeout = HZ;
122 int i;
123 int alive;
124
125 do {
126 alive = 0;
Andrej Krutakb2233d92016-09-18 20:59:21 +0200127 for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100128 if (test_bit(i, &pcms->active_urbs))
129 alive++;
130 }
131 if (!alive)
132 break;
133 set_current_state(TASK_UNINTERRUPTIBLE);
134 schedule_timeout(1);
135 } while (--timeout > 0);
136 if (alive)
Takashi Iwaiccaac9e2015-01-23 16:20:50 +0100137 dev_err(line6pcm->line6->ifcdev,
138 "timeout: still %d active urbs..\n", alive);
Takashi Iwaid8131e62015-01-23 16:18:42 +0100139}
140
Takashi Iwai63e20df2015-01-27 15:24:09 +0100141static inline struct line6_pcm_stream *
142get_stream(struct snd_line6_pcm *line6pcm, int direction)
143{
144 return (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
145 &line6pcm->out : &line6pcm->in;
146}
147
148/* allocate a buffer if not opened yet;
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200149 * call this in line6pcm.state_mutex
Takashi Iwai63e20df2015-01-27 15:24:09 +0100150 */
151static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm,
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200152 struct line6_pcm_stream *pstr, int direction, int type)
Takashi Iwaie90576c2015-01-23 16:25:03 +0100153{
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200154 const int pkt_size =
155 (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
156 line6pcm->max_packet_size_out :
157 line6pcm->max_packet_size_in;
158
Takashi Iwaie90576c2015-01-23 16:25:03 +0100159 /* Invoked multiple times in a row so allocate once only */
Takashi Iwai63e20df2015-01-27 15:24:09 +0100160 if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) {
Andrej Krutakb2233d92016-09-18 20:59:21 +0200161 pstr->buffer = kmalloc(line6pcm->line6->iso_buffers *
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200162 LINE6_ISO_PACKETS * pkt_size, GFP_KERNEL);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100163 if (!pstr->buffer)
164 return -ENOMEM;
165 }
Takashi Iwaie90576c2015-01-23 16:25:03 +0100166 return 0;
167}
168
Takashi Iwai63e20df2015-01-27 15:24:09 +0100169/* free a buffer if all streams are closed;
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200170 * call this in line6pcm.state_mutex
Takashi Iwai63e20df2015-01-27 15:24:09 +0100171 */
172static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
173 struct line6_pcm_stream *pstr, int type)
Takashi Iwaie90576c2015-01-23 16:25:03 +0100174{
Takashi Iwai63e20df2015-01-27 15:24:09 +0100175 clear_bit(type, &pstr->opened);
176 if (!pstr->opened) {
177 line6_wait_clear_audio_urbs(line6pcm, pstr);
178 kfree(pstr->buffer);
179 pstr->buffer = NULL;
Markus Grabner0ca54882012-01-20 00:09:09 +0100180 }
Takashi Iwai63e20df2015-01-27 15:24:09 +0100181}
Markus Grabner0ca54882012-01-20 00:09:09 +0100182
Takashi Iwai63e20df2015-01-27 15:24:09 +0100183/* start a PCM stream */
184static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction,
185 int type)
186{
187 unsigned long flags;
188 struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
189 int ret = 0;
190
191 spin_lock_irqsave(&pstr->lock, flags);
Takashi Iwai4d0e6772015-07-14 15:19:37 +0200192 if (!test_and_set_bit(type, &pstr->running) &&
193 !(pstr->active_urbs || pstr->unlink_urbs)) {
Takashi Iwai63e20df2015-01-27 15:24:09 +0100194 pstr->count = 0;
195 /* Submit all currently available URBs */
196 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
197 ret = line6_submit_audio_out_all_urbs(line6pcm);
198 else
199 ret = line6_submit_audio_in_all_urbs(line6pcm);
Markus Grabner1027f4762010-08-12 01:35:30 +0200200 }
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200201
Takashi Iwai63e20df2015-01-27 15:24:09 +0100202 if (ret < 0)
203 clear_bit(type, &pstr->running);
204 spin_unlock_irqrestore(&pstr->lock, flags);
205 return ret;
Markus Grabner1027f4762010-08-12 01:35:30 +0200206}
207
Takashi Iwai63e20df2015-01-27 15:24:09 +0100208/* stop a PCM stream; this doesn't sync with the unlinked URBs */
209static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
210 int type)
Markus Grabner1027f4762010-08-12 01:35:30 +0200211{
Takashi Iwai63e20df2015-01-27 15:24:09 +0100212 unsigned long flags;
213 struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
Arnd Bergmann9f613602013-06-21 21:55:35 +0200214
Takashi Iwai63e20df2015-01-27 15:24:09 +0100215 spin_lock_irqsave(&pstr->lock, flags);
216 clear_bit(type, &pstr->running);
217 if (!pstr->running) {
Andrej Krutakadc8a432016-08-18 23:52:11 +0200218 spin_unlock_irqrestore(&pstr->lock, flags);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100219 line6_unlink_audio_urbs(line6pcm, pstr);
Andrej Krutakadc8a432016-08-18 23:52:11 +0200220 spin_lock_irqsave(&pstr->lock, flags);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100221 if (direction == SNDRV_PCM_STREAM_CAPTURE) {
222 line6pcm->prev_fbuf = NULL;
223 line6pcm->prev_fsize = 0;
224 }
Takashi Iwaif2bb6142015-01-27 16:17:26 +0100225 }
Takashi Iwai63e20df2015-01-27 15:24:09 +0100226 spin_unlock_irqrestore(&pstr->lock, flags);
Markus Grabner1027f4762010-08-12 01:35:30 +0200227}
228
Takashi Iwai63e20df2015-01-27 15:24:09 +0100229/* common PCM trigger callback */
Markus Grabner705ecec2009-02-27 19:43:04 -0800230int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
231{
232 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800233 struct snd_pcm_substream *s;
Takashi Iwai63e20df2015-01-27 15:24:09 +0100234 int err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800235
Takashi Iwai63e20df2015-01-27 15:24:09 +0100236 clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800237
Markus Grabner705ecec2009-02-27 19:43:04 -0800238 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai7d70c812015-01-19 15:11:17 +0100239 if (s->pcm->card != substream->pcm->card)
240 continue;
Takashi Iwai63e20df2015-01-27 15:24:09 +0100241
242 switch (cmd) {
243 case SNDRV_PCM_TRIGGER_START:
244 case SNDRV_PCM_TRIGGER_RESUME:
Andrej Krutakf56742c2016-09-18 20:59:25 +0200245 if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
246 (line6pcm->line6->properties->capabilities &
247 LINE6_CAP_IN_NEEDS_OUT)) {
248 err = line6_stream_start(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
249 LINE6_STREAM_CAPTURE_HELPER);
250 if (err < 0)
251 return err;
252 }
Takashi Iwai63e20df2015-01-27 15:24:09 +0100253 err = line6_stream_start(line6pcm, s->stream,
254 LINE6_STREAM_PCM);
255 if (err < 0)
256 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800257 break;
258
Takashi Iwai63e20df2015-01-27 15:24:09 +0100259 case SNDRV_PCM_TRIGGER_STOP:
260 case SNDRV_PCM_TRIGGER_SUSPEND:
Andrej Krutakf56742c2016-09-18 20:59:25 +0200261 if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
262 (line6pcm->line6->properties->capabilities &
263 LINE6_CAP_IN_NEEDS_OUT)) {
264 line6_stream_stop(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
265 LINE6_STREAM_CAPTURE_HELPER);
266 }
Takashi Iwai63e20df2015-01-27 15:24:09 +0100267 line6_stream_stop(line6pcm, s->stream,
268 LINE6_STREAM_PCM);
269 break;
270
271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
272 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
273 return -EINVAL;
274 set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
275 break;
276
277 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
278 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
279 return -EINVAL;
280 clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800281 break;
282
283 default:
Takashi Iwai63e20df2015-01-27 15:24:09 +0100284 return -EINVAL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800285 }
286 }
287
Takashi Iwai63e20df2015-01-27 15:24:09 +0100288 return 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800289}
290
Takashi Iwai2954f912015-01-27 15:41:27 +0100291/* common PCM pointer callback */
292snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
293{
294 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
295 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
296
297 return pstr->pos_done;
298}
299
Andrej Krutakf56742c2016-09-18 20:59:25 +0200300/* Acquire and optionally start duplex streams:
Takashi Iwai63e20df2015-01-27 15:24:09 +0100301 * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
302 */
Andrej Krutakf56742c2016-09-18 20:59:25 +0200303int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
Takashi Iwai63e20df2015-01-27 15:24:09 +0100304{
305 struct line6_pcm_stream *pstr;
306 int ret = 0, dir;
307
Andrej Krutakf56742c2016-09-18 20:59:25 +0200308 /* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
Takashi Iwai63e20df2015-01-27 15:24:09 +0100309 mutex_lock(&line6pcm->state_mutex);
310 for (dir = 0; dir < 2; dir++) {
311 pstr = get_stream(line6pcm, dir);
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200312 ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100313 if (ret < 0)
314 goto error;
315 if (!pstr->running)
316 line6_wait_clear_audio_urbs(line6pcm, pstr);
317 }
Andrej Krutakf56742c2016-09-18 20:59:25 +0200318 if (start) {
319 for (dir = 0; dir < 2; dir++) {
320 ret = line6_stream_start(line6pcm, dir, type);
321 if (ret < 0)
322 goto error;
323 }
Takashi Iwai63e20df2015-01-27 15:24:09 +0100324 }
325 error:
326 mutex_unlock(&line6pcm->state_mutex);
327 if (ret < 0)
328 line6_pcm_release(line6pcm, type);
329 return ret;
330}
331EXPORT_SYMBOL_GPL(line6_pcm_acquire);
332
333/* Stop and release duplex streams */
334void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
335{
336 struct line6_pcm_stream *pstr;
337 int dir;
338
339 mutex_lock(&line6pcm->state_mutex);
340 for (dir = 0; dir < 2; dir++)
341 line6_stream_stop(line6pcm, dir, type);
342 for (dir = 0; dir < 2; dir++) {
343 pstr = get_stream(line6pcm, dir);
344 line6_buffer_release(line6pcm, pstr, type);
345 }
346 mutex_unlock(&line6pcm->state_mutex);
347}
348EXPORT_SYMBOL_GPL(line6_pcm_release);
349
350/* common PCM hw_params callback */
351int snd_line6_hw_params(struct snd_pcm_substream *substream,
352 struct snd_pcm_hw_params *hw_params)
353{
354 int ret;
355 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
356 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
357
358 mutex_lock(&line6pcm->state_mutex);
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200359 ret = line6_buffer_acquire(line6pcm, pstr, substream->stream,
360 LINE6_STREAM_PCM);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100361 if (ret < 0)
362 goto error;
363
364 ret = snd_pcm_lib_malloc_pages(substream,
365 params_buffer_bytes(hw_params));
366 if (ret < 0) {
367 line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
368 goto error;
369 }
370
371 pstr->period = params_period_bytes(hw_params);
372 error:
373 mutex_unlock(&line6pcm->state_mutex);
374 return ret;
375}
376
377/* common PCM hw_free callback */
378int snd_line6_hw_free(struct snd_pcm_substream *substream)
379{
380 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
381 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
382
383 mutex_lock(&line6pcm->state_mutex);
384 line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
385 mutex_unlock(&line6pcm->state_mutex);
386 return snd_pcm_lib_free_pages(substream);
387}
388
389
Markus Grabner705ecec2009-02-27 19:43:04 -0800390/* control info callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200391static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
392 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800393{
Markus Grabner705ecec2009-02-27 19:43:04 -0800394 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
395 uinfo->count = 2;
396 uinfo->value.integer.min = 0;
397 uinfo->value.integer.max = 256;
398 return 0;
399}
400
401/* control get callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200402static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
403 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800404{
Markus Grabner705ecec2009-02-27 19:43:04 -0800405 int i;
406 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
407
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100408 for (i = 0; i < 2; i++)
Markus Grabner1027f4762010-08-12 01:35:30 +0200409 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800410
411 return 0;
412}
413
414/* control put callback */
Markus Grabner1027f4762010-08-12 01:35:30 +0200415static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800417{
Markus Grabner705ecec2009-02-27 19:43:04 -0800418 int i, changed = 0;
419 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
420
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100421 for (i = 0; i < 2; i++)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200422 if (line6pcm->volume_playback[i] !=
423 ucontrol->value.integer.value[i]) {
424 line6pcm->volume_playback[i] =
425 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800426 changed = 1;
427 }
428
429 return changed;
430}
431
432/* control definition */
Takashi Iwai075587b2015-01-19 14:28:25 +0100433static struct snd_kcontrol_new line6_controls[] = {
434 {
435 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
436 .name = "PCM Playback Volume",
437 .info = snd_line6_control_playback_info,
438 .get = snd_line6_control_playback_get,
439 .put = snd_line6_control_playback_put
440 },
441 {
442 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
443 .name = "Impulse Response Volume",
444 .info = snd_line6_impulse_volume_info,
445 .get = snd_line6_impulse_volume_get,
446 .put = snd_line6_impulse_volume_put
447 },
448 {
449 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
450 .name = "Impulse Response Period",
451 .info = snd_line6_impulse_period_info,
452 .get = snd_line6_impulse_period_get,
453 .put = snd_line6_impulse_period_put
454 },
Markus Grabner705ecec2009-02-27 19:43:04 -0800455};
456
457/*
458 Cleanup the PCM device.
459*/
Andrej Krutakb2233d92016-09-18 20:59:21 +0200460static void cleanup_urbs(struct line6_pcm_stream *pcms, int iso_buffers)
Markus Grabner705ecec2009-02-27 19:43:04 -0800461{
462 int i;
Markus Grabner705ecec2009-02-27 19:43:04 -0800463
Andrej Krutakb2233d92016-09-18 20:59:21 +0200464 /* Most likely impossible in current code... */
465 if (pcms->urbs == NULL)
466 return;
467
468 for (i = 0; i < iso_buffers; i++) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100469 if (pcms->urbs[i]) {
470 usb_kill_urb(pcms->urbs[i]);
471 usb_free_urb(pcms->urbs[i]);
Markus Grabner705ecec2009-02-27 19:43:04 -0800472 }
473 }
Andrej Krutakb2233d92016-09-18 20:59:21 +0200474 kfree(pcms->urbs);
475 pcms->urbs = NULL;
Takashi Iwaid8131e62015-01-23 16:18:42 +0100476}
477
478static void line6_cleanup_pcm(struct snd_pcm *pcm)
479{
480 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
481
Andrej Krutakb2233d92016-09-18 20:59:21 +0200482 cleanup_urbs(&line6pcm->out, line6pcm->line6->iso_buffers);
483 cleanup_urbs(&line6pcm->in, line6pcm->line6->iso_buffers);
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100484 kfree(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800485}
486
487/* create a PCM device */
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100488static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
Markus Grabner705ecec2009-02-27 19:43:04 -0800489{
490 struct snd_pcm *pcm;
491 int err;
492
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100493 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
494 0, 1, 1, pcm_ret);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800495 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800496 return err;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100497 pcm = *pcm_ret;
498 strcpy(pcm->name, line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800499
500 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600501 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
502 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200503 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800504
505 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800506 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200507 snd_dma_continuous_data
508 (GFP_KERNEL), 64 * 1024,
509 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800510 return 0;
511}
512
513/*
Takashi Iwai5a475312015-01-19 16:15:54 +0100514 Sync with PCM stream stops.
Markus Grabner1027f4762010-08-12 01:35:30 +0200515*/
516void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
517{
Takashi Iwaid8131e62015-01-23 16:18:42 +0100518 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
519 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
520 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
521 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
Markus Grabner1027f4762010-08-12 01:35:30 +0200522}
523
524/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800525 Create and register the PCM device and mixer entries.
526 Create URBs for playback and capture.
527*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800528int line6_init_pcm(struct usb_line6 *line6,
529 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800530{
Takashi Iwai075587b2015-01-19 14:28:25 +0100531 int i, err;
Chris Rorvick16d603d2015-01-12 12:42:55 -0800532 unsigned ep_read = line6->properties->ep_audio_r;
533 unsigned ep_write = line6->properties->ep_audio_w;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100534 struct snd_pcm *pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -0800535 struct snd_line6_pcm *line6pcm;
536
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800537 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200538 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800539
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100540 err = snd_line6_new_pcm(line6, &pcm);
541 if (err < 0)
542 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800543
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100544 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
545 if (!line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800546 return -ENOMEM;
547
Takashi Iwai63e20df2015-01-27 15:24:09 +0100548 mutex_init(&line6pcm->state_mutex);
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100549 line6pcm->pcm = pcm;
550 line6pcm->properties = properties;
Markus Grabner1027f4762010-08-12 01:35:30 +0200551 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
552 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800553 line6pcm->line6 = line6;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000554
Takashi Iwaif595a122019-08-21 20:00:02 +0200555 spin_lock_init(&line6pcm->out.lock);
556 spin_lock_init(&line6pcm->in.lock);
557 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
558
559 line6->line6pcm = line6pcm;
560
561 pcm->private_data = line6pcm;
562 pcm->private_free = line6_cleanup_pcm;
563
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200564 line6pcm->max_packet_size_in =
565 usb_maxpacket(line6->usbdev,
566 usb_rcvisocpipe(line6->usbdev, ep_read), 0);
567 line6pcm->max_packet_size_out =
568 usb_maxpacket(line6->usbdev,
569 usb_sndisocpipe(line6->usbdev, ep_write), 1);
Takashi Iwai8b449e92019-07-02 20:07:21 +0200570 if (!line6pcm->max_packet_size_in || !line6pcm->max_packet_size_out) {
571 dev_err(line6pcm->line6->ifcdev,
572 "cannot get proper max packet size\n");
573 return -EINVAL;
574 }
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000575
Markus Grabner1027f4762010-08-12 01:35:30 +0200576 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800577 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800578 return err;
579
Markus Grabner1027f4762010-08-12 01:35:30 +0200580 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800581 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800582 return err;
583
584 /* mixer: */
Takashi Iwai075587b2015-01-19 14:28:25 +0100585 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
586 err = snd_ctl_add(line6->card,
587 snd_ctl_new1(&line6_controls[i], line6pcm));
588 if (err < 0)
589 return err;
590 }
Markus Grabner1027f4762010-08-12 01:35:30 +0200591
Markus Grabner705ecec2009-02-27 19:43:04 -0800592 return 0;
593}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100594EXPORT_SYMBOL_GPL(line6_init_pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800595
596/* prepare pcm callback */
597int snd_line6_prepare(struct snd_pcm_substream *substream)
598{
599 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Takashi Iwai63e20df2015-01-27 15:24:09 +0100600 struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800601
Takashi Iwai63e20df2015-01-27 15:24:09 +0100602 mutex_lock(&line6pcm->state_mutex);
603 if (!pstr->running)
604 line6_wait_clear_audio_urbs(line6pcm, pstr);
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100605
Takashi Iwai63e20df2015-01-27 15:24:09 +0100606 if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100607 line6pcm->out.count = 0;
608 line6pcm->out.pos = 0;
609 line6pcm->out.pos_done = 0;
610 line6pcm->out.bytes = 0;
611 line6pcm->in.count = 0;
612 line6pcm->in.pos_done = 0;
613 line6pcm->in.bytes = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800614 }
615
Takashi Iwai63e20df2015-01-27 15:24:09 +0100616 mutex_unlock(&line6pcm->state_mutex);
Markus Grabner705ecec2009-02-27 19:43:04 -0800617 return 0;
618}