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 */
70 dev_err(&usx2y
->dev
->dev
,
71 "%s: active frame status %i. Most probably some hardware problem.\n",
73 urb
->iso_frame_desc
[i
].status
);
74 return urb
->iso_frame_desc
[i
].status
;
76 len
= urb
->iso_frame_desc
[i
].actual_length
/ usx2y
->stride
;
78 dev_dbg(&usx2y
->dev
->dev
, "%s: 0 == len ERROR!\n", __func__
);
82 /* copy a data chunk */
83 if ((hwptr_done
+ len
) > runtime
->buffer_size
) {
84 cnt
= runtime
->buffer_size
- hwptr_done
;
85 blen
= cnt
* usx2y
->stride
;
86 memcpy(runtime
->dma_area
+ hwptr_done
* usx2y
->stride
, cp
, blen
);
87 memcpy(runtime
->dma_area
, cp
+ blen
, len
* usx2y
->stride
- blen
);
89 memcpy(runtime
->dma_area
+ hwptr_done
* usx2y
->stride
, cp
,
94 if (hwptr_done
>= runtime
->buffer_size
)
95 hwptr_done
-= runtime
->buffer_size
;
98 subs
->hwptr_done
= hwptr_done
;
99 subs
->transfer_done
+= lens
;
100 /* update the pointer, call callback if necessary */
101 if (subs
->transfer_done
>= runtime
->period_size
) {
102 subs
->transfer_done
-= runtime
->period_size
;
103 snd_pcm_period_elapsed(subs
->pcm_substream
);
109 * prepare urb for playback data pipe
111 * we copy the data directly from the pcm buffer.
112 * the current position to be copied is held in hwptr field.
113 * since a urb can handle only a single linear buffer, if the total
114 * transferred area overflows the buffer boundary, we cannot send
115 * it directly from the buffer. thus the data is once copied to
116 * a temporary buffer and urb points to that.
118 static int usx2y_urb_play_prepare(struct snd_usx2y_substream
*subs
,
122 struct usx2ydev
*usx2y
= subs
->usx2y
;
123 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
124 int count
, counts
, pack
, len
;
127 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
128 /* calculate the size of a packet */
129 counts
= cap_urb
->iso_frame_desc
[pack
].actual_length
/ usx2y
->stride
;
131 if (counts
< 43 || counts
> 50) {
132 dev_err(&usx2y
->dev
->dev
, "%s: should not be here with counts=%i\n",
136 /* set up descriptor */
137 urb
->iso_frame_desc
[pack
].offset
= pack
?
138 urb
->iso_frame_desc
[pack
- 1].offset
+
139 urb
->iso_frame_desc
[pack
- 1].length
:
141 urb
->iso_frame_desc
[pack
].length
= cap_urb
->iso_frame_desc
[pack
].actual_length
;
143 if (atomic_read(&subs
->state
) >= STATE_PRERUNNING
) {
144 if (subs
->hwptr
+ count
> runtime
->buffer_size
) {
145 /* err, the transferred area goes over buffer boundary.
146 * copy the data to the temp buffer.
148 len
= runtime
->buffer_size
- subs
->hwptr
;
149 urb
->transfer_buffer
= subs
->tmpbuf
;
150 memcpy(subs
->tmpbuf
, runtime
->dma_area
+
151 subs
->hwptr
* usx2y
->stride
, len
* usx2y
->stride
);
152 memcpy(subs
->tmpbuf
+ len
* usx2y
->stride
,
153 runtime
->dma_area
, (count
- len
) * usx2y
->stride
);
154 subs
->hwptr
+= count
;
155 subs
->hwptr
-= runtime
->buffer_size
;
157 /* set the buffer pointer */
158 urb
->transfer_buffer
= runtime
->dma_area
+ subs
->hwptr
* usx2y
->stride
;
159 subs
->hwptr
+= count
;
160 if (subs
->hwptr
>= runtime
->buffer_size
)
161 subs
->hwptr
-= runtime
->buffer_size
;
164 urb
->transfer_buffer
= subs
->tmpbuf
;
166 urb
->transfer_buffer_length
= count
* usx2y
->stride
;
171 * process after playback data complete
173 * update the current position and call callback if a period is processed.
175 static void usx2y_urb_play_retire(struct snd_usx2y_substream
*subs
, struct urb
*urb
)
177 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
178 int len
= urb
->actual_length
/ subs
->usx2y
->stride
;
180 subs
->transfer_done
+= len
;
181 subs
->hwptr_done
+= len
;
182 if (subs
->hwptr_done
>= runtime
->buffer_size
)
183 subs
->hwptr_done
-= runtime
->buffer_size
;
184 if (subs
->transfer_done
>= runtime
->period_size
) {
185 subs
->transfer_done
-= runtime
->period_size
;
186 snd_pcm_period_elapsed(subs
->pcm_substream
);
190 static int usx2y_urb_submit(struct snd_usx2y_substream
*subs
, struct urb
*urb
, int frame
)
196 urb
->start_frame
= frame
+ NRURBS
* nr_of_packs(); // let hcd do rollover sanity checks
198 urb
->dev
= subs
->usx2y
->dev
; /* we need to set this at each time */
199 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
201 dev_err(&urb
->dev
->dev
, "%s: usb_submit_urb() returned %i\n",
208 static int usx2y_usbframe_complete(struct snd_usx2y_substream
*capsubs
,
209 struct snd_usx2y_substream
*playbacksubs
,
213 struct urb
*urb
= playbacksubs
->completed_urb
;
215 state
= atomic_read(&playbacksubs
->state
);
217 if (state
== STATE_RUNNING
)
218 usx2y_urb_play_retire(playbacksubs
, urb
);
219 else if (state
>= STATE_PRERUNNING
)
220 atomic_inc(&playbacksubs
->state
);
223 case STATE_STARTING1
:
224 urb
= playbacksubs
->urb
[0];
225 atomic_inc(&playbacksubs
->state
);
227 case STATE_STARTING2
:
228 urb
= playbacksubs
->urb
[1];
229 atomic_inc(&playbacksubs
->state
);
234 err
= usx2y_urb_play_prepare(playbacksubs
, capsubs
->completed_urb
, urb
);
237 err
= usx2y_urb_submit(playbacksubs
, urb
, frame
);
242 playbacksubs
->completed_urb
= NULL
;
244 state
= atomic_read(&capsubs
->state
);
245 if (state
>= STATE_PREPARED
) {
246 if (state
== STATE_RUNNING
) {
247 err
= usx2y_urb_capt_retire(capsubs
);
250 } else if (state
>= STATE_PRERUNNING
) {
251 atomic_inc(&capsubs
->state
);
253 err
= usx2y_urb_submit(capsubs
, capsubs
->completed_urb
, frame
);
257 capsubs
->completed_urb
= NULL
;
261 static void usx2y_clients_stop(struct usx2ydev
*usx2y
)
263 struct snd_usx2y_substream
*subs
;
267 for (s
= 0; s
< 4; s
++) {
268 subs
= usx2y
->subs
[s
];
270 dev_dbg(&usx2y
->dev
->dev
, "%s: %i %p state=%i\n",
271 __func__
, s
, subs
, atomic_read(&subs
->state
));
272 atomic_set(&subs
->state
, STATE_STOPPED
);
275 for (s
= 0; s
< 4; s
++) {
276 subs
= usx2y
->subs
[s
];
278 if (atomic_read(&subs
->state
) >= STATE_PRERUNNING
)
279 snd_pcm_stop_xrun(subs
->pcm_substream
);
280 for (u
= 0; u
< NRURBS
; u
++) {
283 dev_dbg(&usx2y
->dev
->dev
,
284 "%s: %i status=%i start_frame=%i\n",
285 __func__
, u
, urb
->status
, urb
->start_frame
);
289 usx2y
->prepare_subs
= NULL
;
290 wake_up(&usx2y
->prepare_wait_queue
);
293 static void usx2y_error_urb_status(struct usx2ydev
*usx2y
,
294 struct snd_usx2y_substream
*subs
, struct urb
*urb
)
296 dev_err(&usx2y
->dev
->dev
, "%s: ep=%i stalled with status=%i\n",
297 __func__
, subs
->endpoint
, urb
->status
);
299 usx2y_clients_stop(usx2y
);
302 static void i_usx2y_urb_complete(struct urb
*urb
)
304 struct snd_usx2y_substream
*subs
= urb
->context
;
305 struct usx2ydev
*usx2y
= subs
->usx2y
;
306 struct snd_usx2y_substream
*capsubs
, *playbacksubs
;
308 if (unlikely(atomic_read(&subs
->state
) < STATE_PREPARED
)) {
309 dev_dbg(&usx2y
->dev
->dev
,
310 "%s: hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
312 usb_get_current_frame_number(usx2y
->dev
),
313 subs
->endpoint
, usb_pipein(urb
->pipe
) ? "in" : "out",
314 urb
->status
, urb
->start_frame
);
317 if (unlikely(urb
->status
)) {
318 usx2y_error_urb_status(usx2y
, subs
, urb
);
322 subs
->completed_urb
= urb
;
324 capsubs
= usx2y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
325 playbacksubs
= usx2y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
327 if (capsubs
->completed_urb
&&
328 atomic_read(&capsubs
->state
) >= STATE_PREPARED
&&
329 (playbacksubs
->completed_urb
||
330 atomic_read(&playbacksubs
->state
) < STATE_PREPARED
)) {
331 if (!usx2y_usbframe_complete(capsubs
, playbacksubs
, urb
->start_frame
)) {
332 usx2y
->wait_iso_frame
+= nr_of_packs();
334 usx2y_clients_stop(usx2y
);
339 static void usx2y_urbs_set_complete(struct usx2ydev
*usx2y
,
340 void (*complete
)(struct urb
*))
342 struct snd_usx2y_substream
*subs
;
346 for (s
= 0; s
< 4; s
++) {
347 subs
= usx2y
->subs
[s
];
349 for (u
= 0; u
< NRURBS
; u
++) {
352 urb
->complete
= complete
;
358 static void usx2y_subs_startup_finish(struct usx2ydev
*usx2y
)
360 usx2y_urbs_set_complete(usx2y
, i_usx2y_urb_complete
);
361 usx2y
->prepare_subs
= NULL
;
364 static void i_usx2y_subs_startup(struct urb
*urb
)
366 struct snd_usx2y_substream
*subs
= urb
->context
;
367 struct usx2ydev
*usx2y
= subs
->usx2y
;
368 struct snd_usx2y_substream
*prepare_subs
= usx2y
->prepare_subs
;
371 if (urb
->start_frame
== prepare_subs
->urb
[0]->start_frame
) {
372 usx2y_subs_startup_finish(usx2y
);
373 atomic_inc(&prepare_subs
->state
);
374 wake_up(&usx2y
->prepare_wait_queue
);
378 i_usx2y_urb_complete(urb
);
381 static void usx2y_subs_prepare(struct snd_usx2y_substream
*subs
)
383 dev_dbg(&subs
->usx2y
->dev
->dev
,
384 "%s(%p) ep=%i urb0=%p urb1=%p\n",
385 __func__
, subs
, subs
->endpoint
, subs
->urb
[0], subs
->urb
[1]);
386 /* reset the pointer */
388 subs
->hwptr_done
= 0;
389 subs
->transfer_done
= 0;
392 static void usx2y_urb_release(struct urb
**urb
, int free_tb
)
397 kfree((*urb
)->transfer_buffer
);
404 * release a substreams urbs
406 static void usx2y_urbs_release(struct snd_usx2y_substream
*subs
)
410 dev_dbg(&subs
->usx2y
->dev
->dev
, "%s %i\n", __func__
, subs
->endpoint
);
411 for (i
= 0; i
< NRURBS
; i
++)
412 usx2y_urb_release(subs
->urb
+ i
,
413 subs
!= subs
->usx2y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
]);
420 * initialize a substream's urbs
422 static int usx2y_urbs_allocate(struct snd_usx2y_substream
*subs
)
426 int is_playback
= subs
== subs
->usx2y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
427 struct usb_device
*dev
= subs
->usx2y
->dev
;
430 pipe
= is_playback
? usb_sndisocpipe(dev
, subs
->endpoint
) :
431 usb_rcvisocpipe(dev
, subs
->endpoint
);
432 subs
->maxpacksize
= usb_maxpacket(dev
, pipe
);
433 if (!subs
->maxpacksize
)
436 if (is_playback
&& !subs
->tmpbuf
) { /* allocate a temporary buffer for playback */
437 subs
->tmpbuf
= kcalloc(nr_of_packs(), subs
->maxpacksize
, GFP_KERNEL
);
441 /* allocate and initialize data urbs */
442 for (i
= 0; i
< NRURBS
; i
++) {
443 purb
= subs
->urb
+ i
;
448 *purb
= usb_alloc_urb(nr_of_packs(), GFP_KERNEL
);
450 usx2y_urbs_release(subs
);
453 if (!is_playback
&& !(*purb
)->transfer_buffer
) {
454 /* allocate a capture buffer per urb */
455 (*purb
)->transfer_buffer
=
456 kmalloc_array(subs
->maxpacksize
,
457 nr_of_packs(), GFP_KERNEL
);
458 if (!(*purb
)->transfer_buffer
) {
459 usx2y_urbs_release(subs
);
464 (*purb
)->pipe
= pipe
;
465 (*purb
)->number_of_packets
= nr_of_packs();
466 (*purb
)->context
= subs
;
467 (*purb
)->interval
= 1;
468 (*purb
)->complete
= i_usx2y_subs_startup
;
473 static void usx2y_subs_startup(struct snd_usx2y_substream
*subs
)
475 struct usx2ydev
*usx2y
= subs
->usx2y
;
477 usx2y
->prepare_subs
= subs
;
478 subs
->urb
[0]->start_frame
= -1;
480 usx2y_urbs_set_complete(usx2y
, i_usx2y_subs_startup
);
483 static int usx2y_urbs_start(struct snd_usx2y_substream
*subs
)
486 struct usx2ydev
*usx2y
= subs
->usx2y
;
490 err
= usx2y_urbs_allocate(subs
);
493 subs
->completed_urb
= NULL
;
494 for (i
= 0; i
< 4; i
++) {
495 struct snd_usx2y_substream
*subs
= usx2y
->subs
[i
];
497 if (subs
&& atomic_read(&subs
->state
) >= STATE_PREPARED
)
502 usx2y_subs_startup(subs
);
503 for (i
= 0; i
< NRURBS
; i
++) {
505 if (usb_pipein(urb
->pipe
)) {
507 atomic_set(&subs
->state
, STATE_STARTING3
);
508 urb
->dev
= usx2y
->dev
;
509 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
510 urb
->iso_frame_desc
[pack
].offset
= subs
->maxpacksize
* pack
;
511 urb
->iso_frame_desc
[pack
].length
= subs
->maxpacksize
;
513 urb
->transfer_buffer_length
= subs
->maxpacksize
* nr_of_packs();
514 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
516 dev_err(&urb
->dev
->dev
, "%s: cannot submit datapipe for urb %d, err = %d\n",
522 usx2y
->wait_iso_frame
= urb
->start_frame
;
524 urb
->transfer_flags
= 0;
526 atomic_set(&subs
->state
, STATE_STARTING1
);
531 wait_event(usx2y
->prepare_wait_queue
, !usx2y
->prepare_subs
);
532 if (atomic_read(&subs
->state
) != STATE_PREPARED
)
537 usx2y_subs_startup_finish(usx2y
);
538 usx2y_clients_stop(usx2y
); // something is completely wrong > stop everything
544 * return the current pcm pointer. just return the hwptr_done value.
546 static snd_pcm_uframes_t
snd_usx2y_pcm_pointer(struct snd_pcm_substream
*substream
)
548 struct snd_usx2y_substream
*subs
= substream
->runtime
->private_data
;
550 return subs
->hwptr_done
;
554 * start/stop substream
556 static int snd_usx2y_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
558 struct snd_usx2y_substream
*subs
= substream
->runtime
->private_data
;
561 case SNDRV_PCM_TRIGGER_START
:
562 dev_dbg(&subs
->usx2y
->dev
->dev
, "%s(START)\n", __func__
);
563 if (atomic_read(&subs
->state
) == STATE_PREPARED
&&
564 atomic_read(&subs
->usx2y
->subs
[SNDRV_PCM_STREAM_CAPTURE
]->state
) >= STATE_PREPARED
) {
565 atomic_set(&subs
->state
, STATE_PRERUNNING
);
570 case SNDRV_PCM_TRIGGER_STOP
:
571 dev_dbg(&subs
->usx2y
->dev
->dev
, "%s(STOP)\n", __func__
);
572 if (atomic_read(&subs
->state
) >= STATE_PRERUNNING
)
573 atomic_set(&subs
->state
, STATE_PREPARED
);
582 * allocate a buffer, setup samplerate
584 * so far we use a physically linear buffer although packetize transfer
585 * doesn't need a continuous area.
586 * if sg buffer is supported on the later version of alsa, we'll follow
593 static const struct s_c2 setrate_44100
[] = {
594 { 0x14, 0x08}, // this line sets 44100, well actually a little less
595 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
629 static const struct s_c2 setrate_48000
[] = {
630 { 0x14, 0x09}, // this line sets 48000, well actually a little less
631 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
665 #define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000)
667 static void i_usx2y_04int(struct urb
*urb
)
669 struct usx2ydev
*usx2y
= urb
->context
;
672 dev_err(&urb
->dev
->dev
, "%s() urb->status=%i\n",
673 __func__
, urb
->status
);
674 if (!--usx2y
->us04
->len
)
675 wake_up(&usx2y
->in04_wait_queue
);
678 static int usx2y_rate_set(struct usx2ydev
*usx2y
, int rate
)
681 struct snd_usx2y_urb_seq
*us
= NULL
;
683 const struct s_c2
*ra
= rate
== 48000 ? setrate_48000
: setrate_44100
;
686 if (usx2y
->rate
!= rate
) {
687 us
= kzalloc(struct_size(us
, urb
, NOOF_SETRATE_URBS
),
693 us
->len
= NOOF_SETRATE_URBS
;
694 usbdata
= kmalloc_array(NOOF_SETRATE_URBS
, sizeof(int),
700 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
701 us
->urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
706 ((char *)(usbdata
+ i
))[0] = ra
[i
].c1
;
707 ((char *)(usbdata
+ i
))[1] = ra
[i
].c2
;
708 usb_fill_bulk_urb(us
->urb
[i
], usx2y
->dev
, usb_sndbulkpipe(usx2y
->dev
, 4),
709 usbdata
+ i
, 2, i_usx2y_04int
, usx2y
);
711 err
= usb_urb_ep_type_check(us
->urb
[0]);
716 wait_event_timeout(usx2y
->in04_wait_queue
, !us
->len
, HZ
);
722 us
->submitted
= 2*NOOF_SETRATE_URBS
;
723 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
745 static int usx2y_format_set(struct usx2ydev
*usx2y
, snd_pcm_format_t format
)
750 if (format
== SNDRV_PCM_FORMAT_S24_3LE
) {
757 list_for_each(p
, &usx2y
->midi_list
) {
758 snd_usbmidi_input_stop(p
);
760 usb_kill_urb(usx2y
->in04_urb
);
761 err
= usb_set_interface(usx2y
->dev
, 0, alternate
);
763 dev_err(&usx2y
->dev
->dev
, "%s: usb_set_interface error\n",
767 usx2y
->in04_urb
->dev
= usx2y
->dev
;
768 err
= usb_submit_urb(usx2y
->in04_urb
, GFP_KERNEL
);
769 list_for_each(p
, &usx2y
->midi_list
) {
770 snd_usbmidi_input_start(p
);
772 usx2y
->format
= format
;
778 static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream
*substream
,
779 struct snd_pcm_hw_params
*hw_params
)
782 unsigned int rate
= params_rate(hw_params
);
783 snd_pcm_format_t format
= params_format(hw_params
);
784 struct snd_card
*card
= substream
->pstr
->pcm
->card
;
785 struct usx2ydev
*dev
= usx2y(card
);
786 struct snd_usx2y_substream
*subs
;
787 struct snd_pcm_substream
*test_substream
;
790 mutex_lock(&usx2y(card
)->pcm_mutex
);
791 dev_dbg(&dev
->dev
->dev
, "%s(%p, %p)\n", __func__
, substream
, hw_params
);
792 /* all pcm substreams off one usx2y have to operate at the same
795 for (i
= 0; i
< dev
->pcm_devs
* 2; i
++) {
799 test_substream
= subs
->pcm_substream
;
800 if (!test_substream
|| test_substream
== substream
||
801 !test_substream
->runtime
)
803 if ((test_substream
->runtime
->format
&&
804 test_substream
->runtime
->format
!= format
) ||
805 (test_substream
->runtime
->rate
&&
806 test_substream
->runtime
->rate
!= rate
)) {
813 mutex_unlock(&usx2y(card
)->pcm_mutex
);
820 static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream
*substream
)
822 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
823 struct snd_usx2y_substream
*subs
= runtime
->private_data
;
824 struct snd_usx2y_substream
*cap_subs
, *playback_subs
;
826 mutex_lock(&subs
->usx2y
->pcm_mutex
);
827 dev_dbg(&subs
->usx2y
->dev
->dev
, "%s(%p)\n", __func__
, substream
);
829 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
830 cap_subs
= subs
->usx2y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
831 atomic_set(&subs
->state
, STATE_STOPPED
);
832 usx2y_urbs_release(subs
);
833 if (!cap_subs
->pcm_substream
||
834 !cap_subs
->pcm_substream
->runtime
||
835 cap_subs
->pcm_substream
->runtime
->state
< SNDRV_PCM_STATE_PREPARED
) {
836 atomic_set(&cap_subs
->state
, STATE_STOPPED
);
837 usx2y_urbs_release(cap_subs
);
840 playback_subs
= subs
->usx2y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
841 if (atomic_read(&playback_subs
->state
) < STATE_PREPARED
) {
842 atomic_set(&subs
->state
, STATE_STOPPED
);
843 usx2y_urbs_release(subs
);
846 mutex_unlock(&subs
->usx2y
->pcm_mutex
);
853 * set format and initialize urbs
855 static int snd_usx2y_pcm_prepare(struct snd_pcm_substream
*substream
)
857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
858 struct snd_usx2y_substream
*subs
= runtime
->private_data
;
859 struct usx2ydev
*usx2y
= subs
->usx2y
;
860 struct snd_usx2y_substream
*capsubs
= subs
->usx2y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
863 dev_dbg(&usx2y
->dev
->dev
, "%s(%p)\n", __func__
, substream
);
865 mutex_lock(&usx2y
->pcm_mutex
);
866 usx2y_subs_prepare(subs
);
867 // Start hardware streams
868 // SyncStream first....
869 if (atomic_read(&capsubs
->state
) < STATE_PREPARED
) {
870 if (usx2y
->format
!= runtime
->format
) {
871 err
= usx2y_format_set(usx2y
, runtime
->format
);
873 goto up_prepare_mutex
;
875 if (usx2y
->rate
!= runtime
->rate
) {
876 err
= usx2y_rate_set(usx2y
, runtime
->rate
);
878 goto up_prepare_mutex
;
880 dev_dbg(&usx2y
->dev
->dev
, "%s: starting capture pipe for %s\n",
881 __func__
, subs
== capsubs
? "self" : "playpipe");
882 err
= usx2y_urbs_start(capsubs
);
884 goto up_prepare_mutex
;
887 if (subs
!= capsubs
&& atomic_read(&subs
->state
) < STATE_PREPARED
)
888 err
= usx2y_urbs_start(subs
);
891 mutex_unlock(&usx2y
->pcm_mutex
);
895 static const struct snd_pcm_hardware snd_usx2y_2c
= {
896 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
897 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
898 SNDRV_PCM_INFO_MMAP_VALID
|
899 SNDRV_PCM_INFO_BATCH
),
900 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
,
901 .rates
= SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
906 .buffer_bytes_max
= (2*128*1024),
907 .period_bytes_min
= 64,
908 .period_bytes_max
= (128*1024),
914 static int snd_usx2y_pcm_open(struct snd_pcm_substream
*substream
)
916 struct snd_usx2y_substream
*subs
=
917 ((struct snd_usx2y_substream
**)
918 snd_pcm_substream_chip(substream
))[substream
->stream
];
919 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
921 if (subs
->usx2y
->chip_status
& USX2Y_STAT_CHIP_MMAP_PCM_URBS
)
924 runtime
->hw
= snd_usx2y_2c
;
925 runtime
->private_data
= subs
;
926 subs
->pcm_substream
= substream
;
927 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, 1000, 200000);
931 static int snd_usx2y_pcm_close(struct snd_pcm_substream
*substream
)
933 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
934 struct snd_usx2y_substream
*subs
= runtime
->private_data
;
936 subs
->pcm_substream
= NULL
;
941 static const struct snd_pcm_ops snd_usx2y_pcm_ops
= {
942 .open
= snd_usx2y_pcm_open
,
943 .close
= snd_usx2y_pcm_close
,
944 .hw_params
= snd_usx2y_pcm_hw_params
,
945 .hw_free
= snd_usx2y_pcm_hw_free
,
946 .prepare
= snd_usx2y_pcm_prepare
,
947 .trigger
= snd_usx2y_pcm_trigger
,
948 .pointer
= snd_usx2y_pcm_pointer
,
952 * free a usb stream instance
954 static void usx2y_audio_stream_free(struct snd_usx2y_substream
**usx2y_substream
)
958 for_each_pcm_streams(stream
) {
959 kfree(usx2y_substream
[stream
]);
960 usx2y_substream
[stream
] = NULL
;
964 static void snd_usx2y_pcm_private_free(struct snd_pcm
*pcm
)
966 struct snd_usx2y_substream
**usx2y_stream
= pcm
->private_data
;
969 usx2y_audio_stream_free(usx2y_stream
);
972 static int usx2y_audio_stream_new(struct snd_card
*card
, int playback_endpoint
, int capture_endpoint
)
976 struct snd_usx2y_substream
**usx2y_substream
=
977 usx2y(card
)->subs
+ 2 * usx2y(card
)->pcm_devs
;
979 for (i
= playback_endpoint
? SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
980 i
<= SNDRV_PCM_STREAM_CAPTURE
; ++i
) {
981 usx2y_substream
[i
] = kzalloc(sizeof(struct snd_usx2y_substream
), GFP_KERNEL
);
982 if (!usx2y_substream
[i
])
985 usx2y_substream
[i
]->usx2y
= usx2y(card
);
988 if (playback_endpoint
)
989 usx2y_substream
[SNDRV_PCM_STREAM_PLAYBACK
]->endpoint
= playback_endpoint
;
990 usx2y_substream
[SNDRV_PCM_STREAM_CAPTURE
]->endpoint
= capture_endpoint
;
992 err
= snd_pcm_new(card
, NAME_ALLCAPS
" Audio", usx2y(card
)->pcm_devs
,
993 playback_endpoint
? 1 : 0, 1,
996 usx2y_audio_stream_free(usx2y_substream
);
1000 if (playback_endpoint
)
1001 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_usx2y_pcm_ops
);
1002 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_usx2y_pcm_ops
);
1004 pcm
->private_data
= usx2y_substream
;
1005 pcm
->private_free
= snd_usx2y_pcm_private_free
;
1006 pcm
->info_flags
= 0;
1008 sprintf(pcm
->name
, NAME_ALLCAPS
" Audio #%d", usx2y(card
)->pcm_devs
);
1010 if (playback_endpoint
) {
1011 snd_pcm_set_managed_buffer(pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
,
1012 SNDRV_DMA_TYPE_CONTINUOUS
,
1017 snd_pcm_set_managed_buffer(pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
,
1018 SNDRV_DMA_TYPE_CONTINUOUS
,
1021 usx2y(card
)->pcm_devs
++;
1027 * create a chip instance and set its names.
1029 int usx2y_audio_create(struct snd_card
*card
)
1033 err
= usx2y_audio_stream_new(card
, 0xA, 0x8);
1036 if (le16_to_cpu(usx2y(card
)->dev
->descriptor
.idProduct
) == USB_ID_US428
) {
1037 err
= usx2y_audio_stream_new(card
, 0, 0xA);
1041 if (le16_to_cpu(usx2y(card
)->dev
->descriptor
.idProduct
) != USB_ID_US122
)
1042 err
= usx2y_rate_set(usx2y(card
), 44100); // Lets us428 recognize output-volume settings, disturbs us122.