blob: 6234a51625b1b6a556f252c2fea3a150db1bdbc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * US-X2Y AUDIO
3 * Copyright (c) 2002-2004 by Karsten Wiese
4 *
5 * based on
6 *
7 * (Tentative) USB Audio Driver for ALSA
8 *
9 * Main and PCM part
10 *
11 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
12 *
13 * Many codes borrowed from audio.c by
14 * Alan Cox (alan@lxorguk.ukuu.org.uk)
15 * Thomas Sailer (sailer@ife.ee.ethz.ch)
16 *
17 *
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.
22 *
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.
27 *
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
31 */
32
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/usb.h>
Paul Gortmaker31623ca2011-08-31 17:02:47 -040037#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <sound/core.h>
39#include <sound/info.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include "usx2y.h"
43#include "usbusx2y.h"
44
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"
50 .
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.
54 */
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.
59 */
60
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.");
66#else
67 #define nr_of_packs() USX2Y_NRPACKS
68#endif
69
70
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010071static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct urb *urb = subs->completed_urb;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010074 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned char *cp;
76 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010077 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
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 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010082 snd_printk(KERN_ERR "active frame status %i. "
Masanari Iida6e8d5d22012-02-15 00:38:55 +090083 "Most probably some hardware problem.\n",
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010084 urb->iso_frame_desc[i].status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return urb->iso_frame_desc[i].status;
86 }
87 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
88 if (! len) {
89 snd_printd("0 == len ERROR!\n");
90 continue;
91 }
92
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);
99 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100100 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
101 len * usX2Y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 lens += len;
104 if ((hwptr_done += len) >= runtime->buffer_size)
105 hwptr_done -= runtime->buffer_size;
106 }
107
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);
114 }
115 return 0;
116}
117/*
118 * prepare urb for playback data pipe
119 *
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.
126 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100127static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct urb *cap_urb,
129 struct urb *urb)
130{
131 int count, counts, pack;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100132 struct usX2Ydev *usX2Y = subs->usX2Y;
133 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 count = 0;
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;
139 count += counts;
140 if (counts < 43 || counts > 50) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200141 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EPIPE;
143 }
144 /* set up descriptor */
145 urb->iso_frame_desc[pack].offset = pack ?
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100146 urb->iso_frame_desc[pack - 1].offset +
147 urb->iso_frame_desc[pack - 1].length :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 0;
149 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
150 }
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.
155 */
156 int len;
157 len = runtime->buffer_size - subs->hwptr;
158 urb->transfer_buffer = subs->tmpbuf;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100159 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 subs->hwptr += count;
164 subs->hwptr -= runtime->buffer_size;
165 } else {
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;
170 }
171 else
172 urb->transfer_buffer = subs->tmpbuf;
173 urb->transfer_buffer_length = count * usX2Y->stride;
174 return 0;
175}
176
177/*
178 * process after playback data complete
179 *
180 * update the current position and call callback if a period is processed.
181 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100182static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100184 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int len = urb->actual_length / subs->usX2Y->stride;
186
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);
194 }
195}
196
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100197static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int err;
200 if (!urb)
201 return -ENODEV;
202 urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
203 urb->hcpriv = NULL;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100204 urb->dev = subs->usX2Y->dev; /* we need to set this at each time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200206 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return err;
208 }
209 return 0;
210}
211
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100212static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
213 struct snd_usX2Y_substream *playbacksubs,
214 int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int err, state;
Takashi Iwaicb432372005-11-17 10:48:52 +0100217 struct urb *urb = playbacksubs->completed_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Takashi Iwaicb432372005-11-17 10:48:52 +0100219 state = atomic_read(&playbacksubs->state);
220 if (NULL != urb) {
221 if (state == state_RUNNING)
222 usX2Y_urb_play_retire(playbacksubs, urb);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100223 else if (state >= state_PRERUNNING)
224 atomic_inc(&playbacksubs->state);
Takashi Iwaicb432372005-11-17 10:48:52 +0100225 } else {
226 switch (state) {
227 case state_STARTING1:
228 urb = playbacksubs->urb[0];
229 atomic_inc(&playbacksubs->state);
230 break;
231 case state_STARTING2:
232 urb = playbacksubs->urb[1];
233 atomic_inc(&playbacksubs->state);
234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100237 if (urb) {
238 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100239 (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100240 return err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100241 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100242 }
243
244 playbacksubs->completed_urb = NULL;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 state = atomic_read(&capsubs->state);
247 if (state >= state_PREPARED) {
248 if (state == state_RUNNING) {
249 if ((err = usX2Y_urb_capt_retire(capsubs)))
250 return err;
Takashi Iwaicb432372005-11-17 10:48:52 +0100251 } else if (state >= state_PRERUNNING)
252 atomic_inc(&capsubs->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
254 return err;
255 }
256 capsubs->completed_urb = NULL;
257 return 0;
258}
259
260
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100261static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int s, u;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100266 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (subs) {
268 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
269 atomic_set(&subs->state, state_STOPPED);
270 }
271 }
272 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100273 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (subs) {
275 if (atomic_read(&subs->state) >= state_PRERUNNING) {
Takashi Iwai5be1efb2013-07-11 17:58:47 +0200276 unsigned long flags;
277
278 snd_pcm_stream_lock_irqsave(subs->pcm_substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
Takashi Iwai5be1efb2013-07-11 17:58:47 +0200280 snd_pcm_stream_unlock_irqrestore(subs->pcm_substream, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 for (u = 0; u < NRURBS; u++) {
283 struct urb *urb = subs->urb[u];
284 if (NULL != urb)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100285 snd_printdd("%i status=%i start_frame=%i\n",
286 u, urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
289 }
290 usX2Y->prepare_subs = NULL;
291 wake_up(&usX2Y->prepare_wait_queue);
292}
293
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100294static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
295 struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200297 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 urb->status = 0;
299 usX2Y_clients_stop(usX2Y);
300}
301
David Howells7d12e782006-10-05 14:55:46 +0100302static void i_usX2Y_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100304 struct snd_usX2Y_substream *subs = urb->context;
305 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100308 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
Clemens Ladischa014bba2009-11-16 12:26:30 +0100309 usb_get_current_frame_number(usX2Y->dev),
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100310 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
311 urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return;
313 }
314 if (unlikely(urb->status)) {
315 usX2Y_error_urb_status(usX2Y, subs, urb);
316 return;
317 }
Daniel Macka9d14bc2013-10-02 17:49:50 +0200318
319 subs->completed_urb = urb;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100322 struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100324 if (capsubs->completed_urb &&
325 atomic_read(&capsubs->state) >= state_PREPARED &&
326 (playbacksubs->completed_urb ||
327 atomic_read(&playbacksubs->state) < state_PREPARED)) {
Karsten Wiese635bbb32006-10-04 17:17:32 +0200328 if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
329 usX2Y->wait_iso_frame += nr_of_packs();
330 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 snd_printdd("\n");
332 usX2Y_clients_stop(usX2Y);
333 }
334 }
335 }
336}
337
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100338static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
David Howells7d12e782006-10-05 14:55:46 +0100339 void (*complete)(struct urb *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 int s, u;
342 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100343 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (NULL != subs)
345 for (u = 0; u < NRURBS; u++) {
346 struct urb * urb = subs->urb[u];
347 if (NULL != urb)
348 urb->complete = complete;
349 }
350 }
351}
352
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100353static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
356 usX2Y->prepare_subs = NULL;
357}
358
David Howells7d12e782006-10-05 14:55:46 +0100359static void i_usX2Y_subs_startup(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100361 struct snd_usX2Y_substream *subs = urb->context;
362 struct usX2Ydev *usX2Y = subs->usX2Y;
363 struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 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);
369 }
370
David Howells7d12e782006-10-05 14:55:46 +0100371 i_usX2Y_urb_complete(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100374static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100376 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
377 subs, subs->endpoint, subs->urb[0], subs->urb[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* reset the pointer */
379 subs->hwptr = 0;
380 subs->hwptr_done = 0;
381 subs->transfer_done = 0;
382}
383
384
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100385static void usX2Y_urb_release(struct urb **urb, int free_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 if (*urb) {
388 usb_kill_urb(*urb);
389 if (free_tb)
390 kfree((*urb)->transfer_buffer);
391 usb_free_urb(*urb);
392 *urb = NULL;
393 }
394}
395/*
396 * release a substreams urbs
397 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100398static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 int i;
401 snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
402 for (i = 0; i < NRURBS; i++)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100403 usX2Y_urb_release(subs->urb + i,
404 subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jesper Juhl4d572772005-05-30 17:30:32 +0200406 kfree(subs->tmpbuf);
407 subs->tmpbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409/*
410 * initialize a substream's urbs
411 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100412static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int i;
415 unsigned int pipe;
416 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Clemens Ladischa014bba2009-11-16 12:26:30 +0100417 struct usb_device *dev = subs->usX2Y->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
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)
423 return -EINVAL;
424
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");
429 return -ENOMEM;
430 }
431 }
432 /* allocate and initialize data urbs */
433 for (i = 0; i < NRURBS; i++) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100434 struct urb **purb = subs->urb + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (*purb) {
436 usb_kill_urb(*purb);
437 continue;
438 }
439 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
440 if (NULL == *purb) {
441 usX2Y_urbs_release(subs);
442 return -ENOMEM;
443 }
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);
449 return -ENOMEM;
450 }
451 }
452 (*purb)->dev = dev;
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;
458 }
459 return 0;
460}
461
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100462static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100464 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 usX2Y->prepare_subs = subs;
466 subs->urb[0]->start_frame = -1;
467 wmb();
468 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
469}
470
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100471static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int i, err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100474 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if ((err = usX2Y_urbs_allocate(subs)) < 0)
477 return err;
478 subs->completed_urb = NULL;
479 for (i = 0; i < 4; i++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100480 struct snd_usX2Y_substream *subs = usX2Y->subs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
482 goto start;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Takashi Iwaicb432372005-11-17 10:48:52 +0100485 start:
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)) {
490 unsigned long pack;
491 if (0 == i)
492 atomic_set(&subs->state, state_STARTING3);
Clemens Ladischa014bba2009-11-16 12:26:30 +0100493 urb->dev = usX2Y->dev;
Takashi Iwaicb432372005-11-17 10:48:52 +0100494 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;
497 }
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);
501 err = -EPIPE;
502 goto cleanup;
Karsten Wiese635bbb32006-10-04 17:17:32 +0200503 } else
504 if (i == 0)
Takashi Iwaicb432372005-11-17 10:48:52 +0100505 usX2Y->wait_iso_frame = urb->start_frame;
Takashi Iwaicb432372005-11-17 10:48:52 +0100506 urb->transfer_flags = 0;
507 } else {
508 atomic_set(&subs->state, state_STARTING1);
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100512 err = 0;
513 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
514 if (atomic_read(&subs->state) != state_PREPARED)
515 err = -EPIPE;
516
517 cleanup:
518 if (err) {
519 usX2Y_subs_startup_finish(usX2Y);
520 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return err;
523}
524
525/*
526 * return the current pcm pointer. just return the hwptr_done value.
527 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100528static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100530 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return subs->hwptr_done;
532}
533/*
534 * start/stop substream
535 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100536static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100538 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 switch (cmd) {
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);
546 } else {
547 snd_printdd("\n");
548 return -EPIPE;
549 }
550 break;
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);
555 break;
556 default:
557 return -EINVAL;
558 }
559 return 0;
560}
561
562
563/*
564 * allocate a buffer, setup samplerate
565 *
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
569 * that.
570 */
571static struct s_c2
572{
573 char c1, c2;
574}
575 SetRate44100[] =
576{
577 { 0x14, 0x08}, // this line sets 44100, well actually a little less
578 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
579 { 0x18, 0x42},
580 { 0x18, 0x45},
581 { 0x18, 0x46},
582 { 0x18, 0x48},
583 { 0x18, 0x4A},
584 { 0x18, 0x4C},
585 { 0x18, 0x4E},
586 { 0x18, 0x50},
587 { 0x18, 0x52},
588 { 0x18, 0x54},
589 { 0x18, 0x56},
590 { 0x18, 0x58},
591 { 0x18, 0x5A},
592 { 0x18, 0x5C},
593 { 0x18, 0x5E},
594 { 0x18, 0x60},
595 { 0x18, 0x62},
596 { 0x18, 0x64},
597 { 0x18, 0x66},
598 { 0x18, 0x68},
599 { 0x18, 0x6A},
600 { 0x18, 0x6C},
601 { 0x18, 0x6E},
602 { 0x18, 0x70},
603 { 0x18, 0x72},
604 { 0x18, 0x74},
605 { 0x18, 0x76},
606 { 0x18, 0x78},
607 { 0x18, 0x7A},
608 { 0x18, 0x7C},
609 { 0x18, 0x7E}
610};
611static struct s_c2 SetRate48000[] =
612{
613 { 0x14, 0x09}, // this line sets 48000, well actually a little less
614 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
615 { 0x18, 0x42},
616 { 0x18, 0x45},
617 { 0x18, 0x46},
618 { 0x18, 0x48},
619 { 0x18, 0x4A},
620 { 0x18, 0x4C},
621 { 0x18, 0x4E},
622 { 0x18, 0x50},
623 { 0x18, 0x52},
624 { 0x18, 0x54},
625 { 0x18, 0x56},
626 { 0x18, 0x58},
627 { 0x18, 0x5A},
628 { 0x18, 0x5C},
629 { 0x18, 0x5E},
630 { 0x18, 0x60},
631 { 0x18, 0x62},
632 { 0x18, 0x64},
633 { 0x18, 0x66},
634 { 0x18, 0x68},
635 { 0x18, 0x6A},
636 { 0x18, 0x6C},
637 { 0x18, 0x6E},
638 { 0x18, 0x70},
639 { 0x18, 0x73},
640 { 0x18, 0x74},
641 { 0x18, 0x76},
642 { 0x18, 0x78},
643 { 0x18, 0x7A},
644 { 0x18, 0x7C},
645 { 0x18, 0x7E}
646};
647#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
648
David Howells7d12e782006-10-05 14:55:46 +0100649static void i_usX2Y_04Int(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100651 struct usX2Ydev *usX2Y = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200653 if (urb->status)
654 snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (0 == --usX2Y->US04->len)
656 wake_up(&usX2Y->In04WaitQueue);
657}
658
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100659static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int err = 0, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100662 struct snd_usX2Y_urbSeq *us = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int *usbdata = NULL;
664 struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
665
666 if (usX2Y->rate != rate) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100667 us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (NULL == us) {
669 err = -ENOMEM;
670 goto cleanup;
671 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100672 usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (NULL == usbdata) {
674 err = -ENOMEM;
675 goto cleanup;
676 }
677 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
678 if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
679 err = -ENOMEM;
680 goto cleanup;
681 }
682 ((char*)(usbdata + i))[0] = ra[i].c1;
683 ((char*)(usbdata + i))[1] = ra[i].c2;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100684 usb_fill_bulk_urb(us->urb[i], usX2Y->dev, usb_sndbulkpipe(usX2Y->dev, 4),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 usbdata + i, 2, i_usX2Y_04Int, usX2Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 us->submitted = 0;
688 us->len = NOOF_SETRATE_URBS;
689 usX2Y->US04 = us;
690 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
691 usX2Y->US04 = NULL;
692 if (us->len)
693 err = -ENODEV;
694 cleanup:
695 if (us) {
696 us->submitted = 2*NOOF_SETRATE_URBS;
697 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
698 struct urb *urb = us->urb[i];
699 if (urb->status) {
700 if (!err)
701 err = -ENODEV;
702 usb_kill_urb(urb);
703 }
704 usb_free_urb(urb);
705 }
706 usX2Y->US04 = NULL;
707 kfree(usbdata);
708 kfree(us);
Takashi Iwaicb432372005-11-17 10:48:52 +0100709 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 usX2Y->rate = rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 }
713
714 return err;
715}
716
717
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100718static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 int alternate, err;
721 struct list_head* p;
722 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
723 alternate = 2;
724 usX2Y->stride = 6;
725 } else {
726 alternate = 1;
727 usX2Y->stride = 4;
728 }
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100729 list_for_each(p, &usX2Y->midi_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 snd_usbmidi_input_stop(p);
731 }
732 usb_kill_urb(usX2Y->In04urb);
Clemens Ladischa014bba2009-11-16 12:26:30 +0100733 if ((err = usb_set_interface(usX2Y->dev, 0, alternate))) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200734 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return err;
736 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100737 usX2Y->In04urb->dev = usX2Y->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100739 list_for_each(p, &usX2Y->midi_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 snd_usbmidi_input_start(p);
741 }
742 usX2Y->format = format;
743 usX2Y->rate = 0;
744 return err;
745}
746
747
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100748static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
749 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 int err = 0;
752 unsigned int rate = params_rate(hw_params);
753 snd_pcm_format_t format = params_format(hw_params);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100754 struct snd_card *card = substream->pstr->pcm->card;
755 struct list_head *list;
Takashi Iwaicb432372005-11-17 10:48:52 +0100756
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) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100760 struct snd_device *dev;
761 struct snd_pcm *pcm;
Takashi Iwaicb432372005-11-17 10:48:52 +0100762 int s;
763 dev = snd_device(list);
764 if (dev->type != SNDRV_DEV_PCM)
765 continue;
766 pcm = dev->device_data;
767 for (s = 0; s < 2; ++s) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100768 struct snd_pcm_substream *test_substream;
Takashi Iwaicb432372005-11-17 10:48:52 +0100769 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)))
776 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 }
779 if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100780 snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
781 substream, params_buffer_bytes(hw_params), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return err;
783 }
784 return 0;
785}
786
787/*
788 * free the buffer
789 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100790static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100792 struct snd_pcm_runtime *runtime = substream->runtime;
793 struct snd_usX2Y_substream *subs = runtime->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100794 mutex_lock(&subs->usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
796
797 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100798 struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 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);
807 }
808 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100809 struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (atomic_read(&playback_subs->state) < state_PREPARED) {
811 atomic_set(&subs->state, state_STOPPED);
812 usX2Y_urbs_release(subs);
813 }
814 }
Ingo Molnar12aa7572006-01-16 16:36:05 +0100815 mutex_unlock(&subs->usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return snd_pcm_lib_free_pages(substream);
817}
818/*
819 * prepare callback
820 *
821 * set format and initialize urbs
822 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100823static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100825 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];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int err = 0;
830 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
831
Ingo Molnar12aa7572006-01-16 16:36:05 +0100832 mutex_lock(&usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 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;
846 }
847
848 if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
849 err = usX2Y_urbs_start(subs);
850
851 up_prepare_mutex:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100852 mutex_unlock(&usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return err;
854}
855
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100856static struct snd_pcm_hardware snd_usX2Y_2c =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
859 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai2008f132009-04-28 12:25:59 +0200860 SNDRV_PCM_INFO_MMAP_VALID |
861 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
863 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
864 .rate_min = 44100,
865 .rate_max = 48000,
866 .channels_min = 2,
867 .channels_max = 2,
868 .buffer_bytes_max = (2*128*1024),
869 .period_bytes_min = 64,
870 .period_bytes_max = (128*1024),
871 .periods_min = 2,
872 .periods_max = 1024,
873 .fifo_size = 0
874};
875
876
877
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100878static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100880 struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 snd_pcm_substream_chip(substream))[substream->stream];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100882 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
885 return -EBUSY;
886
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);
891 return 0;
892}
893
894
895
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100896static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100898 struct snd_pcm_runtime *runtime = substream->runtime;
899 struct snd_usX2Y_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 subs->pcm_substream = NULL;
902
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100907static struct snd_pcm_ops snd_usX2Y_pcm_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
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,
917};
918
919
920/*
921 * free a usb stream instance
922 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100923static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Richard Knutssonc111b8d2007-05-14 10:38:57 +0200925 kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
926 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
929 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
930}
931
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100932static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100934 struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
Takashi Iwaic3e6f7d2005-11-16 18:43:35 +0100935 if (usX2Y_stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 usX2Y_audio_stream_free(usX2Y_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100939static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100941 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int err, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100943 struct snd_usX2Y_substream **usX2Y_substream =
Clemens Ladischa014bba2009-11-16 12:26:30 +0100944 usX2Y(card)->subs + 2 * usX2Y(card)->pcm_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
947 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100948 usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (NULL == usX2Y_substream[i]) {
950 snd_printk(KERN_ERR "cannot malloc\n");
951 return -ENOMEM;
952 }
953 usX2Y_substream[i]->usX2Y = usX2Y(card);
954 }
955
956 if (playback_endpoint)
957 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
958 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
959
Clemens Ladischa014bba2009-11-16 12:26:30 +0100960 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->pcm_devs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 playback_endpoint ? 1 : 0, 1,
962 &pcm);
963 if (err < 0) {
964 usX2Y_audio_stream_free(usX2Y_substream);
965 return err;
966 }
967
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);
971
972 pcm->private_data = usX2Y_substream;
973 pcm->private_free = snd_usX2Y_pcm_private_free;
974 pcm->info_flags = 0;
975
Clemens Ladischa014bba2009-11-16 12:26:30 +0100976 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->pcm_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
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);
988 return err;
989 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100990 usX2Y(card)->pcm_devs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return 0;
993}
994
995/*
996 * create a chip instance and set its names.
997 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100998int usX2Y_audio_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 int err = 0;
1001
Clemens Ladischa014bba2009-11-16 12:26:30 +01001002 INIT_LIST_HEAD(&usX2Y(card)->pcm_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1005 return err;
Clemens Ladischa014bba2009-11-16 12:26:30 +01001006 if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) == USB_ID_US428)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1008 return err;
Clemens Ladischa014bba2009-11-16 12:26:30 +01001009 if (le16_to_cpu(usX2Y(card)->dev->descriptor.idProduct) != USB_ID_US122)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
1011 return err;
1012}