blob: 26cf431b1a46c17fd3fed1d34ca59f5532c415a2 [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);
166 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 }
170 return;
171}
172
Markus Rechbergera52932b2008-01-05 09:55:47 -0300173static int em28xx_init_audio_isoc(struct em28xx *dev)
174{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300175 int i, errCode;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300176
177 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300178
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300179 /* Start streaming */
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300180 for (i = 0; i < dev->adev.num_urb; i++) {
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300181 memset(dev->adev.transfer_buffer[i], 0x80,
182 dev->adev.urb[i]->transfer_buffer_length);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300183
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300184 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300185 if (errCode) {
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300186 em28xx_errdev("submit of audio urb failed\n");
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;
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300269 if ((dev->alt == 0 || dev->audio_ifnum) && 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);
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300277 if (dev->audio_ifnum)
278 dev->alt = 1;
279 else
280 dev->alt = 7;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300281
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300282 dprintk("changing alternate number on interface %d to %d\n",
283 dev->audio_ifnum, dev->alt);
284 usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt);
285
286 /* Sets volume, mute, etc */
287 dev->mute = 0;
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300288 ret = em28xx_audio_analog_set(dev);
289 if (ret < 0)
290 goto err;
291
292 dev->adev.users++;
293 mutex_unlock(&dev->lock);
294 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300295
296 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300297 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300298
299 return 0;
300err:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300301 mutex_unlock(&dev->lock);
302
Filipe Rosset0bc23082009-11-04 15:31:25 -0300303 em28xx_err("Error while configuring em28xx mixer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300304 return ret;
305}
306
307static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
308{
309 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300310
311 dprintk("closing device\n");
312
313 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300314 mutex_lock(&dev->lock);
Douglas Schilling Landgrafbf510ac32009-02-08 01:11:13 -0300315 dev->adev.users--;
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300316 if (atomic_read(&dev->stream_started) > 0) {
317 atomic_set(&dev->stream_started, 0);
318 schedule_work(&dev->wq_trigger);
319 }
320
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300321 em28xx_audio_analog_set(dev);
Robert Krakora75c74d12009-05-28 11:16:19 -0300322 if (substream->runtime->dma_area) {
323 dprintk("freeing\n");
324 vfree(substream->runtime->dma_area);
325 substream->runtime->dma_area = NULL;
326 }
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300327 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300328
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300329 return 0;
330}
331
332static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *hw_params)
334{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300335 int ret;
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300336 struct em28xx *dev = snd_pcm_substream_chip(substream);
337
338 if (dev->disconnected)
339 return -ENODEV;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300340
341 dprintk("Setting capture parameters\n");
342
343 ret = snd_pcm_alloc_vmalloc_buffer(substream,
344 params_buffer_bytes(hw_params));
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300345 if (ret < 0)
346 return ret;
Hans Verkuile92ba282012-05-14 10:17:35 -0300347#if 0
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300348 /* TODO: set up em28xx audio chip to deliver the correct audio format,
349 current default is 48000hz multiplexed => 96000hz mono
350 which shouldn't matter since analogue TV only supports mono */
Hans Verkuile92ba282012-05-14 10:17:35 -0300351 unsigned int channels, rate, format;
352
353 format = params_format(hw_params);
354 rate = params_rate(hw_params);
355 channels = params_channels(hw_params);
356#endif
357
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300358 return 0;
359}
360
361static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
362{
363 struct em28xx *dev = snd_pcm_substream_chip(substream);
364
365 dprintk("Stop capture, if needed\n");
366
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300367 if (atomic_read(&dev->stream_started) > 0) {
368 atomic_set(&dev->stream_started, 0);
369 schedule_work(&dev->wq_trigger);
370 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300371
372 return 0;
373}
374
375static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
376{
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300377 struct em28xx *dev = snd_pcm_substream_chip(substream);
378
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300379 if (dev->disconnected)
380 return -ENODEV;
381
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300382 dev->adev.hwptr_done_capture = 0;
383 dev->adev.capture_transfer_done = 0;
384
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300385 return 0;
386}
387
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300388static void audio_trigger(struct work_struct *work)
389{
390 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
391
392 if (atomic_read(&dev->stream_started)) {
393 dprintk("starting capture");
394 em28xx_init_audio_isoc(dev);
395 } else {
396 dprintk("stopping capture");
397 em28xx_deinit_isoc_audio(dev);
398 }
399}
400
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300401static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
402 int cmd)
403{
404 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300405 int retval = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300406
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300407 if (dev->disconnected)
408 return -ENODEV;
409
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300410 switch (cmd) {
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300411 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
412 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300413 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300414 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300415 break;
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
417 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300418 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300419 atomic_set(&dev->stream_started, 0);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300420 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300421 default:
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300422 retval = -EINVAL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300423 }
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300424 schedule_work(&dev->wq_trigger);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300425 return retval;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300426}
427
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300428static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
429 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300430{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300431 unsigned long flags;
Robert Krakora53d12e52009-01-16 11:23:25 -0300432 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300433 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300434
Markus Rechbergera52932b2008-01-05 09:55:47 -0300435 dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300436 if (dev->disconnected)
Mauro Carvalho Chehab229295f2014-01-12 12:57:08 -0300437 return SNDRV_PCM_POS_XRUN;
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300438
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300439 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300440 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300441 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300442
Markus Rechbergera52932b2008-01-05 09:55:47 -0300443 return hwptr_done;
444}
445
446static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
447 unsigned long offset)
448{
449 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300450
Markus Rechbergera52932b2008-01-05 09:55:47 -0300451 return vmalloc_to_page(pageptr);
452}
453
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300454/*
455 * AC97 volume control support
456 */
457static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
458 struct snd_ctl_elem_info *info)
459{
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300460 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
461
462 if (dev->disconnected)
463 return -ENODEV;
464
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300465 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
466 info->count = 2;
467 info->value.integer.min = 0;
468 info->value.integer.max = 0x1f;
469
470 return 0;
471}
472
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300473static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
474 struct snd_ctl_elem_value *value)
475{
476 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300477 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300478 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
479 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300480 int nonblock = 0;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300481 int rc;
482
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300483 if (dev->disconnected)
484 return -ENODEV;
485
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300486 if (substream)
487 nonblock = !!(substream->f_flags & O_NONBLOCK);
488 if (nonblock) {
489 if (!mutex_trylock(&dev->lock))
490 return -EAGAIN;
491 } else
492 mutex_lock(&dev->lock);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300493 rc = em28xx_read_ac97(dev, kcontrol->private_value);
494 if (rc < 0)
495 goto err;
496
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300497 val |= rc & 0x8000; /* Preserve the mute flag */
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300498
499 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300500 if (rc < 0)
501 goto err;
502
503 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
504 (val & 0x8000) ? "muted " : "",
505 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
506 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300507
508err:
509 mutex_unlock(&dev->lock);
510 return rc;
511}
512
513static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
514 struct snd_ctl_elem_value *value)
515{
516 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300517 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
518 int nonblock = 0;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300519 int val;
520
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300521 if (dev->disconnected)
522 return -ENODEV;
523
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300524 if (substream)
525 nonblock = !!(substream->f_flags & O_NONBLOCK);
526 if (nonblock) {
527 if (!mutex_trylock(&dev->lock))
528 return -EAGAIN;
529 } else
530 mutex_lock(&dev->lock);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300531 val = em28xx_read_ac97(dev, kcontrol->private_value);
532 mutex_unlock(&dev->lock);
533 if (val < 0)
534 return val;
535
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300536 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
537 (val & 0x8000) ? "muted " : "",
538 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
539 val, (int)kcontrol->private_value);
540
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300541 value->value.integer.value[0] = 0x1f - (val & 0x1f);
542 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300543
544 return 0;
545}
546
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300547static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
548 struct snd_ctl_elem_value *value)
549{
550 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
551 u16 val = value->value.integer.value[0];
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300552 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
553 int nonblock = 0;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300554 int rc;
555
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300556 if (dev->disconnected)
557 return -ENODEV;
558
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300559 if (substream)
560 nonblock = !!(substream->f_flags & O_NONBLOCK);
561 if (nonblock) {
562 if (!mutex_trylock(&dev->lock))
563 return -EAGAIN;
564 } else
565 mutex_lock(&dev->lock);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300566 rc = em28xx_read_ac97(dev, kcontrol->private_value);
567 if (rc < 0)
568 goto err;
569
570 if (val)
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300571 rc &= 0x1f1f;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300572 else
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300573 rc |= 0x8000;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300574
575 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300576 if (rc < 0)
577 goto err;
578
579 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
580 (val & 0x8000) ? "muted " : "",
581 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
582 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300583
584err:
585 mutex_unlock(&dev->lock);
586 return rc;
587}
588
589static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
590 struct snd_ctl_elem_value *value)
591{
592 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300593 struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
594 int nonblock = 0;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300595 int val;
596
Mauro Carvalho Chehab8b1fa572014-01-12 10:21:42 -0300597 if (dev->disconnected)
598 return -ENODEV;
599
Mauro Carvalho Chehab34906632014-01-12 09:22:29 -0300600 if (substream)
601 nonblock = !!(substream->f_flags & O_NONBLOCK);
602 if (nonblock) {
603 if (!mutex_trylock(&dev->lock))
604 return -EAGAIN;
605 } else
606 mutex_lock(&dev->lock);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300607 val = em28xx_read_ac97(dev, kcontrol->private_value);
608 mutex_unlock(&dev->lock);
609 if (val < 0)
610 return val;
611
612 if (val & 0x8000)
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300613 value->value.integer.value[0] = 0;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300614 else
615 value->value.integer.value[0] = 1;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300616
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300617 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
618 (val & 0x8000) ? "muted " : "",
619 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
620 val, (int)kcontrol->private_value);
621
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300622 return 0;
623}
624
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300625static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
626
627static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
628 char *name, int id)
629{
630 int err;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300631 char ctl_name[44];
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300632 struct snd_kcontrol *kctl;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300633 struct snd_kcontrol_new tmp;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300634
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300635 memset (&tmp, 0, sizeof(tmp));
636 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
637 tmp.private_value = id,
638 tmp.name = ctl_name,
639
640 /* Add Mute Control */
641 sprintf(ctl_name, "%s Switch", name);
642 tmp.get = em28xx_vol_get_mute;
643 tmp.put = em28xx_vol_put_mute;
644 tmp.info = snd_ctl_boolean_mono_info;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300645 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300646 err = snd_ctl_add(card, kctl);
647 if (err < 0)
648 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300649 dprintk("Added control %s for ac97 volume control 0x%04x\n",
650 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300651
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300652 memset (&tmp, 0, sizeof(tmp));
653 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
654 tmp.private_value = id,
655 tmp.name = ctl_name,
656
657 /* Add Volume Control */
658 sprintf(ctl_name, "%s Volume", name);
659 tmp.get = em28xx_vol_get;
660 tmp.put = em28xx_vol_put;
661 tmp.info = em28xx_vol_info;
662 tmp.tlv.p = em28xx_db_scale,
663 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300664 err = snd_ctl_add(card, kctl);
665 if (err < 0)
666 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300667 dprintk("Added control %s for ac97 volume control 0x%04x\n",
668 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300669
670 return 0;
671}
672
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300673/*
674 * register/unregister code and data
675 */
Markus Rechbergera52932b2008-01-05 09:55:47 -0300676static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300677 .open = snd_em28xx_capture_open,
678 .close = snd_em28xx_pcm_close,
679 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300680 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300681 .hw_free = snd_em28xx_hw_capture_free,
682 .prepare = snd_em28xx_prepare,
683 .trigger = snd_em28xx_capture_trigger,
684 .pointer = snd_em28xx_capture_pointer,
685 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300686};
687
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300688static void em28xx_audio_free_urb(struct em28xx *dev)
689{
690 int i;
691
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300692 for (i = 0; i < dev->adev.num_urb; i++) {
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300693 struct urb *urb = dev->adev.urb[i];
694
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300695 if (!urb)
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300696 continue;
697
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300698 if (dev->adev.transfer_buffer[i])
699 usb_free_coherent(dev->udev,
700 urb->transfer_buffer_length,
701 dev->adev.transfer_buffer[i],
702 urb->transfer_dma);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300703
704 usb_free_urb(urb);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300705 }
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300706 kfree(dev->adev.urb);
707 kfree(dev->adev.transfer_buffer);
708 dev->adev.num_urb = 0;
709}
710
711/* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
712static int em28xx_audio_ep_packet_size(struct usb_device *udev,
713 struct usb_endpoint_descriptor *e)
714{
715 int size = le16_to_cpu(e->wMaxPacketSize);
716
717 if (udev->speed == USB_SPEED_HIGH)
718 return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03));
719
720 return size & 0x7ff;
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300721}
722
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300723static int em28xx_audio_urb_init(struct em28xx *dev)
724{
725 struct usb_interface *intf;
726 struct usb_endpoint_descriptor *e, *ep = NULL;
727 int i, ep_size, interval, num_urb, npackets;
728 int urb_size, bytes_per_transfer;
729 u8 alt;
730
731 if (dev->audio_ifnum)
732 alt = 1;
733 else
734 alt = 7;
735
736 intf = usb_ifnum_to_if(dev->udev, dev->audio_ifnum);
737
738 if (intf->num_altsetting <= alt) {
739 em28xx_errdev("alt %d doesn't exist on interface %d\n",
740 dev->audio_ifnum, alt);
741 return -ENODEV;
742 }
743
744 for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
745 e = &intf->altsetting[alt].endpoint[i].desc;
746 if (!usb_endpoint_dir_in(e))
747 continue;
748 if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
749 ep = e;
750 break;
751 }
752 }
753
754 if (!ep) {
755 em28xx_errdev("Couldn't find an audio endpoint");
756 return -ENODEV;
757 }
758
759 ep_size = em28xx_audio_ep_packet_size(dev->udev, ep);
760 interval = 1 << (ep->bInterval - 1);
761
762 em28xx_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
763 EM28XX_EP_AUDIO, usb_speed_string(dev->udev->speed),
764 dev->audio_ifnum, alt,
765 interval,
766 ep_size);
767
768 /* Calculate the number and size of URBs to better fit the audio samples */
769
770 /*
771 * Estimate the number of bytes per DMA transfer.
772 *
773 * This is given by the bit rate (for now, only 48000 Hz) multiplied
774 * by 2 channels and 2 bytes/sample divided by the number of microframe
775 * intervals and by the microframe rate (125 us)
776 */
777 bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
778
779 /*
780 * Estimate the number of transfer URBs. Don't let it go past the
781 * maximum number of URBs that is known to be supported by the device.
782 */
783 num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size);
784 if (num_urb > EM28XX_MAX_AUDIO_BUFS)
785 num_urb = EM28XX_MAX_AUDIO_BUFS;
786
787 /*
788 * Now that we know the number of bytes per transfer and the number of
789 * URBs, estimate the typical size of an URB, in order to adjust the
790 * minimal number of packets.
791 */
792 urb_size = bytes_per_transfer / num_urb;
793
794 /*
795 * Now, calculate the amount of audio packets to be filled on each
796 * URB. In order to preserve the old behaviour, use a minimal
797 * threshold for this value.
798 */
799 npackets = EM28XX_MIN_AUDIO_PACKETS;
800 if (urb_size > ep_size * npackets)
801 npackets = DIV_ROUND_UP(urb_size, ep_size);
802
803 em28xx_info("Number of URBs: %d, with %d packets and %d size",
804 num_urb, npackets, urb_size);
805
806 /* Allocate space to store the number of URBs to be used */
807
808 dev->adev.transfer_buffer = kcalloc(num_urb,
809 sizeof(*dev->adev.transfer_buffer),
810 GFP_ATOMIC);
811 if (!dev->adev.transfer_buffer) {
812 return -ENOMEM;
813 }
814
815 dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_ATOMIC);
816 if (!dev->adev.urb) {
817 kfree(dev->adev.transfer_buffer);
818 return -ENOMEM;
819 }
820
821 /* Alloc memory for each URB and for each transfer buffer */
822 dev->adev.num_urb = num_urb;
823 for (i = 0; i < num_urb; i++) {
824 struct urb *urb;
825 int j, k;
826 void *buf;
827
828 urb = usb_alloc_urb(npackets, GFP_ATOMIC);
829 if (!urb) {
830 em28xx_errdev("usb_alloc_urb failed!\n");
831 em28xx_audio_free_urb(dev);
832 return -ENOMEM;
833 }
834 dev->adev.urb[i] = urb;
835
836 buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC,
837 &urb->transfer_dma);
838 if (!buf) {
839 em28xx_errdev("usb_alloc_coherent failed!\n");
840 em28xx_audio_free_urb(dev);
841 return -ENOMEM;
842 }
843 dev->adev.transfer_buffer[i] = buf;
844
845 urb->dev = dev->udev;
846 urb->context = dev;
847 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
848 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
849 urb->transfer_buffer = buf;
850 urb->interval = interval;
851 urb->complete = em28xx_audio_isocirq;
852 urb->number_of_packets = npackets;
853 urb->transfer_buffer_length = ep_size * npackets;
854
855 for (j = k = 0; j < npackets; j++, k += ep_size) {
856 urb->iso_frame_desc[j].offset = k;
857 urb->iso_frame_desc[j].length = ep_size;
858 }
859 }
860
861 return 0;
862}
863
Markus Rechbergera52932b2008-01-05 09:55:47 -0300864static int em28xx_audio_init(struct em28xx *dev)
865{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300866 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300867 struct snd_pcm *pcm;
868 struct snd_card *card;
869 static int devnr;
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300870 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300871
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300872 if (!dev->has_alsa_audio || dev->audio_ifnum < 0) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300873 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300874 the device is expecting the snd-usb-audio module or
875 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300876 return 0;
877 }
878
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300879 em28xx_info("Binding audio extension\n");
880
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300881 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
882 "Rechberger\n");
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300883 printk(KERN_INFO
884 "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300885
Takashi Iwai758021b2009-01-12 15:17:09 +0100886 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
887 &card);
888 if (err < 0)
889 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300890
891 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1b3fd2d2014-01-10 05:53:24 -0300892 adev->sndcard = card;
893 adev->udev = dev->udev;
894
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300895 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300896 if (err < 0)
897 goto card_free;
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300898
Markus Rechbergera52932b2008-01-05 09:55:47 -0300899 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
900 pcm->info_flags = 0;
901 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300902 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300903
904 snd_card_set_dev(card, &dev->udev->dev);
Dan Carpenter4a3eaee2010-03-23 08:40:43 -0300905 strcpy(card->driver, "Em28xx-Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300906 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300907 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300908
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300909 INIT_WORK(&dev->wq_trigger, audio_trigger);
910
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300911 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
Ezequiel García67b8d032012-06-11 15:17:23 -0300912 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
913 em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
914 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
915 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
916 em28xx_cvol_new(card, dev, "CD", AC97_CD);
917 em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
918 em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300919
Ezequiel García67b8d032012-06-11 15:17:23 -0300920 em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
921 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
922 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
923 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
924 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300925 }
926
Mauro Carvalho Chehab966f4162014-01-12 10:10:34 -0300927 err = em28xx_audio_urb_init(dev);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300928 if (err)
929 goto card_free;
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300930
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300931 err = snd_card_register(card);
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300932 if (err < 0)
933 goto urb_free;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300934
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300935 em28xx_info("Audio extension successfully initialized\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300936 return 0;
Mauro Carvalho Chehab0cd03a02014-01-12 17:20:31 -0300937
938urb_free:
939 em28xx_audio_free_urb(dev);
940
941card_free:
942 snd_card_free(card);
943
944 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300945}
946
947static int em28xx_audio_fini(struct em28xx *dev)
948{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300949 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300950 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300951
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300952 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300953 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300954 the device is expecting the snd-usb-audio module or
955 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300956 return 0;
957 }
958
Mauro Carvalho Chehabaa929ad2014-01-12 19:22:07 -0300959 em28xx_info("Closing audio extension");
960
Mauro Carvalho Chehab1fe2e3b2014-01-12 18:14:05 -0300961 snd_card_disconnect(dev->adev.sndcard);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300962 em28xx_audio_free_urb(dev);
963
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300964 if (dev->adev.sndcard) {
965 snd_card_free(dev->adev.sndcard);
966 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300967 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300968
Markus Rechbergera52932b2008-01-05 09:55:47 -0300969 return 0;
970}
971
972static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300973 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300974 .name = "Em28xx Audio Extension",
975 .init = em28xx_audio_init,
976 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300977};
978
979static int __init em28xx_alsa_register(void)
980{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300981 return em28xx_register_extension(&audio_ops);
982}
983
984static void __exit em28xx_alsa_unregister(void)
985{
986 em28xx_unregister_extension(&audio_ops);
987}
988
989MODULE_LICENSE("GPL");
990MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300991MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehabd8992b02013-12-27 11:14:59 -0300992MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
993MODULE_VERSION(EM28XX_VERSION);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300994
995module_init(em28xx_alsa_register);
996module_exit(em28xx_alsa_unregister);