2 * Line6 Linux USB driver - 0.9.1beta
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 <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
25 Software stereo volume control.
27 static void change_volume(struct urb *urb_out, int volume[],
32 if (volume[0] == 256 && volume[1] == 256)
33 return; /* maximum volume - no change */
35 if (bytes_per_frame == 4) {
38 p = (short *)urb_out->transfer_buffer;
39 buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
41 for (; p < buf_end; ++p) {
42 *p = (*p * volume[chn & 1]) >> 8;
45 } else if (bytes_per_frame == 6) {
46 unsigned char *p, *buf_end;
48 p = (unsigned char *)urb_out->transfer_buffer;
49 buf_end = p + urb_out->transfer_buffer_length;
51 for (; p < buf_end; p += 3) {
54 val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
55 val = (val * volume[chn & 1]) >> 8;
64 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
67 Create signal for impulse response test.
69 static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
70 struct urb *urb_out, int bytes_per_frame)
72 int frames = urb_out->transfer_buffer_length / bytes_per_frame;
74 if (bytes_per_frame == 4) {
76 short *pi = (short *)line6pcm->prev_fbuf;
77 short *po = (short *)urb_out->transfer_buffer;
79 for (i = 0; i < frames; ++i) {
85 } else if (bytes_per_frame == 6) {
87 unsigned char *pi = line6pcm->prev_fbuf;
88 unsigned char *po = urb_out->transfer_buffer;
90 for (i = 0; i < frames; ++i) {
91 for (j = 0; j < bytes_per_frame / 2; ++j)
94 for (; j < bytes_per_frame; ++j)
97 pi += bytes_per_frame;
98 po += bytes_per_frame;
101 if (--line6pcm->impulse_count <= 0) {
102 ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
104 line6pcm->impulse_volume;
105 line6pcm->impulse_count = line6pcm->impulse_period;
112 Add signal to buffer for software monitoring.
114 static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
115 int volume, int bytes_per_frame)
118 return; /* zero volume - no change */
120 if (bytes_per_frame == 4) {
121 short *pi, *po, *buf_end;
123 pi = (short *)signal;
124 po = (short *)urb_out->transfer_buffer;
125 buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
127 for (; po < buf_end; ++pi, ++po)
128 *po += (*pi * volume) >> 8;
132 We don't need to handle devices with 6 bytes per frame here
133 since they all support hardware monitoring.
138 Find a free URB, prepare audio data, and submit URB.
140 static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
144 int i, urb_size, urb_frames;
146 const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
147 const int frame_increment =
148 line6pcm->properties->snd_line6_rates.rats[0].num_min;
149 const int frame_factor =
150 line6pcm->properties->snd_line6_rates.rats[0].den *
151 (USB_INTERVALS_PER_SECOND / LINE6_ISO_INTERVAL);
154 spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
156 find_first_zero_bit(&line6pcm->active_urb_out, LINE6_ISO_BUFFERS);
158 if (index < 0 || index >= LINE6_ISO_BUFFERS) {
159 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
160 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
164 urb_out = line6pcm->urb_audio_out[index];
167 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
168 /* compute frame size for given sampling rate */
170 struct usb_iso_packet_descriptor *fout =
171 &urb_out->iso_frame_desc[i];
173 if (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM)
174 fsize = line6pcm->prev_fsize;
179 line6pcm->count_out += frame_increment;
180 n = line6pcm->count_out / frame_factor;
181 line6pcm->count_out -= n * frame_factor;
182 fsize = n * bytes_per_frame;
185 fout->offset = urb_size;
186 fout->length = fsize;
191 /* can't determine URB size */
192 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
193 dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
197 urb_frames = urb_size / bytes_per_frame;
198 urb_out->transfer_buffer =
199 line6pcm->buffer_out +
200 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
201 urb_out->transfer_buffer_length = urb_size;
202 urb_out->context = line6pcm;
204 if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags) &&
205 !test_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags)) {
206 struct snd_pcm_runtime *runtime =
207 get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
209 if (line6pcm->pos_out + urb_frames > runtime->buffer_size) {
211 The transferred area goes over buffer boundary,
212 copy the data to the temp buffer.
216 len = runtime->buffer_size - line6pcm->pos_out;
219 memcpy(urb_out->transfer_buffer,
221 line6pcm->pos_out * bytes_per_frame,
222 len * bytes_per_frame);
223 memcpy(urb_out->transfer_buffer +
224 len * bytes_per_frame, runtime->dma_area,
225 (urb_frames - len) * bytes_per_frame);
227 dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
230 memcpy(urb_out->transfer_buffer,
232 line6pcm->pos_out * bytes_per_frame,
233 urb_out->transfer_buffer_length);
236 line6pcm->pos_out += urb_frames;
237 if (line6pcm->pos_out >= runtime->buffer_size)
238 line6pcm->pos_out -= runtime->buffer_size;
240 memset(urb_out->transfer_buffer, 0,
241 urb_out->transfer_buffer_length);
244 change_volume(urb_out, line6pcm->volume_playback, bytes_per_frame);
246 if (line6pcm->prev_fbuf != NULL) {
247 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
248 if (line6pcm->flags & LINE6_BITS_PCM_IMPULSE) {
249 create_impulse_test_signal(line6pcm, urb_out,
251 if (line6pcm->flags &
252 LINE6_BIT_PCM_ALSA_CAPTURE_STREAM) {
253 line6_capture_copy(line6pcm,
254 urb_out->transfer_buffer,
256 transfer_buffer_length);
257 line6_capture_check_period(line6pcm,
258 urb_out->transfer_buffer_length);
264 properties->capabilities & LINE6_BIT_HWMON)
265 && (line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM)
266 && (line6pcm->flags & LINE6_BITS_CAPTURE_STREAM))
267 add_monitor_signal(urb_out, line6pcm->prev_fbuf,
268 line6pcm->volume_monitor,
270 #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
275 ret = usb_submit_urb(urb_out, GFP_ATOMIC);
278 set_bit(index, &line6pcm->active_urb_out);
280 dev_err(line6pcm->line6->ifcdev,
281 "URB out #%d submission failed (%d)\n", index, ret);
283 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
288 Submit all currently available playback URBs.
290 int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
294 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
295 ret = submit_audio_out_urb(line6pcm);
304 Unlink all currently active playback URBs.
306 void line6_unlink_audio_out_urbs(struct snd_line6_pcm *line6pcm)
310 for (i = LINE6_ISO_BUFFERS; i--;) {
311 if (test_bit(i, &line6pcm->active_urb_out)) {
312 if (!test_and_set_bit(i, &line6pcm->unlink_urb_out)) {
313 struct urb *u = line6pcm->urb_audio_out[i];
322 Wait until unlinking of all currently active playback URBs has been
325 void line6_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
333 for (i = LINE6_ISO_BUFFERS; i--;) {
334 if (test_bit(i, &line6pcm->active_urb_out))
339 set_current_state(TASK_UNINTERRUPTIBLE);
341 } while (--timeout > 0);
343 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
347 Unlink all currently active playback URBs, and wait for finishing.
349 void line6_unlink_wait_clear_audio_out_urbs(struct snd_line6_pcm *line6pcm)
351 line6_unlink_audio_out_urbs(line6pcm);
352 line6_wait_clear_audio_out_urbs(line6pcm);
355 void line6_free_playback_buffer(struct snd_line6_pcm *line6pcm)
357 kfree(line6pcm->buffer_out);
358 line6pcm->buffer_out = NULL;
362 Callback for completed playback URB.
364 static void audio_out_callback(struct urb *urb)
366 int i, index, length = 0, shutdown = 0;
368 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
369 struct snd_pcm_substream *substream =
370 get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
372 #if USE_CLEAR_BUFFER_WORKAROUND
373 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
376 line6pcm->last_frame_out = urb->start_frame;
378 /* find index of URB */
379 for (index = LINE6_ISO_BUFFERS; index--;)
380 if (urb == line6pcm->urb_audio_out[index])
384 return; /* URB has been unlinked asynchronously */
386 for (i = LINE6_ISO_PACKETS; i--;)
387 length += urb->iso_frame_desc[i].length;
389 spin_lock_irqsave(&line6pcm->lock_audio_out, flags);
391 if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM, &line6pcm->flags)) {
392 struct snd_pcm_runtime *runtime = substream->runtime;
394 line6pcm->pos_out_done +=
395 length / line6pcm->properties->bytes_per_frame;
397 if (line6pcm->pos_out_done >= runtime->buffer_size)
398 line6pcm->pos_out_done -= runtime->buffer_size;
401 clear_bit(index, &line6pcm->active_urb_out);
403 for (i = LINE6_ISO_PACKETS; i--;)
404 if (urb->iso_frame_desc[i].status == -EXDEV) {
409 if (test_and_clear_bit(index, &line6pcm->unlink_urb_out))
412 spin_unlock_irqrestore(&line6pcm->lock_audio_out, flags);
415 submit_audio_out_urb(line6pcm);
417 if (test_bit(LINE6_INDEX_PCM_ALSA_PLAYBACK_STREAM,
419 line6pcm->bytes_out += length;
420 if (line6pcm->bytes_out >= line6pcm->period_out) {
421 line6pcm->bytes_out %= line6pcm->period_out;
422 snd_pcm_period_elapsed(substream);
428 /* open playback callback */
429 static int snd_line6_playback_open(struct snd_pcm_substream *substream)
432 struct snd_pcm_runtime *runtime = substream->runtime;
433 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
435 err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
437 properties->snd_line6_rates));
441 runtime->hw = line6pcm->properties->snd_line6_playback_hw;
445 /* close playback callback */
446 static int snd_line6_playback_close(struct snd_pcm_substream *substream)
451 /* hw_params playback callback */
452 static int snd_line6_playback_hw_params(struct snd_pcm_substream *substream,
453 struct snd_pcm_hw_params *hw_params)
456 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
458 /* -- Florian Demski [FD] */
459 /* don't ask me why, but this fixes the bug on my machine */
460 if (line6pcm == NULL) {
461 if (substream->pcm == NULL)
463 if (substream->pcm->private_data == NULL)
465 substream->private_data = substream->pcm->private_data;
466 line6pcm = snd_pcm_substream_chip(substream);
470 ret = line6_pcm_acquire(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
475 ret = snd_pcm_lib_malloc_pages(substream,
476 params_buffer_bytes(hw_params));
478 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
482 line6pcm->period_out = params_period_bytes(hw_params);
486 /* hw_free playback callback */
487 static int snd_line6_playback_hw_free(struct snd_pcm_substream *substream)
489 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
491 line6_pcm_release(line6pcm, LINE6_BIT_PCM_ALSA_PLAYBACK_BUFFER);
492 return snd_pcm_lib_free_pages(substream);
495 /* trigger playback callback */
496 int snd_line6_playback_trigger(struct snd_line6_pcm *line6pcm, int cmd)
501 case SNDRV_PCM_TRIGGER_START:
503 case SNDRV_PCM_TRIGGER_RESUME:
505 err = line6_pcm_acquire(line6pcm,
506 LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
513 case SNDRV_PCM_TRIGGER_STOP:
515 case SNDRV_PCM_TRIGGER_SUSPEND:
517 err = line6_pcm_release(line6pcm,
518 LINE6_BIT_PCM_ALSA_PLAYBACK_STREAM);
525 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
526 set_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
529 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
530 clear_bit(LINE6_INDEX_PAUSE_PLAYBACK, &line6pcm->flags);
540 /* playback pointer callback */
541 static snd_pcm_uframes_t
542 snd_line6_playback_pointer(struct snd_pcm_substream *substream)
544 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
546 return line6pcm->pos_out_done;
549 /* playback operators */
550 struct snd_pcm_ops snd_line6_playback_ops = {
551 .open = snd_line6_playback_open,
552 .close = snd_line6_playback_close,
553 .ioctl = snd_pcm_lib_ioctl,
554 .hw_params = snd_line6_playback_hw_params,
555 .hw_free = snd_line6_playback_hw_free,
556 .prepare = snd_line6_prepare,
557 .trigger = snd_line6_trigger,
558 .pointer = snd_line6_playback_pointer,
561 int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
565 /* create audio URBs and fill in constant values: */
566 for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
569 /* URB for audio out: */
570 urb = line6pcm->urb_audio_out[i] =
571 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
574 dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
578 urb->dev = line6pcm->line6->usbdev;
580 usb_sndisocpipe(line6pcm->line6->usbdev,
581 line6pcm->ep_audio_write &
582 USB_ENDPOINT_NUMBER_MASK);
583 urb->transfer_flags = URB_ISO_ASAP;
584 urb->start_frame = -1;
585 urb->number_of_packets = LINE6_ISO_PACKETS;
586 urb->interval = LINE6_ISO_INTERVAL;
587 urb->error_count = 0;
588 urb->complete = audio_out_callback;