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