blob: a6bbc7a6348f81f828598395642d641eb3b74b2c [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
34#include <sound/driver.h>
35#include <linux/interrupt.h>
36#include <linux/usb.h>
37#include <sound/core.h>
38#include <sound/info.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include "usx2y.h"
42#include "usbusx2y.h"
43
44#define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
45 1 to 4 have been tested ok on uhci.
46 To use 3 on ohci, you'd need a patch:
47 look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
48 "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
49 .
50 1, 2 and 4 work out of the box on ohci, if I recall correctly.
51 Bigger is safer operation,
52 smaller gives lower latencies.
53 */
54#define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
55 nrpacks set to 1, you might as well comment
56 this #define out, and thereby produce smaller, faster code.
57 You'd also set USX2Y_NRPACKS to 1 then.
58 */
59
60#ifdef USX2Y_NRPACKS_VARIABLE
61 static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
62 #define nr_of_packs() nrpacks
63 module_param(nrpacks, int, 0444);
64 MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
65#else
66 #define nr_of_packs() USX2Y_NRPACKS
67#endif
68
69
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010070static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct urb *urb = subs->completed_urb;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010073 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned char *cp;
75 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010076 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 for (i = 0; i < nr_of_packs(); i++) {
79 cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
80 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010081 snd_printk(KERN_ERR "active frame status %i. "
82 "Most propably some hardware problem.\n",
83 urb->iso_frame_desc[i].status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return urb->iso_frame_desc[i].status;
85 }
86 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
87 if (! len) {
88 snd_printd("0 == len ERROR!\n");
89 continue;
90 }
91
92 /* copy a data chunk */
93 if ((hwptr_done + len) > runtime->buffer_size) {
94 int cnt = runtime->buffer_size - hwptr_done;
95 int blen = cnt * usX2Y->stride;
96 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
97 memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
98 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010099 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
100 len * usX2Y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 lens += len;
103 if ((hwptr_done += len) >= runtime->buffer_size)
104 hwptr_done -= runtime->buffer_size;
105 }
106
107 subs->hwptr_done = hwptr_done;
108 subs->transfer_done += lens;
109 /* update the pointer, call callback if necessary */
110 if (subs->transfer_done >= runtime->period_size) {
111 subs->transfer_done -= runtime->period_size;
112 snd_pcm_period_elapsed(subs->pcm_substream);
113 }
114 return 0;
115}
116/*
117 * prepare urb for playback data pipe
118 *
119 * we copy the data directly from the pcm buffer.
120 * the current position to be copied is held in hwptr field.
121 * since a urb can handle only a single linear buffer, if the total
122 * transferred area overflows the buffer boundary, we cannot send
123 * it directly from the buffer. thus the data is once copied to
124 * a temporary buffer and urb points to that.
125 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100126static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct urb *cap_urb,
128 struct urb *urb)
129{
130 int count, counts, pack;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100131 struct usX2Ydev *usX2Y = subs->usX2Y;
132 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 count = 0;
135 for (pack = 0; pack < nr_of_packs(); pack++) {
136 /* calculate the size of a packet */
137 counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
138 count += counts;
139 if (counts < 43 || counts > 50) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200140 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EPIPE;
142 }
143 /* set up descriptor */
144 urb->iso_frame_desc[pack].offset = pack ?
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100145 urb->iso_frame_desc[pack - 1].offset +
146 urb->iso_frame_desc[pack - 1].length :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 0;
148 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
149 }
150 if (atomic_read(&subs->state) >= state_PRERUNNING)
151 if (subs->hwptr + count > runtime->buffer_size) {
152 /* err, the transferred area goes over buffer boundary.
153 * copy the data to the temp buffer.
154 */
155 int len;
156 len = runtime->buffer_size - subs->hwptr;
157 urb->transfer_buffer = subs->tmpbuf;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100158 memcpy(subs->tmpbuf, runtime->dma_area +
159 subs->hwptr * usX2Y->stride, len * usX2Y->stride);
160 memcpy(subs->tmpbuf + len * usX2Y->stride,
161 runtime->dma_area, (count - len) * usX2Y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 subs->hwptr += count;
163 subs->hwptr -= runtime->buffer_size;
164 } else {
165 /* set the buffer pointer */
166 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
167 if ((subs->hwptr += count) >= runtime->buffer_size)
168 subs->hwptr -= runtime->buffer_size;
169 }
170 else
171 urb->transfer_buffer = subs->tmpbuf;
172 urb->transfer_buffer_length = count * usX2Y->stride;
173 return 0;
174}
175
176/*
177 * process after playback data complete
178 *
179 * update the current position and call callback if a period is processed.
180 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100181static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100183 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int len = urb->actual_length / subs->usX2Y->stride;
185
186 subs->transfer_done += len;
187 subs->hwptr_done += len;
188 if (subs->hwptr_done >= runtime->buffer_size)
189 subs->hwptr_done -= runtime->buffer_size;
190 if (subs->transfer_done >= runtime->period_size) {
191 subs->transfer_done -= runtime->period_size;
192 snd_pcm_period_elapsed(subs->pcm_substream);
193 }
194}
195
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100196static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int err;
199 if (!urb)
200 return -ENODEV;
201 urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
202 urb->hcpriv = NULL;
203 urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */
204 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200205 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return err;
207 }
208 return 0;
209}
210
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100211static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
212 struct snd_usX2Y_substream *playbacksubs,
213 int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int err, state;
Takashi Iwaicb432372005-11-17 10:48:52 +0100216 struct urb *urb = playbacksubs->completed_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Takashi Iwaicb432372005-11-17 10:48:52 +0100218 state = atomic_read(&playbacksubs->state);
219 if (NULL != urb) {
220 if (state == state_RUNNING)
221 usX2Y_urb_play_retire(playbacksubs, urb);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100222 else if (state >= state_PRERUNNING)
223 atomic_inc(&playbacksubs->state);
Takashi Iwaicb432372005-11-17 10:48:52 +0100224 } else {
225 switch (state) {
226 case state_STARTING1:
227 urb = playbacksubs->urb[0];
228 atomic_inc(&playbacksubs->state);
229 break;
230 case state_STARTING2:
231 urb = playbacksubs->urb[1];
232 atomic_inc(&playbacksubs->state);
233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100236 if (urb) {
237 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100238 (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100239 return err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100240 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100241 }
242
243 playbacksubs->completed_urb = NULL;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 state = atomic_read(&capsubs->state);
246 if (state >= state_PREPARED) {
247 if (state == state_RUNNING) {
248 if ((err = usX2Y_urb_capt_retire(capsubs)))
249 return err;
Takashi Iwaicb432372005-11-17 10:48:52 +0100250 } else if (state >= state_PRERUNNING)
251 atomic_inc(&capsubs->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
253 return err;
254 }
255 capsubs->completed_urb = NULL;
256 return 0;
257}
258
259
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100260static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int s, u;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100265 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (subs) {
267 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
268 atomic_set(&subs->state, state_STOPPED);
269 }
270 }
271 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100272 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (subs) {
274 if (atomic_read(&subs->state) >= state_PRERUNNING) {
275 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
276 }
277 for (u = 0; u < NRURBS; u++) {
278 struct urb *urb = subs->urb[u];
279 if (NULL != urb)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100280 snd_printdd("%i status=%i start_frame=%i\n",
281 u, urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284 }
285 usX2Y->prepare_subs = NULL;
286 wake_up(&usX2Y->prepare_wait_queue);
287}
288
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100289static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
290 struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200292 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 urb->status = 0;
294 usX2Y_clients_stop(usX2Y);
295}
296
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100297static void usX2Y_error_sequence(struct usX2Ydev *usX2Y,
298 struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200300 snd_printk(KERN_ERR "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
301 KERN_ERR "Most propably some urb of usb-frame %i is still missing.\n"
302 KERN_ERR "Cause could be too long delays in usb-hcd interrupt handling.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 usb_get_current_frame_number(usX2Y->chip.dev),
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100304 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
305 usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 usX2Y_clients_stop(usX2Y);
307}
308
309static void i_usX2Y_urb_complete(struct urb *urb, struct pt_regs *regs)
310{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100311 struct snd_usX2Y_substream *subs = urb->context;
312 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100315 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
316 usb_get_current_frame_number(usX2Y->chip.dev),
317 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
318 urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return;
320 }
321 if (unlikely(urb->status)) {
322 usX2Y_error_urb_status(usX2Y, subs, urb);
323 return;
324 }
325 if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame))
326 subs->completed_urb = urb;
327 else {
328 usX2Y_error_sequence(usX2Y, subs, urb);
329 return;
330 }
331 {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100332 struct snd_usX2Y_substream *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100334 if (capsubs->completed_urb &&
335 atomic_read(&capsubs->state) >= state_PREPARED &&
336 (playbacksubs->completed_urb ||
337 atomic_read(&playbacksubs->state) < state_PREPARED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
339 if (nr_of_packs() <= urb->start_frame &&
340 urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci
341 usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs();
342 else
343 usX2Y->wait_iso_frame += nr_of_packs();
344 } else {
345 snd_printdd("\n");
346 usX2Y_clients_stop(usX2Y);
347 }
348 }
349 }
350}
351
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100352static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
353 void (*complete)(struct urb *, struct pt_regs *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 int s, u;
356 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100357 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (NULL != subs)
359 for (u = 0; u < NRURBS; u++) {
360 struct urb * urb = subs->urb[u];
361 if (NULL != urb)
362 urb->complete = complete;
363 }
364 }
365}
366
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100367static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
370 usX2Y->prepare_subs = NULL;
371}
372
373static void i_usX2Y_subs_startup(struct urb *urb, struct pt_regs *regs)
374{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100375 struct snd_usX2Y_substream *subs = urb->context;
376 struct usX2Ydev *usX2Y = subs->usX2Y;
377 struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (NULL != prepare_subs)
379 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
380 usX2Y_subs_startup_finish(usX2Y);
381 atomic_inc(&prepare_subs->state);
382 wake_up(&usX2Y->prepare_wait_queue);
383 }
384
385 i_usX2Y_urb_complete(urb, regs);
386}
387
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100388static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100390 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
391 subs, subs->endpoint, subs->urb[0], subs->urb[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* reset the pointer */
393 subs->hwptr = 0;
394 subs->hwptr_done = 0;
395 subs->transfer_done = 0;
396}
397
398
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100399static void usX2Y_urb_release(struct urb **urb, int free_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 if (*urb) {
402 usb_kill_urb(*urb);
403 if (free_tb)
404 kfree((*urb)->transfer_buffer);
405 usb_free_urb(*urb);
406 *urb = NULL;
407 }
408}
409/*
410 * release a substreams urbs
411 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100412static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int i;
415 snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
416 for (i = 0; i < NRURBS; i++)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100417 usX2Y_urb_release(subs->urb + i,
418 subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jesper Juhl4d572772005-05-30 17:30:32 +0200420 kfree(subs->tmpbuf);
421 subs->tmpbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423/*
424 * initialize a substream's urbs
425 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100426static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int i;
429 unsigned int pipe;
430 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
431 struct usb_device *dev = subs->usX2Y->chip.dev;
432
433 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
434 usb_rcvisocpipe(dev, subs->endpoint);
435 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
436 if (!subs->maxpacksize)
437 return -EINVAL;
438
439 if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
440 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
441 if (NULL == subs->tmpbuf) {
442 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
443 return -ENOMEM;
444 }
445 }
446 /* allocate and initialize data urbs */
447 for (i = 0; i < NRURBS; i++) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100448 struct urb **purb = subs->urb + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (*purb) {
450 usb_kill_urb(*purb);
451 continue;
452 }
453 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
454 if (NULL == *purb) {
455 usX2Y_urbs_release(subs);
456 return -ENOMEM;
457 }
458 if (!is_playback && !(*purb)->transfer_buffer) {
459 /* allocate a capture buffer per urb */
460 (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
461 if (NULL == (*purb)->transfer_buffer) {
462 usX2Y_urbs_release(subs);
463 return -ENOMEM;
464 }
465 }
466 (*purb)->dev = dev;
467 (*purb)->pipe = pipe;
468 (*purb)->number_of_packets = nr_of_packs();
469 (*purb)->context = subs;
470 (*purb)->interval = 1;
471 (*purb)->complete = i_usX2Y_subs_startup;
472 }
473 return 0;
474}
475
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100476static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100478 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 usX2Y->prepare_subs = subs;
480 subs->urb[0]->start_frame = -1;
481 wmb();
482 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
483}
484
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100485static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 int i, err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100488 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if ((err = usX2Y_urbs_allocate(subs)) < 0)
491 return err;
492 subs->completed_urb = NULL;
493 for (i = 0; i < 4; i++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100494 struct snd_usX2Y_substream *subs = usX2Y->subs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
496 goto start;
497 }
498 usX2Y->wait_iso_frame = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Takashi Iwaicb432372005-11-17 10:48:52 +0100500 start:
501 usX2Y_subs_startup(subs);
502 for (i = 0; i < NRURBS; i++) {
503 struct urb *urb = subs->urb[i];
504 if (usb_pipein(urb->pipe)) {
505 unsigned long pack;
506 if (0 == i)
507 atomic_set(&subs->state, state_STARTING3);
508 urb->dev = usX2Y->chip.dev;
509 urb->transfer_flags = URB_ISO_ASAP;
510 for (pack = 0; pack < nr_of_packs(); pack++) {
511 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
512 urb->iso_frame_desc[pack].length = subs->maxpacksize;
513 }
514 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
515 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
516 snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
517 err = -EPIPE;
518 goto cleanup;
519 } else {
520 if (0 > usX2Y->wait_iso_frame)
521 usX2Y->wait_iso_frame = urb->start_frame;
522 }
523 urb->transfer_flags = 0;
524 } else {
525 atomic_set(&subs->state, state_STARTING1);
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100529 err = 0;
530 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
531 if (atomic_read(&subs->state) != state_PREPARED)
532 err = -EPIPE;
533
534 cleanup:
535 if (err) {
536 usX2Y_subs_startup_finish(usX2Y);
537 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return err;
540}
541
542/*
543 * return the current pcm pointer. just return the hwptr_done value.
544 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100545static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100547 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return subs->hwptr_done;
549}
550/*
551 * start/stop substream
552 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100553static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100555 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 switch (cmd) {
558 case SNDRV_PCM_TRIGGER_START:
559 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
560 if (atomic_read(&subs->state) == state_PREPARED &&
561 atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
562 atomic_set(&subs->state, state_PRERUNNING);
563 } else {
564 snd_printdd("\n");
565 return -EPIPE;
566 }
567 break;
568 case SNDRV_PCM_TRIGGER_STOP:
569 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
570 if (atomic_read(&subs->state) >= state_PRERUNNING)
571 atomic_set(&subs->state, state_PREPARED);
572 break;
573 default:
574 return -EINVAL;
575 }
576 return 0;
577}
578
579
580/*
581 * allocate a buffer, setup samplerate
582 *
583 * so far we use a physically linear buffer although packetize transfer
584 * doesn't need a continuous area.
585 * if sg buffer is supported on the later version of alsa, we'll follow
586 * that.
587 */
588static struct s_c2
589{
590 char c1, c2;
591}
592 SetRate44100[] =
593{
594 { 0x14, 0x08}, // this line sets 44100, well actually a little less
595 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
596 { 0x18, 0x42},
597 { 0x18, 0x45},
598 { 0x18, 0x46},
599 { 0x18, 0x48},
600 { 0x18, 0x4A},
601 { 0x18, 0x4C},
602 { 0x18, 0x4E},
603 { 0x18, 0x50},
604 { 0x18, 0x52},
605 { 0x18, 0x54},
606 { 0x18, 0x56},
607 { 0x18, 0x58},
608 { 0x18, 0x5A},
609 { 0x18, 0x5C},
610 { 0x18, 0x5E},
611 { 0x18, 0x60},
612 { 0x18, 0x62},
613 { 0x18, 0x64},
614 { 0x18, 0x66},
615 { 0x18, 0x68},
616 { 0x18, 0x6A},
617 { 0x18, 0x6C},
618 { 0x18, 0x6E},
619 { 0x18, 0x70},
620 { 0x18, 0x72},
621 { 0x18, 0x74},
622 { 0x18, 0x76},
623 { 0x18, 0x78},
624 { 0x18, 0x7A},
625 { 0x18, 0x7C},
626 { 0x18, 0x7E}
627};
628static struct s_c2 SetRate48000[] =
629{
630 { 0x14, 0x09}, // this line sets 48000, well actually a little less
631 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
632 { 0x18, 0x42},
633 { 0x18, 0x45},
634 { 0x18, 0x46},
635 { 0x18, 0x48},
636 { 0x18, 0x4A},
637 { 0x18, 0x4C},
638 { 0x18, 0x4E},
639 { 0x18, 0x50},
640 { 0x18, 0x52},
641 { 0x18, 0x54},
642 { 0x18, 0x56},
643 { 0x18, 0x58},
644 { 0x18, 0x5A},
645 { 0x18, 0x5C},
646 { 0x18, 0x5E},
647 { 0x18, 0x60},
648 { 0x18, 0x62},
649 { 0x18, 0x64},
650 { 0x18, 0x66},
651 { 0x18, 0x68},
652 { 0x18, 0x6A},
653 { 0x18, 0x6C},
654 { 0x18, 0x6E},
655 { 0x18, 0x70},
656 { 0x18, 0x73},
657 { 0x18, 0x74},
658 { 0x18, 0x76},
659 { 0x18, 0x78},
660 { 0x18, 0x7A},
661 { 0x18, 0x7C},
662 { 0x18, 0x7E}
663};
664#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
665
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100666static void i_usX2Y_04Int(struct urb *urb, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100668 struct usX2Ydev *usX2Y = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200670 if (urb->status)
671 snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (0 == --usX2Y->US04->len)
673 wake_up(&usX2Y->In04WaitQueue);
674}
675
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100676static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int err = 0, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100679 struct snd_usX2Y_urbSeq *us = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int *usbdata = NULL;
681 struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
682
683 if (usX2Y->rate != rate) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100684 us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (NULL == us) {
686 err = -ENOMEM;
687 goto cleanup;
688 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100689 usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (NULL == usbdata) {
691 err = -ENOMEM;
692 goto cleanup;
693 }
694 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
695 if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
696 err = -ENOMEM;
697 goto cleanup;
698 }
699 ((char*)(usbdata + i))[0] = ra[i].c1;
700 ((char*)(usbdata + i))[1] = ra[i].c2;
701 usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4),
702 usbdata + i, 2, i_usX2Y_04Int, usX2Y);
703#ifdef OLD_USB
704 us->urb[i]->transfer_flags = USB_QUEUE_BULK;
705#endif
706 }
707 us->submitted = 0;
708 us->len = NOOF_SETRATE_URBS;
709 usX2Y->US04 = us;
710 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
711 usX2Y->US04 = NULL;
712 if (us->len)
713 err = -ENODEV;
714 cleanup:
715 if (us) {
716 us->submitted = 2*NOOF_SETRATE_URBS;
717 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
718 struct urb *urb = us->urb[i];
719 if (urb->status) {
720 if (!err)
721 err = -ENODEV;
722 usb_kill_urb(urb);
723 }
724 usb_free_urb(urb);
725 }
726 usX2Y->US04 = NULL;
727 kfree(usbdata);
728 kfree(us);
Takashi Iwaicb432372005-11-17 10:48:52 +0100729 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 usX2Y->rate = rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 }
733
734 return err;
735}
736
737
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100738static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 int alternate, err;
741 struct list_head* p;
742 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
743 alternate = 2;
744 usX2Y->stride = 6;
745 } else {
746 alternate = 1;
747 usX2Y->stride = 4;
748 }
749 list_for_each(p, &usX2Y->chip.midi_list) {
750 snd_usbmidi_input_stop(p);
751 }
752 usb_kill_urb(usX2Y->In04urb);
753 if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200754 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return err;
756 }
757 usX2Y->In04urb->dev = usX2Y->chip.dev;
758 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
759 list_for_each(p, &usX2Y->chip.midi_list) {
760 snd_usbmidi_input_start(p);
761 }
762 usX2Y->format = format;
763 usX2Y->rate = 0;
764 return err;
765}
766
767
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100768static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
769 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 int err = 0;
772 unsigned int rate = params_rate(hw_params);
773 snd_pcm_format_t format = params_format(hw_params);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100774 struct snd_card *card = substream->pstr->pcm->card;
775 struct list_head *list;
Takashi Iwaicb432372005-11-17 10:48:52 +0100776
777 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
778 // all pcm substreams off one usX2Y have to operate at the same rate & format
779 list_for_each(list, &card->devices) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100780 struct snd_device *dev;
781 struct snd_pcm *pcm;
Takashi Iwaicb432372005-11-17 10:48:52 +0100782 int s;
783 dev = snd_device(list);
784 if (dev->type != SNDRV_DEV_PCM)
785 continue;
786 pcm = dev->device_data;
787 for (s = 0; s < 2; ++s) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100788 struct snd_pcm_substream *test_substream;
Takashi Iwaicb432372005-11-17 10:48:52 +0100789 test_substream = pcm->streams[s].substream;
790 if (test_substream && test_substream != substream &&
791 test_substream->runtime &&
792 ((test_substream->runtime->format &&
793 test_substream->runtime->format != format) ||
794 (test_substream->runtime->rate &&
795 test_substream->runtime->rate != rate)))
796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 }
799 if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100800 snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
801 substream, params_buffer_bytes(hw_params), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return err;
803 }
804 return 0;
805}
806
807/*
808 * free the buffer
809 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100810static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100812 struct snd_pcm_runtime *runtime = substream->runtime;
813 struct snd_usX2Y_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 down(&subs->usX2Y->prepare_mutex);
815 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
816
817 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100818 struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 atomic_set(&subs->state, state_STOPPED);
820 usX2Y_urbs_release(subs);
821 if (!cap_subs->pcm_substream ||
822 !cap_subs->pcm_substream->runtime ||
823 !cap_subs->pcm_substream->runtime->status ||
824 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
825 atomic_set(&cap_subs->state, state_STOPPED);
826 usX2Y_urbs_release(cap_subs);
827 }
828 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100829 struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (atomic_read(&playback_subs->state) < state_PREPARED) {
831 atomic_set(&subs->state, state_STOPPED);
832 usX2Y_urbs_release(subs);
833 }
834 }
835 up(&subs->usX2Y->prepare_mutex);
836 return snd_pcm_lib_free_pages(substream);
837}
838/*
839 * prepare callback
840 *
841 * set format and initialize urbs
842 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100843static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100845 struct snd_pcm_runtime *runtime = substream->runtime;
846 struct snd_usX2Y_substream *subs = runtime->private_data;
847 struct usX2Ydev *usX2Y = subs->usX2Y;
848 struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int err = 0;
850 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
851
852 down(&usX2Y->prepare_mutex);
853 usX2Y_subs_prepare(subs);
854// Start hardware streams
855// SyncStream first....
856 if (atomic_read(&capsubs->state) < state_PREPARED) {
857 if (usX2Y->format != runtime->format)
858 if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
859 goto up_prepare_mutex;
860 if (usX2Y->rate != runtime->rate)
861 if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
862 goto up_prepare_mutex;
863 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
864 if (0 > (err = usX2Y_urbs_start(capsubs)))
865 goto up_prepare_mutex;
866 }
867
868 if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
869 err = usX2Y_urbs_start(subs);
870
871 up_prepare_mutex:
872 up(&usX2Y->prepare_mutex);
873 return err;
874}
875
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100876static struct snd_pcm_hardware snd_usX2Y_2c =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
879 SNDRV_PCM_INFO_BLOCK_TRANSFER |
880 SNDRV_PCM_INFO_MMAP_VALID),
881 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
882 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
883 .rate_min = 44100,
884 .rate_max = 48000,
885 .channels_min = 2,
886 .channels_max = 2,
887 .buffer_bytes_max = (2*128*1024),
888 .period_bytes_min = 64,
889 .period_bytes_max = (128*1024),
890 .periods_min = 2,
891 .periods_max = 1024,
892 .fifo_size = 0
893};
894
895
896
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100897static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100899 struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 snd_pcm_substream_chip(substream))[substream->stream];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100901 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
904 return -EBUSY;
905
906 runtime->hw = snd_usX2Y_2c;
907 runtime->private_data = subs;
908 subs->pcm_substream = substream;
909 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
910 return 0;
911}
912
913
914
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100915static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100917 struct snd_pcm_runtime *runtime = substream->runtime;
918 struct snd_usX2Y_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 subs->pcm_substream = NULL;
921
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100926static struct snd_pcm_ops snd_usX2Y_pcm_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 .open = snd_usX2Y_pcm_open,
929 .close = snd_usX2Y_pcm_close,
930 .ioctl = snd_pcm_lib_ioctl,
931 .hw_params = snd_usX2Y_pcm_hw_params,
932 .hw_free = snd_usX2Y_pcm_hw_free,
933 .prepare = snd_usX2Y_pcm_prepare,
934 .trigger = snd_usX2Y_pcm_trigger,
935 .pointer = snd_usX2Y_pcm_pointer,
936};
937
938
939/*
940 * free a usb stream instance
941 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100942static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 if (NULL != usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]) {
945 kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
946 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
947 }
948 kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
949 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
950}
951
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100952static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100954 struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
Takashi Iwaic3e6f7d2005-11-16 18:43:35 +0100955 if (usX2Y_stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 usX2Y_audio_stream_free(usX2Y_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100959static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100961 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int err, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100963 struct snd_usX2Y_substream **usX2Y_substream =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs;
965
966 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
967 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100968 usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (NULL == usX2Y_substream[i]) {
970 snd_printk(KERN_ERR "cannot malloc\n");
971 return -ENOMEM;
972 }
973 usX2Y_substream[i]->usX2Y = usX2Y(card);
974 }
975
976 if (playback_endpoint)
977 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
978 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
979
980 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs,
981 playback_endpoint ? 1 : 0, 1,
982 &pcm);
983 if (err < 0) {
984 usX2Y_audio_stream_free(usX2Y_substream);
985 return err;
986 }
987
988 if (playback_endpoint)
989 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
990 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
991
992 pcm->private_data = usX2Y_substream;
993 pcm->private_free = snd_usX2Y_pcm_private_free;
994 pcm->info_flags = 0;
995
996 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs);
997
998 if ((playback_endpoint &&
999 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1000 SNDRV_DMA_TYPE_CONTINUOUS,
1001 snd_dma_continuous_data(GFP_KERNEL),
1002 64*1024, 128*1024))) ||
1003 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1004 SNDRV_DMA_TYPE_CONTINUOUS,
1005 snd_dma_continuous_data(GFP_KERNEL),
1006 64*1024, 128*1024))) {
1007 snd_usX2Y_pcm_private_free(pcm);
1008 return err;
1009 }
1010 usX2Y(card)->chip.pcm_devs++;
1011
1012 return 0;
1013}
1014
1015/*
1016 * create a chip instance and set its names.
1017 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +01001018int usX2Y_audio_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 int err = 0;
1021
1022 INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list);
1023
1024 if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1025 return err;
1026 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428)
1027 if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1028 return err;
1029 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122)
1030 err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
1031 return err;
1032}