blob: e57616ecd2cbba993004c587840ea2ce10e1c5a8 [file] [log] [blame]
Markus Rechbergera52932b2008-01-05 09:55:47 -03001/*
2 * Empiatech em28x1 audio extension
3 *
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5 *
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -03006 * Copyright (C) 2007-2014 Mauro Carvalho Chehab
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03007 * - Port to work with the in-kernel driver
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -03008 * - Cleanups, fixes, alsa-controls, etc.
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03009 *
Markus Rechbergera52932b2008-01-05 09:55:47 -030010 * 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 <linux/kernel.h>
29#include <linux/usb.h>
30#include <linux/init.h>
31#include <linux/sound.h>
32#include <linux/spinlock.h>
33#include <linux/soundcard.h>
34#include <linux/slab.h>
35#include <linux/vmalloc.h>
36#include <linux/proc_fs.h>
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030037#include <linux/module.h>
Markus Rechbergera52932b2008-01-05 09:55:47 -030038#include <sound/core.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/info.h>
42#include <sound/initval.h>
43#include <sound/control.h>
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -030044#include <sound/tlv.h>
Ezequiel García67b8d032012-06-11 15:17:23 -030045#include <sound/ac97_codec.h>
Markus Rechbergera52932b2008-01-05 09:55:47 -030046#include <media/v4l2-common.h>
47#include "em28xx.h"
48
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030049static int debug;
50module_param(debug, int, 0644);
51MODULE_PARM_DESC(debug, "activates debug info");
52
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -030053#define EM28XX_MAX_AUDIO_BUFS 5
54#define EM28XX_MIN_AUDIO_PACKETS 64
Mauro Carvalho Chehab49677ae2014-01-10 13:29:16 -030055
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030056#define dprintk(fmt, arg...) do { \
57 if (debug) \
58 printk(KERN_INFO "em28xx-audio %s: " fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030059 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030060 } while (0)
61
Markus Rechbergera52932b2008-01-05 09:55:47 -030062static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030063
Robert Krakoraaa5a1822009-02-08 13:09:11 -030064static int em28xx_deinit_isoc_audio(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030065{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030066 int i;
67
68 dprintk("Stopping isoc\n");
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -030069 for (i = 0; i < dev->adev.num_urb; i++) {
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030070 struct urb *urb = dev->adev.urb[i];
71
Robert Krakora9c062102009-01-25 13:08:07 -030072 if (!irqs_disabled())
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030073 usb_kill_urb(urb);
Robert Krakora9c062102009-01-25 13:08:07 -030074 else
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030075 usb_unlink_urb(urb);
Markus Rechbergera52932b2008-01-05 09:55:47 -030076 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030077
Markus Rechbergera52932b2008-01-05 09:55:47 -030078 return 0;
79}
80
Markus Rechbergera52932b2008-01-05 09:55:47 -030081static void em28xx_audio_isocirq(struct urb *urb)
82{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030083 struct em28xx *dev = urb->context;
84 int i;
85 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030086 int period_elapsed = 0;
87 int status;
88 unsigned char *cp;
89 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030090 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030091 struct snd_pcm_runtime *runtime;
Robert Krakoraaa5a1822009-02-08 13:09:11 -030092
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -030093 if (dev->disconnected) {
94 dprintk("device disconnected while streaming. URB status=%d.\n", urb->status);
95 atomic_set(&dev->stream_started, 0);
96 return;
97 }
98
Robert Krakoraaa5a1822009-02-08 13:09:11 -030099 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
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300112 if (atomic_read(&dev->stream_started) == 0)
113 return;
114
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300115 if (dev->adev.capture_pcm_substream) {
116 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300117 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300118 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300119
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300120 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;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300125
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300126 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300127 continue;
128
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300129 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300130 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
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300142 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300143
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300144 dev->adev.hwptr_done_capture += length;
145 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300146 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300147 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300148 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300149
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300150 dev->adev.capture_transfer_done += length;
151 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300152 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300153 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300154 runtime->period_size;
155 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300156 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300157
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300158 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300159 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300160 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300161 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300162 }
163 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300164
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300165 status = usb_submit_urb(urb, GFP_ATOMIC);
Mauro Carvalho Chehabbf6e8aa2014-01-14 14:34:13 -0300166 if (status < 0)
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300167 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
168 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300169 return;
170}
171
Markus Rechbergera52932b2008-01-05 09:55:47 -0300172static int em28xx_init_audio_isoc(struct em28xx *dev)
173{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300174 int i, errCode;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300175
176 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300177
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300178 /* Start streaming */
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300179 for (i = 0; i < dev->adev.num_urb; i++) {
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300180 memset(dev->adev.transfer_buffer[i], 0x80,
181 dev->adev.urb[i]->transfer_buffer_length);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300182
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300183 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300184 if (errCode) {
Mauro Carvalho Chehabbf6e8aa2014-01-14 14:34:13 -0300185 em28xx_errdev("submit of audio urb failed (error=%i)\n",
186 errCode);
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300187 em28xx_deinit_isoc_audio(dev);
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300188 atomic_set(&dev->stream_started, 0);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300189 return errCode;
190 }
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300191
Markus Rechbergera52932b2008-01-05 09:55:47 -0300192 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300193
Markus Rechbergera52932b2008-01-05 09:55:47 -0300194 return 0;
195}
196
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300197static 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
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300202 dprintk("Allocating vbuffer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300203 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
218static 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 |
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300222 SNDRV_PCM_INFO_BATCH |
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300223 SNDRV_PCM_INFO_MMAP_VALID,
224
225 .formats = SNDRV_PCM_FMTBIT_S16_LE,
226
Mauro Carvalho Chehab49677ae2014-01-10 13:29:16 -0300227 .rates = SNDRV_PCM_RATE_48000,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300228
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 */
Mauro Carvalho Chehab49677ae2014-01-10 13:29:16 -0300234
235
236 /*
237 * The period is 12.288 bytes. Allow a 10% of variation along its
238 * value, in order to avoid overruns/underruns due to some clock
239 * drift.
240 *
241 * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
242 * Calculate it dynamically.
243 */
244 .period_bytes_min = 11059,
245 .period_bytes_max = 13516,
246
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300247 .periods_min = 2,
248 .periods_max = 98, /* 12544, */
249};
250
251static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
252{
253 struct em28xx *dev = snd_pcm_substream_chip(substream);
254 struct snd_pcm_runtime *runtime = substream->runtime;
255 int ret = 0;
256
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300257 if (!dev) {
Filipe Rosset0bc23082009-11-04 15:31:25 -0300258 em28xx_err("BUG: em28xx can't find device struct."
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300259 " Can't proceed with open\n");
260 return -ENODEV;
261 }
262
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300263 if (dev->disconnected)
264 return -ENODEV;
265
266 dprintk("opening device and trying to acquire exclusive lock\n");
267
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300268 runtime->hw = snd_em28xx_hw_capture;
Frank Schaefer0191a2a2014-01-13 19:02:07 -0300269 if ((dev->alt == 0 || dev->is_audio_only) && dev->adev.users == 0) {
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300270 int nonblock = !!(substream->f_flags & O_NONBLOCK);
271
272 if (nonblock) {
273 if (!mutex_trylock(&dev->lock))
274 return -EAGAIN;
275 } else
276 mutex_lock(&dev->lock);
Frank Schaefer0191a2a2014-01-13 19:02:07 -0300277 if (dev->is_audio_only)
278 /* vendor audio is on a separate interface */
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300279 dev->alt = 1;
280 else
Frank Schaefer0191a2a2014-01-13 19:02:07 -0300281 /* vendor audio is on the same interface as video */
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300282 dev->alt = 7;
Frank Schaefer0191a2a2014-01-13 19:02:07 -0300283 /*
284 * FIXME: The intention seems to be to select the alt
285 * setting with the largest wMaxPacketSize for the video
286 * endpoint.
287 * At least dev->alt should be used instead, but we
288 * should probably not touch it at all if it is
289 * already >0, because wMaxPacketSize of the audio
290 * endpoints seems to be the same for all.
291 */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300292
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300293 dprintk("changing alternate number on interface %d to %d\n",
Frank Schaefer961717b2014-01-13 19:02:06 -0300294 dev->ifnum, dev->alt);
295 usb_set_interface(dev->udev, dev->ifnum, dev->alt);
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300296
297 /* Sets volume, mute, etc */
298 dev->mute = 0;
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300299 ret = em28xx_audio_analog_set(dev);
300 if (ret < 0)
301 goto err;
302
303 dev->adev.users++;
304 mutex_unlock(&dev->lock);
305 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300306
Mauro Carvalho Chehaba02b9c22014-01-13 03:34:23 -0300307 /* Dynamically adjust the period size */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300308 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehaba02b9c22014-01-13 03:34:23 -0300309 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
310 dev->adev.period * 95 / 100,
311 dev->adev.period * 105 / 100);
312
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300313 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300314
315 return 0;
316err:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300317 mutex_unlock(&dev->lock);
318
Filipe Rosset0bc23082009-11-04 15:31:25 -0300319 em28xx_err("Error while configuring em28xx mixer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300320 return ret;
321}
322
323static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
324{
325 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300326
327 dprintk("closing device\n");
328
329 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300330 mutex_lock(&dev->lock);
Douglas Schilling Landgrafbf510ac32009-02-08 01:11:13 -0300331 dev->adev.users--;
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300332 if (atomic_read(&dev->stream_started) > 0) {
333 atomic_set(&dev->stream_started, 0);
334 schedule_work(&dev->wq_trigger);
335 }
336
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300337 em28xx_audio_analog_set(dev);
Robert Krakora75c74d12009-05-28 11:16:19 -0300338 if (substream->runtime->dma_area) {
339 dprintk("freeing\n");
340 vfree(substream->runtime->dma_area);
341 substream->runtime->dma_area = NULL;
342 }
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300343 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300344
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300345 return 0;
346}
347
348static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
349 struct snd_pcm_hw_params *hw_params)
350{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300351 int ret;
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300352 struct em28xx *dev = snd_pcm_substream_chip(substream);
353
354 if (dev->disconnected)
355 return -ENODEV;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300356
357 dprintk("Setting capture parameters\n");
358
359 ret = snd_pcm_alloc_vmalloc_buffer(substream,
360 params_buffer_bytes(hw_params));
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300361 if (ret < 0)
362 return ret;
Hans Verkuile92ba282012-05-14 10:17:35 -0300363#if 0
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300364 /* TODO: set up em28xx audio chip to deliver the correct audio format,
365 current default is 48000hz multiplexed => 96000hz mono
366 which shouldn't matter since analogue TV only supports mono */
Hans Verkuile92ba282012-05-14 10:17:35 -0300367 unsigned int channels, rate, format;
368
369 format = params_format(hw_params);
370 rate = params_rate(hw_params);
371 channels = params_channels(hw_params);
372#endif
373
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300374 return 0;
375}
376
377static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
378{
379 struct em28xx *dev = snd_pcm_substream_chip(substream);
380
381 dprintk("Stop capture, if needed\n");
382
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300383 if (atomic_read(&dev->stream_started) > 0) {
384 atomic_set(&dev->stream_started, 0);
385 schedule_work(&dev->wq_trigger);
386 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300387
388 return 0;
389}
390
391static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
392{
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300393 struct em28xx *dev = snd_pcm_substream_chip(substream);
394
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300395 if (dev->disconnected)
396 return -ENODEV;
397
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300398 dev->adev.hwptr_done_capture = 0;
399 dev->adev.capture_transfer_done = 0;
400
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300401 return 0;
402}
403
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300404static void audio_trigger(struct work_struct *work)
405{
406 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
407
408 if (atomic_read(&dev->stream_started)) {
409 dprintk("starting capture");
410 em28xx_init_audio_isoc(dev);
411 } else {
412 dprintk("stopping capture");
413 em28xx_deinit_isoc_audio(dev);
414 }
415}
416
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300417static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
418 int cmd)
419{
420 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300421 int retval = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300422
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300423 if (dev->disconnected)
424 return -ENODEV;
425
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300426 switch (cmd) {
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300427 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
428 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300429 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300430 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300431 break;
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300432 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
433 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300434 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300435 atomic_set(&dev->stream_started, 0);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300436 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300437 default:
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300438 retval = -EINVAL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300439 }
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300440 schedule_work(&dev->wq_trigger);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300441 return retval;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300442}
443
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300444static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
445 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300446{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300447 unsigned long flags;
Robert Krakora53d12e52009-01-16 11:23:25 -0300448 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300449 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300450
Markus Rechbergera52932b2008-01-05 09:55:47 -0300451 dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300452 if (dev->disconnected)
Mauro Carvalho Chehab229295f2014-01-12 12:57:08 -0300453 return SNDRV_PCM_POS_XRUN;
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300454
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300455 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300456 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300457 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300458
Markus Rechbergera52932b2008-01-05 09:55:47 -0300459 return hwptr_done;
460}
461
462static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
463 unsigned long offset)
464{
465 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300466
Markus Rechbergera52932b2008-01-05 09:55:47 -0300467 return vmalloc_to_page(pageptr);
468}
469
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300470/*
471 * AC97 volume control support
472 */
473static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
474 struct snd_ctl_elem_info *info)
475{
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300476 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
477
478 if (dev->disconnected)
479 return -ENODEV;
480
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300481 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
482 info->count = 2;
483 info->value.integer.min = 0;
484 info->value.integer.max = 0x1f;
485
486 return 0;
487}
488
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300489static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
490 struct snd_ctl_elem_value *value)
491{
492 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300493 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300494 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
495 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300496 int nonblock = 0;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300497 int rc;
498
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300499 if (dev->disconnected)
500 return -ENODEV;
501
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300502 if (substream)
503 nonblock = !!(substream->f_flags & O_NONBLOCK);
504 if (nonblock) {
505 if (!mutex_trylock(&dev->lock))
506 return -EAGAIN;
507 } else
508 mutex_lock(&dev->lock);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300509 rc = em28xx_read_ac97(dev, kcontrol->private_value);
510 if (rc < 0)
511 goto err;
512
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300513 val |= rc & 0x8000; /* Preserve the mute flag */
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300514
515 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300516 if (rc < 0)
517 goto err;
518
519 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
520 (val & 0x8000) ? "muted " : "",
521 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
522 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300523
524err:
525 mutex_unlock(&dev->lock);
526 return rc;
527}
528
529static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
530 struct snd_ctl_elem_value *value)
531{
532 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300533 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
534 int nonblock = 0;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300535 int val;
536
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300537 if (dev->disconnected)
538 return -ENODEV;
539
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300540 if (substream)
541 nonblock = !!(substream->f_flags & O_NONBLOCK);
542 if (nonblock) {
543 if (!mutex_trylock(&dev->lock))
544 return -EAGAIN;
545 } else
546 mutex_lock(&dev->lock);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300547 val = em28xx_read_ac97(dev, kcontrol->private_value);
548 mutex_unlock(&dev->lock);
549 if (val < 0)
550 return val;
551
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300552 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
553 (val & 0x8000) ? "muted " : "",
554 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
555 val, (int)kcontrol->private_value);
556
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300557 value->value.integer.value[0] = 0x1f - (val & 0x1f);
558 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300559
560 return 0;
561}
562
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300563static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
564 struct snd_ctl_elem_value *value)
565{
566 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
567 u16 val = value->value.integer.value[0];
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300568 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
569 int nonblock = 0;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300570 int rc;
571
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300572 if (dev->disconnected)
573 return -ENODEV;
574
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300575 if (substream)
576 nonblock = !!(substream->f_flags & O_NONBLOCK);
577 if (nonblock) {
578 if (!mutex_trylock(&dev->lock))
579 return -EAGAIN;
580 } else
581 mutex_lock(&dev->lock);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300582 rc = em28xx_read_ac97(dev, kcontrol->private_value);
583 if (rc < 0)
584 goto err;
585
586 if (val)
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300587 rc &= 0x1f1f;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300588 else
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300589 rc |= 0x8000;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300590
591 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300592 if (rc < 0)
593 goto err;
594
595 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
596 (val & 0x8000) ? "muted " : "",
597 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
598 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300599
600err:
601 mutex_unlock(&dev->lock);
602 return rc;
603}
604
605static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
606 struct snd_ctl_elem_value *value)
607{
608 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300609 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
610 int nonblock = 0;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300611 int val;
612
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300613 if (dev->disconnected)
614 return -ENODEV;
615
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300616 if (substream)
617 nonblock = !!(substream->f_flags & O_NONBLOCK);
618 if (nonblock) {
619 if (!mutex_trylock(&dev->lock))
620 return -EAGAIN;
621 } else
622 mutex_lock(&dev->lock);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300623 val = em28xx_read_ac97(dev, kcontrol->private_value);
624 mutex_unlock(&dev->lock);
625 if (val < 0)
626 return val;
627
628 if (val & 0x8000)
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300629 value->value.integer.value[0] = 0;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300630 else
631 value->value.integer.value[0] = 1;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300632
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300633 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
634 (val & 0x8000) ? "muted " : "",
635 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
636 val, (int)kcontrol->private_value);
637
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300638 return 0;
639}
640
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300641static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
642
643static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
644 char *name, int id)
645{
646 int err;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300647 char ctl_name[44];
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300648 struct snd_kcontrol *kctl;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300649 struct snd_kcontrol_new tmp;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300650
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300651 memset (&tmp, 0, sizeof(tmp));
652 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
653 tmp.private_value = id,
654 tmp.name = ctl_name,
655
656 /* Add Mute Control */
657 sprintf(ctl_name, "%s Switch", name);
658 tmp.get = em28xx_vol_get_mute;
659 tmp.put = em28xx_vol_put_mute;
660 tmp.info = snd_ctl_boolean_mono_info;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300661 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300662 err = snd_ctl_add(card, kctl);
663 if (err < 0)
664 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300665 dprintk("Added control %s for ac97 volume control 0x%04x\n",
666 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300667
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300668 memset (&tmp, 0, sizeof(tmp));
669 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
670 tmp.private_value = id,
671 tmp.name = ctl_name,
672
673 /* Add Volume Control */
674 sprintf(ctl_name, "%s Volume", name);
675 tmp.get = em28xx_vol_get;
676 tmp.put = em28xx_vol_put;
677 tmp.info = em28xx_vol_info;
678 tmp.tlv.p = em28xx_db_scale,
679 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300680 err = snd_ctl_add(card, kctl);
681 if (err < 0)
682 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300683 dprintk("Added control %s for ac97 volume control 0x%04x\n",
684 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300685
686 return 0;
687}
688
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300689/*
690 * register/unregister code and data
691 */
Markus Rechbergera52932b2008-01-05 09:55:47 -0300692static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300693 .open = snd_em28xx_capture_open,
694 .close = snd_em28xx_pcm_close,
695 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300696 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300697 .hw_free = snd_em28xx_hw_capture_free,
698 .prepare = snd_em28xx_prepare,
699 .trigger = snd_em28xx_capture_trigger,
700 .pointer = snd_em28xx_capture_pointer,
701 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300702};
703
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300704static void em28xx_audio_free_urb(struct em28xx *dev)
705{
706 int i;
707
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300708 for (i = 0; i < dev->adev.num_urb; i++) {
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300709 struct urb *urb = dev->adev.urb[i];
710
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300711 if (!urb)
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300712 continue;
713
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300714 if (dev->adev.transfer_buffer[i])
715 usb_free_coherent(dev->udev,
716 urb->transfer_buffer_length,
717 dev->adev.transfer_buffer[i],
718 urb->transfer_dma);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300719
720 usb_free_urb(urb);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300721 }
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300722 kfree(dev->adev.urb);
723 kfree(dev->adev.transfer_buffer);
724 dev->adev.num_urb = 0;
725}
726
727/* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
728static int em28xx_audio_ep_packet_size(struct usb_device *udev,
729 struct usb_endpoint_descriptor *e)
730{
731 int size = le16_to_cpu(e->wMaxPacketSize);
732
733 if (udev->speed == USB_SPEED_HIGH)
734 return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03));
735
736 return size & 0x7ff;
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300737}
738
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300739static int em28xx_audio_urb_init(struct em28xx *dev)
740{
741 struct usb_interface *intf;
742 struct usb_endpoint_descriptor *e, *ep = NULL;
743 int i, ep_size, interval, num_urb, npackets;
744 int urb_size, bytes_per_transfer;
745 u8 alt;
746
Frank Schaefer961717b2014-01-13 19:02:06 -0300747 if (dev->ifnum)
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300748 alt = 1;
749 else
750 alt = 7;
751
Frank Schaefer961717b2014-01-13 19:02:06 -0300752 intf = usb_ifnum_to_if(dev->udev, dev->ifnum);
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300753
754 if (intf->num_altsetting <= alt) {
755 em28xx_errdev("alt %d doesn't exist on interface %d\n",
Frank Schaefer961717b2014-01-13 19:02:06 -0300756 dev->ifnum, alt);
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300757 return -ENODEV;
758 }
759
760 for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
761 e = &intf->altsetting[alt].endpoint[i].desc;
762 if (!usb_endpoint_dir_in(e))
763 continue;
764 if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
765 ep = e;
766 break;
767 }
768 }
769
770 if (!ep) {
771 em28xx_errdev("Couldn't find an audio endpoint");
772 return -ENODEV;
773 }
774
775 ep_size = em28xx_audio_ep_packet_size(dev->udev, ep);
776 interval = 1 << (ep->bInterval - 1);
777
778 em28xx_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
779 EM28XX_EP_AUDIO, usb_speed_string(dev->udev->speed),
Frank Schaefer961717b2014-01-13 19:02:06 -0300780 dev->ifnum, alt,
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300781 interval,
782 ep_size);
783
784 /* Calculate the number and size of URBs to better fit the audio samples */
785
786 /*
787 * Estimate the number of bytes per DMA transfer.
788 *
789 * This is given by the bit rate (for now, only 48000 Hz) multiplied
790 * by 2 channels and 2 bytes/sample divided by the number of microframe
791 * intervals and by the microframe rate (125 us)
792 */
793 bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
794
795 /*
796 * Estimate the number of transfer URBs. Don't let it go past the
797 * maximum number of URBs that is known to be supported by the device.
798 */
799 num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size);
800 if (num_urb > EM28XX_MAX_AUDIO_BUFS)
801 num_urb = EM28XX_MAX_AUDIO_BUFS;
802
803 /*
804 * Now that we know the number of bytes per transfer and the number of
805 * URBs, estimate the typical size of an URB, in order to adjust the
806 * minimal number of packets.
807 */
808 urb_size = bytes_per_transfer / num_urb;
809
810 /*
811 * Now, calculate the amount of audio packets to be filled on each
812 * URB. In order to preserve the old behaviour, use a minimal
813 * threshold for this value.
814 */
815 npackets = EM28XX_MIN_AUDIO_PACKETS;
816 if (urb_size > ep_size * npackets)
817 npackets = DIV_ROUND_UP(urb_size, ep_size);
818
819 em28xx_info("Number of URBs: %d, with %d packets and %d size",
820 num_urb, npackets, urb_size);
821
Mauro Carvalho Chehaba02b9c22014-01-13 03:34:23 -0300822 /* Estimate the bytes per period */
823 dev->adev.period = urb_size * npackets;
824
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300825 /* Allocate space to store the number of URBs to be used */
826
827 dev->adev.transfer_buffer = kcalloc(num_urb,
828 sizeof(*dev->adev.transfer_buffer),
829 GFP_ATOMIC);
830 if (!dev->adev.transfer_buffer) {
831 return -ENOMEM;
832 }
833
834 dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_ATOMIC);
835 if (!dev->adev.urb) {
836 kfree(dev->adev.transfer_buffer);
837 return -ENOMEM;
838 }
839
840 /* Alloc memory for each URB and for each transfer buffer */
841 dev->adev.num_urb = num_urb;
842 for (i = 0; i < num_urb; i++) {
843 struct urb *urb;
844 int j, k;
845 void *buf;
846
847 urb = usb_alloc_urb(npackets, GFP_ATOMIC);
848 if (!urb) {
849 em28xx_errdev("usb_alloc_urb failed!\n");
850 em28xx_audio_free_urb(dev);
851 return -ENOMEM;
852 }
853 dev->adev.urb[i] = urb;
854
855 buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC,
856 &urb->transfer_dma);
857 if (!buf) {
858 em28xx_errdev("usb_alloc_coherent failed!\n");
859 em28xx_audio_free_urb(dev);
860 return -ENOMEM;
861 }
862 dev->adev.transfer_buffer[i] = buf;
863
864 urb->dev = dev->udev;
865 urb->context = dev;
866 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
867 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
868 urb->transfer_buffer = buf;
869 urb->interval = interval;
870 urb->complete = em28xx_audio_isocirq;
871 urb->number_of_packets = npackets;
872 urb->transfer_buffer_length = ep_size * npackets;
873
874 for (j = k = 0; j < npackets; j++, k += ep_size) {
875 urb->iso_frame_desc[j].offset = k;
876 urb->iso_frame_desc[j].length = ep_size;
877 }
878 }
879
880 return 0;
881}
882
Markus Rechbergera52932b2008-01-05 09:55:47 -0300883static int em28xx_audio_init(struct em28xx *dev)
884{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300885 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300886 struct snd_pcm *pcm;
887 struct snd_card *card;
888 static int devnr;
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300889 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300890
Frank Schaefer961717b2014-01-13 19:02:06 -0300891 if (!dev->has_alsa_audio) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300892 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300893 the device is expecting the snd-usb-audio module or
894 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300895 return 0;
896 }
897
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300898 em28xx_info("Binding audio extension\n");
899
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300900 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
901 "Rechberger\n");
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300902 printk(KERN_INFO
903 "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300904
Takashi Iwai758021b2009-01-12 15:17:09 +0100905 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
906 &card);
907 if (err < 0)
908 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300909
910 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300911 adev->sndcard = card;
912 adev->udev = dev->udev;
913
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300914 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300915 if (err < 0)
916 goto card_free;
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300917
Markus Rechbergera52932b2008-01-05 09:55:47 -0300918 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
919 pcm->info_flags = 0;
920 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300921 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300922
923 snd_card_set_dev(card, &dev->udev->dev);
Dan Carpenter4a3eaee2010-03-23 08:40:43 -0300924 strcpy(card->driver, "Em28xx-Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300925 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300926 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300927
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300928 INIT_WORK(&dev->wq_trigger, audio_trigger);
929
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300930 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
Ezequiel García67b8d032012-06-11 15:17:23 -0300931 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
932 em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
933 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
934 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
935 em28xx_cvol_new(card, dev, "CD", AC97_CD);
936 em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
937 em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300938
Ezequiel García67b8d032012-06-11 15:17:23 -0300939 em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
940 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
941 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
942 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
943 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300944 }
945
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300946 err = em28xx_audio_urb_init(dev);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300947 if (err)
948 goto card_free;
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300949
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300950 err = snd_card_register(card);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300951 if (err < 0)
952 goto urb_free;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300953
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300954 em28xx_info("Audio extension successfully initialized\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300955 return 0;
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300956
957urb_free:
958 em28xx_audio_free_urb(dev);
959
960card_free:
961 snd_card_free(card);
Mauro Carvalho Chehab452f2362014-01-14 14:49:04 -0200962 adev->sndcard = NULL;
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300963
964 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300965}
966
967static int em28xx_audio_fini(struct em28xx *dev)
968{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300969 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300970 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300971
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300972 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300973 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300974 the device is expecting the snd-usb-audio module or
975 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300976 return 0;
977 }
978
Mauro Carvalho Chehabaa929ad2014-01-12 19:22:07 -0300979 em28xx_info("Closing audio extension");
980
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300981 if (dev->adev.sndcard) {
Mauro Carvalho Chehab452f2362014-01-14 14:49:04 -0200982 snd_card_disconnect(dev->adev.sndcard);
983 flush_work(&dev->wq_trigger);
984
985 em28xx_audio_free_urb(dev);
986
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300987 snd_card_free(dev->adev.sndcard);
988 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300989 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300990
Markus Rechbergera52932b2008-01-05 09:55:47 -0300991 return 0;
992}
993
994static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300995 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300996 .name = "Em28xx Audio Extension",
997 .init = em28xx_audio_init,
998 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300999};
1000
1001static int __init em28xx_alsa_register(void)
1002{
Markus Rechbergera52932b2008-01-05 09:55:47 -03001003 return em28xx_register_extension(&audio_ops);
1004}
1005
1006static void __exit em28xx_alsa_unregister(void)
1007{
1008 em28xx_unregister_extension(&audio_ops);
1009}
1010
1011MODULE_LICENSE("GPL");
1012MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -03001013MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehabd8992b02013-12-27 11:14:59 -03001014MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
1015MODULE_VERSION(EM28XX_VERSION);
Markus Rechbergera52932b2008-01-05 09:55:47 -03001016
1017module_init(em28xx_alsa_register);
1018module_exit(em28xx_alsa_unregister);