File Index
Files and directories
Sign in
FILE

usbusx2yaudio.c

File preview with syntax highlighting.

linux-6.19.14/sound/usb/usx2y/usbusx2yaudio.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   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 *
  14 *   Many codes borrowed from audio.c by
  15 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
  16 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
  17 */
  18
  19
  20#include <linux/interrupt.h>
  21#include <linux/slab.h>
  22#include <linux/usb.h>
  23#include <linux/moduleparam.h>
  24#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
  31static int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs)
  32{
  33	struct urb	*urb = subs->completed_urb;
  34	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  35	unsigned char	*cp;
  36	int		i, len, lens = 0, hwptr_done = subs->hwptr_done;
  37	int		cnt, blen;
  38	struct usx2ydev	*usx2y = subs->usx2y;
  39
  40	for (i = 0; i < nr_of_packs(); i++) {
  41		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  42		if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
  43			dev_err(&usx2y->dev->dev,
  44				"%s: active frame status %i. Most probably some hardware problem.\n",
  45				__func__,
  46				urb->iso_frame_desc[i].status);
  47			return urb->iso_frame_desc[i].status;
  48		}
  49		len = urb->iso_frame_desc[i].actual_length / usx2y->stride;
  50		if (!len) {
  51			dev_dbg(&usx2y->dev->dev, "%s: 0 == len ERROR!\n", __func__);
  52			continue;
  53		}
  54
  55		/* copy a data chunk */
  56		if ((hwptr_done + len) > runtime->buffer_size) {
  57			cnt = runtime->buffer_size - hwptr_done;
  58			blen = cnt * usx2y->stride;
  59			memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen);
  60			memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen);
  61		} else {
  62			memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp,
  63			       len * usx2y->stride);
  64		}
  65		lens += len;
  66		hwptr_done += len;
  67		if (hwptr_done >= runtime->buffer_size)
  68			hwptr_done -= runtime->buffer_size;
  69	}
  70
  71	subs->hwptr_done = hwptr_done;
  72	subs->transfer_done += lens;
  73	/* update the pointer, call callback if necessary */
  74	if (subs->transfer_done >= runtime->period_size) {
  75		subs->transfer_done -= runtime->period_size;
  76		snd_pcm_period_elapsed(subs->pcm_substream);
  77	}
  78	return 0;
  79}
  80
  81/*
  82 * prepare urb for playback data pipe
  83 *
  84 * we copy the data directly from the pcm buffer.
  85 * the current position to be copied is held in hwptr field.
  86 * since a urb can handle only a single linear buffer, if the total
  87 * transferred area overflows the buffer boundary, we cannot send
  88 * it directly from the buffer.  thus the data is once copied to
  89 * a temporary buffer and urb points to that.
  90 */
  91static int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs,
  92				  struct urb *cap_urb,
  93				  struct urb *urb)
  94{
  95	struct usx2ydev *usx2y = subs->usx2y;
  96	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  97	int count, counts, pack, len;
  98
  99	count = 0;
 100	for (pack = 0; pack <  nr_of_packs(); pack++) {
 101		/* calculate the size of a packet */
 102		counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride;
 103		count += counts;
 104		if (counts < 43 || counts > 50) {
 105			dev_err(&usx2y->dev->dev, "%s: should not be here with counts=%i\n",
 106				__func__, counts);
 107			return -EPIPE;
 108		}
 109		/* set up descriptor */
 110		urb->iso_frame_desc[pack].offset = pack ?
 111			urb->iso_frame_desc[pack - 1].offset +
 112			urb->iso_frame_desc[pack - 1].length :
 113			0;
 114		urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
 115	}
 116	if (atomic_read(&subs->state) >= STATE_PRERUNNING) {
 117		if (subs->hwptr + count > runtime->buffer_size) {
 118			/* err, the transferred area goes over buffer boundary.
 119			 * copy the data to the temp buffer.
 120			 */
 121			len = runtime->buffer_size - subs->hwptr;
 122			urb->transfer_buffer = subs->tmpbuf;
 123			memcpy(subs->tmpbuf, runtime->dma_area +
 124			       subs->hwptr * usx2y->stride, len * usx2y->stride);
 125			memcpy(subs->tmpbuf + len * usx2y->stride,
 126			       runtime->dma_area, (count - len) * usx2y->stride);
 127			subs->hwptr += count;
 128			subs->hwptr -= runtime->buffer_size;
 129		} else {
 130			/* set the buffer pointer */
 131			urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride;
 132			subs->hwptr += count;
 133			if (subs->hwptr >= runtime->buffer_size)
 134				subs->hwptr -= runtime->buffer_size;
 135		}
 136	} else {
 137		urb->transfer_buffer = subs->tmpbuf;
 138	}
 139	urb->transfer_buffer_length = count * usx2y->stride;
 140	return 0;
 141}
 142
 143/*
 144 * process after playback data complete
 145 *
 146 * update the current position and call callback if a period is processed.
 147 */
 148static void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb)
 149{
 150	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
 151	int		len = urb->actual_length / subs->usx2y->stride;
 152
 153	subs->transfer_done += len;
 154	subs->hwptr_done +=  len;
 155	if (subs->hwptr_done >= runtime->buffer_size)
 156		subs->hwptr_done -= runtime->buffer_size;
 157	if (subs->transfer_done >= runtime->period_size) {
 158		subs->transfer_done -= runtime->period_size;
 159		snd_pcm_period_elapsed(subs->pcm_substream);
 160	}
 161}
 162
 163static int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame)
 164{
 165	int err;
 166
 167	if (!urb)
 168		return -ENODEV;
 169	urb->start_frame = frame + NRURBS * nr_of_packs();  // let hcd do rollover sanity checks
 170	urb->hcpriv = NULL;
 171	urb->dev = subs->usx2y->dev; /* we need to set this at each time */
 172	err = usb_submit_urb(urb, GFP_ATOMIC);
 173	if (err < 0) {
 174		dev_err(&urb->dev->dev, "%s: usb_submit_urb() returned %i\n",
 175			__func__, err);
 176		return err;
 177	}
 178	return 0;
 179}
 180
 181static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs,
 182				   struct snd_usx2y_substream *playbacksubs,
 183				   int frame)
 184{
 185	int err, state;
 186	struct urb *urb = playbacksubs->completed_urb;
 187
 188	state = atomic_read(&playbacksubs->state);
 189	if (urb) {
 190		if (state == STATE_RUNNING)
 191			usx2y_urb_play_retire(playbacksubs, urb);
 192		else if (state >= STATE_PRERUNNING)
 193			atomic_inc(&playbacksubs->state);
 194	} else {
 195		switch (state) {
 196		case STATE_STARTING1:
 197			urb = playbacksubs->urb[0];
 198			atomic_inc(&playbacksubs->state);
 199			break;
 200		case STATE_STARTING2:
 201			urb = playbacksubs->urb[1];
 202			atomic_inc(&playbacksubs->state);
 203			break;
 204		}
 205	}
 206	if (urb) {
 207		err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb);
 208		if (err)
 209			return err;
 210		err = usx2y_urb_submit(playbacksubs, urb, frame);
 211		if (err)
 212			return err;
 213	}
 214
 215	playbacksubs->completed_urb = NULL;
 216
 217	state = atomic_read(&capsubs->state);
 218	if (state >= STATE_PREPARED) {
 219		if (state == STATE_RUNNING) {
 220			err = usx2y_urb_capt_retire(capsubs);
 221			if (err)
 222				return err;
 223		} else if (state >= STATE_PRERUNNING) {
 224			atomic_inc(&capsubs->state);
 225		}
 226		err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame);
 227		if (err)
 228			return err;
 229	}
 230	capsubs->completed_urb = NULL;
 231	return 0;
 232}
 233
 234static void usx2y_clients_stop(struct usx2ydev *usx2y)
 235{
 236	struct snd_usx2y_substream *subs;
 237	struct urb *urb;
 238	int s, u;
 239
 240	for (s = 0; s < 4; s++) {
 241		subs = usx2y->subs[s];
 242		if (subs) {
 243			dev_dbg(&usx2y->dev->dev, "%s: %i %p state=%i\n",
 244				__func__, s, subs, atomic_read(&subs->state));
 245			atomic_set(&subs->state, STATE_STOPPED);
 246		}
 247	}
 248	for (s = 0; s < 4; s++) {
 249		subs = usx2y->subs[s];
 250		if (subs) {
 251			if (atomic_read(&subs->state) >= STATE_PRERUNNING)
 252				snd_pcm_stop_xrun(subs->pcm_substream);
 253			for (u = 0; u < NRURBS; u++) {
 254				urb = subs->urb[u];
 255				if (urb)
 256					dev_dbg(&usx2y->dev->dev,
 257						"%s: %i status=%i start_frame=%i\n",
 258						__func__, u, urb->status, urb->start_frame);
 259			}
 260		}
 261	}
 262	usx2y->prepare_subs = NULL;
 263	wake_up(&usx2y->prepare_wait_queue);
 264}
 265
 266static void usx2y_error_urb_status(struct usx2ydev *usx2y,
 267				   struct snd_usx2y_substream *subs, struct urb *urb)
 268{
 269	dev_err(&usx2y->dev->dev, "%s: ep=%i stalled with status=%i\n",
 270		__func__, subs->endpoint, urb->status);
 271	urb->status = 0;
 272	usx2y_clients_stop(usx2y);
 273}
 274
 275static void i_usx2y_urb_complete(struct urb *urb)
 276{
 277	struct snd_usx2y_substream *subs = urb->context;
 278	struct usx2ydev *usx2y = subs->usx2y;
 279	struct snd_usx2y_substream *capsubs, *playbacksubs;
 280
 281	if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) {
 282		dev_dbg(&usx2y->dev->dev,
 283			"%s: hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
 284			__func__,
 285			usb_get_current_frame_number(usx2y->dev),
 286			subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
 287			urb->status, urb->start_frame);
 288		return;
 289	}
 290	if (unlikely(urb->status)) {
 291		usx2y_error_urb_status(usx2y, subs, urb);
 292		return;
 293	}
 294
 295	subs->completed_urb = urb;
 296
 297	capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
 298	playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
 299
 300	if (capsubs->completed_urb &&
 301	    atomic_read(&capsubs->state) >= STATE_PREPARED &&
 302	    (playbacksubs->completed_urb ||
 303	     atomic_read(&playbacksubs->state) < STATE_PREPARED)) {
 304		if (!usx2y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
 305			usx2y->wait_iso_frame += nr_of_packs();
 306		} else {
 307			usx2y_clients_stop(usx2y);
 308		}
 309	}
 310}
 311
 312static void usx2y_urbs_set_complete(struct usx2ydev *usx2y,
 313				    void (*complete)(struct urb *))
 314{
 315	struct snd_usx2y_substream *subs;
 316	struct urb *urb;
 317	int s, u;
 318
 319	for (s = 0; s < 4; s++) {
 320		subs = usx2y->subs[s];
 321		if (subs) {
 322			for (u = 0; u < NRURBS; u++) {
 323				urb = subs->urb[u];
 324				if (urb)
 325					urb->complete = complete;
 326			}
 327		}
 328	}
 329}
 330
 331static void usx2y_subs_startup_finish(struct usx2ydev *usx2y)
 332{
 333	usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete);
 334	usx2y->prepare_subs = NULL;
 335}
 336
 337static void i_usx2y_subs_startup(struct urb *urb)
 338{
 339	struct snd_usx2y_substream *subs = urb->context;
 340	struct usx2ydev *usx2y = subs->usx2y;
 341	struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs;
 342
 343	if (prepare_subs) {
 344		if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
 345			usx2y_subs_startup_finish(usx2y);
 346			atomic_inc(&prepare_subs->state);
 347			wake_up(&usx2y->prepare_wait_queue);
 348		}
 349	}
 350
 351	i_usx2y_urb_complete(urb);
 352}
 353
 354static void usx2y_subs_prepare(struct snd_usx2y_substream *subs)
 355{
 356	dev_dbg(&subs->usx2y->dev->dev,
 357		"%s(%p) ep=%i urb0=%p urb1=%p\n",
 358		__func__, subs, subs->endpoint, subs->urb[0], subs->urb[1]);
 359	/* reset the pointer */
 360	subs->hwptr = 0;
 361	subs->hwptr_done = 0;
 362	subs->transfer_done = 0;
 363}
 364
 365static void usx2y_urb_release(struct urb **urb, int free_tb)
 366{
 367	if (*urb) {
 368		usb_kill_urb(*urb);
 369		if (free_tb)
 370			kfree((*urb)->transfer_buffer);
 371		usb_free_urb(*urb);
 372		*urb = NULL;
 373	}
 374}
 375
 376/*
 377 * release a substreams urbs
 378 */
 379static void usx2y_urbs_release(struct snd_usx2y_substream *subs)
 380{
 381	int i;
 382
 383	dev_dbg(&subs->usx2y->dev->dev, "%s %i\n", __func__, subs->endpoint);
 384	for (i = 0; i < NRURBS; i++)
 385		usx2y_urb_release(subs->urb + i,
 386				  subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
 387
 388	kfree(subs->tmpbuf);
 389	subs->tmpbuf = NULL;
 390}
 391
 392/*
 393 * initialize a substream's urbs
 394 */
 395static int usx2y_urbs_allocate(struct snd_usx2y_substream *subs)
 396{
 397	int i;
 398	unsigned int pipe;
 399	int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
 400	struct usb_device *dev = subs->usx2y->dev;
 401	struct urb **purb;
 402
 403	pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
 404			usb_rcvisocpipe(dev, subs->endpoint);
 405	subs->maxpacksize = usb_maxpacket(dev, pipe);
 406	if (!subs->maxpacksize)
 407		return -EINVAL;
 408
 409	if (is_playback && !subs->tmpbuf) {	/* allocate a temporary buffer for playback */
 410		subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
 411		if (!subs->tmpbuf)
 412			return -ENOMEM;
 413	}
 414	/* allocate and initialize data urbs */
 415	for (i = 0; i < NRURBS; i++) {
 416		purb = subs->urb + i;
 417		if (*purb) {
 418			usb_kill_urb(*purb);
 419			continue;
 420		}
 421		*purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
 422		if (!*purb) {
 423			usx2y_urbs_release(subs);
 424			return -ENOMEM;
 425		}
 426		if (!is_playback && !(*purb)->transfer_buffer) {
 427			/* allocate a capture buffer per urb */
 428			(*purb)->transfer_buffer =
 429				kmalloc_array(subs->maxpacksize,
 430					      nr_of_packs(), GFP_KERNEL);
 431			if (!(*purb)->transfer_buffer) {
 432				usx2y_urbs_release(subs);
 433				return -ENOMEM;
 434			}
 435		}
 436		(*purb)->dev = dev;
 437		(*purb)->pipe = pipe;
 438		(*purb)->number_of_packets = nr_of_packs();
 439		(*purb)->context = subs;
 440		(*purb)->interval = 1;
 441		(*purb)->complete = i_usx2y_subs_startup;
 442	}
 443	return 0;
 444}
 445
 446static void usx2y_subs_startup(struct snd_usx2y_substream *subs)
 447{
 448	struct usx2ydev *usx2y = subs->usx2y;
 449
 450	usx2y->prepare_subs = subs;
 451	subs->urb[0]->start_frame = -1;
 452	wmb();
 453	usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup);
 454}
 455
 456static int usx2y_urbs_start(struct snd_usx2y_substream *subs)
 457{
 458	int i, err;
 459	struct usx2ydev *usx2y = subs->usx2y;
 460	struct urb *urb;
 461	unsigned long pack;
 462
 463	err = usx2y_urbs_allocate(subs);
 464	if (err < 0)
 465		return err;
 466	subs->completed_urb = NULL;
 467	for (i = 0; i < 4; i++) {
 468		struct snd_usx2y_substream *subs = usx2y->subs[i];
 469
 470		if (subs && atomic_read(&subs->state) >= STATE_PREPARED)
 471			goto start;
 472	}
 473
 474 start:
 475	usx2y_subs_startup(subs);
 476	for (i = 0; i < NRURBS; i++) {
 477		urb = subs->urb[i];
 478		if (usb_pipein(urb->pipe)) {
 479			if (!i)
 480				atomic_set(&subs->state, STATE_STARTING3);
 481			urb->dev = usx2y->dev;
 482			for (pack = 0; pack < nr_of_packs(); pack++) {
 483				urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
 484				urb->iso_frame_desc[pack].length = subs->maxpacksize;
 485			}
 486			urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
 487			err = usb_submit_urb(urb, GFP_ATOMIC);
 488			if (err < 0) {
 489				dev_err(&urb->dev->dev, "%s: cannot submit datapipe for urb %d, err = %d\n",
 490					__func__, i, err);
 491				err = -EPIPE;
 492				goto cleanup;
 493			} else {
 494				if (!i)
 495					usx2y->wait_iso_frame = urb->start_frame;
 496			}
 497			urb->transfer_flags = 0;
 498		} else {
 499			atomic_set(&subs->state, STATE_STARTING1);
 500			break;
 501		}
 502	}
 503	err = 0;
 504	wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs);
 505	if (atomic_read(&subs->state) != STATE_PREPARED)
 506		err = -EPIPE;
 507
 508 cleanup:
 509	if (err) {
 510		usx2y_subs_startup_finish(usx2y);
 511		usx2y_clients_stop(usx2y);	// something is completely wrong > stop everything
 512	}
 513	return err;
 514}
 515
 516/*
 517 * return the current pcm pointer.  just return the hwptr_done value.
 518 */
 519static snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream)
 520{
 521	struct snd_usx2y_substream *subs = substream->runtime->private_data;
 522
 523	return subs->hwptr_done;
 524}
 525
 526/*
 527 * start/stop substream
 528 */
 529static int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 530{
 531	struct snd_usx2y_substream *subs = substream->runtime->private_data;
 532
 533	switch (cmd) {
 534	case SNDRV_PCM_TRIGGER_START:
 535		dev_dbg(&subs->usx2y->dev->dev, "%s(START)\n", __func__);
 536		if (atomic_read(&subs->state) == STATE_PREPARED &&
 537		    atomic_read(&subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= STATE_PREPARED) {
 538			atomic_set(&subs->state, STATE_PRERUNNING);
 539		} else {
 540			return -EPIPE;
 541		}
 542		break;
 543	case SNDRV_PCM_TRIGGER_STOP:
 544		dev_dbg(&subs->usx2y->dev->dev, "%s(STOP)\n", __func__);
 545		if (atomic_read(&subs->state) >= STATE_PRERUNNING)
 546			atomic_set(&subs->state, STATE_PREPARED);
 547		break;
 548	default:
 549		return -EINVAL;
 550	}
 551	return 0;
 552}
 553
 554/*
 555 * allocate a buffer, setup samplerate
 556 *
 557 * so far we use a physically linear buffer although packetize transfer
 558 * doesn't need a continuous area.
 559 * if sg buffer is supported on the later version of alsa, we'll follow
 560 * that.
 561 */
 562struct s_c2 {
 563	char c1, c2;
 564};
 565
 566static const struct s_c2 setrate_44100[] = {
 567	{ 0x14, 0x08},	// this line sets 44100, well actually a little less
 568	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
 569	{ 0x18, 0x42},
 570	{ 0x18, 0x45},
 571	{ 0x18, 0x46},
 572	{ 0x18, 0x48},
 573	{ 0x18, 0x4A},
 574	{ 0x18, 0x4C},
 575	{ 0x18, 0x4E},
 576	{ 0x18, 0x50},
 577	{ 0x18, 0x52},
 578	{ 0x18, 0x54},
 579	{ 0x18, 0x56},
 580	{ 0x18, 0x58},
 581	{ 0x18, 0x5A},
 582	{ 0x18, 0x5C},
 583	{ 0x18, 0x5E},
 584	{ 0x18, 0x60},
 585	{ 0x18, 0x62},
 586	{ 0x18, 0x64},
 587	{ 0x18, 0x66},
 588	{ 0x18, 0x68},
 589	{ 0x18, 0x6A},
 590	{ 0x18, 0x6C},
 591	{ 0x18, 0x6E},
 592	{ 0x18, 0x70},
 593	{ 0x18, 0x72},
 594	{ 0x18, 0x74},
 595	{ 0x18, 0x76},
 596	{ 0x18, 0x78},
 597	{ 0x18, 0x7A},
 598	{ 0x18, 0x7C},
 599	{ 0x18, 0x7E}
 600};
 601
 602static const struct s_c2 setrate_48000[] = {
 603	{ 0x14, 0x09},	// this line sets 48000, well actually a little less
 604	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
 605	{ 0x18, 0x42},
 606	{ 0x18, 0x45},
 607	{ 0x18, 0x46},
 608	{ 0x18, 0x48},
 609	{ 0x18, 0x4A},
 610	{ 0x18, 0x4C},
 611	{ 0x18, 0x4E},
 612	{ 0x18, 0x50},
 613	{ 0x18, 0x52},
 614	{ 0x18, 0x54},
 615	{ 0x18, 0x56},
 616	{ 0x18, 0x58},
 617	{ 0x18, 0x5A},
 618	{ 0x18, 0x5C},
 619	{ 0x18, 0x5E},
 620	{ 0x18, 0x60},
 621	{ 0x18, 0x62},
 622	{ 0x18, 0x64},
 623	{ 0x18, 0x66},
 624	{ 0x18, 0x68},
 625	{ 0x18, 0x6A},
 626	{ 0x18, 0x6C},
 627	{ 0x18, 0x6E},
 628	{ 0x18, 0x70},
 629	{ 0x18, 0x73},
 630	{ 0x18, 0x74},
 631	{ 0x18, 0x76},
 632	{ 0x18, 0x78},
 633	{ 0x18, 0x7A},
 634	{ 0x18, 0x7C},
 635	{ 0x18, 0x7E}
 636};
 637
 638#define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000)
 639
 640static void i_usx2y_04int(struct urb *urb)
 641{
 642	struct usx2ydev *usx2y = urb->context;
 643
 644	if (urb->status)
 645		dev_err(&urb->dev->dev, "%s() urb->status=%i\n",
 646			__func__, urb->status);
 647	if (!--usx2y->us04->len)
 648		wake_up(&usx2y->in04_wait_queue);
 649}
 650
 651static int usx2y_rate_set(struct usx2ydev *usx2y, int rate)
 652{
 653	int err = 0, i;
 654	struct snd_usx2y_urb_seq *us = NULL;
 655	int *usbdata = NULL;
 656	const struct s_c2 *ra = rate == 48000 ? setrate_48000 : setrate_44100;
 657	struct urb *urb;
 658
 659	if (usx2y->rate != rate) {
 660		us = kzalloc(struct_size(us, urb, NOOF_SETRATE_URBS),
 661			     GFP_KERNEL);
 662		if (!us) {
 663			err = -ENOMEM;
 664			goto cleanup;
 665		}
 666		us->len = NOOF_SETRATE_URBS;
 667		usbdata = kmalloc_array(NOOF_SETRATE_URBS, sizeof(int),
 668					GFP_KERNEL);
 669		if (!usbdata) {
 670			err = -ENOMEM;
 671			goto cleanup;
 672		}
 673		for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
 674			us->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
 675			if (!us->urb[i]) {
 676				err = -ENOMEM;
 677				goto cleanup;
 678			}
 679			((char *)(usbdata + i))[0] = ra[i].c1;
 680			((char *)(usbdata + i))[1] = ra[i].c2;
 681			usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4),
 682					  usbdata + i, 2, i_usx2y_04int, usx2y);
 683		}
 684		err = usb_urb_ep_type_check(us->urb[0]);
 685		if (err < 0)
 686			goto cleanup;
 687		us->submitted =	0;
 688		usx2y->us04 =	us;
 689		wait_event_timeout(usx2y->in04_wait_queue, !us->len, HZ);
 690		usx2y->us04 =	NULL;
 691		if (us->len)
 692			err = -ENODEV;
 693	cleanup:
 694		if (us) {
 695			us->submitted =	2*NOOF_SETRATE_URBS;
 696			for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
 697				urb = us->urb[i];
 698				if (!urb)
 699					continue;
 700				if (urb->status) {
 701					if (!err)
 702						err = -ENODEV;
 703					usb_kill_urb(urb);
 704				}
 705				usb_free_urb(urb);
 706			}
 707			usx2y->us04 = NULL;
 708			kfree(usbdata);
 709			kfree(us);
 710			if (!err)
 711				usx2y->rate = rate;
 712		}
 713	}
 714
 715	return err;
 716}
 717
 718static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format)
 719{
 720	int alternate, err;
 721	struct list_head *p;
 722
 723	if (format == SNDRV_PCM_FORMAT_S24_3LE) {
 724		alternate = 2;
 725		usx2y->stride = 6;
 726	} else {
 727		alternate = 1;
 728		usx2y->stride = 4;
 729	}
 730	list_for_each(p, &usx2y->midi_list) {
 731		snd_usbmidi_input_stop(p);
 732	}
 733	usb_kill_urb(usx2y->in04_urb);
 734	err = usb_set_interface(usx2y->dev, 0, alternate);
 735	if (err) {
 736		dev_err(&usx2y->dev->dev, "%s: usb_set_interface error\n",
 737			__func__);
 738		return err;
 739	}
 740	usx2y->in04_urb->dev = usx2y->dev;
 741	err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
 742	list_for_each(p, &usx2y->midi_list) {
 743		snd_usbmidi_input_start(p);
 744	}
 745	usx2y->format = format;
 746	usx2y->rate = 0;
 747	return err;
 748}
 749
 750
 751static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
 752				   struct snd_pcm_hw_params *hw_params)
 753{
 754	unsigned int		rate = params_rate(hw_params);
 755	snd_pcm_format_t	format = params_format(hw_params);
 756	struct snd_card *card = substream->pstr->pcm->card;
 757	struct usx2ydev	*dev = usx2y(card);
 758	struct snd_usx2y_substream *subs;
 759	struct snd_pcm_substream *test_substream;
 760	int i;
 761
 762	guard(mutex)(&usx2y(card)->pcm_mutex);
 763	dev_dbg(&dev->dev->dev, "%s(%p, %p)\n", __func__, substream, hw_params);
 764	/* all pcm substreams off one usx2y have to operate at the same
 765	 * rate & format
 766	 */
 767	for (i = 0; i < dev->pcm_devs * 2; i++) {
 768		subs = dev->subs[i];
 769		if (!subs)
 770			continue;
 771		test_substream = subs->pcm_substream;
 772		if (!test_substream || test_substream == substream ||
 773		    !test_substream->runtime)
 774			continue;
 775		if ((test_substream->runtime->format &&
 776		     test_substream->runtime->format != format) ||
 777		    (test_substream->runtime->rate &&
 778		     test_substream->runtime->rate != rate)) {
 779			return -EINVAL;
 780		}
 781	}
 782
 783	return 0;
 784}
 785
 786/*
 787 * free the buffer
 788 */
 789static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
 790{
 791	struct snd_pcm_runtime *runtime = substream->runtime;
 792	struct snd_usx2y_substream *subs = runtime->private_data;
 793	struct snd_usx2y_substream *cap_subs, *playback_subs;
 794
 795	guard(mutex)(&subs->usx2y->pcm_mutex);
 796	dev_dbg(&subs->usx2y->dev->dev, "%s(%p)\n", __func__, substream);
 797
 798	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 799		cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
 800		atomic_set(&subs->state, STATE_STOPPED);
 801		usx2y_urbs_release(subs);
 802		if (!cap_subs->pcm_substream ||
 803		    !cap_subs->pcm_substream->runtime ||
 804		    cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) {
 805			atomic_set(&cap_subs->state, STATE_STOPPED);
 806			usx2y_urbs_release(cap_subs);
 807		}
 808	} else {
 809		playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
 810		if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
 811			atomic_set(&subs->state, STATE_STOPPED);
 812			usx2y_urbs_release(subs);
 813		}
 814	}
 815	return 0;
 816}
 817
 818/*
 819 * prepare callback
 820 *
 821 * set format and initialize urbs
 822 */
 823static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
 824{
 825	struct snd_pcm_runtime *runtime = substream->runtime;
 826	struct snd_usx2y_substream *subs = runtime->private_data;
 827	struct usx2ydev *usx2y = subs->usx2y;
 828	struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
 829	int err = 0;
 830
 831	dev_dbg(&usx2y->dev->dev, "%s(%p)\n", __func__, substream);
 832
 833	guard(mutex)(&usx2y->pcm_mutex);
 834	usx2y_subs_prepare(subs);
 835	// Start hardware streams
 836	// SyncStream first....
 837	if (atomic_read(&capsubs->state) < STATE_PREPARED) {
 838		if (usx2y->format != runtime->format) {
 839			err = usx2y_format_set(usx2y, runtime->format);
 840			if (err < 0)
 841				return err;
 842		}
 843		if (usx2y->rate != runtime->rate) {
 844			err = usx2y_rate_set(usx2y, runtime->rate);
 845			if (err < 0)
 846				return err;
 847		}
 848		dev_dbg(&usx2y->dev->dev, "%s: starting capture pipe for %s\n",
 849			__func__, subs == capsubs ? "self" : "playpipe");
 850		err = usx2y_urbs_start(capsubs);
 851		if (err < 0)
 852			return err;
 853	}
 854
 855	if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED)
 856		err = usx2y_urbs_start(subs);
 857
 858	return err;
 859}
 860
 861static const struct snd_pcm_hardware snd_usx2y_2c = {
 862	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
 863				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
 864				 SNDRV_PCM_INFO_MMAP_VALID |
 865				 SNDRV_PCM_INFO_BATCH),
 866	.formats =                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
 867	.rates =                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
 868	.rate_min =                44100,
 869	.rate_max =                48000,
 870	.channels_min =            2,
 871	.channels_max =            2,
 872	.buffer_bytes_max =	(2*128*1024),
 873	.period_bytes_min =	64,
 874	.period_bytes_max =	(128*1024),
 875	.periods_min =		2,
 876	.periods_max =		1024,
 877	.fifo_size =              0
 878};
 879
 880static int snd_usx2y_pcm_open(struct snd_pcm_substream *substream)
 881{
 882	struct snd_usx2y_substream	*subs =
 883		((struct snd_usx2y_substream **)
 884		 snd_pcm_substream_chip(substream))[substream->stream];
 885	struct snd_pcm_runtime	*runtime = substream->runtime;
 886
 887	if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
 888		return -EBUSY;
 889
 890	runtime->hw = snd_usx2y_2c;
 891	runtime->private_data = subs;
 892	subs->pcm_substream = substream;
 893	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
 894	return 0;
 895}
 896
 897static int snd_usx2y_pcm_close(struct snd_pcm_substream *substream)
 898{
 899	struct snd_pcm_runtime *runtime = substream->runtime;
 900	struct snd_usx2y_substream *subs = runtime->private_data;
 901
 902	subs->pcm_substream = NULL;
 903
 904	return 0;
 905}
 906
 907static const struct snd_pcm_ops snd_usx2y_pcm_ops = {
 908	.open =		snd_usx2y_pcm_open,
 909	.close =	snd_usx2y_pcm_close,
 910	.hw_params =	snd_usx2y_pcm_hw_params,
 911	.hw_free =	snd_usx2y_pcm_hw_free,
 912	.prepare =	snd_usx2y_pcm_prepare,
 913	.trigger =	snd_usx2y_pcm_trigger,
 914	.pointer =	snd_usx2y_pcm_pointer,
 915};
 916
 917/*
 918 * free a usb stream instance
 919 */
 920static void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream)
 921{
 922	int stream;
 923
 924	for_each_pcm_streams(stream) {
 925		kfree(usx2y_substream[stream]);
 926		usx2y_substream[stream] = NULL;
 927	}
 928}
 929
 930static void snd_usx2y_pcm_private_free(struct snd_pcm *pcm)
 931{
 932	struct snd_usx2y_substream **usx2y_stream = pcm->private_data;
 933
 934	if (usx2y_stream)
 935		usx2y_audio_stream_free(usx2y_stream);
 936}
 937
 938static int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
 939{
 940	struct snd_pcm *pcm;
 941	int err, i;
 942	struct snd_usx2y_substream **usx2y_substream =
 943		usx2y(card)->subs + 2 * usx2y(card)->pcm_devs;
 944
 945	for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
 946	     i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
 947		usx2y_substream[i] = kzalloc(sizeof(struct snd_usx2y_substream), GFP_KERNEL);
 948		if (!usx2y_substream[i])
 949			return -ENOMEM;
 950
 951		usx2y_substream[i]->usx2y = usx2y(card);
 952	}
 953
 954	if (playback_endpoint)
 955		usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
 956	usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
 957
 958	err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs,
 959			  playback_endpoint ? 1 : 0, 1,
 960			  &pcm);
 961	if (err < 0) {
 962		usx2y_audio_stream_free(usx2y_substream);
 963		return err;
 964	}
 965
 966	if (playback_endpoint)
 967		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops);
 968	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops);
 969
 970	pcm->private_data = usx2y_substream;
 971	pcm->private_free = snd_usx2y_pcm_private_free;
 972	pcm->info_flags = 0;
 973
 974	sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs);
 975
 976	if (playback_endpoint) {
 977		snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
 978					   SNDRV_DMA_TYPE_CONTINUOUS,
 979					   NULL,
 980					   64*1024, 128*1024);
 981	}
 982
 983	snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
 984				   SNDRV_DMA_TYPE_CONTINUOUS,
 985				   NULL,
 986				   64*1024, 128*1024);
 987	usx2y(card)->pcm_devs++;
 988
 989	return 0;
 990}
 991
 992/*
 993 * create a chip instance and set its names.
 994 */
 995int usx2y_audio_create(struct snd_card *card)
 996{
 997	int err;
 998
 999	err = usx2y_audio_stream_new(card, 0xA, 0x8);
1000	if (err < 0)
1001		return err;
1002	if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428) {
1003		err = usx2y_audio_stream_new(card, 0, 0xA);
1004		if (err < 0)
1005			return err;
1006	}
1007	if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122)
1008		err = usx2y_rate_set(usx2y(card), 44100);	// Lets us428 recognize output-volume settings, disturbs us122.
1009	return err;
1010}
~/ Read-only file preview.