blob: 7c812565f90db03f5c11fbcf3f92854b0eb3e772 [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
Stefan Hajnoczi140e28b2011-11-23 08:20:45 +000012#include <linux/slab.h>
Markus Grabner705ecec2009-02-27 19:43:04 -080013#include <sound/core.h>
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16
Markus Grabner1027f472010-08-12 01:35:30 +020017#include "capture.h"
18#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080019#include "pcm.h"
Markus Grabner1027f472010-08-12 01:35:30 +020020
Markus Grabner705ecec2009-02-27 19:43:04 -080021/*
22 Find a free URB and submit it.
Takashi Iwai3d3ae442015-01-27 12:50:44 +010023 must be called in line6pcm->in.lock context
Markus Grabner705ecec2009-02-27 19:43:04 -080024*/
Markus Grabner1027f472010-08-12 01:35:30 +020025static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -080026{
Markus Grabner1027f472010-08-12 01:35:30 +020027 int index;
Markus Grabner705ecec2009-02-27 19:43:04 -080028 int i, urb_size;
Markus Grabnere1a164d2010-08-23 01:08:25 +020029 int ret;
Markus Grabner705ecec2009-02-27 19:43:04 -080030 struct urb *urb_in;
31
Andrej Krutakb2233d92016-09-18 20:59:21 +020032 index = find_first_zero_bit(&line6pcm->in.active_urbs,
33 line6pcm->line6->iso_buffers);
Markus Grabner705ecec2009-02-27 19:43:04 -080034
Andrej Krutakb2233d92016-09-18 20:59:21 +020035 if (index < 0 || index >= line6pcm->line6->iso_buffers) {
Markus Grabner1027f472010-08-12 01:35:30 +020036 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
Markus Grabner705ecec2009-02-27 19:43:04 -080037 return -EINVAL;
38 }
39
Takashi Iwaiad0119a2015-01-23 16:10:57 +010040 urb_in = line6pcm->in.urbs[index];
Markus Grabner705ecec2009-02-27 19:43:04 -080041 urb_size = 0;
42
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080043 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Shawn Bohrere0645d62009-11-15 22:17:52 -060044 struct usb_iso_packet_descriptor *fin =
45 &urb_in->iso_frame_desc[i];
Markus Grabner705ecec2009-02-27 19:43:04 -080046 fin->offset = urb_size;
Andrej Krutak7a0f55a2016-09-18 20:59:23 +020047 fin->length = line6pcm->max_packet_size_in;
48 urb_size += line6pcm->max_packet_size_in;
Markus Grabner705ecec2009-02-27 19:43:04 -080049 }
50
Shawn Bohrere0645d62009-11-15 22:17:52 -060051 urb_in->transfer_buffer =
Takashi Iwaiad0119a2015-01-23 16:10:57 +010052 line6pcm->in.buffer +
Andrej Krutak7a0f55a2016-09-18 20:59:23 +020053 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_in;
Markus Grabner705ecec2009-02-27 19:43:04 -080054 urb_in->transfer_buffer_length = urb_size;
Markus Grabner1027f472010-08-12 01:35:30 +020055 urb_in->context = line6pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -080056
Markus Grabnere1a164d2010-08-23 01:08:25 +020057 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
58
59 if (ret == 0)
Takashi Iwaiad0119a2015-01-23 16:10:57 +010060 set_bit(index, &line6pcm->in.active_urbs);
Markus Grabner705ecec2009-02-27 19:43:04 -080061 else
Markus Grabner1027f472010-08-12 01:35:30 +020062 dev_err(line6pcm->line6->ifcdev,
Markus Grabnere1a164d2010-08-23 01:08:25 +020063 "URB in #%d submission failed (%d)\n", index, ret);
Markus Grabner705ecec2009-02-27 19:43:04 -080064
Markus Grabner705ecec2009-02-27 19:43:04 -080065 return 0;
66}
67
68/*
69 Submit all currently available capture URBs.
Takashi Iwai63e20df2015-01-27 15:24:09 +010070 must be called in line6pcm->in.lock context
Markus Grabner705ecec2009-02-27 19:43:04 -080071*/
Markus Grabner1027f472010-08-12 01:35:30 +020072int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -080073{
Takashi Iwai3d3ae442015-01-27 12:50:44 +010074 int ret = 0, i;
Markus Grabner705ecec2009-02-27 19:43:04 -080075
Andrej Krutakb2233d92016-09-18 20:59:21 +020076 for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
Markus Grabner1027f472010-08-12 01:35:30 +020077 ret = submit_audio_in_urb(line6pcm);
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080078 if (ret < 0)
Takashi Iwai3d3ae442015-01-27 12:50:44 +010079 break;
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080080 }
Markus Grabner705ecec2009-02-27 19:43:04 -080081
Takashi Iwai3d3ae442015-01-27 12:50:44 +010082 return ret;
Markus Grabner705ecec2009-02-27 19:43:04 -080083}
84
85/*
Markus Grabner1027f472010-08-12 01:35:30 +020086 Copy data into ALSA capture buffer.
87*/
88void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
89{
90 struct snd_pcm_substream *substream =
91 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
92 struct snd_pcm_runtime *runtime = substream->runtime;
Andrej Krutak97d78ac2016-09-18 20:59:24 +020093 const int bytes_per_frame =
94 line6pcm->properties->bytes_per_channel *
95 line6pcm->properties->capture_hw.channels_max;
Markus Grabner1027f472010-08-12 01:35:30 +020096 int frames = fsize / bytes_per_frame;
97
Peter Huewe597a1e72010-12-07 23:38:02 +010098 if (runtime == NULL)
Markus Grabnerc7fcf252010-09-17 23:33:25 +020099 return;
100
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100101 if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
Markus Grabner1027f472010-08-12 01:35:30 +0200102 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +0200103 The transferred area goes over buffer boundary,
104 copy two separate chunks.
105 */
Markus Grabner1027f472010-08-12 01:35:30 +0200106 int len;
Mikhail Boikob5f87cf2014-03-18 23:59:46 +0200107
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100108 len = runtime->buffer_size - line6pcm->in.pos_done;
Markus Grabner1027f472010-08-12 01:35:30 +0200109
110 if (len > 0) {
111 memcpy(runtime->dma_area +
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100112 line6pcm->in.pos_done * bytes_per_frame, fbuf,
Markus Grabner1027f472010-08-12 01:35:30 +0200113 len * bytes_per_frame);
114 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
115 (frames - len) * bytes_per_frame);
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700116 } else {
117 /* this is somewhat paranoid */
118 dev_err(line6pcm->line6->ifcdev,
119 "driver bug: len = %d\n", len);
120 }
Markus Grabner1027f472010-08-12 01:35:30 +0200121 } else {
122 /* copy single chunk */
123 memcpy(runtime->dma_area +
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100124 line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
Markus Grabner1027f472010-08-12 01:35:30 +0200125 }
126
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100127 line6pcm->in.pos_done += frames;
128 if (line6pcm->in.pos_done >= runtime->buffer_size)
129 line6pcm->in.pos_done -= runtime->buffer_size;
Markus Grabner1027f472010-08-12 01:35:30 +0200130}
131
132void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
133{
134 struct snd_pcm_substream *substream =
135 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
136
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100137 line6pcm->in.bytes += length;
138 if (line6pcm->in.bytes >= line6pcm->in.period) {
139 line6pcm->in.bytes %= line6pcm->in.period;
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100140 spin_unlock(&line6pcm->in.lock);
Markus Grabner1027f472010-08-12 01:35:30 +0200141 snd_pcm_period_elapsed(substream);
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100142 spin_lock(&line6pcm->in.lock);
Markus Grabner1027f472010-08-12 01:35:30 +0200143 }
144}
145
146/*
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700147 * Callback for completed capture URB.
148 */
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800149static void audio_in_callback(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800150{
151 int i, index, length = 0, shutdown = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800152 unsigned long flags;
153
Markus Grabner1027f472010-08-12 01:35:30 +0200154 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
155
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100156 line6pcm->in.last_frame = urb->start_frame;
Markus Grabner705ecec2009-02-27 19:43:04 -0800157
158 /* find index of URB */
Andrej Krutakb2233d92016-09-18 20:59:21 +0200159 for (index = 0; index < line6pcm->line6->iso_buffers; ++index)
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100160 if (urb == line6pcm->in.urbs[index])
Markus Grabner705ecec2009-02-27 19:43:04 -0800161 break;
162
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100163 spin_lock_irqsave(&line6pcm->in.lock, flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800164
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800165 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800166 char *fbuf;
167 int fsize;
168 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
169
Markus Grabnere1a164d2010-08-23 01:08:25 +0200170 if (fin->status == -EXDEV) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800171 shutdown = 1;
172 break;
173 }
174
175 fbuf = urb->transfer_buffer + fin->offset;
176 fsize = fin->actual_length;
Markus Grabner1027f472010-08-12 01:35:30 +0200177
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200178 if (fsize > line6pcm->max_packet_size_in) {
Markus Grabner1027f472010-08-12 01:35:30 +0200179 dev_err(line6pcm->line6->ifcdev,
180 "driver and/or device bug: packet too large (%d > %d)\n",
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200181 fsize, line6pcm->max_packet_size_in);
Markus Grabner1027f472010-08-12 01:35:30 +0200182 }
183
Markus Grabner705ecec2009-02-27 19:43:04 -0800184 length += fsize;
185
Andrej Krutak79faa2b2016-09-18 20:59:22 +0200186 BUILD_BUG_ON_MSG(LINE6_ISO_PACKETS != 1,
187 "The following code assumes LINE6_ISO_PACKETS == 1");
188 /* TODO:
189 * Also, if iso_buffers != 2, the prev frame is almost random at
190 * playback side.
191 * This needs to be redesigned. It should be "stable", but we may
192 * experience sync problems on such high-speed configs.
193 */
194
Markus Grabner1027f472010-08-12 01:35:30 +0200195 line6pcm->prev_fbuf = fbuf;
Andrej Krutak97d78ac2016-09-18 20:59:24 +0200196 line6pcm->prev_fsize = fsize /
197 (line6pcm->properties->bytes_per_channel *
198 line6pcm->properties->capture_hw.channels_max);
Markus Grabner705ecec2009-02-27 19:43:04 -0800199
Takashi Iwai63e20df2015-01-27 15:24:09 +0100200 if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
201 test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) &&
202 fsize > 0)
203 line6_capture_copy(line6pcm, fbuf, fsize);
Markus Grabner705ecec2009-02-27 19:43:04 -0800204 }
205
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100206 clear_bit(index, &line6pcm->in.active_urbs);
Markus Grabner705ecec2009-02-27 19:43:04 -0800207
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100208 if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
Markus Grabner705ecec2009-02-27 19:43:04 -0800209 shutdown = 1;
210
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800211 if (!shutdown) {
Markus Grabner1027f472010-08-12 01:35:30 +0200212 submit_audio_in_urb(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800213
Takashi Iwai63e20df2015-01-27 15:24:09 +0100214 if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
215 test_bit(LINE6_STREAM_PCM, &line6pcm->in.running))
216 line6_capture_check_period(line6pcm, length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800217 }
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100218
219 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800220}
221
222/* open capture callback */
223static int snd_line6_capture_open(struct snd_pcm_substream *substream)
224{
225 int err;
226 struct snd_pcm_runtime *runtime = substream->runtime;
227 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
228
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800229 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
230 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai1263f612015-01-28 15:08:59 +0100231 &line6pcm->properties->rates);
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800232 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800233 return err;
234
Andrej Krutakf56742c2016-09-18 20:59:25 +0200235 line6_pcm_acquire(line6pcm, LINE6_STREAM_CAPTURE_HELPER, false);
236
Takashi Iwai1263f612015-01-28 15:08:59 +0100237 runtime->hw = line6pcm->properties->capture_hw;
Markus Grabner705ecec2009-02-27 19:43:04 -0800238 return 0;
239}
240
241/* close capture callback */
242static int snd_line6_capture_close(struct snd_pcm_substream *substream)
243{
Andrej Krutakf56742c2016-09-18 20:59:25 +0200244 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
245
246 line6_pcm_release(line6pcm, LINE6_STREAM_CAPTURE_HELPER);
Markus Grabner705ecec2009-02-27 19:43:04 -0800247 return 0;
248}
249
Markus Grabner705ecec2009-02-27 19:43:04 -0800250/* capture operators */
251struct snd_pcm_ops snd_line6_capture_ops = {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200252 .open = snd_line6_capture_open,
253 .close = snd_line6_capture_close,
254 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwai63e20df2015-01-27 15:24:09 +0100255 .hw_params = snd_line6_hw_params,
256 .hw_free = snd_line6_hw_free,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200257 .prepare = snd_line6_prepare,
258 .trigger = snd_line6_trigger,
Takashi Iwai2954f912015-01-27 15:41:27 +0100259 .pointer = snd_line6_pointer,
Markus Grabner705ecec2009-02-27 19:43:04 -0800260};
261
Markus Grabner1027f472010-08-12 01:35:30 +0200262int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800263{
Chris Rorvick16d603d2015-01-12 12:42:55 -0800264 struct usb_line6 *line6 = line6pcm->line6;
Markus Grabner705ecec2009-02-27 19:43:04 -0800265 int i;
266
Andrej Krutakb2233d92016-09-18 20:59:21 +0200267 line6pcm->in.urbs = kzalloc(
268 sizeof(struct urb *) * line6->iso_buffers, GFP_KERNEL);
269 if (line6pcm->in.urbs == NULL)
270 return -ENOMEM;
271
Markus Grabner705ecec2009-02-27 19:43:04 -0800272 /* create audio URBs and fill in constant values: */
Andrej Krutakb2233d92016-09-18 20:59:21 +0200273 for (i = 0; i < line6->iso_buffers; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800274 struct urb *urb;
275
276 /* URB for audio in: */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100277 urb = line6pcm->in.urbs[i] =
Shawn Bohrere0645d62009-11-15 22:17:52 -0600278 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
Markus Grabner705ecec2009-02-27 19:43:04 -0800279
Takashi Iwaia019f5e2015-01-19 15:05:10 +0100280 if (urb == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800281 return -ENOMEM;
Markus Grabner705ecec2009-02-27 19:43:04 -0800282
Chris Rorvick16d603d2015-01-12 12:42:55 -0800283 urb->dev = line6->usbdev;
Shawn Bohrere0645d62009-11-15 22:17:52 -0600284 urb->pipe =
Chris Rorvick16d603d2015-01-12 12:42:55 -0800285 usb_rcvisocpipe(line6->usbdev,
286 line6->properties->ep_audio_r &
Markus Grabnere1a164d2010-08-23 01:08:25 +0200287 USB_ENDPOINT_NUMBER_MASK);
Markus Grabner705ecec2009-02-27 19:43:04 -0800288 urb->transfer_flags = URB_ISO_ASAP;
289 urb->start_frame = -1;
290 urb->number_of_packets = LINE6_ISO_PACKETS;
291 urb->interval = LINE6_ISO_INTERVAL;
292 urb->error_count = 0;
293 urb->complete = audio_in_callback;
294 }
295
296 return 0;
297}