blob: dedbe24cbf6073ba8f8b869a4b02d5d70a26a4cf [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
Takashi Iwaie90576c2015-01-23 16:25:03 +0100135static int line6_alloc_stream_buffer(struct snd_line6_pcm *line6pcm,
136 struct line6_pcm_stream *pcms)
137{
138 /* Invoked multiple times in a row so allocate once only */
139 if (pcms->buffer)
140 return 0;
141
142 pcms->buffer = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
143 line6pcm->max_packet_size, GFP_KERNEL);
144 if (!pcms->buffer)
145 return -ENOMEM;
146 return 0;
147}
148
149static void line6_free_stream_buffer(struct snd_line6_pcm *line6pcm,
150 struct line6_pcm_stream *pcms)
151{
152 kfree(pcms->buffer);
153 pcms->buffer = NULL;
154}
155
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100156static bool test_flags(unsigned long flags0, unsigned long flags1,
157 unsigned long mask)
158{
159 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
160}
161
Markus Grabner0ca54882012-01-20 00:09:09 +0100162int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200163{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200164 unsigned long flags_old, flags_new, flags_final;
165 int err;
166
167 do {
168 flags_old = ACCESS_ONCE(line6pcm->flags);
169 flags_new = flags_old | channels;
170 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
171
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100172 flags_final = 0;
Johannes Thumshirn82a74d42012-05-05 16:31:52 +0200173
Markus Grabner0ca54882012-01-20 00:09:09 +0100174 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
Takashi Iwaie90576c2015-01-23 16:25:03 +0100175 err = line6_alloc_stream_buffer(line6pcm, &line6pcm->in);
176 if (err < 0)
177 goto pcm_acquire_error;
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100178 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100179 }
180
181 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200182 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200183 Waiting for completion of active URBs in the stop handler is
184 a bug, we therefore report an error if capturing is restarted
185 too soon.
186 */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100187 if (line6pcm->in.active_urbs || line6pcm->in.unlink_urbs) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100188 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Takashi Iwaieab22e42015-01-23 13:00:03 +0100189 err = -EBUSY;
190 goto pcm_acquire_error;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100191 }
192
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100193 line6pcm->in.count = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200194 err = line6_submit_audio_in_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200195
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100196 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100197 goto pcm_acquire_error;
198
199 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200200 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200201
Markus Grabner0ca54882012-01-20 00:09:09 +0100202 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
Takashi Iwaie90576c2015-01-23 16:25:03 +0100203 err = line6_alloc_stream_buffer(line6pcm, &line6pcm->out);
204 if (err < 0)
205 goto pcm_acquire_error;
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100206 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100207 }
208
209 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200210 /*
Markus Grabner0ca54882012-01-20 00:09:09 +0100211 See comment above regarding PCM restart.
212 */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100213 if (line6pcm->out.active_urbs || line6pcm->out.unlink_urbs) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100214 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Markus Grabner1027f472010-08-12 01:35:30 +0200215 return -EBUSY;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100216 }
217
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100218 line6pcm->out.count = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200219 err = line6_submit_audio_out_all_urbs(line6pcm);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200220
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100221 if (err < 0)
Markus Grabner0ca54882012-01-20 00:09:09 +0100222 goto pcm_acquire_error;
223
224 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
Markus Grabner1027f472010-08-12 01:35:30 +0200225 }
Markus Grabnere1a164d2010-08-23 01:08:25 +0200226
Markus Grabner1027f472010-08-12 01:35:30 +0200227 return 0;
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100228
Markus Grabner0ca54882012-01-20 00:09:09 +0100229pcm_acquire_error:
230 /*
231 If not all requested resources/streams could be obtained, release
232 those which were successfully obtained (if any).
233 */
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100234 line6_pcm_release(line6pcm, flags_final);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100235 return err;
Markus Grabner1027f472010-08-12 01:35:30 +0200236}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100237EXPORT_SYMBOL_GPL(line6_pcm_acquire);
Markus Grabner1027f472010-08-12 01:35:30 +0200238
Markus Grabner0ca54882012-01-20 00:09:09 +0100239int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200240{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200241 unsigned long flags_old, flags_new;
242
243 do {
244 flags_old = ACCESS_ONCE(line6pcm->flags);
245 flags_new = flags_old & ~channels;
246 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
Markus Grabner1027f472010-08-12 01:35:30 +0200247
Takashi Iwaif2bb6142015-01-27 16:17:26 +0100248 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM)) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100249 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
Takashi Iwaif2bb6142015-01-27 16:17:26 +0100250 line6pcm->prev_fbuf = NULL;
251 line6pcm->prev_fsize = 0;
252 }
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100253
Markus Grabner0ca54882012-01-20 00:09:09 +0100254 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100255 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
Takashi Iwaie90576c2015-01-23 16:25:03 +0100256 line6_free_stream_buffer(line6pcm, &line6pcm->in);
Markus Grabner1027f472010-08-12 01:35:30 +0200257 }
258
Markus Grabner0ca54882012-01-20 00:09:09 +0100259 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
Takashi Iwaid8131e62015-01-23 16:18:42 +0100260 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100261
Markus Grabner0ca54882012-01-20 00:09:09 +0100262 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100263 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
Takashi Iwaie90576c2015-01-23 16:25:03 +0100264 line6_free_stream_buffer(line6pcm, &line6pcm->out);
Markus Grabner1027f472010-08-12 01:35:30 +0200265 }
Markus Grabner1027f472010-08-12 01:35:30 +0200266
267 return 0;
268}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100269EXPORT_SYMBOL_GPL(line6_pcm_release);
Markus Grabner1027f472010-08-12 01:35:30 +0200270
Markus Grabner705ecec2009-02-27 19:43:04 -0800271/* trigger callback */
272int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
273{
274 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
Markus Grabner705ecec2009-02-27 19:43:04 -0800275 struct snd_pcm_substream *s;
Takashi Iwaif2a76222015-01-23 14:49:22 +0100276 int err = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800277
Markus Grabner0ca54882012-01-20 00:09:09 +0100278 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800279
Markus Grabner705ecec2009-02-27 19:43:04 -0800280 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai7d70c812015-01-19 15:11:17 +0100281 if (s->pcm->card != substream->pcm->card)
282 continue;
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800283 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800284 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner1027f472010-08-12 01:35:30 +0200285 err = snd_line6_playback_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800286 break;
287
288 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner1027f472010-08-12 01:35:30 +0200289 err = snd_line6_capture_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800290 break;
291
292 default:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200293 dev_err(line6pcm->line6->ifcdev,
294 "Unknown stream direction %d\n", s->stream);
Takashi Iwaif2a76222015-01-23 14:49:22 +0100295 err = -EINVAL;
296 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800297 }
Takashi Iwaif2a76222015-01-23 14:49:22 +0100298 if (err < 0)
299 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800300 }
301
Takashi Iwaif2a76222015-01-23 14:49:22 +0100302 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800303}
304
305/* control info callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200306static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
307 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800308{
Markus Grabner705ecec2009-02-27 19:43:04 -0800309 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
310 uinfo->count = 2;
311 uinfo->value.integer.min = 0;
312 uinfo->value.integer.max = 256;
313 return 0;
314}
315
316/* control get callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200317static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
318 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800319{
Markus Grabner705ecec2009-02-27 19:43:04 -0800320 int i;
321 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
322
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100323 for (i = 0; i < 2; i++)
Markus Grabner1027f472010-08-12 01:35:30 +0200324 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800325
326 return 0;
327}
328
329/* control put callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200330static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
331 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800332{
Markus Grabner705ecec2009-02-27 19:43:04 -0800333 int i, changed = 0;
334 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
335
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100336 for (i = 0; i < 2; i++)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200337 if (line6pcm->volume_playback[i] !=
338 ucontrol->value.integer.value[i]) {
339 line6pcm->volume_playback[i] =
340 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800341 changed = 1;
342 }
343
344 return changed;
345}
346
347/* control definition */
Takashi Iwai075587b2015-01-19 14:28:25 +0100348static struct snd_kcontrol_new line6_controls[] = {
349 {
350 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
351 .name = "PCM Playback Volume",
352 .info = snd_line6_control_playback_info,
353 .get = snd_line6_control_playback_get,
354 .put = snd_line6_control_playback_put
355 },
356 {
357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
358 .name = "Impulse Response Volume",
359 .info = snd_line6_impulse_volume_info,
360 .get = snd_line6_impulse_volume_get,
361 .put = snd_line6_impulse_volume_put
362 },
363 {
364 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
365 .name = "Impulse Response Period",
366 .info = snd_line6_impulse_period_info,
367 .get = snd_line6_impulse_period_get,
368 .put = snd_line6_impulse_period_put
369 },
Markus Grabner705ecec2009-02-27 19:43:04 -0800370};
371
372/*
373 Cleanup the PCM device.
374*/
Takashi Iwaid8131e62015-01-23 16:18:42 +0100375static void cleanup_urbs(struct line6_pcm_stream *pcms)
Markus Grabner705ecec2009-02-27 19:43:04 -0800376{
377 int i;
Markus Grabner705ecec2009-02-27 19:43:04 -0800378
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100379 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
Takashi Iwaid8131e62015-01-23 16:18:42 +0100380 if (pcms->urbs[i]) {
381 usb_kill_urb(pcms->urbs[i]);
382 usb_free_urb(pcms->urbs[i]);
Markus Grabner705ecec2009-02-27 19:43:04 -0800383 }
384 }
Takashi Iwaid8131e62015-01-23 16:18:42 +0100385}
386
387static void line6_cleanup_pcm(struct snd_pcm *pcm)
388{
389 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
390
391 cleanup_urbs(&line6pcm->out);
392 cleanup_urbs(&line6pcm->in);
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100393 kfree(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800394}
395
396/* create a PCM device */
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100397static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
Markus Grabner705ecec2009-02-27 19:43:04 -0800398{
399 struct snd_pcm *pcm;
400 int err;
401
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100402 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
403 0, 1, 1, pcm_ret);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800404 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800405 return err;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100406 pcm = *pcm_ret;
407 strcpy(pcm->name, line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800408
409 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600410 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
411 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200412 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800413
414 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800415 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200416 snd_dma_continuous_data
417 (GFP_KERNEL), 64 * 1024,
418 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800419 return 0;
420}
421
422/*
Takashi Iwai5a475312015-01-19 16:15:54 +0100423 Sync with PCM stream stops.
Markus Grabner1027f472010-08-12 01:35:30 +0200424*/
425void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
426{
Takashi Iwaid8131e62015-01-23 16:18:42 +0100427 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
428 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
429 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
430 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
Markus Grabner1027f472010-08-12 01:35:30 +0200431}
432
433/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800434 Create and register the PCM device and mixer entries.
435 Create URBs for playback and capture.
436*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800437int line6_init_pcm(struct usb_line6 *line6,
438 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800439{
Takashi Iwai075587b2015-01-19 14:28:25 +0100440 int i, err;
Chris Rorvick16d603d2015-01-12 12:42:55 -0800441 unsigned ep_read = line6->properties->ep_audio_r;
442 unsigned ep_write = line6->properties->ep_audio_w;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100443 struct snd_pcm *pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -0800444 struct snd_line6_pcm *line6pcm;
445
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800446 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200447 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800448
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100449 err = snd_line6_new_pcm(line6, &pcm);
450 if (err < 0)
451 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800452
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100453 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
454 if (!line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800455 return -ENOMEM;
456
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100457 line6pcm->pcm = pcm;
458 line6pcm->properties = properties;
Markus Grabner1027f472010-08-12 01:35:30 +0200459 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
460 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800461 line6pcm->line6 = line6;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000462
463 /* Read and write buffers are sized identically, so choose minimum */
464 line6pcm->max_packet_size = min(
465 usb_maxpacket(line6->usbdev,
466 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
467 usb_maxpacket(line6->usbdev,
468 usb_sndisocpipe(line6->usbdev, ep_write), 1));
469
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100470 spin_lock_init(&line6pcm->out.lock);
471 spin_lock_init(&line6pcm->in.lock);
Takashi Iwai075587b2015-01-19 14:28:25 +0100472 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
Markus Grabner705ecec2009-02-27 19:43:04 -0800473
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100474 line6->line6pcm = line6pcm;
475
476 pcm->private_data = line6pcm;
477 pcm->private_free = line6_cleanup_pcm;
478
Markus Grabner1027f472010-08-12 01:35:30 +0200479 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800480 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800481 return err;
482
Markus Grabner1027f472010-08-12 01:35:30 +0200483 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800484 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800485 return err;
486
487 /* mixer: */
Takashi Iwai075587b2015-01-19 14:28:25 +0100488 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
489 err = snd_ctl_add(line6->card,
490 snd_ctl_new1(&line6_controls[i], line6pcm));
491 if (err < 0)
492 return err;
493 }
Markus Grabner1027f472010-08-12 01:35:30 +0200494
Markus Grabner705ecec2009-02-27 19:43:04 -0800495 return 0;
496}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100497EXPORT_SYMBOL_GPL(line6_init_pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800498
499/* prepare pcm callback */
500int snd_line6_prepare(struct snd_pcm_substream *substream)
501{
502 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
503
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100504 switch (substream->stream) {
505 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwaid8131e62015-01-23 16:18:42 +0100506 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0) {
507 line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
508 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
509 }
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100510 break;
511
512 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwaid8131e62015-01-23 16:18:42 +0100513 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0) {
514 line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
515 line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
516 }
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100517 break;
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100518 }
519
Markus Grabner0ca54882012-01-20 00:09:09 +0100520 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100521 line6pcm->out.count = 0;
522 line6pcm->out.pos = 0;
523 line6pcm->out.pos_done = 0;
524 line6pcm->out.bytes = 0;
525 line6pcm->in.count = 0;
526 line6pcm->in.pos_done = 0;
527 line6pcm->in.bytes = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800528 }
529
530 return 0;
531}