blob: 932cc7e194e2a2d8297a5be70bff0a984e230455 [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Markus Grabnere1a164d2010-08-23 01:08:25 +02002 * Line6 Linux USB driver - 0.9.1beta
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f472010-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
19#include "audio.h"
20#include "capture.h"
Markus Grabner1027f472010-08-12 01:35:30 +020021#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080022#include "playback.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080023
Takashi Iwai075587b2015-01-19 14:28:25 +010024/* impulse response volume controls */
25static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
26 struct snd_ctl_elem_info *uinfo)
Markus Grabner1027f472010-08-12 01:35:30 +020027{
Takashi Iwai075587b2015-01-19 14:28:25 +010028 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29 uinfo->count = 1;
30 uinfo->value.integer.min = 0;
31 uinfo->value.integer.max = 255;
32 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020033}
34
Takashi Iwai075587b2015-01-19 14:28:25 +010035static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
36 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f472010-08-12 01:35:30 +020037{
Takashi Iwai075587b2015-01-19 14:28:25 +010038 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
39
40 ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
41 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020042}
43
Takashi Iwai075587b2015-01-19 14:28:25 +010044static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
45 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f472010-08-12 01:35:30 +020046{
Takashi Iwai075587b2015-01-19 14:28:25 +010047 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
48 int value = ucontrol->value.integer.value[0];
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 Grabner1027f472010-08-12 01:35:30 +020053 line6pcm->impulse_volume = value;
Markus Grabnere1a164d2010-08-23 01:08:25 +020054 if (value > 0)
Markus Grabner0ca54882012-01-20 00:09:09 +010055 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
Markus Grabner1027f472010-08-12 01:35:30 +020056 else
Markus Grabner0ca54882012-01-20 00:09:09 +010057 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
Takashi Iwai075587b2015-01-19 14:28:25 +010058 return 1;
Markus Grabner1027f472010-08-12 01:35:30 +020059}
60
Takashi Iwai075587b2015-01-19 14:28:25 +010061/* impulse response period controls */
62static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
63 struct snd_ctl_elem_info *uinfo)
Markus Grabner1027f472010-08-12 01:35:30 +020064{
Takashi Iwai075587b2015-01-19 14:28:25 +010065 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
66 uinfo->count = 1;
67 uinfo->value.integer.min = 0;
68 uinfo->value.integer.max = 2000;
69 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020070}
71
Takashi Iwai075587b2015-01-19 14:28:25 +010072static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
73 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f472010-08-12 01:35:30 +020074{
Takashi Iwai075587b2015-01-19 14:28:25 +010075 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
Laurent Naveta3762902012-11-30 11:57:32 +010076
Takashi Iwai075587b2015-01-19 14:28:25 +010077 ucontrol->value.integer.value[0] = line6pcm->impulse_period;
78 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020079}
Markus Grabner1027f472010-08-12 01:35:30 +020080
Takashi Iwai075587b2015-01-19 14:28:25 +010081static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
82 struct snd_ctl_elem_value *ucontrol)
83{
84 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
85 int value = ucontrol->value.integer.value[0];
86
87 if (line6pcm->impulse_period == value)
88 return 0;
89
90 line6pcm->impulse_period = value;
91 return 1;
92}
Markus Grabner1027f472010-08-12 01:35:30 +020093
Markus Grabner6b02a17e2011-12-10 02:12:32 +010094static bool test_flags(unsigned long flags0, unsigned long flags1,
95 unsigned long mask)
96{
97 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
98}
99
Markus Grabner0ca54882012-01-20 00:09:09 +0100100int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200101{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200102 unsigned long flags_old, flags_new, flags_final;
103 int err;
104
105 do {
106 flags_old = ACCESS_ONCE(line6pcm->flags);
107 flags_new = flags_old | channels;
108 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
109
110 flags_final = flags_old;
Johannes Thumshirn82a74d42012-05-05 16:31:52 +0200111
Markus Grabner1027f472010-08-12 01:35:30 +0200112 line6pcm->prev_fbuf = NULL;
Markus Grabnere1a164d2010-08-23 01:08:25 +0200113
Markus Grabner0ca54882012-01-20 00:09:09 +0100114 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100115 /* Invoked multiple times in a row so allocate once only */
Markus Grabner0ca54882012-01-20 00:09:09 +0100116 if (!line6pcm->buffer_in) {
117 line6pcm->buffer_in =
118 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
119 line6pcm->max_packet_size, GFP_KERNEL);
Markus Grabner0ca54882012-01-20 00:09:09 +0100120 if (!line6pcm->buffer_in) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100121 err = -ENOMEM;
122 goto pcm_acquire_error;
123 }
124
125 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
126 }
127 }
128
129 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200130 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200131 Waiting for completion of active URBs in the stop handler is
132 a bug, we therefore report an error if capturing is restarted
133 too soon.
134 */
Markus Grabner0ca54882012-01-20 00:09:09 +0100135 if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
136 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f472010-08-12 01:35:30 +0200137 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100138 }
139
Markus Grabner1027f472010-08-12 01:35:30 +0200140 line6pcm->count_in = 0;
141 line6pcm->prev_fsize = 0;
142 err = line6_submit_audio_in_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200143
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100144 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100145 goto pcm_acquire_error;
146
147 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200148 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200149
Markus Grabner0ca54882012-01-20 00:09:09 +0100150 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100151 /* Invoked multiple times in a row so allocate once only */
Markus Grabner0ca54882012-01-20 00:09:09 +0100152 if (!line6pcm->buffer_out) {
153 line6pcm->buffer_out =
154 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
155 line6pcm->max_packet_size, GFP_KERNEL);
Markus Grabner0ca54882012-01-20 00:09:09 +0100156 if (!line6pcm->buffer_out) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100157 err = -ENOMEM;
158 goto pcm_acquire_error;
159 }
160
161 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
162 }
163 }
164
165 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200166 /*
Markus Grabner0ca54882012-01-20 00:09:09 +0100167 See comment above regarding PCM restart.
168 */
169 if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
170 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f472010-08-12 01:35:30 +0200171 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100172 }
173
Markus Grabner1027f472010-08-12 01:35:30 +0200174 line6pcm->count_out = 0;
175 err = line6_submit_audio_out_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200176
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100177 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100178 goto pcm_acquire_error;
179
180 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200181 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200182
Markus Grabner1027f472010-08-12 01:35:30 +0200183 return 0;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100184
Markus Grabner0ca54882012-01-20 00:09:09 +0100185pcm_acquire_error:
186 /*
187 If not all requested resources/streams could be obtained, release
188 those which were successfully obtained (if any).
189 */
190 line6_pcm_release(line6pcm, flags_final & channels);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100191 return err;
Markus Grabner1027f472010-08-12 01:35:30 +0200192}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100193EXPORT_SYMBOL_GPL(line6_pcm_acquire);
Markus Grabner1027f472010-08-12 01:35:30 +0200194
Markus Grabner0ca54882012-01-20 00:09:09 +0100195int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200196{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200197 unsigned long flags_old, flags_new;
198
199 do {
200 flags_old = ACCESS_ONCE(line6pcm->flags);
201 flags_new = flags_old & ~channels;
202 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
Markus Grabner1027f472010-08-12 01:35:30 +0200203
Markus Grabner0ca54882012-01-20 00:09:09 +0100204 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
Markus Grabner1027f472010-08-12 01:35:30 +0200205 line6_unlink_audio_in_urbs(line6pcm);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100206
Markus Grabner0ca54882012-01-20 00:09:09 +0100207 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
208 line6_wait_clear_audio_in_urbs(line6pcm);
209 line6_free_capture_buffer(line6pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200210 }
211
Markus Grabner0ca54882012-01-20 00:09:09 +0100212 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
Markus Grabner1027f472010-08-12 01:35:30 +0200213 line6_unlink_audio_out_urbs(line6pcm);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100214
Markus Grabner0ca54882012-01-20 00:09:09 +0100215 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
216 line6_wait_clear_audio_out_urbs(line6pcm);
217 line6_free_playback_buffer(line6pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200218 }
Markus Grabner1027f472010-08-12 01:35:30 +0200219
220 return 0;
221}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100222EXPORT_SYMBOL_GPL(line6_pcm_release);
Markus Grabner1027f472010-08-12 01:35:30 +0200223
Markus Grabner705ecec2009-02-27 19:43:04 -0800224/* trigger callback */
225int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
226{
227 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800228 struct snd_pcm_substream *s;
229 int err;
230 unsigned long flags;
231
232 spin_lock_irqsave(&line6pcm->lock_trigger, flags);
Markus Grabner0ca54882012-01-20 00:09:09 +0100233 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800234
Markus Grabner705ecec2009-02-27 19:43:04 -0800235 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai7d70c812015-01-19 15:11:17 +0100236 if (s->pcm->card != substream->pcm->card)
237 continue;
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800238 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800239 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner1027f472010-08-12 01:35:30 +0200240 err = snd_line6_playback_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800241
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800242 if (err < 0) {
243 spin_unlock_irqrestore(&line6pcm->lock_trigger,
244 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800245 return err;
246 }
247
248 break;
249
250 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner1027f472010-08-12 01:35:30 +0200251 err = snd_line6_capture_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800252
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800253 if (err < 0) {
254 spin_unlock_irqrestore(&line6pcm->lock_trigger,
255 flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800256 return err;
257 }
258
259 break;
260
261 default:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200262 dev_err(line6pcm->line6->ifcdev,
263 "Unknown stream direction %d\n", s->stream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800264 }
265 }
266
267 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
268 return 0;
269}
270
271/* control info callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200272static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800274{
Markus Grabner705ecec2009-02-27 19:43:04 -0800275 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
276 uinfo->count = 2;
277 uinfo->value.integer.min = 0;
278 uinfo->value.integer.max = 256;
279 return 0;
280}
281
282/* control get callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200283static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
284 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800285{
Markus Grabner705ecec2009-02-27 19:43:04 -0800286 int i;
287 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
288
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800289 for (i = 2; i--;)
Markus Grabner1027f472010-08-12 01:35:30 +0200290 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800291
292 return 0;
293}
294
295/* control put callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200296static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
297 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800298{
Markus Grabner705ecec2009-02-27 19:43:04 -0800299 int i, changed = 0;
300 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
301
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800302 for (i = 2; i--;)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200303 if (line6pcm->volume_playback[i] !=
304 ucontrol->value.integer.value[i]) {
305 line6pcm->volume_playback[i] =
306 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800307 changed = 1;
308 }
309
310 return changed;
311}
312
313/* control definition */
Takashi Iwai075587b2015-01-19 14:28:25 +0100314static struct snd_kcontrol_new line6_controls[] = {
315 {
316 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
317 .name = "PCM Playback Volume",
318 .info = snd_line6_control_playback_info,
319 .get = snd_line6_control_playback_get,
320 .put = snd_line6_control_playback_put
321 },
322 {
323 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
324 .name = "Impulse Response Volume",
325 .info = snd_line6_impulse_volume_info,
326 .get = snd_line6_impulse_volume_get,
327 .put = snd_line6_impulse_volume_put
328 },
329 {
330 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
331 .name = "Impulse Response Period",
332 .info = snd_line6_impulse_period_info,
333 .get = snd_line6_impulse_period_get,
334 .put = snd_line6_impulse_period_put
335 },
Markus Grabner705ecec2009-02-27 19:43:04 -0800336};
337
338/*
339 Cleanup the PCM device.
340*/
341static void line6_cleanup_pcm(struct snd_pcm *pcm)
342{
343 int i;
344 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
345
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800346 for (i = LINE6_ISO_BUFFERS; i--;) {
347 if (line6pcm->urb_audio_out[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800348 usb_kill_urb(line6pcm->urb_audio_out[i]);
349 usb_free_urb(line6pcm->urb_audio_out[i]);
350 }
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800351 if (line6pcm->urb_audio_in[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800352 usb_kill_urb(line6pcm->urb_audio_in[i]);
353 usb_free_urb(line6pcm->urb_audio_in[i]);
354 }
355 }
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100356 kfree(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800357}
358
359/* create a PCM device */
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100360static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
Markus Grabner705ecec2009-02-27 19:43:04 -0800361{
362 struct snd_pcm *pcm;
363 int err;
364
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100365 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
366 0, 1, 1, pcm_ret);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800367 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800368 return err;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100369 pcm = *pcm_ret;
370 strcpy(pcm->name, line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800371
372 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
374 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200375 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800376
377 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800378 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200379 snd_dma_continuous_data
380 (GFP_KERNEL), 64 * 1024,
381 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800382 return 0;
383}
384
385/*
Markus Grabner1027f472010-08-12 01:35:30 +0200386 Stop substream if still running.
387*/
388static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
389{
Takashi Iwai86f0b5b2013-07-11 18:02:38 +0200390 if (substream->runtime && snd_pcm_running(substream)) {
391 snd_pcm_stream_lock_irq(substream);
Markus Grabner1027f472010-08-12 01:35:30 +0200392 snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
Takashi Iwai86f0b5b2013-07-11 18:02:38 +0200393 snd_pcm_stream_unlock_irq(substream);
394 }
Markus Grabner1027f472010-08-12 01:35:30 +0200395}
396
397/*
398 Stop PCM stream.
399*/
400void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
401{
Markus Grabnere1a164d2010-08-23 01:08:25 +0200402 pcm_disconnect_substream(get_substream
403 (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
404 pcm_disconnect_substream(get_substream
405 (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
Markus Grabner1027f472010-08-12 01:35:30 +0200406 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
407 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
408}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100409EXPORT_SYMBOL_GPL(line6_pcm_disconnect);
Markus Grabner1027f472010-08-12 01:35:30 +0200410
411/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800412 Create and register the PCM device and mixer entries.
413 Create URBs for playback and capture.
414*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800415int line6_init_pcm(struct usb_line6 *line6,
416 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800417{
Takashi Iwai075587b2015-01-19 14:28:25 +0100418 int i, err;
Chris Rorvick16d603d2015-01-12 12:42:55 -0800419 unsigned ep_read = line6->properties->ep_audio_r;
420 unsigned ep_write = line6->properties->ep_audio_w;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100421 struct snd_pcm *pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -0800422 struct snd_line6_pcm *line6pcm;
423
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800424 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200425 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800426
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100427 err = snd_line6_new_pcm(line6, &pcm);
428 if (err < 0)
429 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800430
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100431 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
432 if (!line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800433 return -ENOMEM;
434
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100435 line6pcm->pcm = pcm;
436 line6pcm->properties = properties;
Markus Grabner1027f472010-08-12 01:35:30 +0200437 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
438 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800439 line6pcm->line6 = line6;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000440
441 /* Read and write buffers are sized identically, so choose minimum */
442 line6pcm->max_packet_size = min(
443 usb_maxpacket(line6->usbdev,
444 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
445 usb_maxpacket(line6->usbdev,
446 usb_sndisocpipe(line6->usbdev, ep_write), 1));
447
Markus Grabner705ecec2009-02-27 19:43:04 -0800448 spin_lock_init(&line6pcm->lock_audio_out);
449 spin_lock_init(&line6pcm->lock_audio_in);
450 spin_lock_init(&line6pcm->lock_trigger);
Takashi Iwai075587b2015-01-19 14:28:25 +0100451 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
Markus Grabner705ecec2009-02-27 19:43:04 -0800452
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100453 line6->line6pcm = line6pcm;
454
455 pcm->private_data = line6pcm;
456 pcm->private_free = line6_cleanup_pcm;
457
Markus Grabner1027f472010-08-12 01:35:30 +0200458 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800459 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800460 return err;
461
Markus Grabner1027f472010-08-12 01:35:30 +0200462 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800463 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800464 return err;
465
466 /* mixer: */
Takashi Iwai075587b2015-01-19 14:28:25 +0100467 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
468 err = snd_ctl_add(line6->card,
469 snd_ctl_new1(&line6_controls[i], line6pcm));
470 if (err < 0)
471 return err;
472 }
Markus Grabner1027f472010-08-12 01:35:30 +0200473
Markus Grabner705ecec2009-02-27 19:43:04 -0800474 return 0;
475}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100476EXPORT_SYMBOL_GPL(line6_init_pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800477
478/* prepare pcm callback */
479int snd_line6_prepare(struct snd_pcm_substream *substream)
480{
481 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
482
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100483 switch (substream->stream) {
484 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner0ca54882012-01-20 00:09:09 +0100485 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100486 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
487
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100488 break;
489
490 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner0ca54882012-01-20 00:09:09 +0100491 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100492 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
493
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100494 break;
495
496 default:
497 MISSING_CASE;
498 }
499
Markus Grabner0ca54882012-01-20 00:09:09 +0100500 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200501 line6pcm->count_out = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800502 line6pcm->pos_out = 0;
503 line6pcm->pos_out_done = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800504 line6pcm->bytes_out = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200505 line6pcm->count_in = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800506 line6pcm->pos_in_done = 0;
507 line6pcm->bytes_in = 0;
508 }
509
510 return 0;
511}