3 * Copyright (c) 2002-2004 by Karsten Wiese
7 * (Tentative) USB Audio Driver for ALSA
11 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
13 * Many codes borrowed from audio.c by
14 * Alan Cox (alan@lxorguk.ukuu.org.uk)
15 * Thomas Sailer (sailer@ife.ee.ethz.ch)
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/interrupt.h>
35 #include <linux/slab.h>
36 #include <linux/usb.h>
37 #include <linux/moduleparam.h>
38 #include <sound/core.h>
39 #include <sound/info.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
45 #define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
46 1 to 4 have been tested ok on uhci.
47 To use 3 on ohci, you'd need a patch:
48 look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
49 "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
51 1, 2 and 4 work out of the box on ohci, if I recall correctly.
52 Bigger is safer operation,
53 smaller gives lower latencies.
55 #define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
56 nrpacks set to 1, you might as well comment
57 this #define out, and thereby produce smaller, faster code.
58 You'd also set USX2Y_NRPACKS to 1 then.
61 #ifdef USX2Y_NRPACKS_VARIABLE
62 static int nrpacks
= USX2Y_NRPACKS
; /* number of packets per urb */
63 #define nr_of_packs() nrpacks
64 module_param(nrpacks
, int, 0444);
65 MODULE_PARM_DESC(nrpacks
, "Number of packets per URB.");
67 #define nr_of_packs() USX2Y_NRPACKS
71 static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream
*subs
)
73 struct urb
*urb
= subs
->completed_urb
;
74 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
76 int i
, len
, lens
= 0, hwptr_done
= subs
->hwptr_done
;
77 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
79 for (i
= 0; i
< nr_of_packs(); i
++) {
80 cp
= (unsigned char*)urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
81 if (urb
->iso_frame_desc
[i
].status
) { /* active? hmm, skip this */
82 snd_printk(KERN_ERR
"active frame status %i. "
83 "Most probably some hardware problem.\n",
84 urb
->iso_frame_desc
[i
].status
);
85 return urb
->iso_frame_desc
[i
].status
;
87 len
= urb
->iso_frame_desc
[i
].actual_length
/ usX2Y
->stride
;
89 snd_printd("0 == len ERROR!\n");
93 /* copy a data chunk */
94 if ((hwptr_done
+ len
) > runtime
->buffer_size
) {
95 int cnt
= runtime
->buffer_size
- hwptr_done
;
96 int blen
= cnt
* usX2Y
->stride
;
97 memcpy(runtime
->dma_area
+ hwptr_done
* usX2Y
->stride
, cp
, blen
);
98 memcpy(runtime
->dma_area
, cp
+ blen
, len
* usX2Y
->stride
- blen
);
100 memcpy(runtime
->dma_area
+ hwptr_done
* usX2Y
->stride
, cp
,
101 len
* usX2Y
->stride
);
104 if ((hwptr_done
+= len
) >= runtime
->buffer_size
)
105 hwptr_done
-= runtime
->buffer_size
;
108 subs
->hwptr_done
= hwptr_done
;
109 subs
->transfer_done
+= lens
;
110 /* update the pointer, call callback if necessary */
111 if (subs
->transfer_done
>= runtime
->period_size
) {
112 subs
->transfer_done
-= runtime
->period_size
;
113 snd_pcm_period_elapsed(subs
->pcm_substream
);
118 * prepare urb for playback data pipe
120 * we copy the data directly from the pcm buffer.
121 * the current position to be copied is held in hwptr field.
122 * since a urb can handle only a single linear buffer, if the total
123 * transferred area overflows the buffer boundary, we cannot send
124 * it directly from the buffer. thus the data is once copied to
125 * a temporary buffer and urb points to that.
127 static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream
*subs
,
131 int count
, counts
, pack
;
132 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
133 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
136 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
137 /* calculate the size of a packet */
138 counts
= cap_urb
->iso_frame_desc
[pack
].actual_length
/ usX2Y
->stride
;
140 if (counts
< 43 || counts
> 50) {
141 snd_printk(KERN_ERR
"should not be here with counts=%i\n", counts
);
144 /* set up descriptor */
145 urb
->iso_frame_desc
[pack
].offset
= pack
?
146 urb
->iso_frame_desc
[pack
- 1].offset
+
147 urb
->iso_frame_desc
[pack
- 1].length
:
149 urb
->iso_frame_desc
[pack
].length
= cap_urb
->iso_frame_desc
[pack
].actual_length
;
151 if (atomic_read(&subs
->state
) >= state_PRERUNNING
)
152 if (subs
->hwptr
+ count
> runtime
->buffer_size
) {
153 /* err, the transferred area goes over buffer boundary.
154 * copy the data to the temp buffer.
157 len
= runtime
->buffer_size
- subs
->hwptr
;
158 urb
->transfer_buffer
= subs
->tmpbuf
;
159 memcpy(subs
->tmpbuf
, runtime
->dma_area
+
160 subs
->hwptr
* usX2Y
->stride
, len
* usX2Y
->stride
);
161 memcpy(subs
->tmpbuf
+ len
* usX2Y
->stride
,
162 runtime
->dma_area
, (count
- len
) * usX2Y
->stride
);
163 subs
->hwptr
+= count
;
164 subs
->hwptr
-= runtime
->buffer_size
;
166 /* set the buffer pointer */
167 urb
->transfer_buffer
= runtime
->dma_area
+ subs
->hwptr
* usX2Y
->stride
;
168 if ((subs
->hwptr
+= count
) >= runtime
->buffer_size
)
169 subs
->hwptr
-= runtime
->buffer_size
;
172 urb
->transfer_buffer
= subs
->tmpbuf
;
173 urb
->transfer_buffer_length
= count
* usX2Y
->stride
;
178 * process after playback data complete
180 * update the current position and call callback if a period is processed.
182 static void usX2Y_urb_play_retire(struct snd_usX2Y_substream
*subs
, struct urb
*urb
)
184 struct snd_pcm_runtime
*runtime
= subs
->pcm_substream
->runtime
;
185 int len
= urb
->actual_length
/ subs
->usX2Y
->stride
;
187 subs
->transfer_done
+= len
;
188 subs
->hwptr_done
+= len
;
189 if (subs
->hwptr_done
>= runtime
->buffer_size
)
190 subs
->hwptr_done
-= runtime
->buffer_size
;
191 if (subs
->transfer_done
>= runtime
->period_size
) {
192 subs
->transfer_done
-= runtime
->period_size
;
193 snd_pcm_period_elapsed(subs
->pcm_substream
);
197 static int usX2Y_urb_submit(struct snd_usX2Y_substream
*subs
, struct urb
*urb
, int frame
)
202 urb
->start_frame
= (frame
+ NRURBS
* nr_of_packs()); // let hcd do rollover sanity checks
204 urb
->dev
= subs
->usX2Y
->dev
; /* we need to set this at each time */
205 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
206 snd_printk(KERN_ERR
"usb_submit_urb() returned %i\n", err
);
212 static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream
*capsubs
,
213 struct snd_usX2Y_substream
*playbacksubs
,
217 struct urb
*urb
= playbacksubs
->completed_urb
;
219 state
= atomic_read(&playbacksubs
->state
);
221 if (state
== state_RUNNING
)
222 usX2Y_urb_play_retire(playbacksubs
, urb
);
223 else if (state
>= state_PRERUNNING
)
224 atomic_inc(&playbacksubs
->state
);
227 case state_STARTING1
:
228 urb
= playbacksubs
->urb
[0];
229 atomic_inc(&playbacksubs
->state
);
231 case state_STARTING2
:
232 urb
= playbacksubs
->urb
[1];
233 atomic_inc(&playbacksubs
->state
);
238 if ((err
= usX2Y_urb_play_prepare(playbacksubs
, capsubs
->completed_urb
, urb
)) ||
239 (err
= usX2Y_urb_submit(playbacksubs
, urb
, frame
))) {
244 playbacksubs
->completed_urb
= NULL
;
246 state
= atomic_read(&capsubs
->state
);
247 if (state
>= state_PREPARED
) {
248 if (state
== state_RUNNING
) {
249 if ((err
= usX2Y_urb_capt_retire(capsubs
)))
251 } else if (state
>= state_PRERUNNING
)
252 atomic_inc(&capsubs
->state
);
253 if ((err
= usX2Y_urb_submit(capsubs
, capsubs
->completed_urb
, frame
)))
256 capsubs
->completed_urb
= NULL
;
261 static void usX2Y_clients_stop(struct usX2Ydev
*usX2Y
)
265 for (s
= 0; s
< 4; s
++) {
266 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
268 snd_printdd("%i %p state=%i\n", s
, subs
, atomic_read(&subs
->state
));
269 atomic_set(&subs
->state
, state_STOPPED
);
272 for (s
= 0; s
< 4; s
++) {
273 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
275 if (atomic_read(&subs
->state
) >= state_PRERUNNING
) {
278 snd_pcm_stream_lock_irqsave(subs
->pcm_substream
, flags
);
279 snd_pcm_stop(subs
->pcm_substream
, SNDRV_PCM_STATE_XRUN
);
280 snd_pcm_stream_unlock_irqrestore(subs
->pcm_substream
, flags
);
282 for (u
= 0; u
< NRURBS
; u
++) {
283 struct urb
*urb
= subs
->urb
[u
];
285 snd_printdd("%i status=%i start_frame=%i\n",
286 u
, urb
->status
, urb
->start_frame
);
290 usX2Y
->prepare_subs
= NULL
;
291 wake_up(&usX2Y
->prepare_wait_queue
);
294 static void usX2Y_error_urb_status(struct usX2Ydev
*usX2Y
,
295 struct snd_usX2Y_substream
*subs
, struct urb
*urb
)
297 snd_printk(KERN_ERR
"ep=%i stalled with status=%i\n", 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
;
307 if (unlikely(atomic_read(&subs
->state
) < state_PREPARED
)) {
308 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
309 usb_get_current_frame_number(usX2Y
->dev
),
310 subs
->endpoint
, usb_pipein(urb
->pipe
) ? "in" : "out",
311 urb
->status
, urb
->start_frame
);
314 if (unlikely(urb
->status
)) {
315 usX2Y_error_urb_status(usX2Y
, subs
, urb
);
319 subs
->completed_urb
= urb
;
322 struct snd_usX2Y_substream
*capsubs
= usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
],
323 *playbacksubs
= usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
324 if (capsubs
->completed_urb
&&
325 atomic_read(&capsubs
->state
) >= state_PREPARED
&&
326 (playbacksubs
->completed_urb
||
327 atomic_read(&playbacksubs
->state
) < state_PREPARED
)) {
328 if (!usX2Y_usbframe_complete(capsubs
, playbacksubs
, urb
->start_frame
))
329 usX2Y
->wait_iso_frame
+= nr_of_packs();
332 usX2Y_clients_stop(usX2Y
);
338 static void usX2Y_urbs_set_complete(struct usX2Ydev
* usX2Y
,
339 void (*complete
)(struct urb
*))
342 for (s
= 0; s
< 4; s
++) {
343 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[s
];
345 for (u
= 0; u
< NRURBS
; u
++) {
346 struct urb
* urb
= subs
->urb
[u
];
348 urb
->complete
= complete
;
353 static void usX2Y_subs_startup_finish(struct usX2Ydev
* usX2Y
)
355 usX2Y_urbs_set_complete(usX2Y
, i_usX2Y_urb_complete
);
356 usX2Y
->prepare_subs
= NULL
;
359 static void i_usX2Y_subs_startup(struct urb
*urb
)
361 struct snd_usX2Y_substream
*subs
= urb
->context
;
362 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
363 struct snd_usX2Y_substream
*prepare_subs
= usX2Y
->prepare_subs
;
364 if (NULL
!= prepare_subs
)
365 if (urb
->start_frame
== prepare_subs
->urb
[0]->start_frame
) {
366 usX2Y_subs_startup_finish(usX2Y
);
367 atomic_inc(&prepare_subs
->state
);
368 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;
385 static void usX2Y_urb_release(struct urb
**urb
, int free_tb
)
390 kfree((*urb
)->transfer_buffer
);
396 * release a substreams urbs
398 static void usX2Y_urbs_release(struct snd_usX2Y_substream
*subs
)
401 snd_printdd("usX2Y_urbs_release() %i\n", subs
->endpoint
);
402 for (i
= 0; i
< NRURBS
; i
++)
403 usX2Y_urb_release(subs
->urb
+ i
,
404 subs
!= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
]);
410 * initialize a substream's urbs
412 static int usX2Y_urbs_allocate(struct snd_usX2Y_substream
*subs
)
416 int is_playback
= subs
== subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_PLAYBACK
];
417 struct usb_device
*dev
= subs
->usX2Y
->dev
;
419 pipe
= is_playback
? usb_sndisocpipe(dev
, subs
->endpoint
) :
420 usb_rcvisocpipe(dev
, subs
->endpoint
);
421 subs
->maxpacksize
= usb_maxpacket(dev
, pipe
, is_playback
);
422 if (!subs
->maxpacksize
)
425 if (is_playback
&& NULL
== subs
->tmpbuf
) { /* allocate a temporary buffer for playback */
426 subs
->tmpbuf
= kcalloc(nr_of_packs(), subs
->maxpacksize
, GFP_KERNEL
);
427 if (NULL
== subs
->tmpbuf
) {
428 snd_printk(KERN_ERR
"cannot malloc tmpbuf\n");
432 /* allocate and initialize data urbs */
433 for (i
= 0; i
< NRURBS
; i
++) {
434 struct urb
**purb
= subs
->urb
+ i
;
439 *purb
= usb_alloc_urb(nr_of_packs(), GFP_KERNEL
);
441 usX2Y_urbs_release(subs
);
444 if (!is_playback
&& !(*purb
)->transfer_buffer
) {
445 /* allocate a capture buffer per urb */
446 (*purb
)->transfer_buffer
= kmalloc(subs
->maxpacksize
* nr_of_packs(), GFP_KERNEL
);
447 if (NULL
== (*purb
)->transfer_buffer
) {
448 usX2Y_urbs_release(subs
);
453 (*purb
)->pipe
= pipe
;
454 (*purb
)->number_of_packets
= nr_of_packs();
455 (*purb
)->context
= subs
;
456 (*purb
)->interval
= 1;
457 (*purb
)->complete
= i_usX2Y_subs_startup
;
462 static void usX2Y_subs_startup(struct snd_usX2Y_substream
*subs
)
464 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
465 usX2Y
->prepare_subs
= subs
;
466 subs
->urb
[0]->start_frame
= -1;
468 usX2Y_urbs_set_complete(usX2Y
, i_usX2Y_subs_startup
);
471 static int usX2Y_urbs_start(struct snd_usX2Y_substream
*subs
)
474 struct usX2Ydev
*usX2Y
= subs
->usX2Y
;
476 if ((err
= usX2Y_urbs_allocate(subs
)) < 0)
478 subs
->completed_urb
= NULL
;
479 for (i
= 0; i
< 4; i
++) {
480 struct snd_usX2Y_substream
*subs
= usX2Y
->subs
[i
];
481 if (subs
!= NULL
&& atomic_read(&subs
->state
) >= state_PREPARED
)
486 usX2Y_subs_startup(subs
);
487 for (i
= 0; i
< NRURBS
; i
++) {
488 struct urb
*urb
= subs
->urb
[i
];
489 if (usb_pipein(urb
->pipe
)) {
492 atomic_set(&subs
->state
, state_STARTING3
);
493 urb
->dev
= usX2Y
->dev
;
494 for (pack
= 0; pack
< nr_of_packs(); pack
++) {
495 urb
->iso_frame_desc
[pack
].offset
= subs
->maxpacksize
* pack
;
496 urb
->iso_frame_desc
[pack
].length
= subs
->maxpacksize
;
498 urb
->transfer_buffer_length
= subs
->maxpacksize
* nr_of_packs();
499 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) < 0) {
500 snd_printk (KERN_ERR
"cannot submit datapipe for urb %d, err = %d\n", i
, err
);
505 usX2Y
->wait_iso_frame
= urb
->start_frame
;
506 urb
->transfer_flags
= 0;
508 atomic_set(&subs
->state
, state_STARTING1
);
513 wait_event(usX2Y
->prepare_wait_queue
, NULL
== usX2Y
->prepare_subs
);
514 if (atomic_read(&subs
->state
) != state_PREPARED
)
519 usX2Y_subs_startup_finish(usX2Y
);
520 usX2Y_clients_stop(usX2Y
); // something is completely wroong > stop evrything
526 * return the current pcm pointer. just return the hwptr_done value.
528 static snd_pcm_uframes_t
snd_usX2Y_pcm_pointer(struct snd_pcm_substream
*substream
)
530 struct snd_usX2Y_substream
*subs
= substream
->runtime
->private_data
;
531 return subs
->hwptr_done
;
534 * start/stop substream
536 static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
538 struct snd_usX2Y_substream
*subs
= substream
->runtime
->private_data
;
541 case SNDRV_PCM_TRIGGER_START
:
542 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
543 if (atomic_read(&subs
->state
) == state_PREPARED
&&
544 atomic_read(&subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
]->state
) >= state_PREPARED
) {
545 atomic_set(&subs
->state
, state_PRERUNNING
);
551 case SNDRV_PCM_TRIGGER_STOP
:
552 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
553 if (atomic_read(&subs
->state
) >= state_PRERUNNING
)
554 atomic_set(&subs
->state
, state_PREPARED
);
564 * allocate a buffer, setup samplerate
566 * so far we use a physically linear buffer although packetize transfer
567 * doesn't need a continuous area.
568 * if sg buffer is supported on the later version of alsa, we'll follow
577 { 0x14, 0x08}, // this line sets 44100, well actually a little less
578 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
611 static struct s_c2 SetRate48000
[] =
613 { 0x14, 0x09}, // this line sets 48000, well actually a little less
614 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
647 #define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
649 static void i_usX2Y_04Int(struct urb
*urb
)
651 struct usX2Ydev
*usX2Y
= urb
->context
;
654 snd_printk(KERN_ERR
"snd_usX2Y_04Int() urb->status=%i\n", urb
->status
);
655 if (0 == --usX2Y
->US04
->len
)
656 wake_up(&usX2Y
->In04WaitQueue
);
659 static int usX2Y_rate_set(struct usX2Ydev
*usX2Y
, int rate
)
662 struct snd_usX2Y_urbSeq
*us
= NULL
;
664 struct s_c2
*ra
= rate
== 48000 ? SetRate48000
: SetRate44100
;
666 if (usX2Y
->rate
!= rate
) {
667 us
= kzalloc(sizeof(*us
) + sizeof(struct urb
*) * NOOF_SETRATE_URBS
, GFP_KERNEL
);
672 usbdata
= kmalloc(sizeof(int) * NOOF_SETRATE_URBS
, GFP_KERNEL
);
673 if (NULL
== usbdata
) {
677 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
678 if (NULL
== (us
->urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
))) {
682 ((char*)(usbdata
+ i
))[0] = ra
[i
].c1
;
683 ((char*)(usbdata
+ i
))[1] = ra
[i
].c2
;
684 usb_fill_bulk_urb(us
->urb
[i
], usX2Y
->dev
, usb_sndbulkpipe(usX2Y
->dev
, 4),
685 usbdata
+ i
, 2, i_usX2Y_04Int
, usX2Y
);
688 us
->len
= NOOF_SETRATE_URBS
;
690 wait_event_timeout(usX2Y
->In04WaitQueue
, 0 == us
->len
, HZ
);
696 us
->submitted
= 2*NOOF_SETRATE_URBS
;
697 for (i
= 0; i
< NOOF_SETRATE_URBS
; ++i
) {
698 struct urb
*urb
= us
->urb
[i
];
718 static int usX2Y_format_set(struct usX2Ydev
*usX2Y
, snd_pcm_format_t format
)
722 if (format
== SNDRV_PCM_FORMAT_S24_3LE
) {
729 list_for_each(p
, &usX2Y
->midi_list
) {
730 snd_usbmidi_input_stop(p
);
732 usb_kill_urb(usX2Y
->In04urb
);
733 if ((err
= usb_set_interface(usX2Y
->dev
, 0, alternate
))) {
734 snd_printk(KERN_ERR
"usb_set_interface error \n");
737 usX2Y
->In04urb
->dev
= usX2Y
->dev
;
738 err
= usb_submit_urb(usX2Y
->In04urb
, GFP_KERNEL
);
739 list_for_each(p
, &usX2Y
->midi_list
) {
740 snd_usbmidi_input_start(p
);
742 usX2Y
->format
= format
;
748 static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream
*substream
,
749 struct snd_pcm_hw_params
*hw_params
)
752 unsigned int rate
= params_rate(hw_params
);
753 snd_pcm_format_t format
= params_format(hw_params
);
754 struct snd_card
*card
= substream
->pstr
->pcm
->card
;
755 struct list_head
*list
;
757 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream
, hw_params
);
758 // all pcm substreams off one usX2Y have to operate at the same rate & format
759 list_for_each(list
, &card
->devices
) {
760 struct snd_device
*dev
;
763 dev
= snd_device(list
);
764 if (dev
->type
!= SNDRV_DEV_PCM
)
766 pcm
= dev
->device_data
;
767 for (s
= 0; s
< 2; ++s
) {
768 struct snd_pcm_substream
*test_substream
;
769 test_substream
= pcm
->streams
[s
].substream
;
770 if (test_substream
&& test_substream
!= substream
&&
771 test_substream
->runtime
&&
772 ((test_substream
->runtime
->format
&&
773 test_substream
->runtime
->format
!= format
) ||
774 (test_substream
->runtime
->rate
&&
775 test_substream
->runtime
->rate
!= rate
)))
779 if (0 > (err
= snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
)))) {
780 snd_printk(KERN_ERR
"snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
781 substream
, params_buffer_bytes(hw_params
), err
);
790 static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream
*substream
)
792 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
793 struct snd_usX2Y_substream
*subs
= runtime
->private_data
;
794 mutex_lock(&subs
->usX2Y
->prepare_mutex
);
795 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream
);
797 if (SNDRV_PCM_STREAM_PLAYBACK
== substream
->stream
) {
798 struct snd_usX2Y_substream
*cap_subs
= subs
->usX2Y
->subs
[SNDRV_PCM_STREAM_CAPTURE
];
799 atomic_set(&subs
->state
, state_STOPPED
);
800 usX2Y_urbs_release(subs
);
801 if (!cap_subs
->pcm_substream
||
802 !cap_subs
->pcm_substream
->runtime
||
803 !cap_subs
->pcm_substream
->runtime
->status
||
804 cap_subs
->pcm_substream
->runtime
->status
->state
< SNDRV_PCM_STATE_PREPARED
) {
805 atomic_set(&cap_subs
->state
, state_STOPPED
);
806 usX2Y_urbs_release(cap_subs
);
809 struct snd_usX2Y_substream
*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
);
815 mutex_unlock(&subs
->usX2Y
->prepare_mutex
);
816 return snd_pcm_lib_free_pages(substream
);
821 * set format and initialize urbs
823 static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream
*substream
)
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
];
830 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream
);
832 mutex_lock(&usX2Y
->prepare_mutex
);
833 usX2Y_subs_prepare(subs
);
834 // Start hardware streams
835 // SyncStream first....
836 if (atomic_read(&capsubs
->state
) < state_PREPARED
) {
837 if (usX2Y
->format
!= runtime
->format
)
838 if ((err
= usX2Y_format_set(usX2Y
, runtime
->format
)) < 0)
839 goto up_prepare_mutex
;
840 if (usX2Y
->rate
!= runtime
->rate
)
841 if ((err
= usX2Y_rate_set(usX2Y
, runtime
->rate
)) < 0)
842 goto up_prepare_mutex
;
843 snd_printdd("starting capture pipe for %s\n", subs
== capsubs
? "self" : "playpipe");
844 if (0 > (err
= usX2Y_urbs_start(capsubs
)))
845 goto up_prepare_mutex
;
848 if (subs
!= capsubs
&& atomic_read(&subs
->state
) < state_PREPARED
)
849 err
= usX2Y_urbs_start(subs
);
852 mutex_unlock(&usX2Y
->prepare_mutex
);
856 static struct snd_pcm_hardware snd_usX2Y_2c
=
858 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
859 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
860 SNDRV_PCM_INFO_MMAP_VALID
|
861 SNDRV_PCM_INFO_BATCH
),
862 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
,
863 .rates
= SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
868 .buffer_bytes_max
= (2*128*1024),
869 .period_bytes_min
= 64,
870 .period_bytes_max
= (128*1024),
878 static int snd_usX2Y_pcm_open(struct snd_pcm_substream
*substream
)
880 struct snd_usX2Y_substream
*subs
= ((struct snd_usX2Y_substream
**)
881 snd_pcm_substream_chip(substream
))[substream
->stream
];
882 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
884 if (subs
->usX2Y
->chip_status
& USX2Y_STAT_CHIP_MMAP_PCM_URBS
)
887 runtime
->hw
= snd_usX2Y_2c
;
888 runtime
->private_data
= subs
;
889 subs
->pcm_substream
= substream
;
890 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, 1000, 200000);
896 static int snd_usX2Y_pcm_close(struct snd_pcm_substream
*substream
)
898 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
899 struct snd_usX2Y_substream
*subs
= runtime
->private_data
;
901 subs
->pcm_substream
= NULL
;
907 static struct snd_pcm_ops snd_usX2Y_pcm_ops
=
909 .open
= snd_usX2Y_pcm_open
,
910 .close
= snd_usX2Y_pcm_close
,
911 .ioctl
= snd_pcm_lib_ioctl
,
912 .hw_params
= snd_usX2Y_pcm_hw_params
,
913 .hw_free
= snd_usX2Y_pcm_hw_free
,
914 .prepare
= snd_usX2Y_pcm_prepare
,
915 .trigger
= snd_usX2Y_pcm_trigger
,
916 .pointer
= snd_usX2Y_pcm_pointer
,
921 * free a usb stream instance
923 static void usX2Y_audio_stream_free(struct snd_usX2Y_substream
**usX2Y_substream
)
925 kfree(usX2Y_substream
[SNDRV_PCM_STREAM_PLAYBACK
]);
926 usX2Y_substream
[SNDRV_PCM_STREAM_PLAYBACK
] = NULL
;
928 kfree(usX2Y_substream
[SNDRV_PCM_STREAM_CAPTURE
]);
929 usX2Y_substream
[SNDRV_PCM_STREAM_CAPTURE
] = NULL
;
932 static void snd_usX2Y_pcm_private_free(struct snd_pcm
*pcm
)
934 struct snd_usX2Y_substream
**usX2Y_stream
= pcm
->private_data
;
936 usX2Y_audio_stream_free(usX2Y_stream
);
939 static int usX2Y_audio_stream_new(struct snd_card
*card
, int playback_endpoint
, int capture_endpoint
)
943 struct snd_usX2Y_substream
**usX2Y_substream
=
944 usX2Y(card
)->subs
+ 2 * usX2Y(card
)->pcm_devs
;
946 for (i
= playback_endpoint
? SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
947 i
<= SNDRV_PCM_STREAM_CAPTURE
; ++i
) {
948 usX2Y_substream
[i
] = kzalloc(sizeof(struct snd_usX2Y_substream
), GFP_KERNEL
);
949 if (NULL
== usX2Y_substream
[i
]) {
950 snd_printk(KERN_ERR
"cannot malloc\n");
953 usX2Y_substream
[i
]->usX2Y
= usX2Y(card
);
956 if (playback_endpoint
)
957 usX2Y_substream
[SNDRV_PCM_STREAM_PLAYBACK
]->endpoint
= playback_endpoint
;
958 usX2Y_substream
[SNDRV_PCM_STREAM_CAPTURE
]->endpoint
= capture_endpoint
;
960 err
= snd_pcm_new(card
, NAME_ALLCAPS
" Audio", usX2Y(card
)->pcm_devs
,
961 playback_endpoint
? 1 : 0, 1,
964 usX2Y_audio_stream_free(usX2Y_substream
);
968 if (playback_endpoint
)
969 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_usX2Y_pcm_ops
);
970 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_usX2Y_pcm_ops
);
972 pcm
->private_data
= usX2Y_substream
;
973 pcm
->private_free
= snd_usX2Y_pcm_private_free
;
976 sprintf(pcm
->name
, NAME_ALLCAPS
" Audio #%d", usX2Y(card
)->pcm_devs
);
978 if ((playback_endpoint
&&
979 0 > (err
= snd_pcm_lib_preallocate_pages(pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
,
980 SNDRV_DMA_TYPE_CONTINUOUS
,
981 snd_dma_continuous_data(GFP_KERNEL
),
982 64*1024, 128*1024))) ||
983 0 > (err
= snd_pcm_lib_preallocate_pages(pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
,
984 SNDRV_DMA_TYPE_CONTINUOUS
,
985 snd_dma_continuous_data(GFP_KERNEL
),
986 64*1024, 128*1024))) {
987 snd_usX2Y_pcm_private_free(pcm
);
990 usX2Y(card
)->pcm_devs
++;
996 * create a chip instance and set its names.
998 int usX2Y_audio_create(struct snd_card
*card
)
1002 INIT_LIST_HEAD(&usX2Y(card
)->pcm_list
);
1004 if (0 > (err
= usX2Y_audio_stream_new(card
, 0xA, 0x8)))
1006 if (le16_to_cpu(usX2Y(card
)->dev
->descriptor
.idProduct
) == USB_ID_US428
)
1007 if (0 > (err
= usX2Y_audio_stream_new(card
, 0, 0xA)))
1009 if (le16_to_cpu(usX2Y(card
)->dev
->descriptor
.idProduct
) != USB_ID_US122
)
1010 err
= usX2Y_rate_set(usX2Y(card
), 44100); // Lets us428 recognize output-volume settings, disturbs us122.