2 * Line 6 Linux USB driver
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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.
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
23 /* impulse response volume controls */
24 static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
25 struct snd_ctl_elem_info *uinfo)
27 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
29 uinfo->value.integer.min = 0;
30 uinfo->value.integer.max = 255;
34 static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
35 struct snd_ctl_elem_value *ucontrol)
37 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
39 ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
43 static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
44 struct snd_ctl_elem_value *ucontrol)
46 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
47 int value = ucontrol->value.integer.value[0];
49 if (line6pcm->impulse_volume == value)
52 line6pcm->impulse_volume = value;
54 line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
56 line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
60 /* impulse response period controls */
61 static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
62 struct snd_ctl_elem_info *uinfo)
64 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
66 uinfo->value.integer.min = 0;
67 uinfo->value.integer.max = 2000;
71 static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_value *ucontrol)
74 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
76 ucontrol->value.integer.value[0] = line6pcm->impulse_period;
80 static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
81 struct snd_ctl_elem_value *ucontrol)
83 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
84 int value = ucontrol->value.integer.value[0];
86 if (line6pcm->impulse_period == value)
89 line6pcm->impulse_period = value;
93 static bool test_flags(unsigned long flags0, unsigned long flags1,
96 return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
99 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
101 unsigned long flags_old, flags_new, flags_final;
105 flags_old = ACCESS_ONCE(line6pcm->flags);
106 flags_new = flags_old | channels;
107 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
111 line6pcm->prev_fbuf = NULL;
113 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
114 /* Invoked multiple times in a row so allocate once only */
115 if (!line6pcm->in.buffer) {
116 line6pcm->in.buffer =
117 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
118 line6pcm->max_packet_size, GFP_KERNEL);
119 if (!line6pcm->in.buffer) {
121 goto pcm_acquire_error;
125 flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
128 if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
130 Waiting for completion of active URBs in the stop handler is
131 a bug, we therefore report an error if capturing is restarted
134 if (line6pcm->in.active_urbs || line6pcm->in.unlink_urbs) {
135 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
137 goto pcm_acquire_error;
140 line6pcm->in.count = 0;
141 line6pcm->prev_fsize = 0;
142 err = line6_submit_audio_in_all_urbs(line6pcm);
145 goto pcm_acquire_error;
147 flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
150 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
151 /* Invoked multiple times in a row so allocate once only */
152 if (!line6pcm->out.buffer) {
153 line6pcm->out.buffer =
154 kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
155 line6pcm->max_packet_size, GFP_KERNEL);
156 if (!line6pcm->out.buffer) {
158 goto pcm_acquire_error;
162 flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
165 if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
167 See comment above regarding PCM restart.
169 if (line6pcm->out.active_urbs || line6pcm->out.unlink_urbs) {
170 dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
174 line6pcm->out.count = 0;
175 err = line6_submit_audio_out_all_urbs(line6pcm);
178 goto pcm_acquire_error;
180 flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
187 If not all requested resources/streams could be obtained, release
188 those which were successfully obtained (if any).
190 line6_pcm_release(line6pcm, flags_final);
193 EXPORT_SYMBOL_GPL(line6_pcm_acquire);
195 int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
197 unsigned long flags_old, flags_new;
200 flags_old = ACCESS_ONCE(line6pcm->flags);
201 flags_new = flags_old & ~channels;
202 } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
204 if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
205 line6_unlink_audio_in_urbs(line6pcm);
207 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);
212 if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
213 line6_unlink_audio_out_urbs(line6pcm);
215 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);
222 EXPORT_SYMBOL_GPL(line6_pcm_release);
224 /* trigger callback */
225 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
227 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
228 struct snd_pcm_substream *s;
231 clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
233 snd_pcm_group_for_each_entry(s, substream) {
234 if (s->pcm->card != substream->pcm->card)
237 case SNDRV_PCM_STREAM_PLAYBACK:
238 err = snd_line6_playback_trigger(line6pcm, cmd);
241 case SNDRV_PCM_STREAM_CAPTURE:
242 err = snd_line6_capture_trigger(line6pcm, cmd);
246 dev_err(line6pcm->line6->ifcdev,
247 "Unknown stream direction %d\n", s->stream);
258 /* control info callback */
259 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_info *uinfo)
262 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
264 uinfo->value.integer.min = 0;
265 uinfo->value.integer.max = 256;
269 /* control get callback */
270 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
271 struct snd_ctl_elem_value *ucontrol)
274 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
276 for (i = 0; i < 2; i++)
277 ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
282 /* control put callback */
283 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
284 struct snd_ctl_elem_value *ucontrol)
287 struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
289 for (i = 0; i < 2; i++)
290 if (line6pcm->volume_playback[i] !=
291 ucontrol->value.integer.value[i]) {
292 line6pcm->volume_playback[i] =
293 ucontrol->value.integer.value[i];
300 /* control definition */
301 static struct snd_kcontrol_new line6_controls[] = {
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
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
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
326 Cleanup the PCM device.
328 static void line6_cleanup_pcm(struct snd_pcm *pcm)
331 struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
333 for (i = 0; i < LINE6_ISO_BUFFERS; i++) {
334 if (line6pcm->out.urbs[i]) {
335 usb_kill_urb(line6pcm->out.urbs[i]);
336 usb_free_urb(line6pcm->out.urbs[i]);
338 if (line6pcm->in.urbs[i]) {
339 usb_kill_urb(line6pcm->in.urbs[i]);
340 usb_free_urb(line6pcm->in.urbs[i]);
346 /* create a PCM device */
347 static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
352 err = snd_pcm_new(line6->card, (char *)line6->properties->name,
357 strcpy(pcm->name, line6->properties->name);
360 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
361 &snd_line6_playback_ops);
362 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
364 /* pre-allocation of buffers */
365 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
366 snd_dma_continuous_data
367 (GFP_KERNEL), 64 * 1024,
373 Sync with PCM stream stops.
375 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
377 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
378 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
382 Create and register the PCM device and mixer entries.
383 Create URBs for playback and capture.
385 int line6_init_pcm(struct usb_line6 *line6,
386 struct line6_pcm_properties *properties)
389 unsigned ep_read = line6->properties->ep_audio_r;
390 unsigned ep_write = line6->properties->ep_audio_w;
392 struct snd_line6_pcm *line6pcm;
394 if (!(line6->properties->capabilities & LINE6_CAP_PCM))
395 return 0; /* skip PCM initialization and report success */
397 err = snd_line6_new_pcm(line6, &pcm);
401 line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
406 line6pcm->properties = properties;
407 line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
408 line6pcm->volume_monitor = 255;
409 line6pcm->line6 = line6;
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));
418 spin_lock_init(&line6pcm->out.lock);
419 spin_lock_init(&line6pcm->in.lock);
420 line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
422 line6->line6pcm = line6pcm;
424 pcm->private_data = line6pcm;
425 pcm->private_free = line6_cleanup_pcm;
427 err = line6_create_audio_out_urbs(line6pcm);
431 err = line6_create_audio_in_urbs(line6pcm);
436 for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
437 err = snd_ctl_add(line6->card,
438 snd_ctl_new1(&line6_controls[i], line6pcm));
445 EXPORT_SYMBOL_GPL(line6_init_pcm);
447 /* prepare pcm callback */
448 int snd_line6_prepare(struct snd_pcm_substream *substream)
450 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
452 switch (substream->stream) {
453 case SNDRV_PCM_STREAM_PLAYBACK:
454 if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
455 line6_unlink_wait_clear_audio_out_urbs(line6pcm);
459 case SNDRV_PCM_STREAM_CAPTURE:
460 if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
461 line6_unlink_wait_clear_audio_in_urbs(line6pcm);
466 if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
467 line6pcm->out.count = 0;
468 line6pcm->out.pos = 0;
469 line6pcm->out.pos_done = 0;
470 line6pcm->out.bytes = 0;
471 line6pcm->in.count = 0;
472 line6pcm->in.pos_done = 0;
473 line6pcm->in.bytes = 0;