^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Line 6 Linux USB driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sound/pcm_params.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "capture.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "driver.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "pcm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "playback.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) Software stereo volume control.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static void change_volume(struct urb *urb_out, int volume[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int bytes_per_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int chn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (volume[0] == 256 && volume[1] == 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return; /* maximum volume - no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (bytes_per_frame == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __le16 *p, *buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) p = (__le16 *)urb_out->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) for (; p < buf_end; ++p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) short pv = le16_to_cpu(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int val = (pv * volume[chn & 1]) >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) pv = clamp(val, -0x8000, 0x7fff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *p = cpu_to_le16(pv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ++chn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) } else if (bytes_per_frame == 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned char *p, *buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) p = (unsigned char *)urb_out->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) buf_end = p + urb_out->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) for (; p < buf_end; p += 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) val = (val * volume[chn & 1]) >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) val = clamp(val, -0x800000, 0x7fffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) p[0] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) p[1] = val >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) p[2] = val >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ++chn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) Create signal for impulse response test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct urb *urb_out, int bytes_per_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int frames = urb_out->transfer_buffer_length / bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (bytes_per_frame == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) short *pi = (short *)line6pcm->prev_fbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) short *po = (short *)urb_out->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for (i = 0; i < frames; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) po[0] = pi[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) po[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pi += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) po += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) } else if (bytes_per_frame == 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned char *pi = line6pcm->prev_fbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned char *po = urb_out->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (i = 0; i < frames; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (j = 0; j < bytes_per_frame / 2; ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) po[j] = pi[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (; j < bytes_per_frame; ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) po[j] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pi += bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) po += bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (--line6pcm->impulse_count <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) 1] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) line6pcm->impulse_volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) line6pcm->impulse_count = line6pcm->impulse_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) Add signal to buffer for software monitoring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int volume, int bytes_per_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (volume == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return; /* zero volume - no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (bytes_per_frame == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __le16 *pi, *po, *buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pi = (__le16 *)signal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) po = (__le16 *)urb_out->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (; po < buf_end; ++pi, ++po) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) short pov = le16_to_cpu(*po);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) short piv = le16_to_cpu(*pi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int val = pov + ((piv * volume) >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pov = clamp(val, -0x8000, 0x7fff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *po = cpu_to_le16(pov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) We don't need to handle devices with 6 bytes per frame here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) since they all support hardware monitoring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) Find a free URB, prepare audio data, and submit URB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) must be called in line6pcm->out.lock context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int i, urb_size, urb_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const int bytes_per_frame =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) line6pcm->properties->bytes_per_channel *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) line6pcm->properties->playback_hw.channels_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const int frame_increment =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) line6pcm->properties->rates.rats[0].num_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) const int frame_factor =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) line6pcm->properties->rates.rats[0].den *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) (line6pcm->line6->intervals_per_second / LINE6_ISO_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct urb *urb_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) index = find_first_zero_bit(&line6pcm->out.active_urbs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) line6pcm->line6->iso_buffers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (index < 0 || index >= line6pcm->line6->iso_buffers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) urb_out = line6pcm->out.urbs[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) urb_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* TODO: this may not work for LINE6_ISO_PACKETS != 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* compute frame size for given sampling rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int fsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct usb_iso_packet_descriptor *fout =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) &urb_out->iso_frame_desc[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) fsize = line6pcm->prev_fsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (fsize == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) line6pcm->out.count += frame_increment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) n = line6pcm->out.count / frame_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) line6pcm->out.count -= n * frame_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) fsize = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) fsize *= bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) fout->offset = urb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) fout->length = fsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) urb_size += fsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (urb_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* can't determine URB size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) urb_frames = urb_size / bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) urb_out->transfer_buffer =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) line6pcm->out.buffer +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) urb_out->transfer_buffer_length = urb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) urb_out->context = line6pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) !test_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct snd_pcm_runtime *runtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (line6pcm->out.pos + urb_frames > runtime->buffer_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) The transferred area goes over buffer boundary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) copy the data to the temp buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) len = runtime->buffer_size - line6pcm->out.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) memcpy(urb_out->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) runtime->dma_area +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) line6pcm->out.pos * bytes_per_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) len * bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) memcpy(urb_out->transfer_buffer +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) len * bytes_per_frame, runtime->dma_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) (urb_frames - len) * bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) memcpy(urb_out->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) runtime->dma_area +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) line6pcm->out.pos * bytes_per_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) urb_out->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) line6pcm->out.pos += urb_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (line6pcm->out.pos >= runtime->buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) line6pcm->out.pos -= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) change_volume(urb_out, line6pcm->volume_playback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) memset(urb_out->transfer_buffer, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) urb_out->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) spin_lock_nested(&line6pcm->in.lock, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (line6pcm->prev_fbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (test_bit(LINE6_STREAM_IMPULSE, &line6pcm->out.running)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) create_impulse_test_signal(line6pcm, urb_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) line6_capture_copy(line6pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) urb_out->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) urb_out->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) line6_capture_check_period(line6pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) urb_out->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!(line6pcm->line6->properties->capabilities & LINE6_CAP_HWMON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) && line6pcm->out.running && line6pcm->in.running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) add_monitor_signal(urb_out, line6pcm->prev_fbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) line6pcm->volume_monitor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) line6pcm->prev_fbuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) line6pcm->prev_fsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) spin_unlock(&line6pcm->in.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = usb_submit_urb(urb_out, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) set_bit(index, &line6pcm->out.active_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev_err(line6pcm->line6->ifcdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) "URB out #%d submission failed (%d)\n", index, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) Submit all currently available playback URBs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) must be called in line6pcm->out.lock context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = submit_audio_out_urb(line6pcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) Callback for completed playback URB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void audio_out_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) int i, index, length = 0, shutdown = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct snd_pcm_substream *substream =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) const int bytes_per_frame =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) line6pcm->properties->bytes_per_channel *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) line6pcm->properties->playback_hw.channels_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #if USE_CLEAR_BUFFER_WORKAROUND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) line6pcm->out.last_frame = urb->start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* find index of URB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) for (index = 0; index < line6pcm->line6->iso_buffers; index++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (urb == line6pcm->out.urbs[index])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (index >= line6pcm->line6->iso_buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return; /* URB has been unlinked asynchronously */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) for (i = 0; i < LINE6_ISO_PACKETS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) length += urb->iso_frame_desc[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) spin_lock_irqsave(&line6pcm->out.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) line6pcm->out.pos_done +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) length / bytes_per_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (line6pcm->out.pos_done >= runtime->buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) line6pcm->out.pos_done -= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) clear_bit(index, &line6pcm->out.active_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) for (i = 0; i < LINE6_ISO_PACKETS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (urb->iso_frame_desc[i].status == -EXDEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) shutdown = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (test_and_clear_bit(index, &line6pcm->out.unlink_urbs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) shutdown = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) submit_audio_out_urb(line6pcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) line6pcm->out.bytes += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (line6pcm->out.bytes >= line6pcm->out.period) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) line6pcm->out.bytes %= line6pcm->out.period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) spin_unlock(&line6pcm->out.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) snd_pcm_period_elapsed(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) spin_lock(&line6pcm->out.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) spin_unlock_irqrestore(&line6pcm->out.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* open playback callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static int snd_line6_playback_open(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) &line6pcm->properties->rates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) runtime->hw = line6pcm->properties->playback_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* close playback callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static int snd_line6_playback_close(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /* playback operators */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) const struct snd_pcm_ops snd_line6_playback_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .open = snd_line6_playback_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) .close = snd_line6_playback_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .hw_params = snd_line6_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .hw_free = snd_line6_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .prepare = snd_line6_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .trigger = snd_line6_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .pointer = snd_line6_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct usb_line6 *line6 = line6pcm->line6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) line6pcm->out.urbs = kcalloc(line6->iso_buffers, sizeof(struct urb *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (line6pcm->out.urbs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* create audio URBs and fill in constant values: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) for (i = 0; i < line6->iso_buffers; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* URB for audio out: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) urb = line6pcm->out.urbs[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (urb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) urb->dev = line6->usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) urb->pipe =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) usb_sndisocpipe(line6->usbdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) line6->properties->ep_audio_w &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) USB_ENDPOINT_NUMBER_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) urb->transfer_flags = URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) urb->start_frame = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) urb->number_of_packets = LINE6_ISO_PACKETS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) urb->interval = LINE6_ISO_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) urb->error_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) urb->complete = audio_out_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (usb_urb_ep_type_check(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }