1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2002-2004 by Karsten Wiese
8 * (Tentative) USB Audio Driver for ALSA
12 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
14 * Many codes borrowed from audio.c by
15 * Alan Cox (alan@lxorguk.ukuu.org.uk)
16 * Thomas Sailer (sailer@ife.ee.ethz.ch)
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>
31 /* 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"
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.
40 #define USX2Y_NRPACKS 4
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.
47 #define USX2Y_NRPACKS_VARIABLE 1
49 #ifdef USX2Y_NRPACKS_VARIABLE
50 static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
51 #define nr_of_packs() nrpacks
52 module_param(nrpacks, int, 0444);
53 MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
55 #define nr_of_packs() USX2Y_NRPACKS
58 static int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs)
60 struct urb *urb = subs->completed_urb;
61 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
63 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
65 struct usx2ydev *usx2y = subs->usx2y;
67 for (i = 0; i < nr_of_packs(); i++) {
68 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
69 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
71 "active frame status %i. Most probably some hardware problem.\n",
72 urb->iso_frame_desc[i].status);
73 return urb->iso_frame_desc[i].status;
75 len = urb->iso_frame_desc[i].actual_length / usx2y->stride;
77 snd_printd("0 == len ERROR!\n");
81 /* copy a data chunk */
82 if ((hwptr_done + len) > runtime->buffer_size) {
83 cnt = runtime->buffer_size - hwptr_done;
84 blen = cnt * usx2y->stride;
85 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen);
86 memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen);
88 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp,
93 if (hwptr_done >= runtime->buffer_size)
94 hwptr_done -= runtime->buffer_size;
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);
108 * prepare urb for playback data pipe
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.
117 static int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs,
121 struct usx2ydev *usx2y = subs->usx2y;
122 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
123 int count, counts, pack, len;
126 for (pack = 0; pack < nr_of_packs(); pack++) {
127 /* calculate the size of a packet */
128 counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride;
130 if (counts < 43 || counts > 50) {
131 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
134 /* set up descriptor */
135 urb->iso_frame_desc[pack].offset = pack ?
136 urb->iso_frame_desc[pack - 1].offset +
137 urb->iso_frame_desc[pack - 1].length :
139 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
141 if (atomic_read(&subs->state) >= STATE_PRERUNNING) {
142 if (subs->hwptr + count > runtime->buffer_size) {
143 /* err, the transferred area goes over buffer boundary.
144 * copy the data to the temp buffer.
146 len = runtime->buffer_size - subs->hwptr;
147 urb->transfer_buffer = subs->tmpbuf;
148 memcpy(subs->tmpbuf, runtime->dma_area +
149 subs->hwptr * usx2y->stride, len * usx2y->stride);
150 memcpy(subs->tmpbuf + len * usx2y->stride,
151 runtime->dma_area, (count - len) * usx2y->stride);
152 subs->hwptr += count;
153 subs->hwptr -= runtime->buffer_size;
155 /* set the buffer pointer */
156 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride;
157 subs->hwptr += count;
158 if (subs->hwptr >= runtime->buffer_size)
159 subs->hwptr -= runtime->buffer_size;
162 urb->transfer_buffer = subs->tmpbuf;
164 urb->transfer_buffer_length = count * usx2y->stride;
169 * process after playback data complete
171 * update the current position and call callback if a period is processed.
173 static void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb)
175 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
176 int len = urb->actual_length / subs->usx2y->stride;
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);
188 static int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame)
194 urb->start_frame = frame + NRURBS * nr_of_packs(); // let hcd do rollover sanity checks
196 urb->dev = subs->usx2y->dev; /* we need to set this at each time */
197 err = usb_submit_urb(urb, GFP_ATOMIC);
199 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
205 static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs,
206 struct snd_usx2y_substream *playbacksubs,
210 struct urb *urb = playbacksubs->completed_urb;
212 state = atomic_read(&playbacksubs->state);
214 if (state == STATE_RUNNING)
215 usx2y_urb_play_retire(playbacksubs, urb);
216 else if (state >= STATE_PRERUNNING)
217 atomic_inc(&playbacksubs->state);
220 case STATE_STARTING1:
221 urb = playbacksubs->urb[0];
222 atomic_inc(&playbacksubs->state);
224 case STATE_STARTING2:
225 urb = playbacksubs->urb[1];
226 atomic_inc(&playbacksubs->state);
231 err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb);
234 err = usx2y_urb_submit(playbacksubs, urb, frame);
239 playbacksubs->completed_urb = NULL;
241 state = atomic_read(&capsubs->state);
242 if (state >= STATE_PREPARED) {
243 if (state == STATE_RUNNING) {
244 err = usx2y_urb_capt_retire(capsubs);
247 } else if (state >= STATE_PRERUNNING) {
248 atomic_inc(&capsubs->state);
250 err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame);
254 capsubs->completed_urb = NULL;
258 static void usx2y_clients_stop(struct usx2ydev *usx2y)
260 struct snd_usx2y_substream *subs;
264 for (s = 0; s < 4; s++) {
265 subs = usx2y->subs[s];
267 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
268 atomic_set(&subs->state, STATE_STOPPED);
271 for (s = 0; s < 4; s++) {
272 subs = usx2y->subs[s];
274 if (atomic_read(&subs->state) >= STATE_PRERUNNING)
275 snd_pcm_stop_xrun(subs->pcm_substream);
276 for (u = 0; u < NRURBS; u++) {
279 snd_printdd("%i status=%i start_frame=%i\n",
280 u, urb->status, urb->start_frame);
284 usx2y->prepare_subs = NULL;
285 wake_up(&usx2y->prepare_wait_queue);
288 static void usx2y_error_urb_status(struct usx2ydev *usx2y,
289 struct snd_usx2y_substream *subs, struct urb *urb)
291 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
293 usx2y_clients_stop(usx2y);
296 static void i_usx2y_urb_complete(struct urb *urb)
298 struct snd_usx2y_substream *subs = urb->context;
299 struct usx2ydev *usx2y = subs->usx2y;
300 struct snd_usx2y_substream *capsubs, *playbacksubs;
302 if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) {
303 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
304 usb_get_current_frame_number(usx2y->dev),
305 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
306 urb->status, urb->start_frame);
309 if (unlikely(urb->status)) {
310 usx2y_error_urb_status(usx2y, subs, urb);
314 subs->completed_urb = urb;
316 capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
317 playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
319 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();
327 usx2y_clients_stop(usx2y);
332 static void usx2y_urbs_set_complete(struct usx2ydev *usx2y,
333 void (*complete)(struct urb *))
335 struct snd_usx2y_substream *subs;
339 for (s = 0; s < 4; s++) {
340 subs = usx2y->subs[s];
342 for (u = 0; u < NRURBS; u++) {
345 urb->complete = complete;
351 static void usx2y_subs_startup_finish(struct usx2ydev *usx2y)
353 usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete);
354 usx2y->prepare_subs = NULL;
357 static void i_usx2y_subs_startup(struct urb *urb)
359 struct snd_usx2y_substream *subs = urb->context;
360 struct usx2ydev *usx2y = subs->usx2y;
361 struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs;
364 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
365 usx2y_subs_startup_finish(usx2y);
366 atomic_inc(&prepare_subs->state);
367 wake_up(&usx2y->prepare_wait_queue);
371 i_usx2y_urb_complete(urb);
374 static void usx2y_subs_prepare(struct snd_usx2y_substream *subs)
376 snd_printdd("usx2y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
377 subs, subs->endpoint, subs->urb[0], subs->urb[1]);
378 /* reset the pointer */
380 subs->hwptr_done = 0;
381 subs->transfer_done = 0;
384 static void usx2y_urb_release(struct urb **urb, int free_tb)
389 kfree((*urb)->transfer_buffer);
396 * release a substreams urbs
398 static void usx2y_urbs_release(struct snd_usx2y_substream *subs)
402 snd_printdd("%s %i\n", __func__, subs->endpoint);
403 for (i = 0; i < NRURBS; i++)
404 usx2y_urb_release(subs->urb + i,
405 subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
412 * initialize a substream's urbs
414 static int usx2y_urbs_allocate(struct snd_usx2y_substream *subs)
418 int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
419 struct usb_device *dev = subs->usx2y->dev;
422 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
423 usb_rcvisocpipe(dev, subs->endpoint);
424 subs->maxpacksize = usb_maxpacket(dev, pipe);
425 if (!subs->maxpacksize)
428 if (is_playback && !subs->tmpbuf) { /* allocate a temporary buffer for playback */
429 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
433 /* allocate and initialize data urbs */
434 for (i = 0; i < NRURBS; i++) {
435 purb = subs->urb + i;
440 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
442 usx2y_urbs_release(subs);
445 if (!is_playback && !(*purb)->transfer_buffer) {
446 /* allocate a capture buffer per urb */
447 (*purb)->transfer_buffer =
448 kmalloc_array(subs->maxpacksize,
449 nr_of_packs(), GFP_KERNEL);
450 if (!(*purb)->transfer_buffer) {
451 usx2y_urbs_release(subs);
456 (*purb)->pipe = pipe;
457 (*purb)->number_of_packets = nr_of_packs();
458 (*purb)->context = subs;
459 (*purb)->interval = 1;
460 (*purb)->complete = i_usx2y_subs_startup;
465 static void usx2y_subs_startup(struct snd_usx2y_substream *subs)
467 struct usx2ydev *usx2y = subs->usx2y;
469 usx2y->prepare_subs = subs;
470 subs->urb[0]->start_frame = -1;
472 usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup);
475 static int usx2y_urbs_start(struct snd_usx2y_substream *subs)
478 struct usx2ydev *usx2y = subs->usx2y;
482 err = usx2y_urbs_allocate(subs);
485 subs->completed_urb = NULL;
486 for (i = 0; i < 4; i++) {
487 struct snd_usx2y_substream *subs = usx2y->subs[i];
489 if (subs && atomic_read(&subs->state) >= STATE_PREPARED)
494 usx2y_subs_startup(subs);
495 for (i = 0; i < NRURBS; i++) {
497 if (usb_pipein(urb->pipe)) {
499 atomic_set(&subs->state, STATE_STARTING3);
500 urb->dev = usx2y->dev;
501 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;
505 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
506 err = usb_submit_urb(urb, GFP_ATOMIC);
508 snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
513 usx2y->wait_iso_frame = urb->start_frame;
515 urb->transfer_flags = 0;
517 atomic_set(&subs->state, STATE_STARTING1);
522 wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs);
523 if (atomic_read(&subs->state) != STATE_PREPARED)
528 usx2y_subs_startup_finish(usx2y);
529 usx2y_clients_stop(usx2y); // something is completely wrong > stop everything
535 * return the current pcm pointer. just return the hwptr_done value.
537 static snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream)
539 struct snd_usx2y_substream *subs = substream->runtime->private_data;
541 return subs->hwptr_done;
545 * start/stop substream
547 static int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
549 struct snd_usx2y_substream *subs = substream->runtime->private_data;
552 case SNDRV_PCM_TRIGGER_START:
553 snd_printdd("%s(START)\n", __func__);
554 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);
562 case SNDRV_PCM_TRIGGER_STOP:
563 snd_printdd("%s(STOP)\n", __func__);
564 if (atomic_read(&subs->state) >= STATE_PRERUNNING)
565 atomic_set(&subs->state, STATE_PREPARED);
574 * allocate a buffer, setup samplerate
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
585 static const struct s_c2 setrate_44100[] = {
586 { 0x14, 0x08}, // this line sets 44100, well actually a little less
587 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
621 static const struct s_c2 setrate_48000[] = {
622 { 0x14, 0x09}, // this line sets 48000, well actually a little less
623 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
657 #define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000)
659 static void i_usx2y_04int(struct urb *urb)
661 struct usx2ydev *usx2y = urb->context;
664 snd_printk(KERN_ERR "snd_usx2y_04int() urb->status=%i\n", urb->status);
665 if (!--usx2y->us04->len)
666 wake_up(&usx2y->in04_wait_queue);
669 static int usx2y_rate_set(struct usx2ydev *usx2y, int rate)
672 struct snd_usx2y_urb_seq *us = NULL;
674 const struct s_c2 *ra = rate == 48000 ? setrate_48000 : setrate_44100;
677 if (usx2y->rate != rate) {
678 us = kzalloc(struct_size(us, urb, NOOF_SETRATE_URBS),
684 us->len = NOOF_SETRATE_URBS;
685 usbdata = kmalloc_array(NOOF_SETRATE_URBS, sizeof(int),
691 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
692 us->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
697 ((char *)(usbdata + i))[0] = ra[i].c1;
698 ((char *)(usbdata + i))[1] = ra[i].c2;
699 usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4),
700 usbdata + i, 2, i_usx2y_04int, usx2y);
702 err = usb_urb_ep_type_check(us->urb[0]);
707 wait_event_timeout(usx2y->in04_wait_queue, !us->len, HZ);
713 us->submitted = 2*NOOF_SETRATE_URBS;
714 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
736 static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format)
741 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
748 list_for_each(p, &usx2y->midi_list) {
749 snd_usbmidi_input_stop(p);
751 usb_kill_urb(usx2y->in04_urb);
752 err = usb_set_interface(usx2y->dev, 0, alternate);
754 snd_printk(KERN_ERR "usb_set_interface error\n");
757 usx2y->in04_urb->dev = usx2y->dev;
758 err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
759 list_for_each(p, &usx2y->midi_list) {
760 snd_usbmidi_input_start(p);
762 usx2y->format = format;
768 static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
769 struct snd_pcm_hw_params *hw_params)
772 unsigned int rate = params_rate(hw_params);
773 snd_pcm_format_t format = params_format(hw_params);
774 struct snd_card *card = substream->pstr->pcm->card;
775 struct usx2ydev *dev = usx2y(card);
776 struct snd_usx2y_substream *subs;
777 struct snd_pcm_substream *test_substream;
780 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
785 for (i = 0; i < dev->pcm_devs * 2; i++) {
789 test_substream = subs->pcm_substream;
790 if (!test_substream || test_substream == substream ||
791 !test_substream->runtime)
793 if ((test_substream->runtime->format &&
794 test_substream->runtime->format != format) ||
795 (test_substream->runtime->rate &&
796 test_substream->runtime->rate != rate)) {
803 mutex_unlock(&usx2y(card)->pcm_mutex);
810 static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
812 struct snd_pcm_runtime *runtime = substream->runtime;
813 struct snd_usx2y_substream *subs = runtime->private_data;
814 struct snd_usx2y_substream *cap_subs, *playback_subs;
816 mutex_lock(&subs->usx2y->pcm_mutex);
817 snd_printdd("snd_usx2y_hw_free(%p)\n", substream);
819 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
820 cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
821 atomic_set(&subs->state, STATE_STOPPED);
822 usx2y_urbs_release(subs);
823 if (!cap_subs->pcm_substream ||
824 !cap_subs->pcm_substream->runtime ||
825 cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) {
826 atomic_set(&cap_subs->state, STATE_STOPPED);
827 usx2y_urbs_release(cap_subs);
830 playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
831 if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
832 atomic_set(&subs->state, STATE_STOPPED);
833 usx2y_urbs_release(subs);
836 mutex_unlock(&subs->usx2y->pcm_mutex);
843 * set format and initialize urbs
845 static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
847 struct snd_pcm_runtime *runtime = substream->runtime;
848 struct snd_usx2y_substream *subs = runtime->private_data;
849 struct usx2ydev *usx2y = subs->usx2y;
850 struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
853 snd_printdd("%s(%p)\n", __func__, substream);
855 mutex_lock(&usx2y->pcm_mutex);
856 usx2y_subs_prepare(subs);
857 // Start hardware streams
858 // SyncStream first....
859 if (atomic_read(&capsubs->state) < STATE_PREPARED) {
860 if (usx2y->format != runtime->format) {
861 err = usx2y_format_set(usx2y, runtime->format);
863 goto up_prepare_mutex;
865 if (usx2y->rate != runtime->rate) {
866 err = usx2y_rate_set(usx2y, runtime->rate);
868 goto up_prepare_mutex;
870 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
871 err = usx2y_urbs_start(capsubs);
873 goto up_prepare_mutex;
876 if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED)
877 err = usx2y_urbs_start(subs);
880 mutex_unlock(&usx2y->pcm_mutex);
884 static const struct snd_pcm_hardware snd_usx2y_2c = {
885 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
886 SNDRV_PCM_INFO_BLOCK_TRANSFER |
887 SNDRV_PCM_INFO_MMAP_VALID |
888 SNDRV_PCM_INFO_BATCH),
889 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
890 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
895 .buffer_bytes_max = (2*128*1024),
896 .period_bytes_min = 64,
897 .period_bytes_max = (128*1024),
903 static int snd_usx2y_pcm_open(struct snd_pcm_substream *substream)
905 struct snd_usx2y_substream *subs =
906 ((struct snd_usx2y_substream **)
907 snd_pcm_substream_chip(substream))[substream->stream];
908 struct snd_pcm_runtime *runtime = substream->runtime;
910 if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
913 runtime->hw = snd_usx2y_2c;
914 runtime->private_data = subs;
915 subs->pcm_substream = substream;
916 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
920 static int snd_usx2y_pcm_close(struct snd_pcm_substream *substream)
922 struct snd_pcm_runtime *runtime = substream->runtime;
923 struct snd_usx2y_substream *subs = runtime->private_data;
925 subs->pcm_substream = NULL;
930 static const struct snd_pcm_ops snd_usx2y_pcm_ops = {
931 .open = snd_usx2y_pcm_open,
932 .close = snd_usx2y_pcm_close,
933 .hw_params = snd_usx2y_pcm_hw_params,
934 .hw_free = snd_usx2y_pcm_hw_free,
935 .prepare = snd_usx2y_pcm_prepare,
936 .trigger = snd_usx2y_pcm_trigger,
937 .pointer = snd_usx2y_pcm_pointer,
941 * free a usb stream instance
943 static void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream)
947 for_each_pcm_streams(stream) {
948 kfree(usx2y_substream[stream]);
949 usx2y_substream[stream] = NULL;
953 static void snd_usx2y_pcm_private_free(struct snd_pcm *pcm)
955 struct snd_usx2y_substream **usx2y_stream = pcm->private_data;
958 usx2y_audio_stream_free(usx2y_stream);
961 static int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
965 struct snd_usx2y_substream **usx2y_substream =
966 usx2y(card)->subs + 2 * usx2y(card)->pcm_devs;
968 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
969 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
970 usx2y_substream[i] = kzalloc(sizeof(struct snd_usx2y_substream), GFP_KERNEL);
971 if (!usx2y_substream[i])
974 usx2y_substream[i]->usx2y = usx2y(card);
977 if (playback_endpoint)
978 usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
979 usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
981 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs,
982 playback_endpoint ? 1 : 0, 1,
985 usx2y_audio_stream_free(usx2y_substream);
989 if (playback_endpoint)
990 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops);
991 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops);
993 pcm->private_data = usx2y_substream;
994 pcm->private_free = snd_usx2y_pcm_private_free;
997 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs);
999 if (playback_endpoint) {
1000 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1001 SNDRV_DMA_TYPE_CONTINUOUS,
1006 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1007 SNDRV_DMA_TYPE_CONTINUOUS,
1010 usx2y(card)->pcm_devs++;
1016 * create a chip instance and set its names.
1018 int usx2y_audio_create(struct snd_card *card)
1022 err = usx2y_audio_stream_new(card, 0xA, 0x8);
1025 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428) {
1026 err = usx2y_audio_stream_new(card, 0, 0xA);
1030 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122)
1031 err = usx2y_rate_set(usx2y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.