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 #define USX2Y_NRPACKS 4 /* 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,
39 smaller gives lower latencies.
41 #define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
42 nrpacks set to 1, you might as well comment
43 this #define out, and thereby produce smaller, faster code.
44 You'd also set USX2Y_NRPACKS to 1 then.
47 #ifdef USX2Y_NRPACKS_VARIABLE
48 static int nrpacks
= USX2Y_NRPACKS
; /* number of packets per urb */
49 #define nr_of_packs() nrpacks
50 module_param(nrpacks
, int, 0444);
51 MODULE_PARM_DESC(nrpacks
, "Number of packets per URB.");
53 #define nr_of_packs() USX2Y_NRPACKS
57 static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream
*subs
)
59 struct urb
*urb
= subs
->completed_urb
;
60 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
62 int i
, len
, lens
= 0, hwptr_done
= subs
->hwptr_done
;
63 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
65 for (i
= 0; i
< nr_of_packs(); i
++) {
66 cp
= (unsigned char*)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
67 if (urb
->iso_frame_desc
[i
].status
) { /* active? hmm, skip this */
68 snd_printk(KERN_ERR
"active frame status %i. "
69 "Most probably some hardware problem.\n",
70 urb
->iso_frame_desc
[i
].status
);
71 return urb
->iso_frame_desc
[i
].status
;
73 len
= urb
->iso_frame_desc
[i
].actual_length
/ usX2Y
->stride
;
75 snd_printd("0 == len ERROR!\n");
79 /* copy a data chunk */
80 if ((hwptr_done
+ len
) > runtime
->buffer_size
) {
81 int cnt
= runtime
->buffer_size
- hwptr_done
;
82 int blen
= cnt
* usX2Y
->stride
;
83 memcpy(runtime
->dma_area
+ hwptr_done
* usX2Y
->stride
, cp
, blen
);
84 memcpy(runtime
->dma_area
, cp
+ blen
, len
* usX2Y
->stride
- blen
);
86 memcpy(runtime
->dma_area
+ hwptr_done
* usX2Y
->stride
, cp
,
90 if ((hwptr_done
+= len
) >= runtime
->buffer_size
)
91 hwptr_done
-= runtime
->buffer_size
;
94 subs
->hwptr_done
= hwptr_done
;
95 subs
->transfer_done
+= lens
;
96 /* update the pointer, call callback if necessary */
97 if (subs
->transfer_done
>= runtime
->period_size
) {
98 subs
->transfer_done
-= runtime
->period_size
;
99 snd_pcm_period_elapsed(subs
->pcm_substream
);
104 * prepare urb for playback data pipe
106 * we copy the data directly from the pcm buffer.
107 * the current position to be copied is held in hwptr field.
108 * since a urb can handle only a single linear buffer, if the total
109 * transferred area overflows the buffer boundary, we cannot send
110 * it directly from the buffer. thus the data is once copied to
111 * a temporary buffer and urb points to that.
113 static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream
*subs
,
117 int count
, counts
, pack
;
118 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
119 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
122 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
123 /* calculate the size of a packet */
124 counts
= cap_urb
->iso_frame_desc
[pack
].actual_length
/ usX2Y
->stride
;
126 if (counts
< 43 || counts
> 50) {
127 snd_printk(KERN_ERR
"should not be here with counts=%i\n", counts
);
130 /* set up descriptor */
131 urb
->iso_frame_desc
[pack
].offset
= pack
?
132 urb
->iso_frame_desc
[pack
- 1].offset
+
133 urb
->iso_frame_desc
[pack
- 1].length
:
135 urb
->iso_frame_desc
[pack
].length
= cap_urb
->iso_frame_desc
[pack
].actual_length
;
137 if (atomic_read(&subs
->state
) >= state_PRERUNNING
)
138 if (subs
->hwptr
+ count
> runtime
->buffer_size
) {
139 /* err, the transferred area goes over buffer boundary.
140 * copy the data to the temp buffer.
143 len
= runtime
->buffer_size
- subs
->hwptr
;
144 urb
->transfer_buffer
= subs
->tmpbuf
;
145 memcpy(subs
->tmpbuf
, runtime
->dma_area
+
146 subs
->hwptr
* usX2Y
->stride
, len
* usX2Y
->stride
);
147 memcpy(subs
->tmpbuf
+ len
* usX2Y
->stride
,
148 runtime
->dma_area
, (count
- len
) * usX2Y
->stride
);
149 subs
->hwptr
+= count
;
150 subs
->hwptr
-= runtime
->buffer_size
;
152 /* set the buffer pointer */
153 urb
->transfer_buffer
= runtime
->dma_area
+ subs
->hwptr
* usX2Y
->stride
;
154 if ((subs
->hwptr
+= count
) >= runtime
->buffer_size
)
155 subs
->hwptr
-= runtime
->buffer_size
;
158 urb
->transfer_buffer
= subs
->tmpbuf
;
159 urb
->transfer_buffer_length
= count
* usX2Y
->stride
;
164 * process after playback data complete
166 * update the current position and call callback if a period is processed.
168 static void usX2Y_urb_play_retire(struct snd_usX2Y_substream
*subs
, struct urb
*urb
)
170 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
171 int len
= urb
->actual_length
/ subs
->usX2Y
->stride
;
173 subs
->transfer_done
+= len
;
174 subs
->hwptr_done
+= len
;
175 if (subs
->hwptr_done
>= runtime
->buffer_size
)
176 subs
->hwptr_done
-= runtime
->buffer_size
;
177 if (subs
->transfer_done
>= runtime
->period_size
) {
178 subs
->transfer_done
-= runtime
->period_size
;
179 snd_pcm_period_elapsed(subs
->pcm_substream
);
183 static int usX2Y_urb_submit(struct snd_usX2Y_substream
*subs
, struct urb
*urb
, int frame
)
188 urb
->start_frame
= (frame
+ NRURBS
* nr_of_packs()); // let hcd do rollover sanity checks
190 urb
->dev
= subs
->usX2Y
->dev
; /* we need to set this at each time */
191 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
192 snd_printk(KERN_ERR
"usb_submit_urb() returned %i\n", err
);
198 static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream
*capsubs
,
199 struct snd_usX2Y_substream
*playbacksubs
,
203 struct urb
*urb
= playbacksubs
->completed_urb
;
205 state
= atomic_read(&playbacksubs
->state
);
207 if (state
== state_RUNNING
)
208 usX2Y_urb_play_retire(playbacksubs
, urb
);
209 else if (state
>= state_PRERUNNING
)
210 atomic_inc(&playbacksubs
->state
);
213 case state_STARTING1
:
214 urb
= playbacksubs
->urb
[0];
215 atomic_inc(&playbacksubs
->state
);
217 case state_STARTING2
:
218 urb
= playbacksubs
->urb
[1];
219 atomic_inc(&playbacksubs
->state
);
224 if ((err
= usX2Y_urb_play_prepare(playbacksubs
, capsubs
->completed_urb
, urb
)) ||
225 (err
= usX2Y_urb_submit(playbacksubs
, urb
, frame
))) {
230 playbacksubs
->completed_urb
= NULL
;
232 state
= atomic_read(&capsubs
->state
);
233 if (state
>= state_PREPARED
) {
234 if (state
== state_RUNNING
) {
235 if ((err
= usX2Y_urb_capt_retire(capsubs
)))
237 } else if (state
>= state_PRERUNNING
)
238 atomic_inc(&capsubs
->state
);
239 if ((err
= usX2Y_urb_submit(capsubs
, capsubs
->completed_urb
, frame
)))
242 capsubs
->completed_urb
= NULL
;
247 static void usX2Y_clients_stop(struct usX2Ydev
*usX2Y
)
251 for (s
= 0; s
< 4; s
++) {
252 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
254 snd_printdd("%i %p state=%i\n", s
, subs
, atomic_read(&subs
->state
));
255 atomic_set(&subs
->state
, state_STOPPED
);
258 for (s
= 0; s
< 4; s
++) {
259 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
261 if (atomic_read(&subs
->state
) >= state_PRERUNNING
)
262 snd_pcm_stop_xrun(subs
->pcm_substream
);
263 for (u
= 0; u
< NRURBS
; u
++) {
264 struct urb
*urb
= subs
->urb
[u
];
266 snd_printdd("%i status=%i start_frame=%i\n",
267 u
, urb
->status
, urb
->start_frame
);
271 usX2Y
->prepare_subs
= NULL
;
272 wake_up(&usX2Y
->prepare_wait_queue
);
275 static void usX2Y_error_urb_status(struct usX2Ydev
*usX2Y
,
276 struct snd_usX2Y_substream
*subs
, struct urb
*urb
)
278 snd_printk(KERN_ERR
"ep=%i stalled with status=%i\n", subs
->endpoint
, urb
->status
);
280 usX2Y_clients_stop(usX2Y
);
283 static void i_usX2Y_urb_complete(struct urb
*urb
)
285 struct snd_usX2Y_substream
*subs
= urb
->context
;
286 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
288 if (unlikely(atomic_read(&subs
->state
) < state_PREPARED
)) {
289 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
290 usb_get_current_frame_number(usX2Y
->dev
),
291 subs
->endpoint
, usb_pipein(urb
->pipe
) ? "in" : "out",
292 urb
->status
, urb
->start_frame
);
295 if (unlikely(urb
->status
)) {
296 usX2Y_error_urb_status(usX2Y
, subs
, urb
);
300 subs
->completed_urb
= urb
;
303 struct snd_usX2Y_substream
*capsubs
= usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
],
304 *playbacksubs
= usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
305 if (capsubs
->completed_urb
&&
306 atomic_read(&capsubs
->state
) >= state_PREPARED
&&
307 (playbacksubs
->completed_urb
||
308 atomic_read(&playbacksubs
->state
) < state_PREPARED
)) {
309 if (!usX2Y_usbframe_complete(capsubs
, playbacksubs
, urb
->start_frame
))
310 usX2Y
->wait_iso_frame
+= nr_of_packs();
313 usX2Y_clients_stop(usX2Y
);
319 static void usX2Y_urbs_set_complete(struct usX2Ydev
* usX2Y
,
320 void (*complete
)(struct urb
*))
323 for (s
= 0; s
< 4; s
++) {
324 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
326 for (u
= 0; u
< NRURBS
; u
++) {
327 struct urb
* urb
= subs
->urb
[u
];
329 urb
->complete
= complete
;
334 static void usX2Y_subs_startup_finish(struct usX2Ydev
* usX2Y
)
336 usX2Y_urbs_set_complete(usX2Y
, i_usX2Y_urb_complete
);
337 usX2Y
->prepare_subs
= NULL
;
340 static void i_usX2Y_subs_startup(struct urb
*urb
)
342 struct snd_usX2Y_substream
*subs
= urb
->context
;
343 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
344 struct snd_usX2Y_substream
*prepare_subs
= usX2Y
->prepare_subs
;
345 if (NULL
!= prepare_subs
)
346 if (urb
->start_frame
== prepare_subs
->urb
[0]->start_frame
) {
347 usX2Y_subs_startup_finish(usX2Y
);
348 atomic_inc(&prepare_subs
->state
);
349 wake_up(&usX2Y
->prepare_wait_queue
);
352 i_usX2Y_urb_complete(urb
);
355 static void usX2Y_subs_prepare(struct snd_usX2Y_substream
*subs
)
357 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
358 subs
, subs
->endpoint
, subs
->urb
[0], subs
->urb
[1]);
359 /* reset the pointer */
361 subs
->hwptr_done
= 0;
362 subs
->transfer_done
= 0;
366 static void usX2Y_urb_release(struct urb
**urb
, int free_tb
)
371 kfree((*urb
)->transfer_buffer
);
377 * release a substreams urbs
379 static void usX2Y_urbs_release(struct snd_usX2Y_substream
*subs
)
382 snd_printdd("usX2Y_urbs_release() %i\n", subs
->endpoint
);
383 for (i
= 0; i
< NRURBS
; i
++)
384 usX2Y_urb_release(subs
->urb
+ i
,
385 subs
!= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
]);
391 * initialize a substream's urbs
393 static int usX2Y_urbs_allocate(struct snd_usX2Y_substream
*subs
)
397 int is_playback
= subs
== subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
398 struct usb_device
*dev
= subs
->usX2Y
->dev
;
400 pipe
= is_playback
? usb_sndisocpipe(dev
, subs
->endpoint
) :
401 usb_rcvisocpipe(dev
, subs
->endpoint
);
402 subs
->maxpacksize
= usb_maxpacket(dev
, pipe
, is_playback
);
403 if (!subs
->maxpacksize
)
406 if (is_playback
&& NULL
== subs
->tmpbuf
) { /* allocate a temporary buffer for playback */
407 subs
->tmpbuf
= kcalloc(nr_of_packs(), subs
->maxpacksize
, GFP_KERNEL
);
411 /* allocate and initialize data urbs */
412 for (i
= 0; i
< NRURBS
; i
++) {
413 struct urb
**purb
= subs
->urb
+ i
;
418 *purb
= usb_alloc_urb(nr_of_packs(), GFP_KERNEL
);
420 usX2Y_urbs_release(subs
);
423 if (!is_playback
&& !(*purb
)->transfer_buffer
) {
424 /* allocate a capture buffer per urb */
425 (*purb
)->transfer_buffer
=
426 kmalloc_array(subs
->maxpacksize
,
427 nr_of_packs(), GFP_KERNEL
);
428 if (NULL
== (*purb
)->transfer_buffer
) {
429 usX2Y_urbs_release(subs
);
434 (*purb
)->pipe
= pipe
;
435 (*purb
)->number_of_packets
= nr_of_packs();
436 (*purb
)->context
= subs
;
437 (*purb
)->interval
= 1;
438 (*purb
)->complete
= i_usX2Y_subs_startup
;
443 static void usX2Y_subs_startup(struct snd_usX2Y_substream
*subs
)
445 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
446 usX2Y
->prepare_subs
= subs
;
447 subs
->urb
[0]->start_frame
= -1;
449 usX2Y_urbs_set_complete(usX2Y
, i_usX2Y_subs_startup
);
452 static int usX2Y_urbs_start(struct snd_usX2Y_substream
*subs
)
455 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
457 if ((err
= usX2Y_urbs_allocate(subs
)) < 0)
459 subs
->completed_urb
= NULL
;
460 for (i
= 0; i
< 4; i
++) {
461 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[i
];
462 if (subs
!= NULL
&& atomic_read(&subs
->state
) >= state_PREPARED
)
467 usX2Y_subs_startup(subs
);
468 for (i
= 0; i
< NRURBS
; i
++) {
469 struct urb
*urb
= subs
->urb
[i
];
470 if (usb_pipein(urb
->pipe
)) {
473 atomic_set(&subs
->state
, state_STARTING3
);
474 urb
->dev
= usX2Y
->dev
;
475 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
476 urb
->iso_frame_desc
[pack
].offset
= subs
->maxpacksize
* pack
;
477 urb
->iso_frame_desc
[pack
].length
= subs
->maxpacksize
;
479 urb
->transfer_buffer_length
= subs
->maxpacksize
* nr_of_packs();
480 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
481 snd_printk (KERN_ERR
"cannot submit datapipe for urb %d, err = %d\n", i
, err
);
486 usX2Y
->wait_iso_frame
= urb
->start_frame
;
487 urb
->transfer_flags
= 0;
489 atomic_set(&subs
->state
, state_STARTING1
);
494 wait_event(usX2Y
->prepare_wait_queue
, NULL
== usX2Y
->prepare_subs
);
495 if (atomic_read(&subs
->state
) != state_PREPARED
)
500 usX2Y_subs_startup_finish(usX2Y
);
501 usX2Y_clients_stop(usX2Y
); // something is completely wroong > stop evrything
507 * return the current pcm pointer. just return the hwptr_done value.
509 static snd_pcm_uframes_t
snd_usX2Y_pcm_pointer(struct snd_pcm_substream
*substream
)
511 struct snd_usX2Y_substream
*subs
= substream
->runtime
->private_data
;
512 return subs
->hwptr_done
;
515 * start/stop substream
517 static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
519 struct snd_usX2Y_substream
*subs
= substream
->runtime
->private_data
;
522 case SNDRV_PCM_TRIGGER_START
:
523 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
524 if (atomic_read(&subs
->state
) == state_PREPARED
&&
525 atomic_read(&subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
]->state
) >= state_PREPARED
) {
526 atomic_set(&subs
->state
, state_PRERUNNING
);
532 case SNDRV_PCM_TRIGGER_STOP
:
533 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
534 if (atomic_read(&subs
->state
) >= state_PRERUNNING
)
535 atomic_set(&subs
->state
, state_PREPARED
);
545 * allocate a buffer, setup samplerate
547 * so far we use a physically linear buffer although packetize transfer
548 * doesn't need a continuous area.
549 * if sg buffer is supported on the later version of alsa, we'll follow
552 static const struct s_c2
558 { 0x14, 0x08}, // this line sets 44100, well actually a little less
559 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
592 static const struct s_c2 SetRate48000
[] =
594 { 0x14, 0x09}, // this line sets 48000, well actually a little less
595 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
628 #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
630 static void i_usX2Y_04Int(struct urb
*urb
)
632 struct usX2Ydev
*usX2Y
= urb
->context
;
635 snd_printk(KERN_ERR
"snd_usX2Y_04Int() urb->status=%i\n", urb
->status
);
636 if (0 == --usX2Y
->US04
->len
)
637 wake_up(&usX2Y
->In04WaitQueue
);
640 static int usX2Y_rate_set(struct usX2Ydev
*usX2Y
, int rate
)
643 struct snd_usX2Y_urbSeq
*us
= NULL
;
645 const struct s_c2
*ra
= rate
== 48000 ? SetRate48000
: SetRate44100
;
647 if (usX2Y
->rate
!= rate
) {
648 us
= kzalloc(sizeof(*us
) + sizeof(struct urb
*) * NOOF_SETRATE_URBS
, GFP_KERNEL
);
653 usbdata
= kmalloc_array(NOOF_SETRATE_URBS
, sizeof(int),
655 if (NULL
== usbdata
) {
659 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
660 if (NULL
== (us
->urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
))) {
664 ((char*)(usbdata
+ i
))[0] = ra
[i
].c1
;
665 ((char*)(usbdata
+ i
))[1] = ra
[i
].c2
;
666 usb_fill_bulk_urb(us
->urb
[i
], usX2Y
->dev
, usb_sndbulkpipe(usX2Y
->dev
, 4),
667 usbdata
+ i
, 2, i_usX2Y_04Int
, usX2Y
);
669 err
= usb_urb_ep_type_check(us
->urb
[0]);
673 us
->len
= NOOF_SETRATE_URBS
;
675 wait_event_timeout(usX2Y
->In04WaitQueue
, 0 == us
->len
, HZ
);
681 us
->submitted
= 2*NOOF_SETRATE_URBS
;
682 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
683 struct urb
*urb
= us
->urb
[i
];
703 static int usX2Y_format_set(struct usX2Ydev
*usX2Y
, snd_pcm_format_t format
)
707 if (format
== SNDRV_PCM_FORMAT_S24_3LE
) {
714 list_for_each(p
, &usX2Y
->midi_list
) {
715 snd_usbmidi_input_stop(p
);
717 usb_kill_urb(usX2Y
->In04urb
);
718 if ((err
= usb_set_interface(usX2Y
->dev
, 0, alternate
))) {
719 snd_printk(KERN_ERR
"usb_set_interface error \n");
722 usX2Y
->In04urb
->dev
= usX2Y
->dev
;
723 err
= usb_submit_urb(usX2Y
->In04urb
, GFP_KERNEL
);
724 list_for_each(p
, &usX2Y
->midi_list
) {
725 snd_usbmidi_input_start(p
);
727 usX2Y
->format
= format
;
733 static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream
*substream
,
734 struct snd_pcm_hw_params
*hw_params
)
737 unsigned int rate
= params_rate(hw_params
);
738 snd_pcm_format_t format
= params_format(hw_params
);
739 struct snd_card
*card
= substream
->pstr
->pcm
->card
;
740 struct usX2Ydev
*dev
= usX2Y(card
);
743 mutex_lock(&usX2Y(card
)->pcm_mutex
);
744 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream
, hw_params
);
745 /* all pcm substreams off one usX2Y have to operate at the same
748 for (i
= 0; i
< dev
->pcm_devs
* 2; i
++) {
749 struct snd_usX2Y_substream
*subs
= dev
->subs
[i
];
750 struct snd_pcm_substream
*test_substream
;
754 test_substream
= subs
->pcm_substream
;
755 if (!test_substream
|| test_substream
== substream
||
756 !test_substream
->runtime
)
758 if ((test_substream
->runtime
->format
&&
759 test_substream
->runtime
->format
!= format
) ||
760 (test_substream
->runtime
->rate
&&
761 test_substream
->runtime
->rate
!= rate
)) {
768 mutex_unlock(&usX2Y(card
)->pcm_mutex
);
775 static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream
*substream
)
777 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
778 struct snd_usX2Y_substream
*subs
= runtime
->private_data
;
779 mutex_lock(&subs
->usX2Y
->pcm_mutex
);
780 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream
);
782 if (SNDRV_PCM_STREAM_PLAYBACK
== substream
->stream
) {
783 struct snd_usX2Y_substream
*cap_subs
= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
784 atomic_set(&subs
->state
, state_STOPPED
);
785 usX2Y_urbs_release(subs
);
786 if (!cap_subs
->pcm_substream
||
787 !cap_subs
->pcm_substream
->runtime
||
788 !cap_subs
->pcm_substream
->runtime
->status
||
789 cap_subs
->pcm_substream
->runtime
->status
->state
< SNDRV_PCM_STATE_PREPARED
) {
790 atomic_set(&cap_subs
->state
, state_STOPPED
);
791 usX2Y_urbs_release(cap_subs
);
794 struct snd_usX2Y_substream
*playback_subs
= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
795 if (atomic_read(&playback_subs
->state
) < state_PREPARED
) {
796 atomic_set(&subs
->state
, state_STOPPED
);
797 usX2Y_urbs_release(subs
);
800 mutex_unlock(&subs
->usX2Y
->pcm_mutex
);
806 * set format and initialize urbs
808 static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream
*substream
)
810 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
811 struct snd_usX2Y_substream
*subs
= runtime
->private_data
;
812 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
813 struct snd_usX2Y_substream
*capsubs
= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
815 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream
);
817 mutex_lock(&usX2Y
->pcm_mutex
);
818 usX2Y_subs_prepare(subs
);
819 // Start hardware streams
820 // SyncStream first....
821 if (atomic_read(&capsubs
->state
) < state_PREPARED
) {
822 if (usX2Y
->format
!= runtime
->format
)
823 if ((err
= usX2Y_format_set(usX2Y
, runtime
->format
)) < 0)
824 goto up_prepare_mutex
;
825 if (usX2Y
->rate
!= runtime
->rate
)
826 if ((err
= usX2Y_rate_set(usX2Y
, runtime
->rate
)) < 0)
827 goto up_prepare_mutex
;
828 snd_printdd("starting capture pipe for %s\n", subs
== capsubs
? "self" : "playpipe");
829 if (0 > (err
= usX2Y_urbs_start(capsubs
)))
830 goto up_prepare_mutex
;
833 if (subs
!= capsubs
&& atomic_read(&subs
->state
) < state_PREPARED
)
834 err
= usX2Y_urbs_start(subs
);
837 mutex_unlock(&usX2Y
->pcm_mutex
);
841 static const struct snd_pcm_hardware snd_usX2Y_2c
=
843 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
844 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
845 SNDRV_PCM_INFO_MMAP_VALID
|
846 SNDRV_PCM_INFO_BATCH
),
847 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
,
848 .rates
= SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
853 .buffer_bytes_max
= (2*128*1024),
854 .period_bytes_min
= 64,
855 .period_bytes_max
= (128*1024),
863 static int snd_usX2Y_pcm_open(struct snd_pcm_substream
*substream
)
865 struct snd_usX2Y_substream
*subs
= ((struct snd_usX2Y_substream
**)
866 snd_pcm_substream_chip(substream
))[substream
->stream
];
867 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
869 if (subs
->usX2Y
->chip_status
& USX2Y_STAT_CHIP_MMAP_PCM_URBS
)
872 runtime
->hw
= snd_usX2Y_2c
;
873 runtime
->private_data
= subs
;
874 subs
->pcm_substream
= substream
;
875 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, 1000, 200000);
881 static int snd_usX2Y_pcm_close(struct snd_pcm_substream
*substream
)
883 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
884 struct snd_usX2Y_substream
*subs
= runtime
->private_data
;
886 subs
->pcm_substream
= NULL
;
892 static const struct snd_pcm_ops snd_usX2Y_pcm_ops
=
894 .open
= snd_usX2Y_pcm_open
,
895 .close
= snd_usX2Y_pcm_close
,
896 .hw_params
= snd_usX2Y_pcm_hw_params
,
897 .hw_free
= snd_usX2Y_pcm_hw_free
,
898 .prepare
= snd_usX2Y_pcm_prepare
,
899 .trigger
= snd_usX2Y_pcm_trigger
,
900 .pointer
= snd_usX2Y_pcm_pointer
,
905 * free a usb stream instance
907 static void usX2Y_audio_stream_free(struct snd_usX2Y_substream
**usX2Y_substream
)
911 for_each_pcm_streams(stream
) {
912 kfree(usX2Y_substream
[stream
]);
913 usX2Y_substream
[stream
] = NULL
;
917 static void snd_usX2Y_pcm_private_free(struct snd_pcm
*pcm
)
919 struct snd_usX2Y_substream
**usX2Y_stream
= pcm
->private_data
;
921 usX2Y_audio_stream_free(usX2Y_stream
);
924 static int usX2Y_audio_stream_new(struct snd_card
*card
, int playback_endpoint
, int capture_endpoint
)
928 struct snd_usX2Y_substream
**usX2Y_substream
=
929 usX2Y(card
)->subs
+ 2 * usX2Y(card
)->pcm_devs
;
931 for (i
= playback_endpoint
? SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
932 i
<= SNDRV_PCM_STREAM_CAPTURE
; ++i
) {
933 usX2Y_substream
[i
] = kzalloc(sizeof(struct snd_usX2Y_substream
), GFP_KERNEL
);
934 if (!usX2Y_substream
[i
])
937 usX2Y_substream
[i
]->usX2Y
= usX2Y(card
);
940 if (playback_endpoint
)
941 usX2Y_substream
[SNDRV_PCM_STREAM_PLAYBACK
]->endpoint
= playback_endpoint
;
942 usX2Y_substream
[SNDRV_PCM_STREAM_CAPTURE
]->endpoint
= capture_endpoint
;
944 err
= snd_pcm_new(card
, NAME_ALLCAPS
" Audio", usX2Y(card
)->pcm_devs
,
945 playback_endpoint
? 1 : 0, 1,
948 usX2Y_audio_stream_free(usX2Y_substream
);
952 if (playback_endpoint
)
953 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_usX2Y_pcm_ops
);
954 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_usX2Y_pcm_ops
);
956 pcm
->private_data
= usX2Y_substream
;
957 pcm
->private_free
= snd_usX2Y_pcm_private_free
;
960 sprintf(pcm
->name
, NAME_ALLCAPS
" Audio #%d", usX2Y(card
)->pcm_devs
);
962 if (playback_endpoint
) {
963 snd_pcm_set_managed_buffer(pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
,
964 SNDRV_DMA_TYPE_CONTINUOUS
,
969 snd_pcm_set_managed_buffer(pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
,
970 SNDRV_DMA_TYPE_CONTINUOUS
,
973 usX2Y(card
)->pcm_devs
++;
979 * create a chip instance and set its names.
981 int usX2Y_audio_create(struct snd_card
*card
)
985 INIT_LIST_HEAD(&usX2Y(card
)->pcm_list
);
987 if (0 > (err
= usX2Y_audio_stream_new(card
, 0xA, 0x8)))
989 if (le16_to_cpu(usX2Y(card
)->dev
->descriptor
.idProduct
) == USB_ID_US428
)
990 if (0 > (err
= usX2Y_audio_stream_new(card
, 0, 0xA)))
992 if (le16_to_cpu(usX2Y(card
)->dev
->descriptor
.idProduct
) != USB_ID_US122
)
993 err
= usX2Y_rate_set(usX2Y(card
), 44100); // Lets us428 recognize output-volume settings, disturbs us122.