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