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