blob: cfc1ea53978d9ad10f7e71e3ab2c5cc4d975e1fa [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * US-X2Y AUDIO
4 * Copyright (c) 2002-2004 by Karsten Wiese
5 *
6 * based on
7 *
8 * (Tentative) USB Audio Driver for ALSA
9 *
10 * Main and PCM part
11 *
12 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
13 *
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020014 * Many codes borrowed from audio.c by
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Alan Cox (alan@lxorguk.ukuu.org.uk)
16 * Thomas Sailer (sailer@ife.ee.ethz.ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/usb.h>
Paul Gortmaker31623ca2011-08-31 17:02:47 -040023#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <sound/core.h>
25#include <sound/info.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include "usx2y.h"
29#include "usbusx2y.h"
30
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020031/* Default value used for nr of packs per urb.
32 * 1 to 4 have been tested ok on uhci.
33 * To use 3 on ohci, you'd need a patch:
34 * look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
35 * "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
36 *
37 * 1, 2 and 4 work out of the box on ohci, if I recall correctly.
38 * Bigger is safer operation, smaller gives lower latencies.
39 */
40#define USX2Y_NRPACKS 4
41
42/* If your system works ok with this module's parameter
43 * nrpacks set to 1, you might as well comment
44 * this define out, and thereby produce smaller, faster code.
45 * You'd also set USX2Y_NRPACKS to 1 then.
46 */
47#define USX2Y_NRPACKS_VARIABLE 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#ifdef USX2Y_NRPACKS_VARIABLE
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020050static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
51#define nr_of_packs() nrpacks
52module_param(nrpacks, int, 0444);
53MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020055#define nr_of_packs() USX2Y_NRPACKS
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57
Takashi Iwaibae3ce42021-05-17 15:15:35 +020058static int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct urb *urb = subs->completed_urb;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010061 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned char *cp;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020063 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
Takashi Iwaia829dd52021-05-17 15:15:37 +020064 int cnt, blen;
Takashi Iwaibae3ce42021-05-17 15:15:35 +020065 struct usx2ydev *usx2y = subs->usx2y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 for (i = 0; i < nr_of_packs(); i++) {
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020068 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020070 snd_printk(KERN_ERR
71 "active frame status %i. Most probably some hardware problem.\n",
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010072 urb->iso_frame_desc[i].status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return urb->iso_frame_desc[i].status;
74 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +020075 len = urb->iso_frame_desc[i].actual_length / usx2y->stride;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +020076 if (!len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 snd_printd("0 == len ERROR!\n");
78 continue;
79 }
80
81 /* copy a data chunk */
82 if ((hwptr_done + len) > runtime->buffer_size) {
Takashi Iwaia829dd52021-05-17 15:15:37 +020083 cnt = runtime->buffer_size - hwptr_done;
84 blen = cnt * usx2y->stride;
Takashi Iwaibae3ce42021-05-17 15:15:35 +020085 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen);
86 memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
Takashi Iwaibae3ce42021-05-17 15:15:35 +020088 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp,
89 len * usx2y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91 lens += len;
Takashi Iwaia829dd52021-05-17 15:15:37 +020092 hwptr_done += len;
93 if (hwptr_done >= runtime->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 hwptr_done -= runtime->buffer_size;
95 }
96
97 subs->hwptr_done = hwptr_done;
98 subs->transfer_done += lens;
99 /* update the pointer, call callback if necessary */
100 if (subs->transfer_done >= runtime->period_size) {
101 subs->transfer_done -= runtime->period_size;
102 snd_pcm_period_elapsed(subs->pcm_substream);
103 }
104 return 0;
105}
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * prepare urb for playback data pipe
109 *
110 * we copy the data directly from the pcm buffer.
111 * the current position to be copied is held in hwptr field.
112 * since a urb can handle only a single linear buffer, if the total
113 * transferred area overflows the buffer boundary, we cannot send
114 * it directly from the buffer. thus the data is once copied to
115 * a temporary buffer and urb points to that.
116 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200117static int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct urb *cap_urb,
119 struct urb *urb)
120{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200121 struct usx2ydev *usx2y = subs->usx2y;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100122 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200123 int count, counts, pack, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 count = 0;
126 for (pack = 0; pack < nr_of_packs(); pack++) {
127 /* calculate the size of a packet */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200128 counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 count += counts;
130 if (counts < 43 || counts > 50) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200131 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EPIPE;
133 }
134 /* set up descriptor */
135 urb->iso_frame_desc[pack].offset = pack ?
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100136 urb->iso_frame_desc[pack - 1].offset +
137 urb->iso_frame_desc[pack - 1].length :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 0;
139 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
140 }
Takashi Iwaia829dd52021-05-17 15:15:37 +0200141 if (atomic_read(&subs->state) >= STATE_PRERUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (subs->hwptr + count > runtime->buffer_size) {
143 /* err, the transferred area goes over buffer boundary.
144 * copy the data to the temp buffer.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 len = runtime->buffer_size - subs->hwptr;
147 urb->transfer_buffer = subs->tmpbuf;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100148 memcpy(subs->tmpbuf, runtime->dma_area +
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200149 subs->hwptr * usx2y->stride, len * usx2y->stride);
150 memcpy(subs->tmpbuf + len * usx2y->stride,
151 runtime->dma_area, (count - len) * usx2y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 subs->hwptr += count;
153 subs->hwptr -= runtime->buffer_size;
154 } else {
155 /* set the buffer pointer */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200156 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200157 subs->hwptr += count;
158 if (subs->hwptr >= runtime->buffer_size)
Colin Ian King82bd59b2015-12-02 11:49:53 +0000159 subs->hwptr -= runtime->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Takashi Iwaia829dd52021-05-17 15:15:37 +0200161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 urb->transfer_buffer = subs->tmpbuf;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200163 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200164 urb->transfer_buffer_length = count * usx2y->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166}
167
168/*
169 * process after playback data complete
170 *
171 * update the current position and call callback if a period is processed.
172 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200173static void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100175 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200176 int len = urb->actual_length / subs->usx2y->stride;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 subs->transfer_done += len;
179 subs->hwptr_done += len;
180 if (subs->hwptr_done >= runtime->buffer_size)
181 subs->hwptr_done -= runtime->buffer_size;
182 if (subs->transfer_done >= runtime->period_size) {
183 subs->transfer_done -= runtime->period_size;
184 snd_pcm_period_elapsed(subs->pcm_substream);
185 }
186}
187
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200188static int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int err;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!urb)
193 return -ENODEV;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200194 urb->start_frame = frame + NRURBS * nr_of_packs(); // let hcd do rollover sanity checks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 urb->hcpriv = NULL;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200196 urb->dev = subs->usx2y->dev; /* we need to set this at each time */
Takashi Iwaia829dd52021-05-17 15:15:37 +0200197 err = usb_submit_urb(urb, GFP_ATOMIC);
198 if (err < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200199 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return err;
201 }
202 return 0;
203}
204
Takashi Iwaia829dd52021-05-17 15:15:37 +0200205static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs,
206 struct snd_usx2y_substream *playbacksubs,
207 int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int err, state;
Takashi Iwaicb432372005-11-17 10:48:52 +0100210 struct urb *urb = playbacksubs->completed_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Takashi Iwaicb432372005-11-17 10:48:52 +0100212 state = atomic_read(&playbacksubs->state);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200213 if (urb) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200214 if (state == STATE_RUNNING)
215 usx2y_urb_play_retire(playbacksubs, urb);
216 else if (state >= STATE_PRERUNNING)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100217 atomic_inc(&playbacksubs->state);
Takashi Iwaicb432372005-11-17 10:48:52 +0100218 } else {
219 switch (state) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200220 case STATE_STARTING1:
Takashi Iwaicb432372005-11-17 10:48:52 +0100221 urb = playbacksubs->urb[0];
222 atomic_inc(&playbacksubs->state);
223 break;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200224 case STATE_STARTING2:
Takashi Iwaicb432372005-11-17 10:48:52 +0100225 urb = playbacksubs->urb[1];
226 atomic_inc(&playbacksubs->state);
227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100230 if (urb) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200231 err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb);
232 if (err)
Takashi Iwaicb432372005-11-17 10:48:52 +0100233 return err;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200234 err = usx2y_urb_submit(playbacksubs, urb, frame);
235 if (err)
236 return err;
Takashi Iwaicb432372005-11-17 10:48:52 +0100237 }
238
239 playbacksubs->completed_urb = NULL;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 state = atomic_read(&capsubs->state);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200242 if (state >= STATE_PREPARED) {
243 if (state == STATE_RUNNING) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200244 err = usx2y_urb_capt_retire(capsubs);
245 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return err;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200247 } else if (state >= STATE_PRERUNNING) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100248 atomic_inc(&capsubs->state);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200249 }
250 err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame);
251 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return err;
253 }
254 capsubs->completed_urb = NULL;
255 return 0;
256}
257
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200258static void usx2y_clients_stop(struct usx2ydev *usx2y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwaia829dd52021-05-17 15:15:37 +0200260 struct snd_usx2y_substream *subs;
261 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int s, u;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 for (s = 0; s < 4; s++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200265 subs = usx2y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (subs) {
267 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200268 atomic_set(&subs->state, STATE_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
271 for (s = 0; s < 4; s++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200272 subs = usx2y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (subs) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200274 if (atomic_read(&subs->state) >= STATE_PRERUNNING)
Takashi Iwai1fb85102014-11-07 17:08:28 +0100275 snd_pcm_stop_xrun(subs->pcm_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 for (u = 0; u < NRURBS; u++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200277 urb = subs->urb[u];
278 if (urb)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100279 snd_printdd("%i status=%i start_frame=%i\n",
280 u, urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
283 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200284 usx2y->prepare_subs = NULL;
285 wake_up(&usx2y->prepare_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200288static void usx2y_error_urb_status(struct usx2ydev *usx2y,
289 struct snd_usx2y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200291 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 urb->status = 0;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200293 usx2y_clients_stop(usx2y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200296static void i_usx2y_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200298 struct snd_usx2y_substream *subs = urb->context;
299 struct usx2ydev *usx2y = subs->usx2y;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200300 struct snd_usx2y_substream *capsubs, *playbacksubs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200302 if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100303 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200304 usb_get_current_frame_number(usx2y->dev),
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100305 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
306 urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return;
308 }
309 if (unlikely(urb->status)) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200310 usx2y_error_urb_status(usx2y, subs, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return;
312 }
Daniel Macka9d14bc2013-10-02 17:49:50 +0200313
314 subs->completed_urb = urb;
315
Takashi Iwaia829dd52021-05-17 15:15:37 +0200316 capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
317 playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200318
Takashi Iwaia829dd52021-05-17 15:15:37 +0200319 if (capsubs->completed_urb &&
320 atomic_read(&capsubs->state) >= STATE_PREPARED &&
321 (playbacksubs->completed_urb ||
322 atomic_read(&playbacksubs->state) < STATE_PREPARED)) {
323 if (!usx2y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
324 usx2y->wait_iso_frame += nr_of_packs();
325 } else {
326 snd_printdd("\n");
327 usx2y_clients_stop(usx2y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 }
330}
331
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200332static void usx2y_urbs_set_complete(struct usx2ydev *usx2y,
David Howells7d12e782006-10-05 14:55:46 +0100333 void (*complete)(struct urb *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Takashi Iwaia829dd52021-05-17 15:15:37 +0200335 struct snd_usx2y_substream *subs;
336 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int s, u;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 for (s = 0; s < 4; s++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200340 subs = usx2y->subs[s];
341 if (subs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 for (u = 0; u < NRURBS; u++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200343 urb = subs->urb[u];
344 if (urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 urb->complete = complete;
346 }
Takashi Iwaia829dd52021-05-17 15:15:37 +0200347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200351static void usx2y_subs_startup_finish(struct usx2ydev *usx2y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200353 usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete);
354 usx2y->prepare_subs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200357static void i_usx2y_subs_startup(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200359 struct snd_usx2y_substream *subs = urb->context;
360 struct usx2ydev *usx2y = subs->usx2y;
361 struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200362
Takashi Iwaia829dd52021-05-17 15:15:37 +0200363 if (prepare_subs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200365 usx2y_subs_startup_finish(usx2y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 atomic_inc(&prepare_subs->state);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200367 wake_up(&usx2y->prepare_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Takashi Iwaia829dd52021-05-17 15:15:37 +0200369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200371 i_usx2y_urb_complete(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200374static void usx2y_subs_prepare(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200376 snd_printdd("usx2y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100377 subs, subs->endpoint, subs->urb[0], subs->urb[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* reset the pointer */
379 subs->hwptr = 0;
380 subs->hwptr_done = 0;
381 subs->transfer_done = 0;
382}
383
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200384static void usx2y_urb_release(struct urb **urb, int free_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 if (*urb) {
387 usb_kill_urb(*urb);
388 if (free_tb)
389 kfree((*urb)->transfer_buffer);
390 usb_free_urb(*urb);
391 *urb = NULL;
392 }
393}
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*
396 * release a substreams urbs
397 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200398static void usx2y_urbs_release(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int i;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200401
Takashi Iwaia829dd52021-05-17 15:15:37 +0200402 snd_printdd("%s %i\n", __func__, subs->endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 for (i = 0; i < NRURBS; i++)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200404 usx2y_urb_release(subs->urb + i,
405 subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jesper Juhl4d572772005-05-30 17:30:32 +0200407 kfree(subs->tmpbuf);
408 subs->tmpbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * initialize a substream's urbs
413 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200414static int usx2y_urbs_allocate(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int i;
417 unsigned int pipe;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200418 int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
419 struct usb_device *dev = subs->usx2y->dev;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200420 struct urb **purb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
423 usb_rcvisocpipe(dev, subs->endpoint);
424 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
425 if (!subs->maxpacksize)
426 return -EINVAL;
427
Takashi Iwaia829dd52021-05-17 15:15:37 +0200428 if (is_playback && !subs->tmpbuf) { /* allocate a temporary buffer for playback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
Markus Elfring6fd9ff72017-08-11 17:40:57 +0200430 if (!subs->tmpbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 /* allocate and initialize data urbs */
434 for (i = 0; i < NRURBS; i++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200435 purb = subs->urb + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (*purb) {
437 usb_kill_urb(*purb);
438 continue;
439 }
440 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200441 if (!*purb) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200442 usx2y_urbs_release(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -ENOMEM;
444 }
445 if (!is_playback && !(*purb)->transfer_buffer) {
446 /* allocate a capture buffer per urb */
Kees Cook6da2ec52018-06-12 13:55:00 -0700447 (*purb)->transfer_buffer =
448 kmalloc_array(subs->maxpacksize,
449 nr_of_packs(), GFP_KERNEL);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200450 if (!(*purb)->transfer_buffer) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200451 usx2y_urbs_release(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -ENOMEM;
453 }
454 }
455 (*purb)->dev = dev;
456 (*purb)->pipe = pipe;
457 (*purb)->number_of_packets = nr_of_packs();
458 (*purb)->context = subs;
459 (*purb)->interval = 1;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200460 (*purb)->complete = i_usx2y_subs_startup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 return 0;
463}
464
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200465static void usx2y_subs_startup(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200467 struct usx2ydev *usx2y = subs->usx2y;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200468
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200469 usx2y->prepare_subs = subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 subs->urb[0]->start_frame = -1;
471 wmb();
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200472 usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200475static int usx2y_urbs_start(struct snd_usx2y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 int i, err;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200478 struct usx2ydev *usx2y = subs->usx2y;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200479 struct urb *urb;
480 unsigned long pack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Takashi Iwaia829dd52021-05-17 15:15:37 +0200482 err = usx2y_urbs_allocate(subs);
483 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return err;
485 subs->completed_urb = NULL;
486 for (i = 0; i < 4; i++) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200487 struct snd_usx2y_substream *subs = usx2y->subs[i];
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200488
Takashi Iwaia829dd52021-05-17 15:15:37 +0200489 if (subs && atomic_read(&subs->state) >= STATE_PREPARED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto start;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Takashi Iwaicb432372005-11-17 10:48:52 +0100493 start:
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200494 usx2y_subs_startup(subs);
Takashi Iwaicb432372005-11-17 10:48:52 +0100495 for (i = 0; i < NRURBS; i++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200496 urb = subs->urb[i];
Takashi Iwaicb432372005-11-17 10:48:52 +0100497 if (usb_pipein(urb->pipe)) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200498 if (!i)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200499 atomic_set(&subs->state, STATE_STARTING3);
500 urb->dev = usx2y->dev;
Takashi Iwaicb432372005-11-17 10:48:52 +0100501 for (pack = 0; pack < nr_of_packs(); pack++) {
502 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
503 urb->iso_frame_desc[pack].length = subs->maxpacksize;
504 }
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200505 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
Takashi Iwaia829dd52021-05-17 15:15:37 +0200506 err = usb_submit_urb(urb, GFP_ATOMIC);
507 if (err < 0) {
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200508 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
Takashi Iwaicb432372005-11-17 10:48:52 +0100509 err = -EPIPE;
510 goto cleanup;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200511 } else {
512 if (!i)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200513 usx2y->wait_iso_frame = urb->start_frame;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200514 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100515 urb->transfer_flags = 0;
516 } else {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200517 atomic_set(&subs->state, STATE_STARTING1);
Takashi Iwaicb432372005-11-17 10:48:52 +0100518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100521 err = 0;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200522 wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200523 if (atomic_read(&subs->state) != STATE_PREPARED)
Takashi Iwaicb432372005-11-17 10:48:52 +0100524 err = -EPIPE;
525
526 cleanup:
527 if (err) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200528 usx2y_subs_startup_finish(usx2y);
gushengxianea57e872021-07-05 02:34:19 -0700529 usx2y_clients_stop(usx2y); // something is completely wrong > stop everything
Takashi Iwaicb432372005-11-17 10:48:52 +0100530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return err;
532}
533
534/*
535 * return the current pcm pointer. just return the hwptr_done value.
536 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200537static snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200539 struct snd_usx2y_substream *subs = substream->runtime->private_data;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return subs->hwptr_done;
542}
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
545 * start/stop substream
546 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200547static int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200549 struct snd_usx2y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 switch (cmd) {
552 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia829dd52021-05-17 15:15:37 +0200553 snd_printdd("%s(START)\n", __func__);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200554 if (atomic_read(&subs->state) == STATE_PREPARED &&
555 atomic_read(&subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= STATE_PREPARED) {
556 atomic_set(&subs->state, STATE_PRERUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
558 snd_printdd("\n");
559 return -EPIPE;
560 }
561 break;
562 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia829dd52021-05-17 15:15:37 +0200563 snd_printdd("%s(STOP)\n", __func__);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200564 if (atomic_read(&subs->state) >= STATE_PRERUNNING)
565 atomic_set(&subs->state, STATE_PREPARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 default:
568 return -EINVAL;
569 }
570 return 0;
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * allocate a buffer, setup samplerate
575 *
576 * so far we use a physically linear buffer although packetize transfer
577 * doesn't need a continuous area.
578 * if sg buffer is supported on the later version of alsa, we'll follow
579 * that.
580 */
Takashi Iwaia829dd52021-05-17 15:15:37 +0200581struct s_c2 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 char c1, c2;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200583};
584
585static const struct s_c2 setrate_44100[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 { 0x14, 0x08}, // this line sets 44100, well actually a little less
587 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
588 { 0x18, 0x42},
589 { 0x18, 0x45},
590 { 0x18, 0x46},
591 { 0x18, 0x48},
592 { 0x18, 0x4A},
593 { 0x18, 0x4C},
594 { 0x18, 0x4E},
595 { 0x18, 0x50},
596 { 0x18, 0x52},
597 { 0x18, 0x54},
598 { 0x18, 0x56},
599 { 0x18, 0x58},
600 { 0x18, 0x5A},
601 { 0x18, 0x5C},
602 { 0x18, 0x5E},
603 { 0x18, 0x60},
604 { 0x18, 0x62},
605 { 0x18, 0x64},
606 { 0x18, 0x66},
607 { 0x18, 0x68},
608 { 0x18, 0x6A},
609 { 0x18, 0x6C},
610 { 0x18, 0x6E},
611 { 0x18, 0x70},
612 { 0x18, 0x72},
613 { 0x18, 0x74},
614 { 0x18, 0x76},
615 { 0x18, 0x78},
616 { 0x18, 0x7A},
617 { 0x18, 0x7C},
618 { 0x18, 0x7E}
619};
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200620
621static const struct s_c2 setrate_48000[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 { 0x14, 0x09}, // this line sets 48000, well actually a little less
623 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
624 { 0x18, 0x42},
625 { 0x18, 0x45},
626 { 0x18, 0x46},
627 { 0x18, 0x48},
628 { 0x18, 0x4A},
629 { 0x18, 0x4C},
630 { 0x18, 0x4E},
631 { 0x18, 0x50},
632 { 0x18, 0x52},
633 { 0x18, 0x54},
634 { 0x18, 0x56},
635 { 0x18, 0x58},
636 { 0x18, 0x5A},
637 { 0x18, 0x5C},
638 { 0x18, 0x5E},
639 { 0x18, 0x60},
640 { 0x18, 0x62},
641 { 0x18, 0x64},
642 { 0x18, 0x66},
643 { 0x18, 0x68},
644 { 0x18, 0x6A},
645 { 0x18, 0x6C},
646 { 0x18, 0x6E},
647 { 0x18, 0x70},
648 { 0x18, 0x73},
649 { 0x18, 0x74},
650 { 0x18, 0x76},
651 { 0x18, 0x78},
652 { 0x18, 0x7A},
653 { 0x18, 0x7C},
654 { 0x18, 0x7E}
655};
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200656
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200657#define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200659static void i_usx2y_04int(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200661 struct usx2ydev *usx2y = urb->context;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200662
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200663 if (urb->status)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200664 snd_printk(KERN_ERR "snd_usx2y_04int() urb->status=%i\n", urb->status);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200665 if (!--usx2y->us04->len)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200666 wake_up(&usx2y->in04_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200669static int usx2y_rate_set(struct usx2ydev *usx2y, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Len Bakerf02f2f12021-09-19 15:37:27 +0200671 int err = 0, i;
672 struct snd_usx2y_urb_seq *us = NULL;
673 int *usbdata = NULL;
674 const struct s_c2 *ra = rate == 48000 ? setrate_48000 : setrate_44100;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200675 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200677 if (usx2y->rate != rate) {
Len Bakerf02f2f12021-09-19 15:37:27 +0200678 us = kzalloc(struct_size(us, urb, NOOF_SETRATE_URBS),
679 GFP_KERNEL);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200680 if (!us) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 err = -ENOMEM;
682 goto cleanup;
683 }
Kees Cook6da2ec52018-06-12 13:55:00 -0700684 usbdata = kmalloc_array(NOOF_SETRATE_URBS, sizeof(int),
685 GFP_KERNEL);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200686 if (!usbdata) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 err = -ENOMEM;
688 goto cleanup;
689 }
690 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200691 us->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
692 if (!us->urb[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 err = -ENOMEM;
694 goto cleanup;
695 }
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200696 ((char *)(usbdata + i))[0] = ra[i].c1;
697 ((char *)(usbdata + i))[1] = ra[i].c2;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200698 usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4),
699 usbdata + i, 2, i_usx2y_04int, usx2y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Takashi Iwai1f100342017-10-04 17:58:41 +0200701 err = usb_urb_ep_type_check(us->urb[0]);
702 if (err < 0)
703 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 us->submitted = 0;
705 us->len = NOOF_SETRATE_URBS;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200706 usx2y->us04 = us;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200707 wait_event_timeout(usx2y->in04_wait_queue, !us->len, HZ);
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200708 usx2y->us04 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (us->len)
710 err = -ENODEV;
711 cleanup:
712 if (us) {
713 us->submitted = 2*NOOF_SETRATE_URBS;
714 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200715 urb = us->urb[i];
Takashi Iwai7686e342020-04-20 09:55:29 +0200716 if (!urb)
717 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (urb->status) {
719 if (!err)
720 err = -ENODEV;
721 usb_kill_urb(urb);
722 }
723 usb_free_urb(urb);
724 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200725 usx2y->us04 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 kfree(usbdata);
727 kfree(us);
Takashi Iwaicb432372005-11-17 10:48:52 +0100728 if (!err)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200729 usx2y->rate = rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
733 return err;
734}
735
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200736static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 int alternate, err;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200739 struct list_head *p;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
742 alternate = 2;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200743 usx2y->stride = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else {
745 alternate = 1;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200746 usx2y->stride = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200748 list_for_each(p, &usx2y->midi_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 snd_usbmidi_input_stop(p);
750 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200751 usb_kill_urb(usx2y->in04_urb);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200752 err = usb_set_interface(usx2y->dev, 0, alternate);
753 if (err) {
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200754 snd_printk(KERN_ERR "usb_set_interface error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return err;
756 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200757 usx2y->in04_urb->dev = usx2y->dev;
758 err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
759 list_for_each(p, &usx2y->midi_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 snd_usbmidi_input_start(p);
761 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200762 usx2y->format = format;
763 usx2y->rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return err;
765}
766
767
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200768static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100769 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 int err = 0;
772 unsigned int rate = params_rate(hw_params);
773 snd_pcm_format_t format = params_format(hw_params);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100774 struct snd_card *card = substream->pstr->pcm->card;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200775 struct usx2ydev *dev = usx2y(card);
Takashi Iwaia829dd52021-05-17 15:15:37 +0200776 struct snd_usx2y_substream *subs;
777 struct snd_pcm_substream *test_substream;
Takashi Iwaie2439a52014-02-14 09:05:47 +0100778 int i;
Takashi Iwaicb432372005-11-17 10:48:52 +0100779
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200780 mutex_lock(&usx2y(card)->pcm_mutex);
781 snd_printdd("snd_usx2y_hw_params(%p, %p)\n", substream, hw_params);
782 /* all pcm substreams off one usx2y have to operate at the same
Takashi Iwaie2439a52014-02-14 09:05:47 +0100783 * rate & format
784 */
785 for (i = 0; i < dev->pcm_devs * 2; i++) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200786 subs = dev->subs[i];
Takashi Iwaie2439a52014-02-14 09:05:47 +0100787 if (!subs)
Takashi Iwaicb432372005-11-17 10:48:52 +0100788 continue;
Takashi Iwaie2439a52014-02-14 09:05:47 +0100789 test_substream = subs->pcm_substream;
790 if (!test_substream || test_substream == substream ||
791 !test_substream->runtime)
792 continue;
793 if ((test_substream->runtime->format &&
794 test_substream->runtime->format != format) ||
795 (test_substream->runtime->rate &&
796 test_substream->runtime->rate != rate)) {
797 err = -EINVAL;
798 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 }
Takashi Iwaie2439a52014-02-14 09:05:47 +0100801
Takashi Iwaie2439a52014-02-14 09:05:47 +0100802 error:
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200803 mutex_unlock(&usx2y(card)->pcm_mutex);
Takashi Iwaie2439a52014-02-14 09:05:47 +0100804 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/*
808 * free the buffer
809 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200810static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100812 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200813 struct snd_usx2y_substream *subs = runtime->private_data;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200814 struct snd_usx2y_substream *cap_subs, *playback_subs;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200815
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200816 mutex_lock(&subs->usx2y->pcm_mutex);
817 snd_printdd("snd_usx2y_hw_free(%p)\n", substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Takashi Iwaia829dd52021-05-17 15:15:37 +0200819 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
820 cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200821 atomic_set(&subs->state, STATE_STOPPED);
822 usx2y_urbs_release(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!cap_subs->pcm_substream ||
824 !cap_subs->pcm_substream->runtime ||
825 !cap_subs->pcm_substream->runtime->status ||
826 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200827 atomic_set(&cap_subs->state, STATE_STOPPED);
828 usx2y_urbs_release(cap_subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 } else {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200831 playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200832 if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
833 atomic_set(&subs->state, STATE_STOPPED);
834 usx2y_urbs_release(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200837 mutex_unlock(&subs->usx2y->pcm_mutex);
Takashi Iwai3f0c9722019-12-09 10:49:41 +0100838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*
842 * prepare callback
843 *
844 * set format and initialize urbs
845 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200846static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100848 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200849 struct snd_usx2y_substream *subs = runtime->private_data;
850 struct usx2ydev *usx2y = subs->usx2y;
851 struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int err = 0;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200853
Takashi Iwaia829dd52021-05-17 15:15:37 +0200854 snd_printdd("%s(%p)\n", __func__, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200856 mutex_lock(&usx2y->pcm_mutex);
857 usx2y_subs_prepare(subs);
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200858 // Start hardware streams
859 // SyncStream first....
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200860 if (atomic_read(&capsubs->state) < STATE_PREPARED) {
Takashi Iwaia829dd52021-05-17 15:15:37 +0200861 if (usx2y->format != runtime->format) {
862 err = usx2y_format_set(usx2y, runtime->format);
863 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto up_prepare_mutex;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200865 }
866 if (usx2y->rate != runtime->rate) {
867 err = usx2y_rate_set(usx2y, runtime->rate);
868 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto up_prepare_mutex;
Takashi Iwaia829dd52021-05-17 15:15:37 +0200870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
Takashi Iwaia829dd52021-05-17 15:15:37 +0200872 err = usx2y_urbs_start(capsubs);
873 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto up_prepare_mutex;
875 }
876
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200877 if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED)
878 err = usx2y_urbs_start(subs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 up_prepare_mutex:
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200881 mutex_unlock(&usx2y->pcm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return err;
883}
884
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200885static const struct snd_pcm_hardware snd_usx2y_2c = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
887 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai2008f132009-04-28 12:25:59 +0200888 SNDRV_PCM_INFO_MMAP_VALID |
889 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
891 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
892 .rate_min = 44100,
893 .rate_max = 48000,
894 .channels_min = 2,
895 .channels_max = 2,
896 .buffer_bytes_max = (2*128*1024),
897 .period_bytes_min = 64,
898 .period_bytes_max = (128*1024),
899 .periods_min = 2,
900 .periods_max = 1024,
901 .fifo_size = 0
902};
903
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200904static int snd_usx2y_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Takashi Iwaia829dd52021-05-17 15:15:37 +0200906 struct snd_usx2y_substream *subs =
907 ((struct snd_usx2y_substream **)
908 snd_pcm_substream_chip(substream))[substream->stream];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100909 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200911 if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -EBUSY;
913
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200914 runtime->hw = snd_usx2y_2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 runtime->private_data = subs;
916 subs->pcm_substream = substream;
917 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
918 return 0;
919}
920
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200921static int snd_usx2y_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100923 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200924 struct snd_usx2y_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 subs->pcm_substream = NULL;
927
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200931static const struct snd_pcm_ops snd_usx2y_pcm_ops = {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200932 .open = snd_usx2y_pcm_open,
933 .close = snd_usx2y_pcm_close,
934 .hw_params = snd_usx2y_pcm_hw_params,
935 .hw_free = snd_usx2y_pcm_hw_free,
936 .prepare = snd_usx2y_pcm_prepare,
937 .trigger = snd_usx2y_pcm_trigger,
938 .pointer = snd_usx2y_pcm_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939};
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941/*
942 * free a usb stream instance
943 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200944static void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Kuninori Morimotoffd11d12020-02-17 17:28:51 +0900946 int stream;
Richard Knutssonc111b8d2007-05-14 10:38:57 +0200947
Kuninori Morimotoffd11d12020-02-17 17:28:51 +0900948 for_each_pcm_streams(stream) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200949 kfree(usx2y_substream[stream]);
950 usx2y_substream[stream] = NULL;
Kuninori Morimotoffd11d12020-02-17 17:28:51 +0900951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200954static void snd_usx2y_pcm_private_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200956 struct snd_usx2y_substream **usx2y_stream = pcm->private_data;
Takashi Iwai4c0a58e2021-05-17 15:15:36 +0200957
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200958 if (usx2y_stream)
959 usx2y_audio_stream_free(usx2y_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200962static int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100964 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int err, i;
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200966 struct snd_usx2y_substream **usx2y_substream =
967 usx2y(card)->subs + 2 * usx2y(card)->pcm_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
970 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200971 usx2y_substream[i] = kzalloc(sizeof(struct snd_usx2y_substream), GFP_KERNEL);
972 if (!usx2y_substream[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return -ENOMEM;
Markus Elfring6fd9ff72017-08-11 17:40:57 +0200974
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200975 usx2y_substream[i]->usx2y = usx2y(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 if (playback_endpoint)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200979 usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
980 usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200982 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 playback_endpoint ? 1 : 0, 1,
984 &pcm);
985 if (err < 0) {
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200986 usx2y_audio_stream_free(usx2y_substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return err;
988 }
989
990 if (playback_endpoint)
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200991 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops);
992 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200994 pcm->private_data = usx2y_substream;
995 pcm->private_free = snd_usx2y_pcm_private_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 pcm->info_flags = 0;
997
Takashi Iwaibae3ce42021-05-17 15:15:35 +0200998 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Takashi Iwai4d1b5302019-02-04 16:36:10 +01001000 if (playback_endpoint) {
Takashi Iwai3f0c9722019-12-09 10:49:41 +01001001 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1002 SNDRV_DMA_TYPE_CONTINUOUS,
1003 NULL,
1004 64*1024, 128*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Takashi Iwai4d1b5302019-02-04 16:36:10 +01001006
Takashi Iwai3f0c9722019-12-09 10:49:41 +01001007 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1008 SNDRV_DMA_TYPE_CONTINUOUS,
1009 NULL,
1010 64*1024, 128*1024);
Takashi Iwaibae3ce42021-05-17 15:15:35 +02001011 usx2y(card)->pcm_devs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return 0;
1014}
1015
1016/*
1017 * create a chip instance and set its names.
1018 */
Takashi Iwaibae3ce42021-05-17 15:15:35 +02001019int usx2y_audio_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Takashi Iwaie8bfa152021-05-17 15:15:45 +02001021 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Takashi Iwaia829dd52021-05-17 15:15:37 +02001023 err = usx2y_audio_stream_new(card, 0xA, 0x8);
1024 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return err;
Takashi Iwaia829dd52021-05-17 15:15:37 +02001026 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428) {
1027 err = usx2y_audio_stream_new(card, 0, 0xA);
1028 if (err < 0)
Takashi Iwai4c0a58e2021-05-17 15:15:36 +02001029 return err;
Takashi Iwaia829dd52021-05-17 15:15:37 +02001030 }
Takashi Iwaibae3ce42021-05-17 15:15:35 +02001031 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122)
1032 err = usx2y_rate_set(usx2y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return err;
1034}