blob: 9efd27f6b52f0ee7da14dda8777a050945c98086 [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>
35#include <linux/usb.h>
36#include <sound/core.h>
37#include <sound/info.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include "usx2y.h"
41#include "usbusx2y.h"
42
43#define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb.
44 1 to 4 have been tested ok on uhci.
45 To use 3 on ohci, you'd need a patch:
46 look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
47 "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
48 .
49 1, 2 and 4 work out of the box on ohci, if I recall correctly.
50 Bigger is safer operation,
51 smaller gives lower latencies.
52 */
53#define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter
54 nrpacks set to 1, you might as well comment
55 this #define out, and thereby produce smaller, faster code.
56 You'd also set USX2Y_NRPACKS to 1 then.
57 */
58
59#ifdef USX2Y_NRPACKS_VARIABLE
60 static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
61 #define nr_of_packs() nrpacks
62 module_param(nrpacks, int, 0444);
63 MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
64#else
65 #define nr_of_packs() USX2Y_NRPACKS
66#endif
67
68
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010069static int usX2Y_urb_capt_retire(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct urb *urb = subs->completed_urb;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010072 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned char *cp;
74 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010075 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 for (i = 0; i < nr_of_packs(); i++) {
78 cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
79 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010080 snd_printk(KERN_ERR "active frame status %i. "
81 "Most propably some hardware problem.\n",
82 urb->iso_frame_desc[i].status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return urb->iso_frame_desc[i].status;
84 }
85 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
86 if (! len) {
87 snd_printd("0 == len ERROR!\n");
88 continue;
89 }
90
91 /* copy a data chunk */
92 if ((hwptr_done + len) > runtime->buffer_size) {
93 int cnt = runtime->buffer_size - hwptr_done;
94 int blen = cnt * usX2Y->stride;
95 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
96 memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
97 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010098 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp,
99 len * usX2Y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 lens += len;
102 if ((hwptr_done += len) >= runtime->buffer_size)
103 hwptr_done -= runtime->buffer_size;
104 }
105
106 subs->hwptr_done = hwptr_done;
107 subs->transfer_done += lens;
108 /* update the pointer, call callback if necessary */
109 if (subs->transfer_done >= runtime->period_size) {
110 subs->transfer_done -= runtime->period_size;
111 snd_pcm_period_elapsed(subs->pcm_substream);
112 }
113 return 0;
114}
115/*
116 * prepare urb for playback data pipe
117 *
118 * we copy the data directly from the pcm buffer.
119 * the current position to be copied is held in hwptr field.
120 * since a urb can handle only a single linear buffer, if the total
121 * transferred area overflows the buffer boundary, we cannot send
122 * it directly from the buffer. thus the data is once copied to
123 * a temporary buffer and urb points to that.
124 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100125static int usX2Y_urb_play_prepare(struct snd_usX2Y_substream *subs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct urb *cap_urb,
127 struct urb *urb)
128{
129 int count, counts, pack;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100130 struct usX2Ydev *usX2Y = subs->usX2Y;
131 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 count = 0;
134 for (pack = 0; pack < nr_of_packs(); pack++) {
135 /* calculate the size of a packet */
136 counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
137 count += counts;
138 if (counts < 43 || counts > 50) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200139 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -EPIPE;
141 }
142 /* set up descriptor */
143 urb->iso_frame_desc[pack].offset = pack ?
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100144 urb->iso_frame_desc[pack - 1].offset +
145 urb->iso_frame_desc[pack - 1].length :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 0;
147 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
148 }
149 if (atomic_read(&subs->state) >= state_PRERUNNING)
150 if (subs->hwptr + count > runtime->buffer_size) {
151 /* err, the transferred area goes over buffer boundary.
152 * copy the data to the temp buffer.
153 */
154 int len;
155 len = runtime->buffer_size - subs->hwptr;
156 urb->transfer_buffer = subs->tmpbuf;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100157 memcpy(subs->tmpbuf, runtime->dma_area +
158 subs->hwptr * usX2Y->stride, len * usX2Y->stride);
159 memcpy(subs->tmpbuf + len * usX2Y->stride,
160 runtime->dma_area, (count - len) * usX2Y->stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 subs->hwptr += count;
162 subs->hwptr -= runtime->buffer_size;
163 } else {
164 /* set the buffer pointer */
165 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
166 if ((subs->hwptr += count) >= runtime->buffer_size)
167 subs->hwptr -= runtime->buffer_size;
168 }
169 else
170 urb->transfer_buffer = subs->tmpbuf;
171 urb->transfer_buffer_length = count * usX2Y->stride;
172 return 0;
173}
174
175/*
176 * process after playback data complete
177 *
178 * update the current position and call callback if a period is processed.
179 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100180static void usX2Y_urb_play_retire(struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100182 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int len = urb->actual_length / subs->usX2Y->stride;
184
185 subs->transfer_done += len;
186 subs->hwptr_done += len;
187 if (subs->hwptr_done >= runtime->buffer_size)
188 subs->hwptr_done -= runtime->buffer_size;
189 if (subs->transfer_done >= runtime->period_size) {
190 subs->transfer_done -= runtime->period_size;
191 snd_pcm_period_elapsed(subs->pcm_substream);
192 }
193}
194
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100195static int usX2Y_urb_submit(struct snd_usX2Y_substream *subs, struct urb *urb, int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int err;
198 if (!urb)
199 return -ENODEV;
200 urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
201 urb->hcpriv = NULL;
202 urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */
203 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200204 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return err;
206 }
207 return 0;
208}
209
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100210static inline int usX2Y_usbframe_complete(struct snd_usX2Y_substream *capsubs,
211 struct snd_usX2Y_substream *playbacksubs,
212 int frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int err, state;
Takashi Iwaicb432372005-11-17 10:48:52 +0100215 struct urb *urb = playbacksubs->completed_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Takashi Iwaicb432372005-11-17 10:48:52 +0100217 state = atomic_read(&playbacksubs->state);
218 if (NULL != urb) {
219 if (state == state_RUNNING)
220 usX2Y_urb_play_retire(playbacksubs, urb);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100221 else if (state >= state_PRERUNNING)
222 atomic_inc(&playbacksubs->state);
Takashi Iwaicb432372005-11-17 10:48:52 +0100223 } else {
224 switch (state) {
225 case state_STARTING1:
226 urb = playbacksubs->urb[0];
227 atomic_inc(&playbacksubs->state);
228 break;
229 case state_STARTING2:
230 urb = playbacksubs->urb[1];
231 atomic_inc(&playbacksubs->state);
232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100235 if (urb) {
236 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100237 (err = usX2Y_urb_submit(playbacksubs, urb, frame))) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100238 return err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100239 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100240 }
241
242 playbacksubs->completed_urb = NULL;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 state = atomic_read(&capsubs->state);
245 if (state >= state_PREPARED) {
246 if (state == state_RUNNING) {
247 if ((err = usX2Y_urb_capt_retire(capsubs)))
248 return err;
Takashi Iwaicb432372005-11-17 10:48:52 +0100249 } else if (state >= state_PRERUNNING)
250 atomic_inc(&capsubs->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
252 return err;
253 }
254 capsubs->completed_urb = NULL;
255 return 0;
256}
257
258
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100259static void usX2Y_clients_stop(struct usX2Ydev *usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int s, u;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100264 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (subs) {
266 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
267 atomic_set(&subs->state, state_STOPPED);
268 }
269 }
270 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100271 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (subs) {
273 if (atomic_read(&subs->state) >= state_PRERUNNING) {
274 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
275 }
276 for (u = 0; u < NRURBS; u++) {
277 struct urb *urb = subs->urb[u];
278 if (NULL != urb)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100279 snd_printdd("%i status=%i start_frame=%i\n",
280 u, urb->status, urb->start_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
283 }
284 usX2Y->prepare_subs = NULL;
285 wake_up(&usX2Y->prepare_wait_queue);
286}
287
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100288static void usX2Y_error_urb_status(struct usX2Ydev *usX2Y,
289 struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200291 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 urb->status = 0;
293 usX2Y_clients_stop(usX2Y);
294}
295
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100296static void usX2Y_error_sequence(struct usX2Ydev *usX2Y,
297 struct snd_usX2Y_substream *subs, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Joe Perchesad361c92009-07-06 13:05:40 -0700299 snd_printk(KERN_ERR
300"Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
301"Most propably some urb of usb-frame %i is still missing.\n"
302"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
David Howells7d12e782006-10-05 14:55:46 +0100309static void i_usX2Y_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
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 }
Karsten Wiesebc6191b2007-01-10 19:02:26 +0100325 if (likely((urb->start_frame & 0xFFFF) == (usX2Y->wait_iso_frame & 0xFFFF)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 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)) {
Karsten Wiese635bbb32006-10-04 17:17:32 +0200338 if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
339 usX2Y->wait_iso_frame += nr_of_packs();
340 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 snd_printdd("\n");
342 usX2Y_clients_stop(usX2Y);
343 }
344 }
345 }
346}
347
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100348static void usX2Y_urbs_set_complete(struct usX2Ydev * usX2Y,
David Howells7d12e782006-10-05 14:55:46 +0100349 void (*complete)(struct urb *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 int s, u;
352 for (s = 0; s < 4; s++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100353 struct snd_usX2Y_substream *subs = usX2Y->subs[s];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (NULL != subs)
355 for (u = 0; u < NRURBS; u++) {
356 struct urb * urb = subs->urb[u];
357 if (NULL != urb)
358 urb->complete = complete;
359 }
360 }
361}
362
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100363static void usX2Y_subs_startup_finish(struct usX2Ydev * usX2Y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
366 usX2Y->prepare_subs = NULL;
367}
368
David Howells7d12e782006-10-05 14:55:46 +0100369static void i_usX2Y_subs_startup(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100371 struct snd_usX2Y_substream *subs = urb->context;
372 struct usX2Ydev *usX2Y = subs->usX2Y;
373 struct snd_usX2Y_substream *prepare_subs = usX2Y->prepare_subs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (NULL != prepare_subs)
375 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
376 usX2Y_subs_startup_finish(usX2Y);
377 atomic_inc(&prepare_subs->state);
378 wake_up(&usX2Y->prepare_wait_queue);
379 }
380
David Howells7d12e782006-10-05 14:55:46 +0100381 i_usX2Y_urb_complete(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100384static void usX2Y_subs_prepare(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100386 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
387 subs, subs->endpoint, subs->urb[0], subs->urb[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* reset the pointer */
389 subs->hwptr = 0;
390 subs->hwptr_done = 0;
391 subs->transfer_done = 0;
392}
393
394
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100395static void usX2Y_urb_release(struct urb **urb, int free_tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 if (*urb) {
398 usb_kill_urb(*urb);
399 if (free_tb)
400 kfree((*urb)->transfer_buffer);
401 usb_free_urb(*urb);
402 *urb = NULL;
403 }
404}
405/*
406 * release a substreams urbs
407 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100408static void usX2Y_urbs_release(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int i;
411 snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
412 for (i = 0; i < NRURBS; i++)
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100413 usX2Y_urb_release(subs->urb + i,
414 subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jesper Juhl4d572772005-05-30 17:30:32 +0200416 kfree(subs->tmpbuf);
417 subs->tmpbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419/*
420 * initialize a substream's urbs
421 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100422static int usX2Y_urbs_allocate(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int i;
425 unsigned int pipe;
426 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
427 struct usb_device *dev = subs->usX2Y->chip.dev;
428
429 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
430 usb_rcvisocpipe(dev, subs->endpoint);
431 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
432 if (!subs->maxpacksize)
433 return -EINVAL;
434
435 if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
436 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
437 if (NULL == subs->tmpbuf) {
438 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
439 return -ENOMEM;
440 }
441 }
442 /* allocate and initialize data urbs */
443 for (i = 0; i < NRURBS; i++) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100444 struct urb **purb = subs->urb + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (*purb) {
446 usb_kill_urb(*purb);
447 continue;
448 }
449 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
450 if (NULL == *purb) {
451 usX2Y_urbs_release(subs);
452 return -ENOMEM;
453 }
454 if (!is_playback && !(*purb)->transfer_buffer) {
455 /* allocate a capture buffer per urb */
456 (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
457 if (NULL == (*purb)->transfer_buffer) {
458 usX2Y_urbs_release(subs);
459 return -ENOMEM;
460 }
461 }
462 (*purb)->dev = dev;
463 (*purb)->pipe = pipe;
464 (*purb)->number_of_packets = nr_of_packs();
465 (*purb)->context = subs;
466 (*purb)->interval = 1;
467 (*purb)->complete = i_usX2Y_subs_startup;
468 }
469 return 0;
470}
471
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100472static void usX2Y_subs_startup(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100474 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 usX2Y->prepare_subs = subs;
476 subs->urb[0]->start_frame = -1;
477 wmb();
478 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
479}
480
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100481static int usX2Y_urbs_start(struct snd_usX2Y_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 int i, err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100484 struct usX2Ydev *usX2Y = subs->usX2Y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if ((err = usX2Y_urbs_allocate(subs)) < 0)
487 return err;
488 subs->completed_urb = NULL;
489 for (i = 0; i < 4; i++) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100490 struct snd_usX2Y_substream *subs = usX2Y->subs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
492 goto start;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Takashi Iwaicb432372005-11-17 10:48:52 +0100495 start:
496 usX2Y_subs_startup(subs);
497 for (i = 0; i < NRURBS; i++) {
498 struct urb *urb = subs->urb[i];
499 if (usb_pipein(urb->pipe)) {
500 unsigned long pack;
501 if (0 == i)
502 atomic_set(&subs->state, state_STARTING3);
503 urb->dev = usX2Y->chip.dev;
504 urb->transfer_flags = URB_ISO_ASAP;
505 for (pack = 0; pack < nr_of_packs(); pack++) {
506 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
507 urb->iso_frame_desc[pack].length = subs->maxpacksize;
508 }
509 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
510 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
511 snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
512 err = -EPIPE;
513 goto cleanup;
Karsten Wiese635bbb32006-10-04 17:17:32 +0200514 } else
515 if (i == 0)
Takashi Iwaicb432372005-11-17 10:48:52 +0100516 usX2Y->wait_iso_frame = urb->start_frame;
Takashi Iwaicb432372005-11-17 10:48:52 +0100517 urb->transfer_flags = 0;
518 } else {
519 atomic_set(&subs->state, state_STARTING1);
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100523 err = 0;
524 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
525 if (atomic_read(&subs->state) != state_PREPARED)
526 err = -EPIPE;
527
528 cleanup:
529 if (err) {
530 usX2Y_subs_startup_finish(usX2Y);
531 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return err;
534}
535
536/*
537 * return the current pcm pointer. just return the hwptr_done value.
538 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100539static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100541 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return subs->hwptr_done;
543}
544/*
545 * start/stop substream
546 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100547static int snd_usX2Y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100549 struct snd_usX2Y_substream *subs = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 switch (cmd) {
552 case SNDRV_PCM_TRIGGER_START:
553 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
554 if (atomic_read(&subs->state) == state_PREPARED &&
555 atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
556 atomic_set(&subs->state, state_PRERUNNING);
557 } else {
558 snd_printdd("\n");
559 return -EPIPE;
560 }
561 break;
562 case SNDRV_PCM_TRIGGER_STOP:
563 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
564 if (atomic_read(&subs->state) >= state_PRERUNNING)
565 atomic_set(&subs->state, state_PREPARED);
566 break;
567 default:
568 return -EINVAL;
569 }
570 return 0;
571}
572
573
574/*
575 * allocate a buffer, setup samplerate
576 *
577 * so far we use a physically linear buffer although packetize transfer
578 * doesn't need a continuous area.
579 * if sg buffer is supported on the later version of alsa, we'll follow
580 * that.
581 */
582static struct s_c2
583{
584 char c1, c2;
585}
586 SetRate44100[] =
587{
588 { 0x14, 0x08}, // this line sets 44100, well actually a little less
589 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
590 { 0x18, 0x42},
591 { 0x18, 0x45},
592 { 0x18, 0x46},
593 { 0x18, 0x48},
594 { 0x18, 0x4A},
595 { 0x18, 0x4C},
596 { 0x18, 0x4E},
597 { 0x18, 0x50},
598 { 0x18, 0x52},
599 { 0x18, 0x54},
600 { 0x18, 0x56},
601 { 0x18, 0x58},
602 { 0x18, 0x5A},
603 { 0x18, 0x5C},
604 { 0x18, 0x5E},
605 { 0x18, 0x60},
606 { 0x18, 0x62},
607 { 0x18, 0x64},
608 { 0x18, 0x66},
609 { 0x18, 0x68},
610 { 0x18, 0x6A},
611 { 0x18, 0x6C},
612 { 0x18, 0x6E},
613 { 0x18, 0x70},
614 { 0x18, 0x72},
615 { 0x18, 0x74},
616 { 0x18, 0x76},
617 { 0x18, 0x78},
618 { 0x18, 0x7A},
619 { 0x18, 0x7C},
620 { 0x18, 0x7E}
621};
622static struct s_c2 SetRate48000[] =
623{
624 { 0x14, 0x09}, // this line sets 48000, well actually a little less
625 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
626 { 0x18, 0x42},
627 { 0x18, 0x45},
628 { 0x18, 0x46},
629 { 0x18, 0x48},
630 { 0x18, 0x4A},
631 { 0x18, 0x4C},
632 { 0x18, 0x4E},
633 { 0x18, 0x50},
634 { 0x18, 0x52},
635 { 0x18, 0x54},
636 { 0x18, 0x56},
637 { 0x18, 0x58},
638 { 0x18, 0x5A},
639 { 0x18, 0x5C},
640 { 0x18, 0x5E},
641 { 0x18, 0x60},
642 { 0x18, 0x62},
643 { 0x18, 0x64},
644 { 0x18, 0x66},
645 { 0x18, 0x68},
646 { 0x18, 0x6A},
647 { 0x18, 0x6C},
648 { 0x18, 0x6E},
649 { 0x18, 0x70},
650 { 0x18, 0x73},
651 { 0x18, 0x74},
652 { 0x18, 0x76},
653 { 0x18, 0x78},
654 { 0x18, 0x7A},
655 { 0x18, 0x7C},
656 { 0x18, 0x7E}
657};
658#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
659
David Howells7d12e782006-10-05 14:55:46 +0100660static void i_usX2Y_04Int(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100662 struct usX2Ydev *usX2Y = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200664 if (urb->status)
665 snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (0 == --usX2Y->US04->len)
667 wake_up(&usX2Y->In04WaitQueue);
668}
669
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100670static int usX2Y_rate_set(struct usX2Ydev *usX2Y, int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int err = 0, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100673 struct snd_usX2Y_urbSeq *us = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int *usbdata = NULL;
675 struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
676
677 if (usX2Y->rate != rate) {
Takashi Iwaicb432372005-11-17 10:48:52 +0100678 us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (NULL == us) {
680 err = -ENOMEM;
681 goto cleanup;
682 }
Takashi Iwaicb432372005-11-17 10:48:52 +0100683 usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (NULL == usbdata) {
685 err = -ENOMEM;
686 goto cleanup;
687 }
688 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
689 if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
690 err = -ENOMEM;
691 goto cleanup;
692 }
693 ((char*)(usbdata + i))[0] = ra[i].c1;
694 ((char*)(usbdata + i))[1] = ra[i].c2;
695 usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4),
696 usbdata + i, 2, i_usX2Y_04Int, usX2Y);
697#ifdef OLD_USB
698 us->urb[i]->transfer_flags = USB_QUEUE_BULK;
699#endif
700 }
701 us->submitted = 0;
702 us->len = NOOF_SETRATE_URBS;
703 usX2Y->US04 = us;
704 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
705 usX2Y->US04 = NULL;
706 if (us->len)
707 err = -ENODEV;
708 cleanup:
709 if (us) {
710 us->submitted = 2*NOOF_SETRATE_URBS;
711 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
712 struct urb *urb = us->urb[i];
713 if (urb->status) {
714 if (!err)
715 err = -ENODEV;
716 usb_kill_urb(urb);
717 }
718 usb_free_urb(urb);
719 }
720 usX2Y->US04 = NULL;
721 kfree(usbdata);
722 kfree(us);
Takashi Iwaicb432372005-11-17 10:48:52 +0100723 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 usX2Y->rate = rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
727
728 return err;
729}
730
731
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100732static int usX2Y_format_set(struct usX2Ydev *usX2Y, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 int alternate, err;
735 struct list_head* p;
736 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
737 alternate = 2;
738 usX2Y->stride = 6;
739 } else {
740 alternate = 1;
741 usX2Y->stride = 4;
742 }
743 list_for_each(p, &usX2Y->chip.midi_list) {
744 snd_usbmidi_input_stop(p);
745 }
746 usb_kill_urb(usX2Y->In04urb);
747 if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200748 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return err;
750 }
751 usX2Y->In04urb->dev = usX2Y->chip.dev;
752 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
753 list_for_each(p, &usX2Y->chip.midi_list) {
754 snd_usbmidi_input_start(p);
755 }
756 usX2Y->format = format;
757 usX2Y->rate = 0;
758 return err;
759}
760
761
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100762static int snd_usX2Y_pcm_hw_params(struct snd_pcm_substream *substream,
763 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 int err = 0;
766 unsigned int rate = params_rate(hw_params);
767 snd_pcm_format_t format = params_format(hw_params);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100768 struct snd_card *card = substream->pstr->pcm->card;
769 struct list_head *list;
Takashi Iwaicb432372005-11-17 10:48:52 +0100770
771 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
772 // all pcm substreams off one usX2Y have to operate at the same rate & format
773 list_for_each(list, &card->devices) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100774 struct snd_device *dev;
775 struct snd_pcm *pcm;
Takashi Iwaicb432372005-11-17 10:48:52 +0100776 int s;
777 dev = snd_device(list);
778 if (dev->type != SNDRV_DEV_PCM)
779 continue;
780 pcm = dev->device_data;
781 for (s = 0; s < 2; ++s) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100782 struct snd_pcm_substream *test_substream;
Takashi Iwaicb432372005-11-17 10:48:52 +0100783 test_substream = pcm->streams[s].substream;
784 if (test_substream && test_substream != substream &&
785 test_substream->runtime &&
786 ((test_substream->runtime->format &&
787 test_substream->runtime->format != format) ||
788 (test_substream->runtime->rate &&
789 test_substream->runtime->rate != rate)))
790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 }
793 if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100794 snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n",
795 substream, params_buffer_bytes(hw_params), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return err;
797 }
798 return 0;
799}
800
801/*
802 * free the buffer
803 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100804static int snd_usX2Y_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100806 struct snd_pcm_runtime *runtime = substream->runtime;
807 struct snd_usX2Y_substream *subs = runtime->private_data;
Ingo Molnar12aa7572006-01-16 16:36:05 +0100808 mutex_lock(&subs->usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
810
811 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100812 struct snd_usX2Y_substream *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 atomic_set(&subs->state, state_STOPPED);
814 usX2Y_urbs_release(subs);
815 if (!cap_subs->pcm_substream ||
816 !cap_subs->pcm_substream->runtime ||
817 !cap_subs->pcm_substream->runtime->status ||
818 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
819 atomic_set(&cap_subs->state, state_STOPPED);
820 usX2Y_urbs_release(cap_subs);
821 }
822 } else {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100823 struct snd_usX2Y_substream *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (atomic_read(&playback_subs->state) < state_PREPARED) {
825 atomic_set(&subs->state, state_STOPPED);
826 usX2Y_urbs_release(subs);
827 }
828 }
Ingo Molnar12aa7572006-01-16 16:36:05 +0100829 mutex_unlock(&subs->usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return snd_pcm_lib_free_pages(substream);
831}
832/*
833 * prepare callback
834 *
835 * set format and initialize urbs
836 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100837static int snd_usX2Y_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100839 struct snd_pcm_runtime *runtime = substream->runtime;
840 struct snd_usX2Y_substream *subs = runtime->private_data;
841 struct usX2Ydev *usX2Y = subs->usX2Y;
842 struct snd_usX2Y_substream *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int err = 0;
844 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
845
Ingo Molnar12aa7572006-01-16 16:36:05 +0100846 mutex_lock(&usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 usX2Y_subs_prepare(subs);
848// Start hardware streams
849// SyncStream first....
850 if (atomic_read(&capsubs->state) < state_PREPARED) {
851 if (usX2Y->format != runtime->format)
852 if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
853 goto up_prepare_mutex;
854 if (usX2Y->rate != runtime->rate)
855 if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
856 goto up_prepare_mutex;
857 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
858 if (0 > (err = usX2Y_urbs_start(capsubs)))
859 goto up_prepare_mutex;
860 }
861
862 if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
863 err = usX2Y_urbs_start(subs);
864
865 up_prepare_mutex:
Ingo Molnar12aa7572006-01-16 16:36:05 +0100866 mutex_unlock(&usX2Y->prepare_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return err;
868}
869
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100870static struct snd_pcm_hardware snd_usX2Y_2c =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
873 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai2008f132009-04-28 12:25:59 +0200874 SNDRV_PCM_INFO_MMAP_VALID |
875 SNDRV_PCM_INFO_BATCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
877 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
878 .rate_min = 44100,
879 .rate_max = 48000,
880 .channels_min = 2,
881 .channels_max = 2,
882 .buffer_bytes_max = (2*128*1024),
883 .period_bytes_min = 64,
884 .period_bytes_max = (128*1024),
885 .periods_min = 2,
886 .periods_max = 1024,
887 .fifo_size = 0
888};
889
890
891
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100892static int snd_usX2Y_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100894 struct snd_usX2Y_substream *subs = ((struct snd_usX2Y_substream **)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 snd_pcm_substream_chip(substream))[substream->stream];
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100896 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
899 return -EBUSY;
900
901 runtime->hw = snd_usX2Y_2c;
902 runtime->private_data = subs;
903 subs->pcm_substream = substream;
904 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
905 return 0;
906}
907
908
909
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100910static int snd_usX2Y_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100912 struct snd_pcm_runtime *runtime = substream->runtime;
913 struct snd_usX2Y_substream *subs = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 subs->pcm_substream = NULL;
916
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100921static struct snd_pcm_ops snd_usX2Y_pcm_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 .open = snd_usX2Y_pcm_open,
924 .close = snd_usX2Y_pcm_close,
925 .ioctl = snd_pcm_lib_ioctl,
926 .hw_params = snd_usX2Y_pcm_hw_params,
927 .hw_free = snd_usX2Y_pcm_hw_free,
928 .prepare = snd_usX2Y_pcm_prepare,
929 .trigger = snd_usX2Y_pcm_trigger,
930 .pointer = snd_usX2Y_pcm_pointer,
931};
932
933
934/*
935 * free a usb stream instance
936 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100937static void usX2Y_audio_stream_free(struct snd_usX2Y_substream **usX2Y_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Richard Knutssonc111b8d2007-05-14 10:38:57 +0200939 kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
940 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
943 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
944}
945
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100946static void snd_usX2Y_pcm_private_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100948 struct snd_usX2Y_substream **usX2Y_stream = pcm->private_data;
Takashi Iwaic3e6f7d2005-11-16 18:43:35 +0100949 if (usX2Y_stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 usX2Y_audio_stream_free(usX2Y_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100953static int usX2Y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100955 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int err, i;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100957 struct snd_usX2Y_substream **usX2Y_substream =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs;
959
960 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
961 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100962 usX2Y_substream[i] = kzalloc(sizeof(struct snd_usX2Y_substream), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (NULL == usX2Y_substream[i]) {
964 snd_printk(KERN_ERR "cannot malloc\n");
965 return -ENOMEM;
966 }
967 usX2Y_substream[i]->usX2Y = usX2Y(card);
968 }
969
970 if (playback_endpoint)
971 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
972 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
973
974 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs,
975 playback_endpoint ? 1 : 0, 1,
976 &pcm);
977 if (err < 0) {
978 usX2Y_audio_stream_free(usX2Y_substream);
979 return err;
980 }
981
982 if (playback_endpoint)
983 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
984 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
985
986 pcm->private_data = usX2Y_substream;
987 pcm->private_free = snd_usX2Y_pcm_private_free;
988 pcm->info_flags = 0;
989
990 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs);
991
992 if ((playback_endpoint &&
993 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
994 SNDRV_DMA_TYPE_CONTINUOUS,
995 snd_dma_continuous_data(GFP_KERNEL),
996 64*1024, 128*1024))) ||
997 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
998 SNDRV_DMA_TYPE_CONTINUOUS,
999 snd_dma_continuous_data(GFP_KERNEL),
1000 64*1024, 128*1024))) {
1001 snd_usX2Y_pcm_private_free(pcm);
1002 return err;
1003 }
1004 usX2Y(card)->chip.pcm_devs++;
1005
1006 return 0;
1007}
1008
1009/*
1010 * create a chip instance and set its names.
1011 */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +01001012int usX2Y_audio_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 int err = 0;
1015
1016 INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list);
1017
1018 if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1019 return err;
1020 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428)
1021 if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1022 return err;
1023 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122)
1024 err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
1025 return err;
1026}