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