blob: 43474c4ebb6bc516af23d9fb8ac09d3bd7efa09b [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
Markus Grabner6b02a17e2011-12-10 02:12:32 +010093static bool test_flags(unsigned long flags0, unsigned long flags1,
94 unsigned long mask)
95{
96 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
97}
98
Markus Grabner0ca54882012-01-20 00:09:09 +010099int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
Markus Grabner1027f472010-08-12 01:35:30 +0200100{
Arnd Bergmann9f613602013-06-21 21:55:35 +0200101 unsigned long flags_old, flags_new, flags_final;
102 int err;
103
104 do {
105 flags_old = ACCESS_ONCE(line6pcm->flags);
106 flags_new = flags_old | channels;
107 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
108
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100109 flags_final = 0;
Johannes Thumshirn82a74d42012-05-05 16:31:52 +0200110
Markus Grabner1027f472010-08-12 01:35:30 +0200111 line6pcm->prev_fbuf = NULL;
Markus Grabnere1a164d2010-08-23 01:08:25 +0200112
Markus Grabner0ca54882012-01-20 00:09:09 +0100113 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
Stefan Hajnoczi928f25e2012-11-11 13:24:41 +0100114 /* Invoked multiple times in a row so allocate once only */
Markus Grabner0ca54882012-01-20 00:09:09 +0100115 if (!line6pcm->buffer_in) {
116 line6pcm->buffer_in =
117 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
118 line6pcm->max_packet_size, GFP_KERNEL);
Markus Grabner0ca54882012-01-20 00:09:09 +0100119 if (!line6pcm->buffer_in) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100120 err = -ENOMEM;
121 goto pcm_acquire_error;
122 }
Markus Grabner0ca54882012-01-20 00:09:09 +0100123 }
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100124
125 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100126 }
127
128 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200129 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200130 Waiting for completion of active URBs in the stop handler is
131 a bug, we therefore report an error if capturing is restarted
132 too soon.
133 */
Takashi Iwai6aa7f8e2015-01-23 14:31:03 +0100134 if (line6pcm->active_urb_in || line6pcm->unlink_urb_in) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100135 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
Takashi Iwaieab22e42015-01-23 13:00:03 +0100136 err = -EBUSY;
137 goto pcm_acquire_error;
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 }
Markus Grabner0ca54882012-01-20 00:09:09 +0100160 }
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100161
162 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
Markus Grabner0ca54882012-01-20 00:09:09 +0100163 }
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 */
Takashi Iwai6aa7f8e2015-01-23 14:31:03 +0100169 if (line6pcm->active_urb_out || line6pcm->unlink_urb_out) {
Markus Grabner0ca54882012-01-20 00:09:09 +0100170 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 */
Takashi Iwai5343ecf42015-01-23 14:34:42 +0100190 line6_pcm_release(line6pcm, flags_final);
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;
Takashi Iwaif2a76222015-01-23 14:49:22 +0100229 int err = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800230
Markus Grabner0ca54882012-01-20 00:09:09 +0100231 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800232
Markus Grabner705ecec2009-02-27 19:43:04 -0800233 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai7d70c812015-01-19 15:11:17 +0100234 if (s->pcm->card != substream->pcm->card)
235 continue;
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800236 switch (s->stream) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800237 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner1027f472010-08-12 01:35:30 +0200238 err = snd_line6_playback_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800239 break;
240
241 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner1027f472010-08-12 01:35:30 +0200242 err = snd_line6_capture_trigger(line6pcm, cmd);
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 break;
244
245 default:
Markus Grabnere1a164d2010-08-23 01:08:25 +0200246 dev_err(line6pcm->line6->ifcdev,
247 "Unknown stream direction %d\n", s->stream);
Takashi Iwaif2a76222015-01-23 14:49:22 +0100248 err = -EINVAL;
249 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800250 }
Takashi Iwaif2a76222015-01-23 14:49:22 +0100251 if (err < 0)
252 break;
Markus Grabner705ecec2009-02-27 19:43:04 -0800253 }
254
Takashi Iwaif2a76222015-01-23 14:49:22 +0100255 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800256}
257
258/* control info callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200259static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_info *uinfo)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800261{
Markus Grabner705ecec2009-02-27 19:43:04 -0800262 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
263 uinfo->count = 2;
264 uinfo->value.integer.min = 0;
265 uinfo->value.integer.max = 256;
266 return 0;
267}
268
269/* control get callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200270static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_value *ucontrol)
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800272{
Markus Grabner705ecec2009-02-27 19:43:04 -0800273 int i;
274 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
275
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100276 for (i = 0; i < 2; i++)
Markus Grabner1027f472010-08-12 01:35:30 +0200277 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800278
279 return 0;
280}
281
282/* control put callback */
Markus Grabner1027f472010-08-12 01:35:30 +0200283static int snd_line6_control_playback_put(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, changed = 0;
287 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
288
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100289 for (i = 0; i < 2; i++)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200290 if (line6pcm->volume_playback[i] !=
291 ucontrol->value.integer.value[i]) {
292 line6pcm->volume_playback[i] =
293 ucontrol->value.integer.value[i];
Markus Grabner705ecec2009-02-27 19:43:04 -0800294 changed = 1;
295 }
296
297 return changed;
298}
299
300/* control definition */
Takashi Iwai075587b2015-01-19 14:28:25 +0100301static struct snd_kcontrol_new line6_controls[] = {
302 {
303 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
304 .name = "PCM Playback Volume",
305 .info = snd_line6_control_playback_info,
306 .get = snd_line6_control_playback_get,
307 .put = snd_line6_control_playback_put
308 },
309 {
310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
311 .name = "Impulse Response Volume",
312 .info = snd_line6_impulse_volume_info,
313 .get = snd_line6_impulse_volume_get,
314 .put = snd_line6_impulse_volume_put
315 },
316 {
317 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
318 .name = "Impulse Response Period",
319 .info = snd_line6_impulse_period_info,
320 .get = snd_line6_impulse_period_get,
321 .put = snd_line6_impulse_period_put
322 },
Markus Grabner705ecec2009-02-27 19:43:04 -0800323};
324
325/*
326 Cleanup the PCM device.
327*/
328static void line6_cleanup_pcm(struct snd_pcm *pcm)
329{
330 int i;
331 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
332
Takashi Iwai9fb754b2015-01-23 15:08:40 +0100333 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800334 if (line6pcm->urb_audio_out[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800335 usb_kill_urb(line6pcm->urb_audio_out[i]);
336 usb_free_urb(line6pcm->urb_audio_out[i]);
337 }
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800338 if (line6pcm->urb_audio_in[i]) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800339 usb_kill_urb(line6pcm->urb_audio_in[i]);
340 usb_free_urb(line6pcm->urb_audio_in[i]);
341 }
342 }
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100343 kfree(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800344}
345
346/* create a PCM device */
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100347static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
Markus Grabner705ecec2009-02-27 19:43:04 -0800348{
349 struct snd_pcm *pcm;
350 int err;
351
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100352 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
353 0, 1, 1, pcm_ret);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800354 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800355 return err;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100356 pcm = *pcm_ret;
357 strcpy(pcm->name, line6->properties->name);
Markus Grabner705ecec2009-02-27 19:43:04 -0800358
359 /* set operators */
Shawn Bohrerafb90912009-11-15 22:17:57 -0600360 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
361 &snd_line6_playback_ops);
Markus Grabnere1a164d2010-08-23 01:08:25 +0200362 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
Markus Grabner705ecec2009-02-27 19:43:04 -0800363
364 /* pre-allocation of buffers */
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800365 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200366 snd_dma_continuous_data
367 (GFP_KERNEL), 64 * 1024,
368 128 * 1024);
Markus Grabner705ecec2009-02-27 19:43:04 -0800369 return 0;
370}
371
372/*
Takashi Iwai5a475312015-01-19 16:15:54 +0100373 Sync with PCM stream stops.
Markus Grabner1027f472010-08-12 01:35:30 +0200374*/
375void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
376{
Markus Grabner1027f472010-08-12 01:35:30 +0200377 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
378 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
379}
380
381/*
Markus Grabner705ecec2009-02-27 19:43:04 -0800382 Create and register the PCM device and mixer entries.
383 Create URBs for playback and capture.
384*/
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800385int line6_init_pcm(struct usb_line6 *line6,
386 struct line6_pcm_properties *properties)
Markus Grabner705ecec2009-02-27 19:43:04 -0800387{
Takashi Iwai075587b2015-01-19 14:28:25 +0100388 int i, err;
Chris Rorvick16d603d2015-01-12 12:42:55 -0800389 unsigned ep_read = line6->properties->ep_audio_r;
390 unsigned ep_write = line6->properties->ep_audio_w;
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100391 struct snd_pcm *pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -0800392 struct snd_line6_pcm *line6pcm;
393
Chris Rorvick4cb1a4a2015-01-12 12:42:45 -0800394 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
Markus Grabnere1a164d2010-08-23 01:08:25 +0200395 return 0; /* skip PCM initialization and report success */
Markus Grabner705ecec2009-02-27 19:43:04 -0800396
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100397 err = snd_line6_new_pcm(line6, &pcm);
398 if (err < 0)
399 return err;
Markus Grabner705ecec2009-02-27 19:43:04 -0800400
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100401 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
402 if (!line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800403 return -ENOMEM;
404
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100405 line6pcm->pcm = pcm;
406 line6pcm->properties = properties;
Markus Grabner1027f472010-08-12 01:35:30 +0200407 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
408 line6pcm->volume_monitor = 255;
Markus Grabner705ecec2009-02-27 19:43:04 -0800409 line6pcm->line6 = line6;
Stefan Hajnoczi3b08db32011-11-23 08:20:44 +0000410
411 /* Read and write buffers are sized identically, so choose minimum */
412 line6pcm->max_packet_size = min(
413 usb_maxpacket(line6->usbdev,
414 usb_rcvisocpipe(line6->usbdev, ep_read), 0),
415 usb_maxpacket(line6->usbdev,
416 usb_sndisocpipe(line6->usbdev, ep_write), 1));
417
Markus Grabner705ecec2009-02-27 19:43:04 -0800418 spin_lock_init(&line6pcm->lock_audio_out);
419 spin_lock_init(&line6pcm->lock_audio_in);
Takashi Iwai075587b2015-01-19 14:28:25 +0100420 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
Markus Grabner705ecec2009-02-27 19:43:04 -0800421
Takashi Iwaib45a7c52015-01-19 14:41:57 +0100422 line6->line6pcm = line6pcm;
423
424 pcm->private_data = line6pcm;
425 pcm->private_free = line6_cleanup_pcm;
426
Markus Grabner1027f472010-08-12 01:35:30 +0200427 err = line6_create_audio_out_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800428 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800429 return err;
430
Markus Grabner1027f472010-08-12 01:35:30 +0200431 err = line6_create_audio_in_urbs(line6pcm);
Greg Kroah-Hartman68dc3dd2009-02-27 22:43:30 -0800432 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800433 return err;
434
435 /* mixer: */
Takashi Iwai075587b2015-01-19 14:28:25 +0100436 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
437 err = snd_ctl_add(line6->card,
438 snd_ctl_new1(&line6_controls[i], line6pcm));
439 if (err < 0)
440 return err;
441 }
Markus Grabner1027f472010-08-12 01:35:30 +0200442
Markus Grabner705ecec2009-02-27 19:43:04 -0800443 return 0;
444}
Takashi Iwaiccddbe42015-01-15 08:22:31 +0100445EXPORT_SYMBOL_GPL(line6_init_pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800446
447/* prepare pcm callback */
448int snd_line6_prepare(struct snd_pcm_substream *substream)
449{
450 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
451
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100452 switch (substream->stream) {
453 case SNDRV_PCM_STREAM_PLAYBACK:
Markus Grabner0ca54882012-01-20 00:09:09 +0100454 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100455 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
456
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100457 break;
458
459 case SNDRV_PCM_STREAM_CAPTURE:
Markus Grabner0ca54882012-01-20 00:09:09 +0100460 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
Markus Grabner6b02a17e2011-12-10 02:12:32 +0100461 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
462
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100463 break;
Stefan Hajnoczi665f3f52011-12-10 02:12:31 +0100464 }
465
Markus Grabner0ca54882012-01-20 00:09:09 +0100466 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
Markus Grabner1027f472010-08-12 01:35:30 +0200467 line6pcm->count_out = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800468 line6pcm->pos_out = 0;
469 line6pcm->pos_out_done = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800470 line6pcm->bytes_out = 0;
Markus Grabner1027f472010-08-12 01:35:30 +0200471 line6pcm->count_in = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800472 line6pcm->pos_in_done = 0;
473 line6pcm->bytes_in = 0;
474 }
475
476 return 0;
477}