]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/usb/usx2y/usbusx2yaudio.c
[ALSA] Remove xxx_t typedefs: USB-Audio
[net-next-2.6.git] / sound / usb / usx2y / usbusx2yaudio.c
CommitLineData
1da177e4
LT
1/*
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
70static int usX2Y_urb_capt_retire(snd_usX2Y_substream_t *subs)
71{
72 struct urb *urb = subs->completed_urb;
73 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
74 unsigned char *cp;
75 int i, len, lens = 0, hwptr_done = subs->hwptr_done;
76 usX2Ydev_t *usX2Y = subs->usX2Y;
77
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 */
d3d579f8 81 snd_printk(KERN_ERR "activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status);
1da177e4
LT
82 return urb->iso_frame_desc[i].status;
83 }
84 len = urb->iso_frame_desc[i].actual_length / usX2Y->stride;
85 if (! len) {
86 snd_printd("0 == len ERROR!\n");
87 continue;
88 }
89
90 /* copy a data chunk */
91 if ((hwptr_done + len) > runtime->buffer_size) {
92 int cnt = runtime->buffer_size - hwptr_done;
93 int blen = cnt * usX2Y->stride;
94 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen);
95 memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen);
96 } else {
97 memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, len * usX2Y->stride);
98 }
99 lens += len;
100 if ((hwptr_done += len) >= runtime->buffer_size)
101 hwptr_done -= runtime->buffer_size;
102 }
103
104 subs->hwptr_done = hwptr_done;
105 subs->transfer_done += lens;
106 /* update the pointer, call callback if necessary */
107 if (subs->transfer_done >= runtime->period_size) {
108 subs->transfer_done -= runtime->period_size;
109 snd_pcm_period_elapsed(subs->pcm_substream);
110 }
111 return 0;
112}
113/*
114 * prepare urb for playback data pipe
115 *
116 * we copy the data directly from the pcm buffer.
117 * the current position to be copied is held in hwptr field.
118 * since a urb can handle only a single linear buffer, if the total
119 * transferred area overflows the buffer boundary, we cannot send
120 * it directly from the buffer. thus the data is once copied to
121 * a temporary buffer and urb points to that.
122 */
123static int usX2Y_urb_play_prepare(snd_usX2Y_substream_t *subs,
124 struct urb *cap_urb,
125 struct urb *urb)
126{
127 int count, counts, pack;
128 usX2Ydev_t* usX2Y = subs->usX2Y;
129 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
130
131 count = 0;
132 for (pack = 0; pack < nr_of_packs(); pack++) {
133 /* calculate the size of a packet */
134 counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride;
135 count += counts;
136 if (counts < 43 || counts > 50) {
d3d579f8 137 snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
1da177e4
LT
138 return -EPIPE;
139 }
140 /* set up descriptor */
141 urb->iso_frame_desc[pack].offset = pack ?
142 urb->iso_frame_desc[pack - 1].offset + urb->iso_frame_desc[pack - 1].length :
143 0;
144 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
145 }
146 if (atomic_read(&subs->state) >= state_PRERUNNING)
147 if (subs->hwptr + count > runtime->buffer_size) {
148 /* err, the transferred area goes over buffer boundary.
149 * copy the data to the temp buffer.
150 */
151 int len;
152 len = runtime->buffer_size - subs->hwptr;
153 urb->transfer_buffer = subs->tmpbuf;
154 memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * usX2Y->stride, len * usX2Y->stride);
155 memcpy(subs->tmpbuf + len * usX2Y->stride, runtime->dma_area, (count - len) * usX2Y->stride);
156 subs->hwptr += count;
157 subs->hwptr -= runtime->buffer_size;
158 } else {
159 /* set the buffer pointer */
160 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride;
161 if ((subs->hwptr += count) >= runtime->buffer_size)
162 subs->hwptr -= runtime->buffer_size;
163 }
164 else
165 urb->transfer_buffer = subs->tmpbuf;
166 urb->transfer_buffer_length = count * usX2Y->stride;
167 return 0;
168}
169
170/*
171 * process after playback data complete
172 *
173 * update the current position and call callback if a period is processed.
174 */
175static void usX2Y_urb_play_retire(snd_usX2Y_substream_t *subs, struct urb *urb)
176{
177 snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime;
178 int len = urb->actual_length / subs->usX2Y->stride;
179
180 subs->transfer_done += len;
181 subs->hwptr_done += len;
182 if (subs->hwptr_done >= runtime->buffer_size)
183 subs->hwptr_done -= runtime->buffer_size;
184 if (subs->transfer_done >= runtime->period_size) {
185 subs->transfer_done -= runtime->period_size;
186 snd_pcm_period_elapsed(subs->pcm_substream);
187 }
188}
189
190static int usX2Y_urb_submit(snd_usX2Y_substream_t *subs, struct urb *urb, int frame)
191{
192 int err;
193 if (!urb)
194 return -ENODEV;
195 urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks
196 urb->hcpriv = NULL;
197 urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */
198 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
d3d579f8 199 snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
1da177e4
LT
200 return err;
201 }
202 return 0;
203}
204
205static inline int usX2Y_usbframe_complete(snd_usX2Y_substream_t *capsubs, snd_usX2Y_substream_t *playbacksubs, int frame)
206{
207 int err, state;
cb432379
TI
208 struct urb *urb = playbacksubs->completed_urb;
209
210 state = atomic_read(&playbacksubs->state);
211 if (NULL != urb) {
212 if (state == state_RUNNING)
213 usX2Y_urb_play_retire(playbacksubs, urb);
214 else
215 if (state >= state_PRERUNNING)
1da177e4 216 atomic_inc(&playbacksubs->state);
cb432379
TI
217 } else {
218 switch (state) {
219 case state_STARTING1:
220 urb = playbacksubs->urb[0];
221 atomic_inc(&playbacksubs->state);
222 break;
223 case state_STARTING2:
224 urb = playbacksubs->urb[1];
225 atomic_inc(&playbacksubs->state);
226 break;
1da177e4 227 }
1da177e4 228 }
cb432379
TI
229 if (urb) {
230 if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
231 (err = usX2Y_urb_submit(playbacksubs, urb, frame)))
232 return err;
233 }
234
235 playbacksubs->completed_urb = NULL;
236
1da177e4
LT
237 state = atomic_read(&capsubs->state);
238 if (state >= state_PREPARED) {
239 if (state == state_RUNNING) {
240 if ((err = usX2Y_urb_capt_retire(capsubs)))
241 return err;
cb432379
TI
242 } else if (state >= state_PRERUNNING)
243 atomic_inc(&capsubs->state);
1da177e4
LT
244 if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame)))
245 return err;
246 }
247 capsubs->completed_urb = NULL;
248 return 0;
249}
250
251
252static void usX2Y_clients_stop(usX2Ydev_t *usX2Y)
253{
254 int s, u;
255 for (s = 0; s < 4; s++) {
256 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
257 if (subs) {
258 snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
259 atomic_set(&subs->state, state_STOPPED);
260 }
261 }
262 for (s = 0; s < 4; s++) {
263 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
264 if (subs) {
265 if (atomic_read(&subs->state) >= state_PRERUNNING) {
266 snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
267 }
268 for (u = 0; u < NRURBS; u++) {
269 struct urb *urb = subs->urb[u];
270 if (NULL != urb)
271 snd_printdd("%i status=%i start_frame=%i\n", u, urb->status, urb->start_frame);
272 }
273 }
274 }
275 usX2Y->prepare_subs = NULL;
276 wake_up(&usX2Y->prepare_wait_queue);
277}
278
279static void usX2Y_error_urb_status(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
280{
d3d579f8 281 snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
1da177e4
LT
282 urb->status = 0;
283 usX2Y_clients_stop(usX2Y);
284}
285
286static void usX2Y_error_sequence(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb)
287{
d3d579f8
TI
288 snd_printk(KERN_ERR "Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n"
289 KERN_ERR "Most propably some urb of usb-frame %i is still missing.\n"
290 KERN_ERR "Cause could be too long delays in usb-hcd interrupt handling.\n",
1da177e4
LT
291 usb_get_current_frame_number(usX2Y->chip.dev),
292 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame);
293 usX2Y_clients_stop(usX2Y);
294}
295
296static void i_usX2Y_urb_complete(struct urb *urb, struct pt_regs *regs)
297{
298 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
299 usX2Ydev_t *usX2Y = subs->usX2Y;
300
301 if (unlikely(atomic_read(&subs->state) < state_PREPARED)) {
302 snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame);
303 return;
304 }
305 if (unlikely(urb->status)) {
306 usX2Y_error_urb_status(usX2Y, subs, urb);
307 return;
308 }
309 if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame))
310 subs->completed_urb = urb;
311 else {
312 usX2Y_error_sequence(usX2Y, subs, urb);
313 return;
314 }
315 {
316 snd_usX2Y_substream_t *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE],
317 *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
318 if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED &&
319 (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) {
320 if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) {
321 if (nr_of_packs() <= urb->start_frame &&
322 urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci
323 usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs();
324 else
325 usX2Y->wait_iso_frame += nr_of_packs();
326 } else {
327 snd_printdd("\n");
328 usX2Y_clients_stop(usX2Y);
329 }
330 }
331 }
332}
333
334static void usX2Y_urbs_set_complete(usX2Ydev_t * usX2Y, void (*complete)(struct urb *, struct pt_regs *))
335{
336 int s, u;
337 for (s = 0; s < 4; s++) {
338 snd_usX2Y_substream_t *subs = usX2Y->subs[s];
339 if (NULL != subs)
340 for (u = 0; u < NRURBS; u++) {
341 struct urb * urb = subs->urb[u];
342 if (NULL != urb)
343 urb->complete = complete;
344 }
345 }
346}
347
348static void usX2Y_subs_startup_finish(usX2Ydev_t * usX2Y)
349{
350 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete);
351 usX2Y->prepare_subs = NULL;
352}
353
354static void i_usX2Y_subs_startup(struct urb *urb, struct pt_regs *regs)
355{
356 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context;
357 usX2Ydev_t *usX2Y = subs->usX2Y;
358 snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs;
359 if (NULL != prepare_subs)
360 if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
361 usX2Y_subs_startup_finish(usX2Y);
362 atomic_inc(&prepare_subs->state);
363 wake_up(&usX2Y->prepare_wait_queue);
364 }
365
366 i_usX2Y_urb_complete(urb, regs);
367}
368
369static void usX2Y_subs_prepare(snd_usX2Y_substream_t *subs)
370{
371 snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n", subs, subs->endpoint, subs->urb[0], subs->urb[1]);
372 /* reset the pointer */
373 subs->hwptr = 0;
374 subs->hwptr_done = 0;
375 subs->transfer_done = 0;
376}
377
378
379static void usX2Y_urb_release(struct urb** urb, int free_tb)
380{
381 if (*urb) {
382 usb_kill_urb(*urb);
383 if (free_tb)
384 kfree((*urb)->transfer_buffer);
385 usb_free_urb(*urb);
386 *urb = NULL;
387 }
388}
389/*
390 * release a substreams urbs
391 */
392static void usX2Y_urbs_release(snd_usX2Y_substream_t *subs)
393{
394 int i;
395 snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint);
396 for (i = 0; i < NRURBS; i++)
397 usX2Y_urb_release(subs->urb + i, subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
398
4d572776
JJ
399 kfree(subs->tmpbuf);
400 subs->tmpbuf = NULL;
1da177e4
LT
401}
402/*
403 * initialize a substream's urbs
404 */
405static int usX2Y_urbs_allocate(snd_usX2Y_substream_t *subs)
406{
407 int i;
408 unsigned int pipe;
409 int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
410 struct usb_device *dev = subs->usX2Y->chip.dev;
411
412 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
413 usb_rcvisocpipe(dev, subs->endpoint);
414 subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
415 if (!subs->maxpacksize)
416 return -EINVAL;
417
418 if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */
419 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
420 if (NULL == subs->tmpbuf) {
421 snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
422 return -ENOMEM;
423 }
424 }
425 /* allocate and initialize data urbs */
426 for (i = 0; i < NRURBS; i++) {
cb432379 427 struct urb **purb = subs->urb + i;
1da177e4
LT
428 if (*purb) {
429 usb_kill_urb(*purb);
430 continue;
431 }
432 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
433 if (NULL == *purb) {
434 usX2Y_urbs_release(subs);
435 return -ENOMEM;
436 }
437 if (!is_playback && !(*purb)->transfer_buffer) {
438 /* allocate a capture buffer per urb */
439 (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL);
440 if (NULL == (*purb)->transfer_buffer) {
441 usX2Y_urbs_release(subs);
442 return -ENOMEM;
443 }
444 }
445 (*purb)->dev = dev;
446 (*purb)->pipe = pipe;
447 (*purb)->number_of_packets = nr_of_packs();
448 (*purb)->context = subs;
449 (*purb)->interval = 1;
450 (*purb)->complete = i_usX2Y_subs_startup;
451 }
452 return 0;
453}
454
455static void usX2Y_subs_startup(snd_usX2Y_substream_t *subs)
456{
457 usX2Ydev_t *usX2Y = subs->usX2Y;
458 usX2Y->prepare_subs = subs;
459 subs->urb[0]->start_frame = -1;
460 wmb();
461 usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup);
462}
463
464static int usX2Y_urbs_start(snd_usX2Y_substream_t *subs)
465{
466 int i, err;
467 usX2Ydev_t *usX2Y = subs->usX2Y;
468
469 if ((err = usX2Y_urbs_allocate(subs)) < 0)
470 return err;
471 subs->completed_urb = NULL;
472 for (i = 0; i < 4; i++) {
473 snd_usX2Y_substream_t *subs = usX2Y->subs[i];
474 if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED)
475 goto start;
476 }
477 usX2Y->wait_iso_frame = -1;
cb432379 478
1da177e4 479 start:
cb432379
TI
480 usX2Y_subs_startup(subs);
481 for (i = 0; i < NRURBS; i++) {
482 struct urb *urb = subs->urb[i];
483 if (usb_pipein(urb->pipe)) {
484 unsigned long pack;
485 if (0 == i)
486 atomic_set(&subs->state, state_STARTING3);
487 urb->dev = usX2Y->chip.dev;
488 urb->transfer_flags = URB_ISO_ASAP;
489 for (pack = 0; pack < nr_of_packs(); pack++) {
490 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
491 urb->iso_frame_desc[pack].length = subs->maxpacksize;
492 }
493 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
494 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
495 snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
496 err = -EPIPE;
497 goto cleanup;
1da177e4 498 } else {
cb432379
TI
499 if (0 > usX2Y->wait_iso_frame)
500 usX2Y->wait_iso_frame = urb->start_frame;
1da177e4 501 }
cb432379
TI
502 urb->transfer_flags = 0;
503 } else {
504 atomic_set(&subs->state, state_STARTING1);
505 break;
1da177e4 506 }
cb432379
TI
507 }
508 err = 0;
509 wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs);
510 if (atomic_read(&subs->state) != state_PREPARED)
511 err = -EPIPE;
512
513 cleanup:
514 if (err) {
515 usX2Y_subs_startup_finish(usX2Y);
516 usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything
1da177e4
LT
517 }
518 return err;
519}
520
521/*
522 * return the current pcm pointer. just return the hwptr_done value.
523 */
524static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(snd_pcm_substream_t *substream)
525{
526 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
527 return subs->hwptr_done;
528}
529/*
530 * start/stop substream
531 */
532static int snd_usX2Y_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
533{
534 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data;
535
536 switch (cmd) {
537 case SNDRV_PCM_TRIGGER_START:
538 snd_printdd("snd_usX2Y_pcm_trigger(START)\n");
539 if (atomic_read(&subs->state) == state_PREPARED &&
540 atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) {
541 atomic_set(&subs->state, state_PRERUNNING);
542 } else {
543 snd_printdd("\n");
544 return -EPIPE;
545 }
546 break;
547 case SNDRV_PCM_TRIGGER_STOP:
548 snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n");
549 if (atomic_read(&subs->state) >= state_PRERUNNING)
550 atomic_set(&subs->state, state_PREPARED);
551 break;
552 default:
553 return -EINVAL;
554 }
555 return 0;
556}
557
558
559/*
560 * allocate a buffer, setup samplerate
561 *
562 * so far we use a physically linear buffer although packetize transfer
563 * doesn't need a continuous area.
564 * if sg buffer is supported on the later version of alsa, we'll follow
565 * that.
566 */
567static struct s_c2
568{
569 char c1, c2;
570}
571 SetRate44100[] =
572{
573 { 0x14, 0x08}, // this line sets 44100, well actually a little less
574 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
575 { 0x18, 0x42},
576 { 0x18, 0x45},
577 { 0x18, 0x46},
578 { 0x18, 0x48},
579 { 0x18, 0x4A},
580 { 0x18, 0x4C},
581 { 0x18, 0x4E},
582 { 0x18, 0x50},
583 { 0x18, 0x52},
584 { 0x18, 0x54},
585 { 0x18, 0x56},
586 { 0x18, 0x58},
587 { 0x18, 0x5A},
588 { 0x18, 0x5C},
589 { 0x18, 0x5E},
590 { 0x18, 0x60},
591 { 0x18, 0x62},
592 { 0x18, 0x64},
593 { 0x18, 0x66},
594 { 0x18, 0x68},
595 { 0x18, 0x6A},
596 { 0x18, 0x6C},
597 { 0x18, 0x6E},
598 { 0x18, 0x70},
599 { 0x18, 0x72},
600 { 0x18, 0x74},
601 { 0x18, 0x76},
602 { 0x18, 0x78},
603 { 0x18, 0x7A},
604 { 0x18, 0x7C},
605 { 0x18, 0x7E}
606};
607static struct s_c2 SetRate48000[] =
608{
609 { 0x14, 0x09}, // this line sets 48000, well actually a little less
610 { 0x18, 0x40}, // only tascam / frontier design knows the further lines .......
611 { 0x18, 0x42},
612 { 0x18, 0x45},
613 { 0x18, 0x46},
614 { 0x18, 0x48},
615 { 0x18, 0x4A},
616 { 0x18, 0x4C},
617 { 0x18, 0x4E},
618 { 0x18, 0x50},
619 { 0x18, 0x52},
620 { 0x18, 0x54},
621 { 0x18, 0x56},
622 { 0x18, 0x58},
623 { 0x18, 0x5A},
624 { 0x18, 0x5C},
625 { 0x18, 0x5E},
626 { 0x18, 0x60},
627 { 0x18, 0x62},
628 { 0x18, 0x64},
629 { 0x18, 0x66},
630 { 0x18, 0x68},
631 { 0x18, 0x6A},
632 { 0x18, 0x6C},
633 { 0x18, 0x6E},
634 { 0x18, 0x70},
635 { 0x18, 0x73},
636 { 0x18, 0x74},
637 { 0x18, 0x76},
638 { 0x18, 0x78},
639 { 0x18, 0x7A},
640 { 0x18, 0x7C},
641 { 0x18, 0x7E}
642};
643#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000)
644
645static void i_usX2Y_04Int(struct urb* urb, struct pt_regs *regs)
646{
647 usX2Ydev_t* usX2Y = urb->context;
648
d3d579f8
TI
649 if (urb->status)
650 snd_printk(KERN_ERR "snd_usX2Y_04Int() urb->status=%i\n", urb->status);
1da177e4
LT
651 if (0 == --usX2Y->US04->len)
652 wake_up(&usX2Y->In04WaitQueue);
653}
654
655static int usX2Y_rate_set(usX2Ydev_t *usX2Y, int rate)
656{
657 int err = 0, i;
658 snd_usX2Y_urbSeq_t *us = NULL;
659 int *usbdata = NULL;
660 struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100;
661
662 if (usX2Y->rate != rate) {
cb432379 663 us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
1da177e4
LT
664 if (NULL == us) {
665 err = -ENOMEM;
666 goto cleanup;
667 }
cb432379 668 usbdata = kmalloc(sizeof(int) * NOOF_SETRATE_URBS, GFP_KERNEL);
1da177e4
LT
669 if (NULL == usbdata) {
670 err = -ENOMEM;
671 goto cleanup;
672 }
673 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
674 if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
675 err = -ENOMEM;
676 goto cleanup;
677 }
678 ((char*)(usbdata + i))[0] = ra[i].c1;
679 ((char*)(usbdata + i))[1] = ra[i].c2;
680 usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4),
681 usbdata + i, 2, i_usX2Y_04Int, usX2Y);
682#ifdef OLD_USB
683 us->urb[i]->transfer_flags = USB_QUEUE_BULK;
684#endif
685 }
686 us->submitted = 0;
687 us->len = NOOF_SETRATE_URBS;
688 usX2Y->US04 = us;
689 wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ);
690 usX2Y->US04 = NULL;
691 if (us->len)
692 err = -ENODEV;
693 cleanup:
694 if (us) {
695 us->submitted = 2*NOOF_SETRATE_URBS;
696 for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
697 struct urb *urb = us->urb[i];
698 if (urb->status) {
699 if (!err)
700 err = -ENODEV;
701 usb_kill_urb(urb);
702 }
703 usb_free_urb(urb);
704 }
705 usX2Y->US04 = NULL;
706 kfree(usbdata);
707 kfree(us);
cb432379 708 if (!err)
1da177e4 709 usX2Y->rate = rate;
1da177e4
LT
710 }
711 }
712
713 return err;
714}
715
716
717static int usX2Y_format_set(usX2Ydev_t *usX2Y, snd_pcm_format_t format)
718{
719 int alternate, err;
720 struct list_head* p;
721 if (format == SNDRV_PCM_FORMAT_S24_3LE) {
722 alternate = 2;
723 usX2Y->stride = 6;
724 } else {
725 alternate = 1;
726 usX2Y->stride = 4;
727 }
728 list_for_each(p, &usX2Y->chip.midi_list) {
729 snd_usbmidi_input_stop(p);
730 }
731 usb_kill_urb(usX2Y->In04urb);
732 if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) {
d3d579f8 733 snd_printk(KERN_ERR "usb_set_interface error \n");
1da177e4
LT
734 return err;
735 }
736 usX2Y->In04urb->dev = usX2Y->chip.dev;
737 err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL);
738 list_for_each(p, &usX2Y->chip.midi_list) {
739 snd_usbmidi_input_start(p);
740 }
741 usX2Y->format = format;
742 usX2Y->rate = 0;
743 return err;
744}
745
746
747static int snd_usX2Y_pcm_hw_params(snd_pcm_substream_t *substream,
748 snd_pcm_hw_params_t *hw_params)
749{
750 int err = 0;
751 unsigned int rate = params_rate(hw_params);
752 snd_pcm_format_t format = params_format(hw_params);
1da177e4
LT
753 snd_card_t *card = substream->pstr->pcm->card;
754 struct list_head *list;
cb432379
TI
755
756 snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params);
757 // all pcm substreams off one usX2Y have to operate at the same rate & format
758 list_for_each(list, &card->devices) {
759 snd_device_t *dev;
760 snd_pcm_t *pcm;
761 int s;
762 dev = snd_device(list);
763 if (dev->type != SNDRV_DEV_PCM)
764 continue;
765 pcm = dev->device_data;
766 for (s = 0; s < 2; ++s) {
767 snd_pcm_substream_t *test_substream;
768 test_substream = pcm->streams[s].substream;
769 if (test_substream && test_substream != substream &&
770 test_substream->runtime &&
771 ((test_substream->runtime->format &&
772 test_substream->runtime->format != format) ||
773 (test_substream->runtime->rate &&
774 test_substream->runtime->rate != rate)))
775 return -EINVAL;
1da177e4
LT
776 }
777 }
778 if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) {
d3d579f8 779 snd_printk(KERN_ERR "snd_pcm_lib_malloc_pages(%p, %i) returned %i\n", substream, params_buffer_bytes(hw_params), err);
1da177e4
LT
780 return err;
781 }
782 return 0;
783}
784
785/*
786 * free the buffer
787 */
788static int snd_usX2Y_pcm_hw_free(snd_pcm_substream_t *substream)
789{
790 snd_pcm_runtime_t *runtime = substream->runtime;
791 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
792 down(&subs->usX2Y->prepare_mutex);
793 snd_printdd("snd_usX2Y_hw_free(%p)\n", substream);
794
795 if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
796 snd_usX2Y_substream_t *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
797 atomic_set(&subs->state, state_STOPPED);
798 usX2Y_urbs_release(subs);
799 if (!cap_subs->pcm_substream ||
800 !cap_subs->pcm_substream->runtime ||
801 !cap_subs->pcm_substream->runtime->status ||
802 cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
803 atomic_set(&cap_subs->state, state_STOPPED);
804 usX2Y_urbs_release(cap_subs);
805 }
806 } else {
807 snd_usX2Y_substream_t *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK];
808 if (atomic_read(&playback_subs->state) < state_PREPARED) {
809 atomic_set(&subs->state, state_STOPPED);
810 usX2Y_urbs_release(subs);
811 }
812 }
813 up(&subs->usX2Y->prepare_mutex);
814 return snd_pcm_lib_free_pages(substream);
815}
816/*
817 * prepare callback
818 *
819 * set format and initialize urbs
820 */
821static int snd_usX2Y_pcm_prepare(snd_pcm_substream_t *substream)
822{
823 snd_pcm_runtime_t *runtime = substream->runtime;
824 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
825 usX2Ydev_t *usX2Y = subs->usX2Y;
826 snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE];
827 int err = 0;
828 snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream);
829
830 down(&usX2Y->prepare_mutex);
831 usX2Y_subs_prepare(subs);
832// Start hardware streams
833// SyncStream first....
834 if (atomic_read(&capsubs->state) < state_PREPARED) {
835 if (usX2Y->format != runtime->format)
836 if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0)
837 goto up_prepare_mutex;
838 if (usX2Y->rate != runtime->rate)
839 if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0)
840 goto up_prepare_mutex;
841 snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
842 if (0 > (err = usX2Y_urbs_start(capsubs)))
843 goto up_prepare_mutex;
844 }
845
846 if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED)
847 err = usX2Y_urbs_start(subs);
848
849 up_prepare_mutex:
850 up(&usX2Y->prepare_mutex);
851 return err;
852}
853
854static snd_pcm_hardware_t snd_usX2Y_2c =
855{
856 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
857 SNDRV_PCM_INFO_BLOCK_TRANSFER |
858 SNDRV_PCM_INFO_MMAP_VALID),
859 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
860 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
861 .rate_min = 44100,
862 .rate_max = 48000,
863 .channels_min = 2,
864 .channels_max = 2,
865 .buffer_bytes_max = (2*128*1024),
866 .period_bytes_min = 64,
867 .period_bytes_max = (128*1024),
868 .periods_min = 2,
869 .periods_max = 1024,
870 .fifo_size = 0
871};
872
873
874
875static int snd_usX2Y_pcm_open(snd_pcm_substream_t *substream)
876{
877 snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **)
878 snd_pcm_substream_chip(substream))[substream->stream];
879 snd_pcm_runtime_t *runtime = substream->runtime;
880
881 if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
882 return -EBUSY;
883
884 runtime->hw = snd_usX2Y_2c;
885 runtime->private_data = subs;
886 subs->pcm_substream = substream;
887 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
888 return 0;
889}
890
891
892
893static int snd_usX2Y_pcm_close(snd_pcm_substream_t *substream)
894{
895 snd_pcm_runtime_t *runtime = substream->runtime;
896 snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data;
897 int err = 0;
898
899 subs->pcm_substream = NULL;
900
901 return err;
902}
903
904
905static snd_pcm_ops_t snd_usX2Y_pcm_ops =
906{
907 .open = snd_usX2Y_pcm_open,
908 .close = snd_usX2Y_pcm_close,
909 .ioctl = snd_pcm_lib_ioctl,
910 .hw_params = snd_usX2Y_pcm_hw_params,
911 .hw_free = snd_usX2Y_pcm_hw_free,
912 .prepare = snd_usX2Y_pcm_prepare,
913 .trigger = snd_usX2Y_pcm_trigger,
914 .pointer = snd_usX2Y_pcm_pointer,
915};
916
917
918/*
919 * free a usb stream instance
920 */
921static void usX2Y_audio_stream_free(snd_usX2Y_substream_t **usX2Y_substream)
922{
923 if (NULL != usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]) {
924 kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]);
925 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL;
926 }
927 kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]);
928 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL;
929}
930
931static void snd_usX2Y_pcm_private_free(snd_pcm_t *pcm)
932{
933 snd_usX2Y_substream_t **usX2Y_stream = pcm->private_data;
c3e6f7d8 934 if (usX2Y_stream)
1da177e4 935 usX2Y_audio_stream_free(usX2Y_stream);
1da177e4
LT
936}
937
938static int usX2Y_audio_stream_new(snd_card_t *card, int playback_endpoint, int capture_endpoint)
939{
940 snd_pcm_t *pcm;
941 int err, i;
942 snd_usX2Y_substream_t **usX2Y_substream =
943 usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs;
944
945 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
946 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
561b220a 947 usX2Y_substream[i] = kzalloc(sizeof(snd_usX2Y_substream_t), GFP_KERNEL);
1da177e4
LT
948 if (NULL == usX2Y_substream[i]) {
949 snd_printk(KERN_ERR "cannot malloc\n");
950 return -ENOMEM;
951 }
952 usX2Y_substream[i]->usX2Y = usX2Y(card);
953 }
954
955 if (playback_endpoint)
956 usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
957 usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
958
959 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs,
960 playback_endpoint ? 1 : 0, 1,
961 &pcm);
962 if (err < 0) {
963 usX2Y_audio_stream_free(usX2Y_substream);
964 return err;
965 }
966
967 if (playback_endpoint)
968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops);
969 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops);
970
971 pcm->private_data = usX2Y_substream;
972 pcm->private_free = snd_usX2Y_pcm_private_free;
973 pcm->info_flags = 0;
974
975 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs);
976
977 if ((playback_endpoint &&
978 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
979 SNDRV_DMA_TYPE_CONTINUOUS,
980 snd_dma_continuous_data(GFP_KERNEL),
981 64*1024, 128*1024))) ||
982 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
983 SNDRV_DMA_TYPE_CONTINUOUS,
984 snd_dma_continuous_data(GFP_KERNEL),
985 64*1024, 128*1024))) {
986 snd_usX2Y_pcm_private_free(pcm);
987 return err;
988 }
989 usX2Y(card)->chip.pcm_devs++;
990
991 return 0;
992}
993
994/*
995 * create a chip instance and set its names.
996 */
997int usX2Y_audio_create(snd_card_t* card)
998{
999 int err = 0;
1000
1001 INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list);
1002
1003 if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8)))
1004 return err;
1005 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428)
1006 if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA)))
1007 return err;
1008 if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122)
1009 err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122.
1010 return err;
1011}