blob: 444bb637ce13c600ec49032829b1ae3414c4a22b [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack523f1dc2007-03-26 19:11:24 +02002/*
Daniel Mack8d048842008-04-14 15:39:14 +02003 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese
Daniel Mack523f1dc2007-03-26 19:11:24 +02004*/
5
Daniel Mackf1f6b8f2013-03-03 20:46:22 +01006#include <linux/device.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +02007#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Daniel Macke431cf42009-03-28 21:19:49 +01009#include <linux/init.h>
10#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020011#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020012#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020013
Daniel Mack936e7d02009-04-01 19:05:39 +020014#include "device.h"
15#include "audio.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020016
17#define N_URBS 32
18#define CLOCK_DRIFT_TOLERANCE 5
19#define FRAMES_PER_URB 8
20#define BYTES_PER_FRAME 512
21#define CHANNELS_PER_STREAM 2
22#define BYTES_PER_SAMPLE 3
23#define BYTES_PER_SAMPLE_USB 4
24#define MAX_BUFFER_SIZE (128*1024)
Daniel Mack6e9fc6b2008-04-14 15:40:31 +020025#define MAX_ENDPOINT_SIZE 512
26
Daniel Mack523f1dc2007-03-26 19:11:24 +020027#define ENDPOINT_CAPTURE 2
28#define ENDPOINT_PLAYBACK 6
29
Daniel Mack1c8470c2013-03-03 20:46:21 +010030#define MAKE_CHECKBYTE(cdev,stream,i) \
31 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +020032
33static struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = {
Daniel Mack9318dce2009-06-01 21:36:23 +020034 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Daniel Mack523f1dc2007-03-26 19:11:24 +020035 SNDRV_PCM_INFO_BLOCK_TRANSFER),
36 .formats = SNDRV_PCM_FMTBIT_S24_3BE,
Daniel Mack9318dce2009-06-01 21:36:23 +020037 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
Daniel Mack523f1dc2007-03-26 19:11:24 +020038 SNDRV_PCM_RATE_96000),
39 .rate_min = 44100,
40 .rate_max = 0, /* will overwrite later */
41 .channels_min = CHANNELS_PER_STREAM,
42 .channels_max = CHANNELS_PER_STREAM,
43 .buffer_bytes_max = MAX_BUFFER_SIZE,
Daniel Mack09189ac2008-01-24 18:46:42 +010044 .period_bytes_min = 128,
Daniel Mack523f1dc2007-03-26 19:11:24 +020045 .period_bytes_max = MAX_BUFFER_SIZE,
46 .periods_min = 1,
47 .periods_max = 1024,
48};
49
50static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010051activate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020052 struct snd_pcm_substream *sub)
53{
Daniel Mack1c8470c2013-03-03 20:46:21 +010054 spin_lock(&cdev->spinlock);
Mark Hillsac9dd9d2009-10-24 12:59:36 +010055
Daniel Mack523f1dc2007-03-26 19:11:24 +020056 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010057 cdev->sub_playback[sub->number] = sub;
Daniel Mack523f1dc2007-03-26 19:11:24 +020058 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010059 cdev->sub_capture[sub->number] = sub;
Mark Hillsac9dd9d2009-10-24 12:59:36 +010060
Daniel Mack1c8470c2013-03-03 20:46:21 +010061 spin_unlock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +020062}
63
Daniel Mack9318dce2009-06-01 21:36:23 +020064static void
Daniel Mack1c8470c2013-03-03 20:46:21 +010065deactivate_substream(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020066 struct snd_pcm_substream *sub)
67{
Daniel Mack8d048842008-04-14 15:39:14 +020068 unsigned long flags;
Daniel Mack1c8470c2013-03-03 20:46:21 +010069 spin_lock_irqsave(&cdev->spinlock, flags);
Daniel Mack8d048842008-04-14 15:39:14 +020070
Daniel Mack523f1dc2007-03-26 19:11:24 +020071 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack1c8470c2013-03-03 20:46:21 +010072 cdev->sub_playback[sub->number] = NULL;
Daniel Mack523f1dc2007-03-26 19:11:24 +020073 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010074 cdev->sub_capture[sub->number] = NULL;
Daniel Mack8d048842008-04-14 15:39:14 +020075
Daniel Mack1c8470c2013-03-03 20:46:21 +010076 spin_unlock_irqrestore(&cdev->spinlock, flags);
Daniel Mack523f1dc2007-03-26 19:11:24 +020077}
78
79static int
80all_substreams_zero(struct snd_pcm_substream **subs)
81{
82 int i;
83 for (i = 0; i < MAX_STREAMS; i++)
84 if (subs[i] != NULL)
85 return 0;
86 return 1;
87}
88
Daniel Mack1c8470c2013-03-03 20:46:21 +010089static int stream_start(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +020090{
91 int i, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010092 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +020093
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010094 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +020095
Daniel Mack1c8470c2013-03-03 20:46:21 +010096 if (cdev->streaming)
Daniel Mack8d048842008-04-14 15:39:14 +020097 return -EINVAL;
98
Daniel Mack1c8470c2013-03-03 20:46:21 +010099 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
100 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
101 cdev->input_panic = 0;
102 cdev->output_panic = 0;
103 cdev->first_packet = 4;
104 cdev->streaming = 1;
105 cdev->warned = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200106
107 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100108 ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200109 if (ret) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100110 dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
111 i, ret);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100112 cdev->streaming = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200113 return -EPIPE;
114 }
115 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200116
Daniel Mack523f1dc2007-03-26 19:11:24 +0200117 return 0;
118}
119
Daniel Mack1c8470c2013-03-03 20:46:21 +0100120static void stream_stop(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200121{
122 int i;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100123 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack8d048842008-04-14 15:39:14 +0200124
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100125 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100126 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200127 return;
Daniel Mack9318dce2009-06-01 21:36:23 +0200128
Daniel Mack1c8470c2013-03-03 20:46:21 +0100129 cdev->streaming = 0;
Daniel Mack8d048842008-04-14 15:39:14 +0200130
Daniel Mack523f1dc2007-03-26 19:11:24 +0200131 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100132 usb_kill_urb(cdev->data_urbs_in[i]);
Daniel Mackda6094e2011-08-14 11:31:16 +0200133
Daniel Mack1c8470c2013-03-03 20:46:21 +0100134 if (test_bit(i, &cdev->outurb_active_mask))
135 usb_kill_urb(cdev->data_urbs_out[i]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200136 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200137
Daniel Mack1c8470c2013-03-03 20:46:21 +0100138 cdev->outurb_active_mask = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200139}
140
141static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream)
142{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100143 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100144 struct device *dev = caiaqdev_to_dev(cdev);
145
146 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100147 substream->runtime->hw = cdev->pcm_info;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200148 snd_pcm_limit_hw_rates(substream->runtime);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100149
Daniel Mack523f1dc2007-03-26 19:11:24 +0200150 return 0;
151}
152
153static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
154{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100155 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100156 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200157
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100158 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100159 if (all_substreams_zero(cdev->sub_playback) &&
160 all_substreams_zero(cdev->sub_capture)) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200161 /* when the last client has stopped streaming,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200162 * all sample rates are allowed again */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100163 stream_stop(cdev);
164 cdev->pcm_info.rates = cdev->samplerates;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200165 }
Daniel Mack8d048842008-04-14 15:39:14 +0200166
Daniel Mack523f1dc2007-03-26 19:11:24 +0200167 return 0;
168}
169
170static int snd_usb_caiaq_pcm_hw_params(struct snd_pcm_substream *sub,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800171 struct snd_pcm_hw_params *hw_params)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200172{
Antonio Ospitefc76f862013-06-21 13:11:49 +0200173 return snd_pcm_lib_alloc_vmalloc_buffer(sub,
174 params_buffer_bytes(hw_params));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200175}
176
177static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub)
178{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100179 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100180 deactivate_substream(cdev, sub);
Antonio Ospitefc76f862013-06-21 13:11:49 +0200181 return snd_pcm_lib_free_vmalloc_buffer(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200182}
183
184/* this should probably go upstream */
185#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
186#error "Change this table"
187#endif
188
189static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800190 48000, 64000, 88200, 96000, 176400, 192000 };
Daniel Mack523f1dc2007-03-26 19:11:24 +0200191
192static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
193{
194 int bytes_per_sample, bpp, ret, i;
195 int index = substream->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100196 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200197 struct snd_pcm_runtime *runtime = substream->runtime;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100198 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200199
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100200 dev_dbg(dev, "%s(%p)\n", __func__, substream);
Daniel Mack9318dce2009-06-01 21:36:23 +0200201
Daniel Macka9b487f2009-04-27 12:18:05 +0200202 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800203 int out_pos;
204
Daniel Mack1c8470c2013-03-03 20:46:21 +0100205 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800206 case 0:
207 case 2:
208 out_pos = BYTES_PER_SAMPLE + 1;
209 break;
210 case 3:
211 default:
212 out_pos = 0;
213 break;
214 }
215
Daniel Mack1c8470c2013-03-03 20:46:21 +0100216 cdev->period_out_count[index] = out_pos;
217 cdev->audio_out_buf_pos[index] = out_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200218 } else {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800219 int in_pos;
220
Daniel Mack1c8470c2013-03-03 20:46:21 +0100221 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800222 case 0:
223 in_pos = BYTES_PER_SAMPLE + 2;
224 break;
225 case 2:
226 in_pos = BYTES_PER_SAMPLE;
227 break;
228 case 3:
229 default:
230 in_pos = 0;
231 break;
232 }
233
Daniel Mack1c8470c2013-03-03 20:46:21 +0100234 cdev->period_in_count[index] = in_pos;
235 cdev->audio_in_buf_pos[index] = in_pos;
Daniel Macka9b487f2009-04-27 12:18:05 +0200236 }
237
Daniel Mack1c8470c2013-03-03 20:46:21 +0100238 if (cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200239 return 0;
Daniel Mack9318dce2009-06-01 21:36:23 +0200240
Daniel Mack523f1dc2007-03-26 19:11:24 +0200241 /* the first client that opens a stream defines the sample rate
242 * setting for all subsequent calls, until the last client closed. */
243 for (i=0; i < ARRAY_SIZE(rates); i++)
244 if (runtime->rate == rates[i])
Daniel Mack1c8470c2013-03-03 20:46:21 +0100245 cdev->pcm_info.rates = 1 << i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200246
Daniel Mack523f1dc2007-03-26 19:11:24 +0200247 snd_pcm_limit_hw_rates(runtime);
248
249 bytes_per_sample = BYTES_PER_SAMPLE;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100250 if (cdev->spec.data_alignment >= 2)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200251 bytes_per_sample++;
Daniel Mack9318dce2009-06-01 21:36:23 +0200252
Daniel Mack523f1dc2007-03-26 19:11:24 +0200253 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100254 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams;
Daniel Mack6e9fc6b2008-04-14 15:40:31 +0200255
256 if (bpp > MAX_ENDPOINT_SIZE)
257 bpp = MAX_ENDPOINT_SIZE;
258
Daniel Mack1c8470c2013-03-03 20:46:21 +0100259 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200260 runtime->sample_bits, bpp);
261 if (ret)
262 return ret;
263
Daniel Mack1c8470c2013-03-03 20:46:21 +0100264 ret = stream_start(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200265 if (ret)
266 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200267
Daniel Mack1c8470c2013-03-03 20:46:21 +0100268 cdev->output_running = 0;
269 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ);
270 if (!cdev->output_running) {
271 stream_stop(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200272 return -EPIPE;
273 }
274
275 return 0;
276}
277
278static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd)
279{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100280 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100281 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200282
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100283 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800284
Daniel Mack523f1dc2007-03-26 19:11:24 +0200285 switch (cmd) {
286 case SNDRV_PCM_TRIGGER_START:
287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100288 activate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200289 break;
290 case SNDRV_PCM_TRIGGER_STOP:
291 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100292 deactivate_substream(cdev, sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200293 break;
294 default:
295 return -EINVAL;
296 }
297
298 return 0;
299}
300
301static snd_pcm_uframes_t
302snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
303{
304 int index = sub->number;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100305 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
Mark Hills3702b082009-10-24 12:59:35 +0100306 snd_pcm_uframes_t ptr;
307
Daniel Mack1c8470c2013-03-03 20:46:21 +0100308 spin_lock(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200309
Daniel Mack1c8470c2013-03-03 20:46:21 +0100310 if (cdev->input_panic || cdev->output_panic) {
Mark Hills3702b082009-10-24 12:59:35 +0100311 ptr = SNDRV_PCM_POS_XRUN;
Mark Hillscb74eb12012-02-21 21:26:31 +0000312 goto unlock;
313 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200314
315 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Hills3702b082009-10-24 12:59:35 +0100316 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100317 cdev->audio_out_buf_pos[index]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200318 else
Mark Hills3702b082009-10-24 12:59:35 +0100319 ptr = bytes_to_frames(sub->runtime,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100320 cdev->audio_in_buf_pos[index]);
Mark Hills3702b082009-10-24 12:59:35 +0100321
Mark Hillscb74eb12012-02-21 21:26:31 +0000322unlock:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100323 spin_unlock(&cdev->spinlock);
Mark Hills3702b082009-10-24 12:59:35 +0100324 return ptr;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200325}
326
327/* operators for both playback and capture */
Arvind Yadav31cb1fb2017-08-18 13:15:21 +0530328static const struct snd_pcm_ops snd_usb_caiaq_ops = {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200329 .open = snd_usb_caiaq_substream_open,
330 .close = snd_usb_caiaq_substream_close,
331 .ioctl = snd_pcm_lib_ioctl,
332 .hw_params = snd_usb_caiaq_pcm_hw_params,
333 .hw_free = snd_usb_caiaq_pcm_hw_free,
334 .prepare = snd_usb_caiaq_pcm_prepare,
335 .trigger = snd_usb_caiaq_pcm_trigger,
Antonio Ospitefc76f862013-06-21 13:11:49 +0200336 .pointer = snd_usb_caiaq_pcm_pointer,
337 .page = snd_pcm_lib_get_vmalloc_page,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200338};
Daniel Mack9318dce2009-06-01 21:36:23 +0200339
Daniel Mack1c8470c2013-03-03 20:46:21 +0100340static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200341 struct snd_pcm_substream **subs)
342{
343 int stream, pb, *cnt;
344 struct snd_pcm_substream *sub;
345
Daniel Mack1c8470c2013-03-03 20:46:21 +0100346 for (stream = 0; stream < cdev->n_streams; stream++) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200347 sub = subs[stream];
348 if (!sub)
349 continue;
350
Daniel Macka9b487f2009-04-27 12:18:05 +0200351 pb = snd_pcm_lib_period_bytes(sub);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200352 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack1c8470c2013-03-03 20:46:21 +0100353 &cdev->period_out_count[stream] :
354 &cdev->period_in_count[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200355
356 if (*cnt >= pb) {
357 snd_pcm_period_elapsed(sub);
358 *cnt %= pb;
359 }
360 }
361}
362
Daniel Mack1c8470c2013-03-03 20:46:21 +0100363static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200364 const struct urb *urb,
365 const struct usb_iso_packet_descriptor *iso)
366{
367 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
368 struct snd_pcm_substream *sub;
369 int stream, i;
370
Daniel Mack1c8470c2013-03-03 20:46:21 +0100371 if (all_substreams_zero(cdev->sub_capture))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200372 return;
373
Daniel Mack523f1dc2007-03-26 19:11:24 +0200374 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100375 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
376 sub = cdev->sub_capture[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200377 if (sub) {
378 struct snd_pcm_runtime *rt = sub->runtime;
379 char *audio_buf = rt->dma_area;
380 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100381 audio_buf[cdev->audio_in_buf_pos[stream]++]
Daniel Mack523f1dc2007-03-26 19:11:24 +0200382 = usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100383 cdev->period_in_count[stream]++;
384 if (cdev->audio_in_buf_pos[stream] == sz)
385 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200386 }
387 }
388 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200389}
390
Daniel Mack1c8470c2013-03-03 20:46:21 +0100391static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200392 const struct urb *urb,
393 const struct usb_iso_packet_descriptor *iso)
394{
395 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
396 unsigned char check_byte;
397 struct snd_pcm_substream *sub;
398 int stream, i;
399
Daniel Mack523f1dc2007-03-26 19:11:24 +0200400 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100401 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200402 for (stream = 0;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100403 stream < cdev->n_streams;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200404 stream++, i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100405 if (cdev->first_packet)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200406 continue;
407
Daniel Mack1c8470c2013-03-03 20:46:21 +0100408 check_byte = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack9318dce2009-06-01 21:36:23 +0200409
Daniel Mack523f1dc2007-03-26 19:11:24 +0200410 if ((usb_buf[i] & 0x3f) != check_byte)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100411 cdev->input_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200412
413 if (usb_buf[i] & 0x80)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100414 cdev->output_panic = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200415 }
416 }
Daniel Mack1c8470c2013-03-03 20:46:21 +0100417 cdev->first_packet = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200418
Daniel Mack1c8470c2013-03-03 20:46:21 +0100419 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
420 sub = cdev->sub_capture[stream];
421 if (cdev->input_panic)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100422 usb_buf[i] = 0;
423
Daniel Mack523f1dc2007-03-26 19:11:24 +0200424 if (sub) {
425 struct snd_pcm_runtime *rt = sub->runtime;
426 char *audio_buf = rt->dma_area;
427 int sz = frames_to_bytes(rt, rt->buffer_size);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100428 audio_buf[cdev->audio_in_buf_pos[stream]++] =
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200429 usb_buf[i];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100430 cdev->period_in_count[stream]++;
431 if (cdev->audio_in_buf_pos[stream] == sz)
432 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200433 }
434 }
435 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200436}
437
Daniel Mack1c8470c2013-03-03 20:46:21 +0100438static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800439 const struct urb *urb,
440 const struct usb_iso_packet_descriptor *iso)
441{
442 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100443 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800444 int stream, i;
445
446 /* paranoia check */
447 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM))
448 return;
449
450 for (i = 0; i < iso->actual_length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100451 for (stream = 0; stream < cdev->n_streams; stream++) {
452 struct snd_pcm_substream *sub = cdev->sub_capture[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800453 char *audio_buf = NULL;
454 int c, n, sz = 0;
455
Daniel Mack1c8470c2013-03-03 20:46:21 +0100456 if (sub && !cdev->input_panic) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800457 struct snd_pcm_runtime *rt = sub->runtime;
458 audio_buf = rt->dma_area;
459 sz = frames_to_bytes(rt, rt->buffer_size);
460 }
461
462 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
463 /* 3 audio data bytes, followed by 1 check byte */
464 if (audio_buf) {
465 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100466 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800467
Daniel Mack1c8470c2013-03-03 20:46:21 +0100468 if (cdev->audio_in_buf_pos[stream] == sz)
469 cdev->audio_in_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800470 }
471
Daniel Mack1c8470c2013-03-03 20:46:21 +0100472 cdev->period_in_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800473 }
474
475 i += BYTES_PER_SAMPLE;
476
477 if (usb_buf[i] != ((stream << 1) | c) &&
Daniel Mack1c8470c2013-03-03 20:46:21 +0100478 !cdev->first_packet) {
479 if (!cdev->input_panic)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100480 dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n",
481 ((stream << 1) | c), usb_buf[i], c, stream, i);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100482 cdev->input_panic = 1;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800483 }
484
485 i++;
486 }
487 }
488 }
489
Daniel Mack1c8470c2013-03-03 20:46:21 +0100490 if (cdev->first_packet > 0)
491 cdev->first_packet--;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800492}
493
Daniel Mack1c8470c2013-03-03 20:46:21 +0100494static void read_in_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200495 const struct urb *urb,
496 const struct usb_iso_packet_descriptor *iso)
497{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100498 struct device *dev = caiaqdev_to_dev(cdev);
499
Daniel Mack1c8470c2013-03-03 20:46:21 +0100500 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200501 return;
502
Daniel Mack1c8470c2013-03-03 20:46:21 +0100503 if (iso->actual_length < cdev->bpp)
Daniel Mack9311c9b2009-03-18 11:03:54 +0100504 return;
505
Daniel Mack1c8470c2013-03-03 20:46:21 +0100506 switch (cdev->spec.data_alignment) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200507 case 0:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100508 read_in_urb_mode0(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200509 break;
510 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100511 read_in_urb_mode2(cdev, urb, iso);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200512 break;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800513 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100514 read_in_urb_mode3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800515 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200516 }
517
Daniel Mack1c8470c2013-03-03 20:46:21 +0100518 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100519 dev_warn(dev, "streaming error detected %s %s\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100520 cdev->input_panic ? "(input)" : "",
521 cdev->output_panic ? "(output)" : "");
522 cdev->warned = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200523 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200524}
525
Daniel Mack1c8470c2013-03-03 20:46:21 +0100526static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800527 struct urb *urb,
528 const struct usb_iso_packet_descriptor *iso)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200529{
530 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
531 struct snd_pcm_substream *sub;
532 int stream, i;
Daniel Mack9318dce2009-06-01 21:36:23 +0200533
Daniel Mack523f1dc2007-03-26 19:11:24 +0200534 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100535 for (stream = 0; stream < cdev->n_streams; stream++, i++) {
536 sub = cdev->sub_playback[stream];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200537 if (sub) {
538 struct snd_pcm_runtime *rt = sub->runtime;
539 char *audio_buf = rt->dma_area;
540 int sz = frames_to_bytes(rt, rt->buffer_size);
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200541 usb_buf[i] =
Daniel Mack1c8470c2013-03-03 20:46:21 +0100542 audio_buf[cdev->audio_out_buf_pos[stream]];
543 cdev->period_out_count[stream]++;
544 cdev->audio_out_buf_pos[stream]++;
545 if (cdev->audio_out_buf_pos[stream] == sz)
546 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200547 } else
Karsten Wiesea971c3d42007-03-29 17:02:45 +0200548 usb_buf[i] = 0;
549 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200550
551 /* fill in the check bytes */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100552 if (cdev->spec.data_alignment == 2 &&
553 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) ==
554 (cdev->n_streams * CHANNELS_PER_STREAM))
555 for (stream = 0; stream < cdev->n_streams; stream++, i++)
556 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800557 }
558}
559
Daniel Mack1c8470c2013-03-03 20:46:21 +0100560static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800561 struct urb *urb,
562 const struct usb_iso_packet_descriptor *iso)
563{
564 unsigned char *usb_buf = urb->transfer_buffer + iso->offset;
565 int stream, i;
566
567 for (i = 0; i < iso->length;) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100568 for (stream = 0; stream < cdev->n_streams; stream++) {
569 struct snd_pcm_substream *sub = cdev->sub_playback[stream];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800570 char *audio_buf = NULL;
571 int c, n, sz = 0;
572
573 if (sub) {
574 struct snd_pcm_runtime *rt = sub->runtime;
575 audio_buf = rt->dma_area;
576 sz = frames_to_bytes(rt, rt->buffer_size);
577 }
578
579 for (c = 0; c < CHANNELS_PER_STREAM; c++) {
580 for (n = 0; n < BYTES_PER_SAMPLE; n++) {
581 if (audio_buf) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100582 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++];
Daniel Mack15c5ab62010-09-10 17:04:57 +0800583
Daniel Mack1c8470c2013-03-03 20:46:21 +0100584 if (cdev->audio_out_buf_pos[stream] == sz)
585 cdev->audio_out_buf_pos[stream] = 0;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800586 } else {
587 usb_buf[i+n] = 0;
588 }
589 }
590
591 if (audio_buf)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100592 cdev->period_out_count[stream] += BYTES_PER_SAMPLE;
Daniel Mack15c5ab62010-09-10 17:04:57 +0800593
594 i += BYTES_PER_SAMPLE;
595
596 /* fill in the check byte pattern */
597 usb_buf[i++] = (stream << 1) | c;
598 }
599 }
600 }
601}
602
Daniel Mack1c8470c2013-03-03 20:46:21 +0100603static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev,
Daniel Mack15c5ab62010-09-10 17:04:57 +0800604 struct urb *urb,
605 const struct usb_iso_packet_descriptor *iso)
606{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100607 switch (cdev->spec.data_alignment) {
Daniel Mack15c5ab62010-09-10 17:04:57 +0800608 case 0:
609 case 2:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100610 fill_out_urb_mode_0(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800611 break;
612 case 3:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100613 fill_out_urb_mode_3(cdev, urb, iso);
Daniel Mack15c5ab62010-09-10 17:04:57 +0800614 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200615 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200616}
617
618static void read_completed(struct urb *urb)
619{
Daniel Mack9318dce2009-06-01 21:36:23 +0200620 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100621 struct snd_usb_caiaqdev *cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100622 struct device *dev;
Daniel Mackda6094e2011-08-14 11:31:16 +0200623 struct urb *out = NULL;
624 int i, frame, len, send_it = 0, outframe = 0;
John Ogness9b112332018-07-01 17:28:07 +0200625 unsigned long flags;
Daniel Mack15439bd2011-08-05 13:49:52 +0200626 size_t offset = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200627
628 if (urb->status || !info)
629 return;
630
Daniel Mack1c8470c2013-03-03 20:46:21 +0100631 cdev = info->cdev;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100632 dev = caiaqdev_to_dev(cdev);
Daniel Mack8d048842008-04-14 15:39:14 +0200633
Daniel Mack1c8470c2013-03-03 20:46:21 +0100634 if (!cdev->streaming)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200635 return;
636
Daniel Mackda6094e2011-08-14 11:31:16 +0200637 /* find an unused output urb that is unused */
638 for (i = 0; i < N_URBS; i++)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100639 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) {
640 out = cdev->data_urbs_out[i];
Daniel Mackda6094e2011-08-14 11:31:16 +0200641 break;
642 }
643
644 if (!out) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100645 dev_err(dev, "Unable to find an output urb to use\n");
Daniel Mackda6094e2011-08-14 11:31:16 +0200646 goto requeue;
647 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200648
649 /* read the recently received packet and send back one which has
650 * the same layout */
651 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
652 if (urb->iso_frame_desc[frame].status)
653 continue;
654
655 len = urb->iso_frame_desc[outframe].actual_length;
656 out->iso_frame_desc[outframe].length = len;
657 out->iso_frame_desc[outframe].actual_length = 0;
Daniel Mack15439bd2011-08-05 13:49:52 +0200658 out->iso_frame_desc[outframe].offset = offset;
659 offset += len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200660
Daniel Mack523f1dc2007-03-26 19:11:24 +0200661 if (len > 0) {
John Ogness9b112332018-07-01 17:28:07 +0200662 spin_lock_irqsave(&cdev->spinlock, flags);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100663 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
664 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
John Ogness9b112332018-07-01 17:28:07 +0200665 spin_unlock_irqrestore(&cdev->spinlock, flags);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100666 check_for_elapsed_periods(cdev, cdev->sub_playback);
667 check_for_elapsed_periods(cdev, cdev->sub_capture);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200668 send_it = 1;
669 }
670
671 outframe++;
672 }
673
674 if (send_it) {
Daniel Mack15439bd2011-08-05 13:49:52 +0200675 out->number_of_packets = outframe;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200676 usb_submit_urb(out, GFP_ATOMIC);
Daniel Mackda6094e2011-08-14 11:31:16 +0200677 } else {
678 struct snd_usb_caiaq_cb_info *oinfo = out->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100679 clear_bit(oinfo->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200680 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200681
Daniel Mackda6094e2011-08-14 11:31:16 +0200682requeue:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200683 /* re-submit inbound urb */
684 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
685 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame;
686 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME;
687 urb->iso_frame_desc[frame].actual_length = 0;
688 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200689
Daniel Mack523f1dc2007-03-26 19:11:24 +0200690 urb->number_of_packets = FRAMES_PER_URB;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200691 usb_submit_urb(urb, GFP_ATOMIC);
692}
693
694static void write_completed(struct urb *urb)
695{
696 struct snd_usb_caiaq_cb_info *info = urb->context;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100697 struct snd_usb_caiaqdev *cdev = info->cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200698
Daniel Mack1c8470c2013-03-03 20:46:21 +0100699 if (!cdev->output_running) {
700 cdev->output_running = 1;
701 wake_up(&cdev->prepare_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200702 }
Daniel Mackda6094e2011-08-14 11:31:16 +0200703
Daniel Mack1c8470c2013-03-03 20:46:21 +0100704 clear_bit(info->index, &cdev->outurb_active_mask);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200705}
706
Daniel Mack1c8470c2013-03-03 20:46:21 +0100707static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200708{
709 int i, frame;
710 struct urb **urbs;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100711 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200712 unsigned int pipe;
713
Daniel Mack9318dce2009-06-01 21:36:23 +0200714 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ?
Daniel Mack523f1dc2007-03-26 19:11:24 +0200715 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) :
716 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE);
717
Kees Cook6da2ec52018-06-12 13:55:00 -0700718 urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200719 if (!urbs) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200720 *ret = -ENOMEM;
721 return NULL;
722 }
723
724 for (i = 0; i < N_URBS; i++) {
725 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL);
726 if (!urbs[i]) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200727 *ret = -ENOMEM;
728 return urbs;
729 }
730
Daniel Mack9318dce2009-06-01 21:36:23 +0200731 urbs[i]->transfer_buffer =
Kees Cook6da2ec52018-06-12 13:55:00 -0700732 kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB,
733 GFP_KERNEL);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200734 if (!urbs[i]->transfer_buffer) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200735 *ret = -ENOMEM;
736 return urbs;
737 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200738
Daniel Mack523f1dc2007-03-26 19:11:24 +0200739 for (frame = 0; frame < FRAMES_PER_URB; frame++) {
Daniel Mack9318dce2009-06-01 21:36:23 +0200740 struct usb_iso_packet_descriptor *iso =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200741 &urbs[i]->iso_frame_desc[frame];
Daniel Mack9318dce2009-06-01 21:36:23 +0200742
Daniel Mack523f1dc2007-03-26 19:11:24 +0200743 iso->offset = BYTES_PER_FRAME * frame;
744 iso->length = BYTES_PER_FRAME;
745 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200746
Daniel Mack523f1dc2007-03-26 19:11:24 +0200747 urbs[i]->dev = usb_dev;
748 urbs[i]->pipe = pipe;
Daniel Mack9318dce2009-06-01 21:36:23 +0200749 urbs[i]->transfer_buffer_length = FRAMES_PER_URB
Daniel Mack523f1dc2007-03-26 19:11:24 +0200750 * BYTES_PER_FRAME;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100751 urbs[i]->context = &cdev->data_cb_info[i];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200752 urbs[i]->interval = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200753 urbs[i]->number_of_packets = FRAMES_PER_URB;
754 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ?
755 read_completed : write_completed;
756 }
757
758 *ret = 0;
759 return urbs;
760}
761
762static void free_urbs(struct urb **urbs)
763{
764 int i;
765
766 if (!urbs)
767 return;
768
769 for (i = 0; i < N_URBS; i++) {
770 if (!urbs[i])
771 continue;
Daniel Mack9318dce2009-06-01 21:36:23 +0200772
Daniel Mack523f1dc2007-03-26 19:11:24 +0200773 usb_kill_urb(urbs[i]);
774 kfree(urbs[i]->transfer_buffer);
775 usb_free_urb(urbs[i]);
776 }
777
778 kfree(urbs);
779}
780
Daniel Mack1c8470c2013-03-03 20:46:21 +0100781int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200782{
783 int i, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100784 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200785
Daniel Mack1c8470c2013-03-03 20:46:21 +0100786 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in,
787 cdev->spec.num_digital_audio_in) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200788 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100789 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out,
790 cdev->spec.num_digital_audio_out) /
Daniel Mack523f1dc2007-03-26 19:11:24 +0200791 CHANNELS_PER_STREAM;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100792 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200793
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100794 dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in);
795 dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out);
796 dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200797
Daniel Mack1c8470c2013-03-03 20:46:21 +0100798 if (cdev->n_streams > MAX_STREAMS) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100799 dev_err(dev, "unable to initialize device, too many streams.\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200800 return -EINVAL;
801 }
802
Daniel Mack49cdd5b2015-01-04 19:59:29 +0100803 if (cdev->n_streams < 1) {
Daniel Mack897c3292014-10-07 14:25:13 +0200804 dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams);
805 return -EINVAL;
806 }
807
Daniel Mack1c8470c2013-03-03 20:46:21 +0100808 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0,
809 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200810
811 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100812 dev_err(dev, "snd_pcm_new() returned %d\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200813 return ret;
814 }
815
Daniel Mack1c8470c2013-03-03 20:46:21 +0100816 cdev->pcm->private_data = cdev;
817 strlcpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200818
Daniel Mack1c8470c2013-03-03 20:46:21 +0100819 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback));
820 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture));
Daniel Mack9318dce2009-06-01 21:36:23 +0200821
Daniel Mack1c8470c2013-03-03 20:46:21 +0100822 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200823 sizeof(snd_usb_caiaq_pcm_hardware));
824
825 /* setup samplerates */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100826 cdev->samplerates = cdev->pcm_info.rates;
827 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200828 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
Daniel Mackad1e34b2007-09-17 14:45:14 +0200829 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200830 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO):
Daniel Mack21655922009-01-16 11:03:19 +0100831 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100832 cdev->samplerates |= SNDRV_PCM_RATE_192000;
Daniel Mack21655922009-01-16 11:03:19 +0100833 /* fall thru */
Daniel Mackb30c4942009-07-22 14:13:35 +0200834 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ):
Daniel Mack21655922009-01-16 11:03:19 +0100835 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
Daniel Mack523f1dc2007-03-26 19:11:24 +0200836 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800837 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2):
Daniel Mack1c8470c2013-03-03 20:46:21 +0100838 cdev->samplerates |= SNDRV_PCM_RATE_88200;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200839 break;
840 }
841
Daniel Mack1c8470c2013-03-03 20:46:21 +0100842 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200843 &snd_usb_caiaq_ops);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100844 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200845 &snd_usb_caiaq_ops);
846
Daniel Mack1c8470c2013-03-03 20:46:21 +0100847 cdev->data_cb_info =
Kees Cook6da2ec52018-06-12 13:55:00 -0700848 kmalloc_array(N_URBS, sizeof(struct snd_usb_caiaq_cb_info),
Daniel Mack523f1dc2007-03-26 19:11:24 +0200849 GFP_KERNEL);
850
Daniel Mack1c8470c2013-03-03 20:46:21 +0100851 if (!cdev->data_cb_info)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200852 return -ENOMEM;
853
Daniel Mack1c8470c2013-03-03 20:46:21 +0100854 cdev->outurb_active_mask = 0;
855 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8));
Daniel Mackda6094e2011-08-14 11:31:16 +0200856
Daniel Mack523f1dc2007-03-26 19:11:24 +0200857 for (i = 0; i < N_URBS; i++) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100858 cdev->data_cb_info[i].cdev = cdev;
859 cdev->data_cb_info[i].index = i;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200860 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200861
Daniel Mack1c8470c2013-03-03 20:46:21 +0100862 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200863 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100864 kfree(cdev->data_cb_info);
865 free_urbs(cdev->data_urbs_in);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200866 return ret;
867 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200868
Daniel Mack1c8470c2013-03-03 20:46:21 +0100869 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200870 if (ret < 0) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100871 kfree(cdev->data_cb_info);
872 free_urbs(cdev->data_urbs_in);
873 free_urbs(cdev->data_urbs_out);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200874 return ret;
875 }
876
877 return 0;
878}
879
Daniel Mack1c8470c2013-03-03 20:46:21 +0100880void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200881{
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100882 struct device *dev = caiaqdev_to_dev(cdev);
883
884 dev_dbg(dev, "%s(%p)\n", __func__, cdev);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100885 stream_stop(cdev);
886 free_urbs(cdev->data_urbs_in);
887 free_urbs(cdev->data_urbs_out);
888 kfree(cdev->data_cb_info);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200889}
890