1 /* 2 * Empiatech em28x1 audio extension 3 * 4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com> 5 * 6 * Copyright (C) 2007-2016 Mauro Carvalho Chehab 7 * - Port to work with the in-kernel driver 8 * - Cleanups, fixes, alsa-controls, etc. 9 * 10 * This driver is based on my previous au600 usb pstn audio driver 11 * and inherits all the copyrights 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 */ 27 28 #include "em28xx.h" 29 30 #include <linux/kernel.h> 31 #include <linux/usb.h> 32 #include <linux/init.h> 33 #include <linux/sound.h> 34 #include <linux/spinlock.h> 35 #include <linux/soundcard.h> 36 #include <linux/slab.h> 37 #include <linux/vmalloc.h> 38 #include <linux/proc_fs.h> 39 #include <linux/module.h> 40 #include <sound/core.h> 41 #include <sound/pcm.h> 42 #include <sound/pcm_params.h> 43 #include <sound/info.h> 44 #include <sound/initval.h> 45 #include <sound/control.h> 46 #include <sound/tlv.h> 47 #include <sound/ac97_codec.h> 48 #include <media/v4l2-common.h> 49 50 static int debug; 51 module_param(debug, int, 0644); 52 MODULE_PARM_DESC(debug, "activates debug info"); 53 54 #define EM28XX_MAX_AUDIO_BUFS 5 55 #define EM28XX_MIN_AUDIO_PACKETS 64 56 57 #define dprintk(fmt, arg...) do { \ 58 if (debug) \ 59 printk(KERN_DEBUG pr_fmt("audio: %s: " fmt), \ 60 __func__, ##arg); } while (0) 61 62 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 63 64 static int em28xx_deinit_isoc_audio(struct em28xx *dev) 65 { 66 int i; 67 68 dprintk("Stopping isoc\n"); 69 for (i = 0; i < dev->adev.num_urb; i++) { 70 struct urb *urb = dev->adev.urb[i]; 71 72 if (!irqs_disabled()) 73 usb_kill_urb(urb); 74 else 75 usb_unlink_urb(urb); 76 } 77 78 return 0; 79 } 80 81 static void em28xx_audio_isocirq(struct urb *urb) 82 { 83 struct em28xx *dev = urb->context; 84 int i; 85 unsigned int oldptr; 86 int period_elapsed = 0; 87 int status; 88 unsigned char *cp; 89 unsigned int stride; 90 struct snd_pcm_substream *substream; 91 struct snd_pcm_runtime *runtime; 92 93 if (dev->disconnected) { 94 dprintk("device disconnected while streaming. URB status=%d.\n", urb->status); 95 atomic_set(&dev->adev.stream_started, 0); 96 return; 97 } 98 99 switch (urb->status) { 100 case 0: /* success */ 101 case -ETIMEDOUT: /* NAK */ 102 break; 103 case -ECONNRESET: /* kill */ 104 case -ENOENT: 105 case -ESHUTDOWN: 106 return; 107 default: /* error */ 108 dprintk("urb completition error %d.\n", urb->status); 109 break; 110 } 111 112 if (atomic_read(&dev->adev.stream_started) == 0) 113 return; 114 115 if (dev->adev.capture_pcm_substream) { 116 substream = dev->adev.capture_pcm_substream; 117 runtime = substream->runtime; 118 stride = runtime->frame_bits >> 3; 119 120 for (i = 0; i < urb->number_of_packets; i++) { 121 int length = 122 urb->iso_frame_desc[i].actual_length / stride; 123 cp = (unsigned char *)urb->transfer_buffer + 124 urb->iso_frame_desc[i].offset; 125 126 if (!length) 127 continue; 128 129 oldptr = dev->adev.hwptr_done_capture; 130 if (oldptr + length >= runtime->buffer_size) { 131 unsigned int cnt = 132 runtime->buffer_size - oldptr; 133 memcpy(runtime->dma_area + oldptr * stride, cp, 134 cnt * stride); 135 memcpy(runtime->dma_area, cp + cnt * stride, 136 length * stride - cnt * stride); 137 } else { 138 memcpy(runtime->dma_area + oldptr * stride, cp, 139 length * stride); 140 } 141 142 snd_pcm_stream_lock(substream); 143 144 dev->adev.hwptr_done_capture += length; 145 if (dev->adev.hwptr_done_capture >= 146 runtime->buffer_size) 147 dev->adev.hwptr_done_capture -= 148 runtime->buffer_size; 149 150 dev->adev.capture_transfer_done += length; 151 if (dev->adev.capture_transfer_done >= 152 runtime->period_size) { 153 dev->adev.capture_transfer_done -= 154 runtime->period_size; 155 period_elapsed = 1; 156 } 157 158 snd_pcm_stream_unlock(substream); 159 } 160 if (period_elapsed) 161 snd_pcm_period_elapsed(substream); 162 } 163 urb->status = 0; 164 165 status = usb_submit_urb(urb, GFP_ATOMIC); 166 if (status < 0) 167 pr_err("resubmit of audio urb failed (error=%i)\n", 168 status); 169 return; 170 } 171 172 static int em28xx_init_audio_isoc(struct em28xx *dev) 173 { 174 int i, errCode; 175 176 dprintk("Starting isoc transfers\n"); 177 178 /* Start streaming */ 179 for (i = 0; i < dev->adev.num_urb; i++) { 180 memset(dev->adev.transfer_buffer[i], 0x80, 181 dev->adev.urb[i]->transfer_buffer_length); 182 183 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC); 184 if (errCode) { 185 pr_err("submit of audio urb failed (error=%i)\n", 186 errCode); 187 em28xx_deinit_isoc_audio(dev); 188 atomic_set(&dev->adev.stream_started, 0); 189 return errCode; 190 } 191 192 } 193 194 return 0; 195 } 196 197 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, 198 size_t size) 199 { 200 struct snd_pcm_runtime *runtime = subs->runtime; 201 202 dprintk("Allocating vbuffer\n"); 203 if (runtime->dma_area) { 204 if (runtime->dma_bytes > size) 205 return 0; 206 207 vfree(runtime->dma_area); 208 } 209 runtime->dma_area = vmalloc(size); 210 if (!runtime->dma_area) 211 return -ENOMEM; 212 213 runtime->dma_bytes = size; 214 215 return 0; 216 } 217 218 static struct snd_pcm_hardware snd_em28xx_hw_capture = { 219 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | 220 SNDRV_PCM_INFO_MMAP | 221 SNDRV_PCM_INFO_INTERLEAVED | 222 SNDRV_PCM_INFO_BATCH | 223 SNDRV_PCM_INFO_MMAP_VALID, 224 225 .formats = SNDRV_PCM_FMTBIT_S16_LE, 226 227 .rates = SNDRV_PCM_RATE_48000, 228 229 .rate_min = 48000, 230 .rate_max = 48000, 231 .channels_min = 2, 232 .channels_max = 2, 233 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ 234 235 /* 236 * The period is 12.288 bytes. Allow a 10% of variation along its 237 * value, in order to avoid overruns/underruns due to some clock 238 * drift. 239 * 240 * FIXME: This period assumes 64 packets, and a 48000 PCM rate. 241 * Calculate it dynamically. 242 */ 243 .period_bytes_min = 11059, 244 .period_bytes_max = 13516, 245 246 .periods_min = 2, 247 .periods_max = 98, /* 12544, */ 248 }; 249 250 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream) 251 { 252 struct em28xx *dev = snd_pcm_substream_chip(substream); 253 struct snd_pcm_runtime *runtime = substream->runtime; 254 int nonblock, ret = 0; 255 256 if (!dev) { 257 pr_err("BUG: em28xx can't find device struct. Can't proceed with open\n"); 258 return -ENODEV; 259 } 260 261 if (dev->disconnected) 262 return -ENODEV; 263 264 dprintk("opening device and trying to acquire exclusive lock\n"); 265 266 nonblock = !!(substream->f_flags & O_NONBLOCK); 267 if (nonblock) { 268 if (!mutex_trylock(&dev->lock)) 269 return -EAGAIN; 270 } else 271 mutex_lock(&dev->lock); 272 273 runtime->hw = snd_em28xx_hw_capture; 274 275 if (dev->adev.users == 0) { 276 if (dev->alt == 0 || dev->is_audio_only) { 277 if (dev->is_audio_only) 278 /* audio is on a separate interface */ 279 dev->alt = 1; 280 else 281 /* audio is on the same interface as video */ 282 dev->alt = 7; 283 /* 284 * FIXME: The intention seems to be to select 285 * the alt setting with the largest 286 * wMaxPacketSize for the video endpoint. 287 * At least dev->alt should be used instead, but 288 * we should probably not touch it at all if it 289 * is already >0, because wMaxPacketSize of the 290 * audio endpoints seems to be the same for all. 291 */ 292 dprintk("changing alternate number on interface %d to %d\n", 293 dev->ifnum, dev->alt); 294 usb_set_interface(dev->udev, dev->ifnum, dev->alt); 295 } 296 297 /* Sets volume, mute, etc */ 298 dev->mute = 0; 299 ret = em28xx_audio_analog_set(dev); 300 if (ret < 0) 301 goto err; 302 } 303 304 kref_get(&dev->ref); 305 dev->adev.users++; 306 mutex_unlock(&dev->lock); 307 308 /* Dynamically adjust the period size */ 309 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 310 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 311 dev->adev.period * 95 / 100, 312 dev->adev.period * 105 / 100); 313 314 dev->adev.capture_pcm_substream = substream; 315 316 return 0; 317 err: 318 mutex_unlock(&dev->lock); 319 320 pr_err("Error while configuring em28xx mixer\n"); 321 return ret; 322 } 323 324 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream) 325 { 326 struct em28xx *dev = snd_pcm_substream_chip(substream); 327 328 dprintk("closing device\n"); 329 330 dev->mute = 1; 331 mutex_lock(&dev->lock); 332 dev->adev.users--; 333 if (atomic_read(&dev->adev.stream_started) > 0) { 334 atomic_set(&dev->adev.stream_started, 0); 335 schedule_work(&dev->adev.wq_trigger); 336 } 337 338 em28xx_audio_analog_set(dev); 339 if (substream->runtime->dma_area) { 340 dprintk("freeing\n"); 341 vfree(substream->runtime->dma_area); 342 substream->runtime->dma_area = NULL; 343 } 344 mutex_unlock(&dev->lock); 345 kref_put(&dev->ref, em28xx_free_device); 346 347 return 0; 348 } 349 350 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream, 351 struct snd_pcm_hw_params *hw_params) 352 { 353 int ret; 354 struct em28xx *dev = snd_pcm_substream_chip(substream); 355 356 if (dev->disconnected) 357 return -ENODEV; 358 359 dprintk("Setting capture parameters\n"); 360 361 ret = snd_pcm_alloc_vmalloc_buffer(substream, 362 params_buffer_bytes(hw_params)); 363 if (ret < 0) 364 return ret; 365 #if 0 366 /* TODO: set up em28xx audio chip to deliver the correct audio format, 367 current default is 48000hz multiplexed => 96000hz mono 368 which shouldn't matter since analogue TV only supports mono */ 369 unsigned int channels, rate, format; 370 371 format = params_format(hw_params); 372 rate = params_rate(hw_params); 373 channels = params_channels(hw_params); 374 #endif 375 376 return 0; 377 } 378 379 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream) 380 { 381 struct em28xx *dev = snd_pcm_substream_chip(substream); 382 struct em28xx_audio *adev = &dev->adev; 383 384 dprintk("Stop capture, if needed\n"); 385 386 if (atomic_read(&adev->stream_started) > 0) { 387 atomic_set(&adev->stream_started, 0); 388 schedule_work(&adev->wq_trigger); 389 } 390 391 return 0; 392 } 393 394 static int snd_em28xx_prepare(struct snd_pcm_substream *substream) 395 { 396 struct em28xx *dev = snd_pcm_substream_chip(substream); 397 398 if (dev->disconnected) 399 return -ENODEV; 400 401 dev->adev.hwptr_done_capture = 0; 402 dev->adev.capture_transfer_done = 0; 403 404 return 0; 405 } 406 407 static void audio_trigger(struct work_struct *work) 408 { 409 struct em28xx_audio *adev = 410 container_of(work, struct em28xx_audio, wq_trigger); 411 struct em28xx *dev = container_of(adev, struct em28xx, adev); 412 413 if (atomic_read(&adev->stream_started)) { 414 dprintk("starting capture"); 415 em28xx_init_audio_isoc(dev); 416 } else { 417 dprintk("stopping capture"); 418 em28xx_deinit_isoc_audio(dev); 419 } 420 } 421 422 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, 423 int cmd) 424 { 425 struct em28xx *dev = snd_pcm_substream_chip(substream); 426 int retval = 0; 427 428 if (dev->disconnected) 429 return -ENODEV; 430 431 switch (cmd) { 432 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ 433 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ 434 case SNDRV_PCM_TRIGGER_START: 435 atomic_set(&dev->adev.stream_started, 1); 436 break; 437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ 438 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ 439 case SNDRV_PCM_TRIGGER_STOP: 440 atomic_set(&dev->adev.stream_started, 0); 441 break; 442 default: 443 retval = -EINVAL; 444 } 445 schedule_work(&dev->adev.wq_trigger); 446 return retval; 447 } 448 449 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream 450 *substream) 451 { 452 unsigned long flags; 453 struct em28xx *dev; 454 snd_pcm_uframes_t hwptr_done; 455 456 dev = snd_pcm_substream_chip(substream); 457 if (dev->disconnected) 458 return SNDRV_PCM_POS_XRUN; 459 460 spin_lock_irqsave(&dev->adev.slock, flags); 461 hwptr_done = dev->adev.hwptr_done_capture; 462 spin_unlock_irqrestore(&dev->adev.slock, flags); 463 464 return hwptr_done; 465 } 466 467 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, 468 unsigned long offset) 469 { 470 void *pageptr = subs->runtime->dma_area + offset; 471 472 return vmalloc_to_page(pageptr); 473 } 474 475 /* 476 * AC97 volume control support 477 */ 478 static int em28xx_vol_info(struct snd_kcontrol *kcontrol, 479 struct snd_ctl_elem_info *info) 480 { 481 struct em28xx *dev = snd_kcontrol_chip(kcontrol); 482 483 if (dev->disconnected) 484 return -ENODEV; 485 486 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 487 info->count = 2; 488 info->value.integer.min = 0; 489 info->value.integer.max = 0x1f; 490 491 return 0; 492 } 493 494 static int em28xx_vol_put(struct snd_kcontrol *kcontrol, 495 struct snd_ctl_elem_value *value) 496 { 497 struct em28xx *dev = snd_kcontrol_chip(kcontrol); 498 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; 499 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) | 500 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8; 501 int nonblock = 0; 502 int rc; 503 504 if (dev->disconnected) 505 return -ENODEV; 506 507 if (substream) 508 nonblock = !!(substream->f_flags & O_NONBLOCK); 509 if (nonblock) { 510 if (!mutex_trylock(&dev->lock)) 511 return -EAGAIN; 512 } else 513 mutex_lock(&dev->lock); 514 rc = em28xx_read_ac97(dev, kcontrol->private_value); 515 if (rc < 0) 516 goto err; 517 518 val |= rc & 0x8000; /* Preserve the mute flag */ 519 520 rc = em28xx_write_ac97(dev, kcontrol->private_value, val); 521 if (rc < 0) 522 goto err; 523 524 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", 525 (val & 0x8000) ? "muted " : "", 526 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), 527 val, (int)kcontrol->private_value); 528 529 err: 530 mutex_unlock(&dev->lock); 531 return rc; 532 } 533 534 static int em28xx_vol_get(struct snd_kcontrol *kcontrol, 535 struct snd_ctl_elem_value *value) 536 { 537 struct em28xx *dev = snd_kcontrol_chip(kcontrol); 538 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; 539 int nonblock = 0; 540 int val; 541 542 if (dev->disconnected) 543 return -ENODEV; 544 545 if (substream) 546 nonblock = !!(substream->f_flags & O_NONBLOCK); 547 if (nonblock) { 548 if (!mutex_trylock(&dev->lock)) 549 return -EAGAIN; 550 } else 551 mutex_lock(&dev->lock); 552 val = em28xx_read_ac97(dev, kcontrol->private_value); 553 mutex_unlock(&dev->lock); 554 if (val < 0) 555 return val; 556 557 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", 558 (val & 0x8000) ? "muted " : "", 559 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), 560 val, (int)kcontrol->private_value); 561 562 value->value.integer.value[0] = 0x1f - (val & 0x1f); 563 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f); 564 565 return 0; 566 } 567 568 static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol, 569 struct snd_ctl_elem_value *value) 570 { 571 struct em28xx *dev = snd_kcontrol_chip(kcontrol); 572 u16 val = value->value.integer.value[0]; 573 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; 574 int nonblock = 0; 575 int rc; 576 577 if (dev->disconnected) 578 return -ENODEV; 579 580 if (substream) 581 nonblock = !!(substream->f_flags & O_NONBLOCK); 582 if (nonblock) { 583 if (!mutex_trylock(&dev->lock)) 584 return -EAGAIN; 585 } else 586 mutex_lock(&dev->lock); 587 rc = em28xx_read_ac97(dev, kcontrol->private_value); 588 if (rc < 0) 589 goto err; 590 591 if (val) 592 rc &= 0x1f1f; 593 else 594 rc |= 0x8000; 595 596 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc); 597 if (rc < 0) 598 goto err; 599 600 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", 601 (val & 0x8000) ? "muted " : "", 602 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), 603 val, (int)kcontrol->private_value); 604 605 err: 606 mutex_unlock(&dev->lock); 607 return rc; 608 } 609 610 static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol, 611 struct snd_ctl_elem_value *value) 612 { 613 struct em28xx *dev = snd_kcontrol_chip(kcontrol); 614 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; 615 int nonblock = 0; 616 int val; 617 618 if (dev->disconnected) 619 return -ENODEV; 620 621 if (substream) 622 nonblock = !!(substream->f_flags & O_NONBLOCK); 623 if (nonblock) { 624 if (!mutex_trylock(&dev->lock)) 625 return -EAGAIN; 626 } else 627 mutex_lock(&dev->lock); 628 val = em28xx_read_ac97(dev, kcontrol->private_value); 629 mutex_unlock(&dev->lock); 630 if (val < 0) 631 return val; 632 633 if (val & 0x8000) 634 value->value.integer.value[0] = 0; 635 else 636 value->value.integer.value[0] = 1; 637 638 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", 639 (val & 0x8000) ? "muted " : "", 640 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), 641 val, (int)kcontrol->private_value); 642 643 return 0; 644 } 645 646 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0); 647 648 static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev, 649 char *name, int id) 650 { 651 int err; 652 char ctl_name[44]; 653 struct snd_kcontrol *kctl; 654 struct snd_kcontrol_new tmp; 655 656 memset(&tmp, 0, sizeof(tmp)); 657 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, 658 tmp.private_value = id, 659 tmp.name = ctl_name, 660 661 /* Add Mute Control */ 662 sprintf(ctl_name, "%s Switch", name); 663 tmp.get = em28xx_vol_get_mute; 664 tmp.put = em28xx_vol_put_mute; 665 tmp.info = snd_ctl_boolean_mono_info; 666 kctl = snd_ctl_new1(&tmp, dev); 667 err = snd_ctl_add(card, kctl); 668 if (err < 0) 669 return err; 670 dprintk("Added control %s for ac97 volume control 0x%04x\n", 671 ctl_name, id); 672 673 memset(&tmp, 0, sizeof(tmp)); 674 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, 675 tmp.private_value = id, 676 tmp.name = ctl_name, 677 678 /* Add Volume Control */ 679 sprintf(ctl_name, "%s Volume", name); 680 tmp.get = em28xx_vol_get; 681 tmp.put = em28xx_vol_put; 682 tmp.info = em28xx_vol_info; 683 tmp.tlv.p = em28xx_db_scale, 684 kctl = snd_ctl_new1(&tmp, dev); 685 err = snd_ctl_add(card, kctl); 686 if (err < 0) 687 return err; 688 dprintk("Added control %s for ac97 volume control 0x%04x\n", 689 ctl_name, id); 690 691 return 0; 692 } 693 694 /* 695 * register/unregister code and data 696 */ 697 static const struct snd_pcm_ops snd_em28xx_pcm_capture = { 698 .open = snd_em28xx_capture_open, 699 .close = snd_em28xx_pcm_close, 700 .ioctl = snd_pcm_lib_ioctl, 701 .hw_params = snd_em28xx_hw_capture_params, 702 .hw_free = snd_em28xx_hw_capture_free, 703 .prepare = snd_em28xx_prepare, 704 .trigger = snd_em28xx_capture_trigger, 705 .pointer = snd_em28xx_capture_pointer, 706 .page = snd_pcm_get_vmalloc_page, 707 }; 708 709 static void em28xx_audio_free_urb(struct em28xx *dev) 710 { 711 int i; 712 713 for (i = 0; i < dev->adev.num_urb; i++) { 714 struct urb *urb = dev->adev.urb[i]; 715 716 if (!urb) 717 continue; 718 719 usb_free_coherent(dev->udev, urb->transfer_buffer_length, 720 dev->adev.transfer_buffer[i], 721 urb->transfer_dma); 722 723 usb_free_urb(urb); 724 } 725 kfree(dev->adev.urb); 726 kfree(dev->adev.transfer_buffer); 727 dev->adev.num_urb = 0; 728 } 729 730 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */ 731 static int em28xx_audio_ep_packet_size(struct usb_device *udev, 732 struct usb_endpoint_descriptor *e) 733 { 734 int size = le16_to_cpu(e->wMaxPacketSize); 735 736 if (udev->speed == USB_SPEED_HIGH) 737 return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03)); 738 739 return size & 0x7ff; 740 } 741 742 static int em28xx_audio_urb_init(struct em28xx *dev) 743 { 744 struct usb_interface *intf; 745 struct usb_endpoint_descriptor *e, *ep = NULL; 746 int i, ep_size, interval, num_urb, npackets; 747 int urb_size, bytes_per_transfer; 748 u8 alt; 749 750 if (dev->ifnum) 751 alt = 1; 752 else 753 alt = 7; 754 755 intf = usb_ifnum_to_if(dev->udev, dev->ifnum); 756 757 if (intf->num_altsetting <= alt) { 758 pr_err("alt %d doesn't exist on interface %d\n", 759 dev->ifnum, alt); 760 return -ENODEV; 761 } 762 763 for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) { 764 e = &intf->altsetting[alt].endpoint[i].desc; 765 if (!usb_endpoint_dir_in(e)) 766 continue; 767 if (e->bEndpointAddress == EM28XX_EP_AUDIO) { 768 ep = e; 769 break; 770 } 771 } 772 773 if (!ep) { 774 pr_err("Couldn't find an audio endpoint"); 775 return -ENODEV; 776 } 777 778 ep_size = em28xx_audio_ep_packet_size(dev->udev, ep); 779 interval = 1 << (ep->bInterval - 1); 780 781 pr_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n", 782 EM28XX_EP_AUDIO, usb_speed_string(dev->udev->speed), 783 dev->ifnum, alt, 784 interval, 785 ep_size); 786 787 /* Calculate the number and size of URBs to better fit the audio samples */ 788 789 /* 790 * Estimate the number of bytes per DMA transfer. 791 * 792 * This is given by the bit rate (for now, only 48000 Hz) multiplied 793 * by 2 channels and 2 bytes/sample divided by the number of microframe 794 * intervals and by the microframe rate (125 us) 795 */ 796 bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval); 797 798 /* 799 * Estimate the number of transfer URBs. Don't let it go past the 800 * maximum number of URBs that is known to be supported by the device. 801 */ 802 num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size); 803 if (num_urb > EM28XX_MAX_AUDIO_BUFS) 804 num_urb = EM28XX_MAX_AUDIO_BUFS; 805 806 /* 807 * Now that we know the number of bytes per transfer and the number of 808 * URBs, estimate the typical size of an URB, in order to adjust the 809 * minimal number of packets. 810 */ 811 urb_size = bytes_per_transfer / num_urb; 812 813 /* 814 * Now, calculate the amount of audio packets to be filled on each 815 * URB. In order to preserve the old behaviour, use a minimal 816 * threshold for this value. 817 */ 818 npackets = EM28XX_MIN_AUDIO_PACKETS; 819 if (urb_size > ep_size * npackets) 820 npackets = DIV_ROUND_UP(urb_size, ep_size); 821 822 pr_info("Number of URBs: %d, with %d packets and %d size\n", 823 num_urb, npackets, urb_size); 824 825 /* Estimate the bytes per period */ 826 dev->adev.period = urb_size * npackets; 827 828 /* Allocate space to store the number of URBs to be used */ 829 830 dev->adev.transfer_buffer = kcalloc(num_urb, 831 sizeof(*dev->adev.transfer_buffer), 832 GFP_ATOMIC); 833 if (!dev->adev.transfer_buffer) { 834 return -ENOMEM; 835 } 836 837 dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_ATOMIC); 838 if (!dev->adev.urb) { 839 kfree(dev->adev.transfer_buffer); 840 return -ENOMEM; 841 } 842 843 /* Alloc memory for each URB and for each transfer buffer */ 844 dev->adev.num_urb = num_urb; 845 for (i = 0; i < num_urb; i++) { 846 struct urb *urb; 847 int j, k; 848 void *buf; 849 850 urb = usb_alloc_urb(npackets, GFP_ATOMIC); 851 if (!urb) { 852 em28xx_audio_free_urb(dev); 853 return -ENOMEM; 854 } 855 dev->adev.urb[i] = urb; 856 857 buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC, 858 &urb->transfer_dma); 859 if (!buf) { 860 pr_err("usb_alloc_coherent failed!\n"); 861 em28xx_audio_free_urb(dev); 862 return -ENOMEM; 863 } 864 dev->adev.transfer_buffer[i] = buf; 865 866 urb->dev = dev->udev; 867 urb->context = dev; 868 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO); 869 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 870 urb->transfer_buffer = buf; 871 urb->interval = interval; 872 urb->complete = em28xx_audio_isocirq; 873 urb->number_of_packets = npackets; 874 urb->transfer_buffer_length = ep_size * npackets; 875 876 for (j = k = 0; j < npackets; j++, k += ep_size) { 877 urb->iso_frame_desc[j].offset = k; 878 urb->iso_frame_desc[j].length = ep_size; 879 } 880 } 881 882 return 0; 883 } 884 885 static int em28xx_audio_init(struct em28xx *dev) 886 { 887 struct em28xx_audio *adev = &dev->adev; 888 struct snd_pcm *pcm; 889 struct snd_card *card; 890 static int devnr; 891 int err; 892 893 if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) { 894 /* This device does not support the extension (in this case 895 the device is expecting the snd-usb-audio module or 896 doesn't have analog audio support at all) */ 897 return 0; 898 } 899 900 pr_info("Binding audio extension\n"); 901 902 kref_get(&dev->ref); 903 904 pr_info("em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n"); 905 pr_info("em28xx-audio.c: Copyright (C) 2007-2016 Mauro Carvalho Chehab\n"); 906 907 err = snd_card_new(&dev->udev->dev, index[devnr], "Em28xx Audio", 908 THIS_MODULE, 0, &card); 909 if (err < 0) 910 return err; 911 912 spin_lock_init(&adev->slock); 913 adev->sndcard = card; 914 adev->udev = dev->udev; 915 916 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm); 917 if (err < 0) 918 goto card_free; 919 920 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture); 921 pcm->info_flags = 0; 922 pcm->private_data = dev; 923 strcpy(pcm->name, "Empia 28xx Capture"); 924 925 strcpy(card->driver, "Em28xx-Audio"); 926 strcpy(card->shortname, "Em28xx Audio"); 927 strcpy(card->longname, "Empia Em28xx Audio"); 928 929 INIT_WORK(&adev->wq_trigger, audio_trigger); 930 931 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { 932 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO); 933 em28xx_cvol_new(card, dev, "Line In", AC97_LINE); 934 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE); 935 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC); 936 em28xx_cvol_new(card, dev, "CD", AC97_CD); 937 em28xx_cvol_new(card, dev, "AUX", AC97_AUX); 938 em28xx_cvol_new(card, dev, "PCM", AC97_PCM); 939 940 em28xx_cvol_new(card, dev, "Master", AC97_MASTER); 941 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE); 942 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO); 943 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER); 944 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER); 945 } 946 947 err = em28xx_audio_urb_init(dev); 948 if (err) 949 goto card_free; 950 951 err = snd_card_register(card); 952 if (err < 0) 953 goto urb_free; 954 955 pr_info("Audio extension successfully initialized\n"); 956 return 0; 957 958 urb_free: 959 em28xx_audio_free_urb(dev); 960 961 card_free: 962 snd_card_free(card); 963 adev->sndcard = NULL; 964 965 return err; 966 } 967 968 static int em28xx_audio_fini(struct em28xx *dev) 969 { 970 if (dev == NULL) 971 return 0; 972 973 if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) { 974 /* This device does not support the extension (in this case 975 the device is expecting the snd-usb-audio module or 976 doesn't have analog audio support at all) */ 977 return 0; 978 } 979 980 pr_info("Closing audio extension\n"); 981 982 if (dev->adev.sndcard) { 983 snd_card_disconnect(dev->adev.sndcard); 984 flush_work(&dev->adev.wq_trigger); 985 986 em28xx_audio_free_urb(dev); 987 988 snd_card_free(dev->adev.sndcard); 989 dev->adev.sndcard = NULL; 990 } 991 992 kref_put(&dev->ref, em28xx_free_device); 993 return 0; 994 } 995 996 static int em28xx_audio_suspend(struct em28xx *dev) 997 { 998 if (dev == NULL) 999 return 0; 1000 1001 if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) 1002 return 0; 1003 1004 pr_info("Suspending audio extension\n"); 1005 em28xx_deinit_isoc_audio(dev); 1006 atomic_set(&dev->adev.stream_started, 0); 1007 return 0; 1008 } 1009 1010 static int em28xx_audio_resume(struct em28xx *dev) 1011 { 1012 if (dev == NULL) 1013 return 0; 1014 1015 if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) 1016 return 0; 1017 1018 pr_info("Resuming audio extension\n"); 1019 /* Nothing to do other than schedule_work() ?? */ 1020 schedule_work(&dev->adev.wq_trigger); 1021 return 0; 1022 } 1023 1024 static struct em28xx_ops audio_ops = { 1025 .id = EM28XX_AUDIO, 1026 .name = "Em28xx Audio Extension", 1027 .init = em28xx_audio_init, 1028 .fini = em28xx_audio_fini, 1029 .suspend = em28xx_audio_suspend, 1030 .resume = em28xx_audio_resume, 1031 }; 1032 1033 static int __init em28xx_alsa_register(void) 1034 { 1035 return em28xx_register_extension(&audio_ops); 1036 } 1037 1038 static void __exit em28xx_alsa_unregister(void) 1039 { 1040 em28xx_unregister_extension(&audio_ops); 1041 } 1042 1043 MODULE_LICENSE("GPL"); 1044 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>"); 1045 MODULE_AUTHOR("Mauro Carvalho Chehab"); 1046 MODULE_DESCRIPTION(DRIVER_DESC " - audio interface"); 1047 MODULE_VERSION(EM28XX_VERSION); 1048 1049 module_init(em28xx_alsa_register); 1050 module_exit(em28xx_alsa_unregister); 1051