blob: 970c9bdce0b21648a3ba57fe1444748d9bfcafa4 [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Markus Grabner705ecec2009-02-27 19:43:04 -08002/*
Chris Rorvickc078a4a2015-01-20 02:20:50 -06003 * Line 6 Linux USB driver
Markus Grabner705ecec2009-02-27 19:43:04 -08004 *
Markus Grabner1027f472010-08-12 01:35:30 +02005 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08006 */
7
Stefan Hajnoczi140e28b2011-11-23 08:20:45 +00008#include <linux/slab.h>
Markus Grabner705ecec2009-02-27 19:43:04 -08009#include <sound/core.h>
10#include <sound/pcm.h>
11#include <sound/pcm_params.h>
12
Markus Grabner1027f472010-08-12 01:35:30 +020013#include "capture.h"
14#include "driver.h"
Markus Grabner705ecec2009-02-27 19:43:04 -080015#include "pcm.h"
Markus Grabner1027f472010-08-12 01:35:30 +020016
Markus Grabner705ecec2009-02-27 19:43:04 -080017/*
18 Find a free URB and submit it.
Takashi Iwai3d3ae442015-01-27 12:50:44 +010019 must be called in line6pcm->in.lock context
Markus Grabner705ecec2009-02-27 19:43:04 -080020*/
Markus Grabner1027f472010-08-12 01:35:30 +020021static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -080022{
Markus Grabner1027f472010-08-12 01:35:30 +020023 int index;
Markus Grabner705ecec2009-02-27 19:43:04 -080024 int i, urb_size;
Markus Grabnere1a164d2010-08-23 01:08:25 +020025 int ret;
Markus Grabner705ecec2009-02-27 19:43:04 -080026 struct urb *urb_in;
27
Andrej Krutakb2233d92016-09-18 20:59:21 +020028 index = find_first_zero_bit(&line6pcm->in.active_urbs,
29 line6pcm->line6->iso_buffers);
Markus Grabner705ecec2009-02-27 19:43:04 -080030
Andrej Krutakb2233d92016-09-18 20:59:21 +020031 if (index < 0 || index >= line6pcm->line6->iso_buffers) {
Markus Grabner1027f472010-08-12 01:35:30 +020032 dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
Markus Grabner705ecec2009-02-27 19:43:04 -080033 return -EINVAL;
34 }
35
Takashi Iwaiad0119a2015-01-23 16:10:57 +010036 urb_in = line6pcm->in.urbs[index];
Markus Grabner705ecec2009-02-27 19:43:04 -080037 urb_size = 0;
38
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080039 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Shawn Bohrere0645d62009-11-15 22:17:52 -060040 struct usb_iso_packet_descriptor *fin =
41 &urb_in->iso_frame_desc[i];
Markus Grabner705ecec2009-02-27 19:43:04 -080042 fin->offset = urb_size;
Andrej Krutak7a0f55a2016-09-18 20:59:23 +020043 fin->length = line6pcm->max_packet_size_in;
44 urb_size += line6pcm->max_packet_size_in;
Markus Grabner705ecec2009-02-27 19:43:04 -080045 }
46
Shawn Bohrere0645d62009-11-15 22:17:52 -060047 urb_in->transfer_buffer =
Takashi Iwaiad0119a2015-01-23 16:10:57 +010048 line6pcm->in.buffer +
Andrej Krutak7a0f55a2016-09-18 20:59:23 +020049 index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_in;
Markus Grabner705ecec2009-02-27 19:43:04 -080050 urb_in->transfer_buffer_length = urb_size;
Markus Grabner1027f472010-08-12 01:35:30 +020051 urb_in->context = line6pcm;
Markus Grabner705ecec2009-02-27 19:43:04 -080052
Markus Grabnere1a164d2010-08-23 01:08:25 +020053 ret = usb_submit_urb(urb_in, GFP_ATOMIC);
54
55 if (ret == 0)
Takashi Iwaiad0119a2015-01-23 16:10:57 +010056 set_bit(index, &line6pcm->in.active_urbs);
Markus Grabner705ecec2009-02-27 19:43:04 -080057 else
Markus Grabner1027f472010-08-12 01:35:30 +020058 dev_err(line6pcm->line6->ifcdev,
Markus Grabnere1a164d2010-08-23 01:08:25 +020059 "URB in #%d submission failed (%d)\n", index, ret);
Markus Grabner705ecec2009-02-27 19:43:04 -080060
Markus Grabner705ecec2009-02-27 19:43:04 -080061 return 0;
62}
63
64/*
65 Submit all currently available capture URBs.
Takashi Iwai63e20df2015-01-27 15:24:09 +010066 must be called in line6pcm->in.lock context
Markus Grabner705ecec2009-02-27 19:43:04 -080067*/
Markus Grabner1027f472010-08-12 01:35:30 +020068int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -080069{
Takashi Iwai3d3ae442015-01-27 12:50:44 +010070 int ret = 0, i;
Markus Grabner705ecec2009-02-27 19:43:04 -080071
Andrej Krutakb2233d92016-09-18 20:59:21 +020072 for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
Markus Grabner1027f472010-08-12 01:35:30 +020073 ret = submit_audio_in_urb(line6pcm);
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080074 if (ret < 0)
Takashi Iwai3d3ae442015-01-27 12:50:44 +010075 break;
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -080076 }
Markus Grabner705ecec2009-02-27 19:43:04 -080077
Takashi Iwai3d3ae442015-01-27 12:50:44 +010078 return ret;
Markus Grabner705ecec2009-02-27 19:43:04 -080079}
80
81/*
Markus Grabner1027f472010-08-12 01:35:30 +020082 Copy data into ALSA capture buffer.
83*/
84void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
85{
86 struct snd_pcm_substream *substream =
87 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
88 struct snd_pcm_runtime *runtime = substream->runtime;
Andrej Krutak97d78ac2016-09-18 20:59:24 +020089 const int bytes_per_frame =
90 line6pcm->properties->bytes_per_channel *
91 line6pcm->properties->capture_hw.channels_max;
Markus Grabner1027f472010-08-12 01:35:30 +020092 int frames = fsize / bytes_per_frame;
93
Peter Huewe597a1e72010-12-07 23:38:02 +010094 if (runtime == NULL)
Markus Grabnerc7fcf252010-09-17 23:33:25 +020095 return;
96
Takashi Iwaiad0119a2015-01-23 16:10:57 +010097 if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
Markus Grabner1027f472010-08-12 01:35:30 +020098 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +020099 The transferred area goes over buffer boundary,
100 copy two separate chunks.
101 */
Markus Grabner1027f472010-08-12 01:35:30 +0200102 int len;
Mikhail Boikob5f87cf2014-03-18 23:59:46 +0200103
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100104 len = runtime->buffer_size - line6pcm->in.pos_done;
Markus Grabner1027f472010-08-12 01:35:30 +0200105
106 if (len > 0) {
107 memcpy(runtime->dma_area +
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100108 line6pcm->in.pos_done * bytes_per_frame, fbuf,
Markus Grabner1027f472010-08-12 01:35:30 +0200109 len * bytes_per_frame);
110 memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
111 (frames - len) * bytes_per_frame);
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700112 } else {
113 /* this is somewhat paranoid */
114 dev_err(line6pcm->line6->ifcdev,
115 "driver bug: len = %d\n", len);
116 }
Markus Grabner1027f472010-08-12 01:35:30 +0200117 } else {
118 /* copy single chunk */
119 memcpy(runtime->dma_area +
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100120 line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
Markus Grabner1027f472010-08-12 01:35:30 +0200121 }
122
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100123 line6pcm->in.pos_done += frames;
124 if (line6pcm->in.pos_done >= runtime->buffer_size)
125 line6pcm->in.pos_done -= runtime->buffer_size;
Markus Grabner1027f472010-08-12 01:35:30 +0200126}
127
128void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
129{
130 struct snd_pcm_substream *substream =
131 get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
132
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100133 line6pcm->in.bytes += length;
134 if (line6pcm->in.bytes >= line6pcm->in.period) {
135 line6pcm->in.bytes %= line6pcm->in.period;
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100136 spin_unlock(&line6pcm->in.lock);
Markus Grabner1027f472010-08-12 01:35:30 +0200137 snd_pcm_period_elapsed(substream);
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100138 spin_lock(&line6pcm->in.lock);
Markus Grabner1027f472010-08-12 01:35:30 +0200139 }
140}
141
142/*
Greg Kroah-Hartman027360c2010-09-21 16:58:00 -0700143 * Callback for completed capture URB.
144 */
Greg Kroah-Hartman0c7ab152009-02-27 20:28:04 -0800145static void audio_in_callback(struct urb *urb)
Markus Grabner705ecec2009-02-27 19:43:04 -0800146{
147 int i, index, length = 0, shutdown = 0;
Markus Grabner705ecec2009-02-27 19:43:04 -0800148 unsigned long flags;
149
Markus Grabner1027f472010-08-12 01:35:30 +0200150 struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
151
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100152 line6pcm->in.last_frame = urb->start_frame;
Markus Grabner705ecec2009-02-27 19:43:04 -0800153
154 /* find index of URB */
Andrej Krutakb2233d92016-09-18 20:59:21 +0200155 for (index = 0; index < line6pcm->line6->iso_buffers; ++index)
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100156 if (urb == line6pcm->in.urbs[index])
Markus Grabner705ecec2009-02-27 19:43:04 -0800157 break;
158
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100159 spin_lock_irqsave(&line6pcm->in.lock, flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800160
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800161 for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800162 char *fbuf;
163 int fsize;
164 struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
165
Markus Grabnere1a164d2010-08-23 01:08:25 +0200166 if (fin->status == -EXDEV) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800167 shutdown = 1;
168 break;
169 }
170
171 fbuf = urb->transfer_buffer + fin->offset;
172 fsize = fin->actual_length;
Markus Grabner1027f472010-08-12 01:35:30 +0200173
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200174 if (fsize > line6pcm->max_packet_size_in) {
Markus Grabner1027f472010-08-12 01:35:30 +0200175 dev_err(line6pcm->line6->ifcdev,
176 "driver and/or device bug: packet too large (%d > %d)\n",
Andrej Krutak7a0f55a2016-09-18 20:59:23 +0200177 fsize, line6pcm->max_packet_size_in);
Markus Grabner1027f472010-08-12 01:35:30 +0200178 }
179
Markus Grabner705ecec2009-02-27 19:43:04 -0800180 length += fsize;
181
Andrej Krutak79faa2b2016-09-18 20:59:22 +0200182 BUILD_BUG_ON_MSG(LINE6_ISO_PACKETS != 1,
183 "The following code assumes LINE6_ISO_PACKETS == 1");
184 /* TODO:
185 * Also, if iso_buffers != 2, the prev frame is almost random at
186 * playback side.
187 * This needs to be redesigned. It should be "stable", but we may
188 * experience sync problems on such high-speed configs.
189 */
190
Markus Grabner1027f472010-08-12 01:35:30 +0200191 line6pcm->prev_fbuf = fbuf;
Andrej Krutak97d78ac2016-09-18 20:59:24 +0200192 line6pcm->prev_fsize = fsize /
193 (line6pcm->properties->bytes_per_channel *
194 line6pcm->properties->capture_hw.channels_max);
Markus Grabner705ecec2009-02-27 19:43:04 -0800195
Takashi Iwai63e20df2015-01-27 15:24:09 +0100196 if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
197 test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) &&
198 fsize > 0)
199 line6_capture_copy(line6pcm, fbuf, fsize);
Markus Grabner705ecec2009-02-27 19:43:04 -0800200 }
201
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100202 clear_bit(index, &line6pcm->in.active_urbs);
Markus Grabner705ecec2009-02-27 19:43:04 -0800203
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100204 if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
Markus Grabner705ecec2009-02-27 19:43:04 -0800205 shutdown = 1;
206
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800207 if (!shutdown) {
Markus Grabner1027f472010-08-12 01:35:30 +0200208 submit_audio_in_urb(line6pcm);
Markus Grabner705ecec2009-02-27 19:43:04 -0800209
Takashi Iwai63e20df2015-01-27 15:24:09 +0100210 if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
211 test_bit(LINE6_STREAM_PCM, &line6pcm->in.running))
212 line6_capture_check_period(line6pcm, length);
Markus Grabner705ecec2009-02-27 19:43:04 -0800213 }
Takashi Iwai3d3ae442015-01-27 12:50:44 +0100214
215 spin_unlock_irqrestore(&line6pcm->in.lock, flags);
Markus Grabner705ecec2009-02-27 19:43:04 -0800216}
217
218/* open capture callback */
219static int snd_line6_capture_open(struct snd_pcm_substream *substream)
220{
221 int err;
222 struct snd_pcm_runtime *runtime = substream->runtime;
223 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
224
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800225 err = snd_pcm_hw_constraint_ratdens(runtime, 0,
226 SNDRV_PCM_HW_PARAM_RATE,
Takashi Iwai1263f612015-01-28 15:08:59 +0100227 &line6pcm->properties->rates);
Greg Kroah-Hartman6efc5662009-02-27 22:39:22 -0800228 if (err < 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800229 return err;
230
Andrej Krutakf56742c2016-09-18 20:59:25 +0200231 line6_pcm_acquire(line6pcm, LINE6_STREAM_CAPTURE_HELPER, false);
232
Takashi Iwai1263f612015-01-28 15:08:59 +0100233 runtime->hw = line6pcm->properties->capture_hw;
Markus Grabner705ecec2009-02-27 19:43:04 -0800234 return 0;
235}
236
237/* close capture callback */
238static int snd_line6_capture_close(struct snd_pcm_substream *substream)
239{
Andrej Krutakf56742c2016-09-18 20:59:25 +0200240 struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
241
242 line6_pcm_release(line6pcm, LINE6_STREAM_CAPTURE_HELPER);
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 return 0;
244}
245
Markus Grabner705ecec2009-02-27 19:43:04 -0800246/* capture operators */
Bhumika Goyale195a332017-09-27 11:49:27 +0530247const struct snd_pcm_ops snd_line6_capture_ops = {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200248 .open = snd_line6_capture_open,
249 .close = snd_line6_capture_close,
Takashi Iwai63e20df2015-01-27 15:24:09 +0100250 .hw_params = snd_line6_hw_params,
251 .hw_free = snd_line6_hw_free,
Markus Grabnere1a164d2010-08-23 01:08:25 +0200252 .prepare = snd_line6_prepare,
253 .trigger = snd_line6_trigger,
Takashi Iwai2954f912015-01-27 15:41:27 +0100254 .pointer = snd_line6_pointer,
Markus Grabner705ecec2009-02-27 19:43:04 -0800255};
256
Markus Grabner1027f472010-08-12 01:35:30 +0200257int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
Markus Grabner705ecec2009-02-27 19:43:04 -0800258{
Chris Rorvick16d603d2015-01-12 12:42:55 -0800259 struct usb_line6 *line6 = line6pcm->line6;
Markus Grabner705ecec2009-02-27 19:43:04 -0800260 int i;
261
Kees Cook6396bb22018-06-12 14:03:40 -0700262 line6pcm->in.urbs = kcalloc(line6->iso_buffers, sizeof(struct urb *),
263 GFP_KERNEL);
Andrej Krutakb2233d92016-09-18 20:59:21 +0200264 if (line6pcm->in.urbs == NULL)
265 return -ENOMEM;
266
Markus Grabner705ecec2009-02-27 19:43:04 -0800267 /* create audio URBs and fill in constant values: */
Andrej Krutakb2233d92016-09-18 20:59:21 +0200268 for (i = 0; i < line6->iso_buffers; ++i) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800269 struct urb *urb;
270
271 /* URB for audio in: */
Takashi Iwaiad0119a2015-01-23 16:10:57 +0100272 urb = line6pcm->in.urbs[i] =
Shawn Bohrere0645d62009-11-15 22:17:52 -0600273 usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
Markus Grabner705ecec2009-02-27 19:43:04 -0800274
Takashi Iwaia019f5e2015-01-19 15:05:10 +0100275 if (urb == NULL)
Markus Grabner705ecec2009-02-27 19:43:04 -0800276 return -ENOMEM;
Markus Grabner705ecec2009-02-27 19:43:04 -0800277
Chris Rorvick16d603d2015-01-12 12:42:55 -0800278 urb->dev = line6->usbdev;
Shawn Bohrere0645d62009-11-15 22:17:52 -0600279 urb->pipe =
Chris Rorvick16d603d2015-01-12 12:42:55 -0800280 usb_rcvisocpipe(line6->usbdev,
281 line6->properties->ep_audio_r &
Markus Grabnere1a164d2010-08-23 01:08:25 +0200282 USB_ENDPOINT_NUMBER_MASK);
Markus Grabner705ecec2009-02-27 19:43:04 -0800283 urb->transfer_flags = URB_ISO_ASAP;
284 urb->start_frame = -1;
285 urb->number_of_packets = LINE6_ISO_PACKETS;
286 urb->interval = LINE6_ISO_INTERVAL;
287 urb->error_count = 0;
288 urb->complete = audio_in_callback;
Takashi Iwai6e8a9142020-07-10 15:33:51 +0200289 if (usb_urb_ep_type_check(urb))
290 return -EINVAL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800291 }
292
293 return 0;
294}