0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/slab.h>
0009 #include <sound/core.h>
0010 #include <sound/pcm.h>
0011 #include <sound/pcm_params.h>
0012
0013 #include "capture.h"
0014 #include "driver.h"
0015 #include "pcm.h"
0016 #include "playback.h"
0017
0018
0019
0020
0021 static void change_volume(struct urb *urb_out, int volume[],
0022 int bytes_per_frame)
0023 {
0024 int chn = 0;
0025
0026 if (volume[0] == 256 && volume[1] == 256)
0027 return;
0028
0029 if (bytes_per_frame == 4) {
0030 __le16 *p, *buf_end;
0031
0032 p = (__le16 *)urb_out->transfer_buffer;
0033 buf_end = p + urb_out->transfer_buffer_length / sizeof(*p);
0034
0035 for (; p < buf_end; ++p) {
0036 short pv = le16_to_cpu(*p);
0037 int val = (pv * volume[chn & 1]) >> 8;
0038 pv = clamp(val, -0x8000, 0x7fff);
0039 *p = cpu_to_le16(pv);
0040 ++chn;
0041 }
0042 } else if (bytes_per_frame == 6) {
0043 unsigned char *p, *buf_end;
0044
0045 p = (unsigned char *)urb_out->transfer_buffer;
0046 buf_end = p + urb_out->transfer_buffer_length;
0047
0048 for (; p < buf_end; p += 3) {
0049 int val;
0050
0051 val = p[0] + (p[1] << 8) + ((signed char)p[2] << 16);
0052 val = (val * volume[chn & 1]) >> 8;
0053 val = clamp(val, -0x800000, 0x7fffff);
0054 p[0] = val;
0055 p[1] = val >> 8;
0056 p[2] = val >> 16;
0057 ++chn;
0058 }
0059 }
0060 }
0061
0062
0063
0064
0065 static void create_impulse_test_signal(struct snd_line6_pcm *line6pcm,
0066 struct urb *urb_out, int bytes_per_frame)
0067 {
0068 int frames = urb_out->transfer_buffer_length / bytes_per_frame;
0069
0070 if (bytes_per_frame == 4) {
0071 int i;
0072 short *pi = (short *)line6pcm->prev_fbuf;
0073 short *po = (short *)urb_out->transfer_buffer;
0074
0075 for (i = 0; i < frames; ++i) {
0076 po[0] = pi[0];
0077 po[1] = 0;
0078 pi += 2;
0079 po += 2;
0080 }
0081 } else if (bytes_per_frame == 6) {
0082 int i, j;
0083 unsigned char *pi = line6pcm->prev_fbuf;
0084 unsigned char *po = urb_out->transfer_buffer;
0085
0086 for (i = 0; i < frames; ++i) {
0087 for (j = 0; j < bytes_per_frame / 2; ++j)
0088 po[j] = pi[j];
0089
0090 for (; j < bytes_per_frame; ++j)
0091 po[j] = 0;
0092
0093 pi += bytes_per_frame;
0094 po += bytes_per_frame;
0095 }
0096 }
0097 if (--line6pcm->impulse_count <= 0) {
0098 ((unsigned char *)(urb_out->transfer_buffer))[bytes_per_frame -
0099 1] =
0100 line6pcm->impulse_volume;
0101 line6pcm->impulse_count = line6pcm->impulse_period;
0102 }
0103 }
0104
0105
0106
0107
0108 static void add_monitor_signal(struct urb *urb_out, unsigned char *signal,
0109 int volume, int bytes_per_frame)
0110 {
0111 if (volume == 0)
0112 return;
0113
0114 if (bytes_per_frame == 4) {
0115 __le16 *pi, *po, *buf_end;
0116
0117 pi = (__le16 *)signal;
0118 po = (__le16 *)urb_out->transfer_buffer;
0119 buf_end = po + urb_out->transfer_buffer_length / sizeof(*po);
0120
0121 for (; po < buf_end; ++pi, ++po) {
0122 short pov = le16_to_cpu(*po);
0123 short piv = le16_to_cpu(*pi);
0124 int val = pov + ((piv * volume) >> 8);
0125 pov = clamp(val, -0x8000, 0x7fff);
0126 *po = cpu_to_le16(pov);
0127 }
0128 }
0129
0130
0131
0132
0133
0134 }
0135
0136
0137
0138
0139
0140 static int submit_audio_out_urb(struct snd_line6_pcm *line6pcm)
0141 {
0142 int index;
0143 int i, urb_size, urb_frames;
0144 int ret;
0145 const int bytes_per_frame =
0146 line6pcm->properties->bytes_per_channel *
0147 line6pcm->properties->playback_hw.channels_max;
0148 const int frame_increment =
0149 line6pcm->properties->rates.rats[0].num_min;
0150 const int frame_factor =
0151 line6pcm->properties->rates.rats[0].den *
0152 (line6pcm->line6->intervals_per_second / LINE6_ISO_INTERVAL);
0153 struct urb *urb_out;
0154
0155 index = find_first_zero_bit(&line6pcm->out.active_urbs,
0156 line6pcm->line6->iso_buffers);
0157
0158 if (index < 0 || index >= line6pcm->line6->iso_buffers) {
0159 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
0160 return -EINVAL;
0161 }
0162
0163 urb_out = line6pcm->out.urbs[index];
0164 urb_size = 0;
0165
0166
0167 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
0168
0169 int fsize = 0;
0170 struct usb_iso_packet_descriptor *fout =
0171 &urb_out->iso_frame_desc[i];
0172
0173 fsize = line6pcm->prev_fsize;
0174 if (fsize == 0) {
0175 int n;
0176
0177 line6pcm->out.count += frame_increment;
0178 n = line6pcm->out.count / frame_factor;
0179 line6pcm->out.count -= n * frame_factor;
0180 fsize = n;
0181 }
0182
0183 fsize *= bytes_per_frame;
0184
0185 fout->offset = urb_size;
0186 fout->length = fsize;
0187 urb_size += fsize;
0188 }
0189
0190 if (urb_size == 0) {
0191
0192 dev_err(line6pcm->line6->ifcdev, "driver bug: urb_size = 0\n");
0193 return -EINVAL;
0194 }
0195
0196 urb_frames = urb_size / bytes_per_frame;
0197 urb_out->transfer_buffer =
0198 line6pcm->out.buffer +
0199 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_out;
0200 urb_out->transfer_buffer_length = urb_size;
0201 urb_out->context = line6pcm;
0202
0203 if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running) &&
0204 !test_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags)) {
0205 struct snd_pcm_runtime *runtime =
0206 get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK)->runtime;
0207
0208 if (line6pcm->out.pos + urb_frames > runtime->buffer_size) {
0209
0210
0211
0212
0213 int len;
0214
0215 len = runtime->buffer_size - line6pcm->out.pos;
0216
0217 if (len > 0) {
0218 memcpy(urb_out->transfer_buffer,
0219 runtime->dma_area +
0220 line6pcm->out.pos * bytes_per_frame,
0221 len * bytes_per_frame);
0222 memcpy(urb_out->transfer_buffer +
0223 len * bytes_per_frame, runtime->dma_area,
0224 (urb_frames - len) * bytes_per_frame);
0225 } else
0226 dev_err(line6pcm->line6->ifcdev, "driver bug: len = %d\n",
0227 len);
0228 } else {
0229 memcpy(urb_out->transfer_buffer,
0230 runtime->dma_area +
0231 line6pcm->out.pos * bytes_per_frame,
0232 urb_out->transfer_buffer_length);
0233 }
0234
0235 line6pcm->out.pos += urb_frames;
0236 if (line6pcm->out.pos >= runtime->buffer_size)
0237 line6pcm->out.pos -= runtime->buffer_size;
0238
0239 change_volume(urb_out, line6pcm->volume_playback,
0240 bytes_per_frame);
0241 } else {
0242 memset(urb_out->transfer_buffer, 0,
0243 urb_out->transfer_buffer_length);
0244 }
0245
0246 spin_lock_nested(&line6pcm->in.lock, SINGLE_DEPTH_NESTING);
0247 if (line6pcm->prev_fbuf) {
0248 if (test_bit(LINE6_STREAM_IMPULSE, &line6pcm->out.running)) {
0249 create_impulse_test_signal(line6pcm, urb_out,
0250 bytes_per_frame);
0251 if (test_bit(LINE6_STREAM_PCM, &line6pcm->in.running)) {
0252 line6_capture_copy(line6pcm,
0253 urb_out->transfer_buffer,
0254 urb_out->
0255 transfer_buffer_length);
0256 line6_capture_check_period(line6pcm,
0257 urb_out->transfer_buffer_length);
0258 }
0259 } else {
0260 if (!(line6pcm->line6->properties->capabilities & LINE6_CAP_HWMON)
0261 && line6pcm->out.running && line6pcm->in.running)
0262 add_monitor_signal(urb_out, line6pcm->prev_fbuf,
0263 line6pcm->volume_monitor,
0264 bytes_per_frame);
0265 }
0266 line6pcm->prev_fbuf = NULL;
0267 line6pcm->prev_fsize = 0;
0268 }
0269 spin_unlock(&line6pcm->in.lock);
0270
0271 ret = usb_submit_urb(urb_out, GFP_ATOMIC);
0272
0273 if (ret == 0)
0274 set_bit(index, &line6pcm->out.active_urbs);
0275 else
0276 dev_err(line6pcm->line6->ifcdev,
0277 "URB out #%d submission failed (%d)\n", index, ret);
0278
0279 return 0;
0280 }
0281
0282
0283
0284
0285
0286 int line6_submit_audio_out_all_urbs(struct snd_line6_pcm *line6pcm)
0287 {
0288 int ret = 0, i;
0289
0290 for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
0291 ret = submit_audio_out_urb(line6pcm);
0292 if (ret < 0)
0293 break;
0294 }
0295
0296 return ret;
0297 }
0298
0299
0300
0301
0302 static void audio_out_callback(struct urb *urb)
0303 {
0304 int i, index, length = 0, shutdown = 0;
0305 unsigned long flags;
0306 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
0307 struct snd_pcm_substream *substream =
0308 get_substream(line6pcm, SNDRV_PCM_STREAM_PLAYBACK);
0309 const int bytes_per_frame =
0310 line6pcm->properties->bytes_per_channel *
0311 line6pcm->properties->playback_hw.channels_max;
0312
0313 #if USE_CLEAR_BUFFER_WORKAROUND
0314 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
0315 #endif
0316
0317 line6pcm->out.last_frame = urb->start_frame;
0318
0319
0320 for (index = 0; index < line6pcm->line6->iso_buffers; index++)
0321 if (urb == line6pcm->out.urbs[index])
0322 break;
0323
0324 if (index >= line6pcm->line6->iso_buffers)
0325 return;
0326
0327 for (i = 0; i < LINE6_ISO_PACKETS; i++)
0328 length += urb->iso_frame_desc[i].length;
0329
0330 spin_lock_irqsave(&line6pcm->out.lock, flags);
0331
0332 if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
0333 struct snd_pcm_runtime *runtime = substream->runtime;
0334
0335 line6pcm->out.pos_done +=
0336 length / bytes_per_frame;
0337
0338 if (line6pcm->out.pos_done >= runtime->buffer_size)
0339 line6pcm->out.pos_done -= runtime->buffer_size;
0340 }
0341
0342 clear_bit(index, &line6pcm->out.active_urbs);
0343
0344 for (i = 0; i < LINE6_ISO_PACKETS; i++)
0345 if (urb->iso_frame_desc[i].status == -EXDEV) {
0346 shutdown = 1;
0347 break;
0348 }
0349
0350 if (test_and_clear_bit(index, &line6pcm->out.unlink_urbs))
0351 shutdown = 1;
0352
0353 if (!shutdown) {
0354 submit_audio_out_urb(line6pcm);
0355
0356 if (test_bit(LINE6_STREAM_PCM, &line6pcm->out.running)) {
0357 line6pcm->out.bytes += length;
0358 if (line6pcm->out.bytes >= line6pcm->out.period) {
0359 line6pcm->out.bytes %= line6pcm->out.period;
0360 spin_unlock(&line6pcm->out.lock);
0361 snd_pcm_period_elapsed(substream);
0362 spin_lock(&line6pcm->out.lock);
0363 }
0364 }
0365 }
0366 spin_unlock_irqrestore(&line6pcm->out.lock, flags);
0367 }
0368
0369
0370 static int snd_line6_playback_open(struct snd_pcm_substream *substream)
0371 {
0372 int err;
0373 struct snd_pcm_runtime *runtime = substream->runtime;
0374 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0375
0376 err = snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0377 &line6pcm->properties->rates);
0378 if (err < 0)
0379 return err;
0380
0381 runtime->hw = line6pcm->properties->playback_hw;
0382 return 0;
0383 }
0384
0385
0386 static int snd_line6_playback_close(struct snd_pcm_substream *substream)
0387 {
0388 return 0;
0389 }
0390
0391
0392 const struct snd_pcm_ops snd_line6_playback_ops = {
0393 .open = snd_line6_playback_open,
0394 .close = snd_line6_playback_close,
0395 .hw_params = snd_line6_hw_params,
0396 .hw_free = snd_line6_hw_free,
0397 .prepare = snd_line6_prepare,
0398 .trigger = snd_line6_trigger,
0399 .pointer = snd_line6_pointer,
0400 };
0401
0402 int line6_create_audio_out_urbs(struct snd_line6_pcm *line6pcm)
0403 {
0404 struct usb_line6 *line6 = line6pcm->line6;
0405 int i;
0406
0407 line6pcm->out.urbs = kcalloc(line6->iso_buffers, sizeof(struct urb *),
0408 GFP_KERNEL);
0409 if (line6pcm->out.urbs == NULL)
0410 return -ENOMEM;
0411
0412
0413 for (i = 0; i < line6->iso_buffers; ++i) {
0414 struct urb *urb;
0415
0416
0417 urb = line6pcm->out.urbs[i] =
0418 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
0419
0420 if (urb == NULL)
0421 return -ENOMEM;
0422
0423 urb->dev = line6->usbdev;
0424 urb->pipe =
0425 usb_sndisocpipe(line6->usbdev,
0426 line6->properties->ep_audio_w &
0427 USB_ENDPOINT_NUMBER_MASK);
0428 urb->transfer_flags = URB_ISO_ASAP;
0429 urb->start_frame = -1;
0430 urb->number_of_packets = LINE6_ISO_PACKETS;
0431 urb->interval = LINE6_ISO_INTERVAL;
0432 urb->error_count = 0;
0433 urb->complete = audio_out_callback;
0434 if (usb_urb_ep_type_check(urb))
0435 return -EINVAL;
0436 }
0437
0438 return 0;
0439 }