blob: 4152d92105b15a85fbf3249b695eb7c043e1937f [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 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
Markus Grabner705ecec2009-02-27 19:43:04 -080019#include "capture.h"
Markus Grabner1027f472010-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 Grabner1027f472010-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 Grabner1027f472010-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 Grabner1027f472010-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 Grabner1027f472010-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 Grabner1027f472010-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];
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020048
Takashi Iwai075587b2015-01-19 14:28:25 +010049 if (line6pcm->impulse_volume == value)
50 return 0;
Johannes Thumshirncdf5e552012-08-06 14:08:50 +020051
Markus Grabner1027f472010-08-12 01:35:30 +020052 line6pcm->impulse_volume = value;
Markus Grabnere1a164d2010-08-23 01:08:25 +020053 if (value > 0)
Markus Grabner0ca54882012-01-20 00:09:09 +010054 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
Markus Grabner1027f472010-08-12 01:35:30 +020055 else
Markus Grabner0ca54882012-01-20 00:09:09 +010056 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
Takashi Iwai075587b2015-01-19 14:28:25 +010057 return 1;
Markus Grabner1027f472010-08-12 01:35:30 +020058}
59
Takashi Iwai075587b2015-01-19 14:28:25 +010060/* impulse response period controls */
61static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
62 struct snd_ctl_elem_info *uinfo)
Markus Grabner1027f472010-08-12 01:35:30 +020063{
Takashi Iwai075587b2015-01-19 14:28:25 +010064 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
65 uinfo->count = 1;
66 uinfo->value.integer.min = 0;
67 uinfo->value.integer.max = 2000;
68 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020069}
70
Takashi Iwai075587b2015-01-19 14:28:25 +010071static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_value *ucontrol)
Markus Grabner1027f472010-08-12 01:35:30 +020073{
Takashi Iwai075587b2015-01-19 14:28:25 +010074 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
Laurent Naveta3762902012-11-30 11:57:32 +010075
Takashi Iwai075587b2015-01-19 14:28:25 +010076 ucontrol->value.integer.value[0] = line6pcm->impulse_period;
77 return 0;
Markus Grabner1027f472010-08-12 01:35:30 +020078}
Markus Grabner1027f472010-08-12 01:35:30 +020079
Takashi Iwai075587b2015-01-19 14:28:25 +010080static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
81 struct snd_ctl_elem_value *ucontrol)
82{
83 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
84 int value = ucontrol->value.integer.value[0];
85
86 if (line6pcm->impulse_period == value)
87 return 0;
88
89 line6pcm->impulse_period = value;
90 return 1;
91}
Markus Grabner1027f472010-08-12 01:35:30 +020092
Takashi Iwaid8131e62015-01-23 16:18:42 +010093/*
94 Unlink all currently active URBs.
95*/
96static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
97 struct line6_pcm_stream *pcms)
98{
99 int i;
100
101 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
102 if (test_bit(i, &pcms->active_urbs)) {
103 if (!test_and_set_bit(i, &pcms->unlink_urbs))
104 usb_unlink_urb(pcms->urbs[i]);
105 }
106 }
107}
108
109/*
110 Wait until unlinking of all currently active URBs has been finished.
111*/
112static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
113 struct line6_pcm_stream *pcms)
114{
115 int timeout = HZ;
116 int i;
117 int alive;
118
119 do {
120 alive = 0;
121 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
122 if (test_bit(i, &pcms->active_urbs))
123 alive++;
124 }
125 if (!alive)
126 break;
127 set_current_state(TASK_UNINTERRUPTIBLE);
128 schedule_timeout(1);
129 } while (--timeout > 0);
130 if (alive)
Takashi Iwaiccaac9e2015-01-23 16:20:50 +0100131 dev_err(line6pcm->line6->ifcdev,
132 "timeout: still %d active urbs..\n", alive);
Takashi Iwaid8131e62015-01-23 16:18:42 +0100133}
134
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100135static bool test_flags(unsigned long flags0, unsigned long flags1,
136 unsigned long mask)
137{
138 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
139}
140
Markus Grabner0ca54882012-01-20 00:09:09 +0100141int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200142{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200143 unsigned long flags_old, flags_new, flags_final;
144 int err;
145
146 do {
147 flags_old = ACCESS_ONCE(line6pcm->flags);
148 flags_new = flags_old | channels;
149 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
150
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100151 flags_final = 0;
Johannes Thumshirn82a74d42012-05-05 16:31:52 +0200152
Markus Grabner1027f472010-08-12 01:35:30 +0200153 line6pcm->prev_fbuf = NULL;
Markus Grabnere1a164d2010-08-23 01:08:25 +0200154
Markus Grabner0ca54882012-01-20 00:09:09 +0100155 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100156 /* Invoked multiple times in a row so allocate once only */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100157 if (!line6pcm->in.buffer) {
158 line6pcm->in.buffer =
Markus Grabner0ca54882012-01-20 00:09:09 +0100159 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
160 line6pcm->max_packet_size, GFP_KERNEL);
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100161 if (!line6pcm->in.buffer) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100162 err = -ENOMEM;
163 goto pcm_acquire_error;
164 }
Markus Grabner0ca54882012-01-20 00:09:09 +0100165 }
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100166
167 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100168 }
169
170 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200171 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200172 Waiting for completion of active URBs in the stop handler is
173 a bug, we therefore report an error if capturing is restarted
174 too soon.
175 */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100176 if (line6pcm->in.active_urbs || line6pcm->in.unlink_urbs) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100177 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Takashi Iwaieab22e42015-01-23 13:00:03 +0100178 err = -EBUSY;
179 goto pcm_acquire_error;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100180 }
181
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100182 line6pcm->in.count = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200183 line6pcm->prev_fsize = 0;
184 err = line6_submit_audio_in_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200185
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100186 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100187 goto pcm_acquire_error;
188
189 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200190 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200191
Markus Grabner0ca54882012-01-20 00:09:09 +0100192 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100193 /* Invoked multiple times in a row so allocate once only */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100194 if (!line6pcm->out.buffer) {
195 line6pcm->out.buffer =
Markus Grabner0ca54882012-01-20 00:09:09 +0100196 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
197 line6pcm->max_packet_size, GFP_KERNEL);
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100198 if (!line6pcm->out.buffer) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100199 err = -ENOMEM;
200 goto pcm_acquire_error;
201 }
Markus Grabner0ca54882012-01-20 00:09:09 +0100202 }
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100203
204 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100205 }
206
207 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200208 /*
Markus Grabner0ca54882012-01-20 00:09:09 +0100209 See comment above regarding PCM restart.
210 */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100211 if (line6pcm->out.active_urbs || line6pcm->out.unlink_urbs) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100212 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f472010-08-12 01:35:30 +0200213 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100214 }
215
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100216 line6pcm->out.count = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200217 err = line6_submit_audio_out_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200218
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100219 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100220 goto pcm_acquire_error;
221
222 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200223 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200224
Markus Grabner1027f472010-08-12 01:35:30 +0200225 return 0;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100226
Markus Grabner0ca54882012-01-20 00:09:09 +0100227pcm_acquire_error:
228 /*
229 If not all requested resources/streams could be obtained, release
230 those which were successfully obtained (if any).
231 */
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100232 line6_pcm_release(line6pcm, flags_final);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100233 return err;
Markus Grabner1027f472010-08-12 01:35:30 +0200234}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100235EXPORT_SYMBOL_GPL(line6_pcm_acquire);
Markus Grabner1027f472010-08-12 01:35:30 +0200236
Markus Grabner0ca54882012-01-20 00:09:09 +0100237int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200238{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200239 unsigned long flags_old, flags_new;
240
241 do {
242 flags_old = ACCESS_ONCE(line6pcm->flags);
243 flags_new = flags_old & ~channels;
244 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
Markus Grabner1027f472010-08-12 01:35:30 +0200245
Markus Grabner0ca54882012-01-20 00:09:09 +0100246 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
Takashi Iwaid8131e62015-01-23 16:18:42 +0100247 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100248
Markus Grabner0ca54882012-01-20 00:09:09 +0100249 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100250 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
Markus Grabner0ca54882012-01-20 00:09:09 +0100251 line6_free_capture_buffer(line6pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200252 }
253
Markus Grabner0ca54882012-01-20 00:09:09 +0100254 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
Takashi Iwaid8131e62015-01-23 16:18:42 +0100255 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100256
Markus Grabner0ca54882012-01-20 00:09:09 +0100257 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100258 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
Markus Grabner0ca54882012-01-20 00:09:09 +0100259 line6_free_playback_buffer(line6pcm);
Markus Grabner1027f472010-08-12 01:35:30 +0200260 }
Markus Grabner1027f472010-08-12 01:35:30 +0200261
262 return 0;
263}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100264EXPORT_SYMBOL_GPL(line6_pcm_release);
Markus Grabner1027f472010-08-12 01:35:30 +0200265
Markus Grabner705ecec2009-02-27 19:43:04 -0800266/* trigger callback */
267int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
268{
269 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800270 struct snd_pcm_substream *s;
Takashi Iwaif2a76222015-01-23 14:49:22 +0100271 int err = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800272
Markus Grabner0ca54882012-01-20 00:09:09 +0100273 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800274
Markus Grabner705ecec2009-02-27 19:43:04 -0800275 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai7d70c812015-01-19 15:11:17 +0100276 if (s->pcm->card != substream->pcm->card)
277 continue;
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800278 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800279 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner1027f472010-08-12 01:35:30 +0200280 err = snd_line6_playback_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800281 break;
282
283 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner1027f472010-08-12 01:35:30 +0200284 err = snd_line6_capture_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800285 break;
286
287 default:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200288 dev_err(line6pcm->line6->ifcdev,
289 "Unknown stream direction %d\n", s->stream);
Takashi Iwaif2a76222015-01-23 14:49:22 +0100290 err = -EINVAL;
291 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800292 }
Takashi Iwaif2a76222015-01-23 14:49:22 +0100293 if (err < 0)
294 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800295 }
296
Takashi Iwaif2a76222015-01-23 14:49:22 +0100297 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800298}
299
300/* control info callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200301static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
302 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800303{
Markus Grabner705ecec2009-02-27 19:43:04 -0800304 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
305 uinfo->count = 2;
306 uinfo->value.integer.min = 0;
307 uinfo->value.integer.max = 256;
308 return 0;
309}
310
311/* control get callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200312static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
313 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800314{
Markus Grabner705ecec2009-02-27 19:43:04 -0800315 int i;
316 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
317
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100318 for (i = 0; i < 2; i++)
Markus Grabner1027f472010-08-12 01:35:30 +0200319 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800320
321 return 0;
322}
323
324/* control put callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200325static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800327{
Markus Grabner705ecec2009-02-27 19:43:04 -0800328 int i, changed = 0;
329 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
330
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100331 for (i = 0; i < 2; i++)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200332 if (line6pcm->volume_playback[i] !=
333 ucontrol->value.integer.value[i]) {
334 line6pcm->volume_playback[i] =
335 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800336 changed = 1;
337 }
338
339 return changed;
340}
341
342/* control definition */
Takashi Iwai075587b2015-01-19 14:28:25 +0100343static struct snd_kcontrol_new line6_controls[] = {
344 {
345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
346 .name = "PCM Playback Volume",
347 .info = snd_line6_control_playback_info,
348 .get = snd_line6_control_playback_get,
349 .put = snd_line6_control_playback_put
350 },
351 {
352 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
353 .name = "Impulse Response Volume",
354 .info = snd_line6_impulse_volume_info,
355 .get = snd_line6_impulse_volume_get,
356 .put = snd_line6_impulse_volume_put
357 },
358 {
359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
360 .name = "Impulse Response Period",
361 .info = snd_line6_impulse_period_info,
362 .get = snd_line6_impulse_period_get,
363 .put = snd_line6_impulse_period_put
364 },
Markus Grabner705ecec2009-02-27 19:43:04 -0800365};
366
367/*
368 Cleanup the PCM device.
369*/
Takashi Iwaid8131e62015-01-23 16:18:42 +0100370static void cleanup_urbs(struct line6_pcm_stream *pcms)
Markus Grabner705ecec2009-02-27 19:43:04 -0800371{
372 int i;
Markus Grabner705ecec2009-02-27 19:43:04 -0800373
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100374 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100375 if (pcms->urbs[i]) {
376 usb_kill_urb(pcms->urbs[i]);
377 usb_free_urb(pcms->urbs[i]);
Markus Grabner705ecec2009-02-27 19:43:04 -0800378 }
379 }
Takashi Iwaid8131e62015-01-23 16:18:42 +0100380}
381
382static void line6_cleanup_pcm(struct snd_pcm *pcm)
383{
384 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
385
386 cleanup_urbs(&line6pcm->out);
387 cleanup_urbs(&line6pcm->in);
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100388 kfree(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800389}
390
391/* create a PCM device */
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100392static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
Markus Grabner705ecec2009-02-27 19:43:04 -0800393{
394 struct snd_pcm *pcm;
395 int err;
396
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100397 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
398 0, 1, 1, pcm_ret);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800399 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800400 return err;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100401 pcm = *pcm_ret;
402 strcpy(pcm->name, line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800403
404 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600405 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
406 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200407 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800408
409 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800410 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200411 snd_dma_continuous_data
412 (GFP_KERNEL), 64 * 1024,
413 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800414 return 0;
415}
416
417/*
Takashi Iwai5a475312015-01-19 16:15:54 +0100418 Sync with PCM stream stops.
Markus Grabner1027f472010-08-12 01:35:30 +0200419*/
420void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
421{
Takashi Iwaid8131e62015-01-23 16:18:42 +0100422 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
423 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
424 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
425 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
Markus Grabner1027f472010-08-12 01:35:30 +0200426}
427
428/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800429 Create and register the PCM device and mixer entries.
430 Create URBs for playback and capture.
431*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800432int line6_init_pcm(struct usb_line6 *line6,
433 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800434{
Takashi Iwai075587b2015-01-19 14:28:25 +0100435 int i, err;
Chris Rorvick16d603d2015-01-12 12:42:55 -0800436 unsigned ep_read = line6->properties->ep_audio_r;
437 unsigned ep_write = line6->properties->ep_audio_w;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100438 struct snd_pcm *pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -0800439 struct snd_line6_pcm *line6pcm;
440
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800441 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200442 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800443
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100444 err = snd_line6_new_pcm(line6, &pcm);
445 if (err < 0)
446 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800447
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100448 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
449 if (!line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800450 return -ENOMEM;
451
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100452 line6pcm->pcm = pcm;
453 line6pcm->properties = properties;
Markus Grabner1027f472010-08-12 01:35:30 +0200454 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
455 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800456 line6pcm->line6 = line6;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000457
458 /* Read and write buffers are sized identically, so choose minimum */
459 line6pcm->max_packet_size = min(
460 usb_maxpacket(line6->usbdev,
461 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
462 usb_maxpacket(line6->usbdev,
463 usb_sndisocpipe(line6->usbdev, ep_write), 1));
464
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100465 spin_lock_init(&line6pcm->out.lock);
466 spin_lock_init(&line6pcm->in.lock);
Takashi Iwai075587b2015-01-19 14:28:25 +0100467 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
Markus Grabner705ecec2009-02-27 19:43:04 -0800468
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100469 line6->line6pcm = line6pcm;
470
471 pcm->private_data = line6pcm;
472 pcm->private_free = line6_cleanup_pcm;
473
Markus Grabner1027f472010-08-12 01:35:30 +0200474 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800475 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800476 return err;
477
Markus Grabner1027f472010-08-12 01:35:30 +0200478 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800479 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800480 return err;
481
482 /* mixer: */
Takashi Iwai075587b2015-01-19 14:28:25 +0100483 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
484 err = snd_ctl_add(line6->card,
485 snd_ctl_new1(&line6_controls[i], line6pcm));
486 if (err < 0)
487 return err;
488 }
Markus Grabner1027f472010-08-12 01:35:30 +0200489
Markus Grabner705ecec2009-02-27 19:43:04 -0800490 return 0;
491}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100492EXPORT_SYMBOL_GPL(line6_init_pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800493
494/* prepare pcm callback */
495int snd_line6_prepare(struct snd_pcm_substream *substream)
496{
497 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
498
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100499 switch (substream->stream) {
500 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwaid8131e62015-01-23 16:18:42 +0100501 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0) {
502 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
503 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
504 }
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100505 break;
506
507 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwaid8131e62015-01-23 16:18:42 +0100508 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0) {
509 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
510 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
511 }
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100512 break;
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100513 }
514
Markus Grabner0ca54882012-01-20 00:09:09 +0100515 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100516 line6pcm->out.count = 0;
517 line6pcm->out.pos = 0;
518 line6pcm->out.pos_done = 0;
519 line6pcm->out.bytes = 0;
520 line6pcm->in.count = 0;
521 line6pcm->in.pos_done = 0;
522 line6pcm->in.bytes = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800523 }
524
525 return 0;
526}