blob: 8e6f04873422040af9c5e47bf7b2f928b61da234 [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
53#define dprintk(fmt, arg...) do { \
54 if (debug) \
55 printk(KERN_INFO "em28xx-audio %s: " fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030056 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030057 } while (0)
58
Markus Rechbergera52932b2008-01-05 09:55:47 -030059static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030060
Robert Krakoraaa5a1822009-02-08 13:09:11 -030061static int em28xx_deinit_isoc_audio(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030062{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030063 int i;
64
65 dprintk("Stopping isoc\n");
Douglas Schilling Landgraf3e099ba2009-02-08 10:45:34 -030066 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030067 struct urb *urb = dev->adev.urb[i];
68
Robert Krakora9c062102009-01-25 13:08:07 -030069 if (!irqs_disabled())
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030070 usb_kill_urb(urb);
Robert Krakora9c062102009-01-25 13:08:07 -030071 else
Mauro Carvalho Chehab46c704d2013-12-28 11:47:16 -030072 usb_unlink_urb(urb);
Markus Rechbergera52932b2008-01-05 09:55:47 -030073 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030074
Markus Rechbergera52932b2008-01-05 09:55:47 -030075 return 0;
76}
77
Markus Rechbergera52932b2008-01-05 09:55:47 -030078static void em28xx_audio_isocirq(struct urb *urb)
79{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030080 struct em28xx *dev = urb->context;
81 int i;
82 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030083 int period_elapsed = 0;
84 int status;
85 unsigned char *cp;
86 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030087 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030088 struct snd_pcm_runtime *runtime;
Robert Krakoraaa5a1822009-02-08 13:09:11 -030089
90 switch (urb->status) {
91 case 0: /* success */
92 case -ETIMEDOUT: /* NAK */
93 break;
94 case -ECONNRESET: /* kill */
95 case -ENOENT:
96 case -ESHUTDOWN:
97 return;
98 default: /* error */
99 dprintk("urb completition error %d.\n", urb->status);
100 break;
101 }
102
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300103 if (atomic_read(&dev->stream_started) == 0)
104 return;
105
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300106 if (dev->adev.capture_pcm_substream) {
107 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300108 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300109 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300110
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300111 for (i = 0; i < urb->number_of_packets; i++) {
112 int length =
113 urb->iso_frame_desc[i].actual_length / stride;
114 cp = (unsigned char *)urb->transfer_buffer +
115 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300116
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300117 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300118 continue;
119
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300120 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300121 if (oldptr + length >= runtime->buffer_size) {
122 unsigned int cnt =
123 runtime->buffer_size - oldptr;
124 memcpy(runtime->dma_area + oldptr * stride, cp,
125 cnt * stride);
126 memcpy(runtime->dma_area, cp + cnt * stride,
127 length * stride - cnt * stride);
128 } else {
129 memcpy(runtime->dma_area + oldptr * stride, cp,
130 length * stride);
131 }
132
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300133 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300134
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300135 dev->adev.hwptr_done_capture += length;
136 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300137 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300138 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300139 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300140
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300141 dev->adev.capture_transfer_done += length;
142 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300143 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300144 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300145 runtime->period_size;
146 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300147 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300148
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300149 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300150 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300151 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300152 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300153 }
154 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300155
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300156 status = usb_submit_urb(urb, GFP_ATOMIC);
157 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300158 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
159 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300160 }
161 return;
162}
163
Markus Rechbergera52932b2008-01-05 09:55:47 -0300164static int em28xx_init_audio_isoc(struct em28xx *dev)
165{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300166 int i, errCode;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300167
168 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300169
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300170 /* Start streaming */
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300171 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300172 memset(dev->adev.transfer_buffer[i], 0x80,
173 dev->adev.urb[i]->transfer_buffer_length);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300174
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300175 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300176 if (errCode) {
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300177 em28xx_errdev("submit of audio urb failed\n");
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300178 em28xx_deinit_isoc_audio(dev);
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300179 atomic_set(&dev->stream_started, 0);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300180 return errCode;
181 }
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300182
Markus Rechbergera52932b2008-01-05 09:55:47 -0300183 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300184
Markus Rechbergera52932b2008-01-05 09:55:47 -0300185 return 0;
186}
187
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300188static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
189 size_t size)
190{
191 struct snd_pcm_runtime *runtime = subs->runtime;
192
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300193 dprintk("Allocating vbuffer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300194 if (runtime->dma_area) {
195 if (runtime->dma_bytes > size)
196 return 0;
197
198 vfree(runtime->dma_area);
199 }
200 runtime->dma_area = vmalloc(size);
201 if (!runtime->dma_area)
202 return -ENOMEM;
203
204 runtime->dma_bytes = size;
205
206 return 0;
207}
208
209static struct snd_pcm_hardware snd_em28xx_hw_capture = {
210 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
211 SNDRV_PCM_INFO_MMAP |
212 SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300213 SNDRV_PCM_INFO_BATCH |
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300214 SNDRV_PCM_INFO_MMAP_VALID,
215
216 .formats = SNDRV_PCM_FMTBIT_S16_LE,
217
218 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
219
220 .rate_min = 48000,
221 .rate_max = 48000,
222 .channels_min = 2,
223 .channels_max = 2,
224 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
225 .period_bytes_min = 64, /* 12544/2, */
226 .period_bytes_max = 12544,
227 .periods_min = 2,
228 .periods_max = 98, /* 12544, */
229};
230
231static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
232{
233 struct em28xx *dev = snd_pcm_substream_chip(substream);
234 struct snd_pcm_runtime *runtime = substream->runtime;
235 int ret = 0;
236
237 dprintk("opening device and trying to acquire exclusive lock\n");
238
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300239 if (!dev) {
Filipe Rosset0bc23082009-11-04 15:31:25 -0300240 em28xx_err("BUG: em28xx can't find device struct."
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300241 " Can't proceed with open\n");
242 return -ENODEV;
243 }
244
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300245 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300246 if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) {
247 if (dev->audio_ifnum)
248 dev->alt = 1;
249 else
250 dev->alt = 7;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300251
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300252 dprintk("changing alternate number on interface %d to %d\n",
253 dev->audio_ifnum, dev->alt);
254 usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt);
255
256 /* Sets volume, mute, etc */
257 dev->mute = 0;
258 mutex_lock(&dev->lock);
259 ret = em28xx_audio_analog_set(dev);
260 if (ret < 0)
261 goto err;
262
263 dev->adev.users++;
264 mutex_unlock(&dev->lock);
265 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300266
267 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300268 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300269
270 return 0;
271err:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300272 mutex_unlock(&dev->lock);
273
Filipe Rosset0bc23082009-11-04 15:31:25 -0300274 em28xx_err("Error while configuring em28xx mixer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300275 return ret;
276}
277
278static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
279{
280 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300281
282 dprintk("closing device\n");
283
284 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300285 mutex_lock(&dev->lock);
Douglas Schilling Landgrafbf510ac32009-02-08 01:11:13 -0300286 dev->adev.users--;
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300287 if (atomic_read(&dev->stream_started) > 0) {
288 atomic_set(&dev->stream_started, 0);
289 schedule_work(&dev->wq_trigger);
290 }
291
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300292 em28xx_audio_analog_set(dev);
Robert Krakora75c74d12009-05-28 11:16:19 -0300293 if (substream->runtime->dma_area) {
294 dprintk("freeing\n");
295 vfree(substream->runtime->dma_area);
296 substream->runtime->dma_area = NULL;
297 }
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300298 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300299
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300300 return 0;
301}
302
303static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
304 struct snd_pcm_hw_params *hw_params)
305{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300306 int ret;
307
308 dprintk("Setting capture parameters\n");
309
310 ret = snd_pcm_alloc_vmalloc_buffer(substream,
311 params_buffer_bytes(hw_params));
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300312 if (ret < 0)
313 return ret;
Hans Verkuile92ba282012-05-14 10:17:35 -0300314#if 0
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300315 /* TODO: set up em28xx audio chip to deliver the correct audio format,
316 current default is 48000hz multiplexed => 96000hz mono
317 which shouldn't matter since analogue TV only supports mono */
Hans Verkuile92ba282012-05-14 10:17:35 -0300318 unsigned int channels, rate, format;
319
320 format = params_format(hw_params);
321 rate = params_rate(hw_params);
322 channels = params_channels(hw_params);
323#endif
324
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300325 return 0;
326}
327
328static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
329{
330 struct em28xx *dev = snd_pcm_substream_chip(substream);
331
332 dprintk("Stop capture, if needed\n");
333
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300334 if (atomic_read(&dev->stream_started) > 0) {
335 atomic_set(&dev->stream_started, 0);
336 schedule_work(&dev->wq_trigger);
337 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300338
339 return 0;
340}
341
342static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
343{
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300344 struct em28xx *dev = snd_pcm_substream_chip(substream);
345
346 dev->adev.hwptr_done_capture = 0;
347 dev->adev.capture_transfer_done = 0;
348
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300349 return 0;
350}
351
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300352static void audio_trigger(struct work_struct *work)
353{
354 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
355
356 if (atomic_read(&dev->stream_started)) {
357 dprintk("starting capture");
358 em28xx_init_audio_isoc(dev);
359 } else {
360 dprintk("stopping capture");
361 em28xx_deinit_isoc_audio(dev);
362 }
363}
364
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300365static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
366 int cmd)
367{
368 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300369 int retval = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300370
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300371 switch (cmd) {
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300372 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
373 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300374 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300375 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300376 break;
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300377 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
378 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300379 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300380 atomic_set(&dev->stream_started, 0);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300381 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300382 default:
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300383 retval = -EINVAL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300384 }
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300385 schedule_work(&dev->wq_trigger);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300386 return retval;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300387}
388
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300389static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
390 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300391{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300392 unsigned long flags;
Robert Krakora53d12e52009-01-16 11:23:25 -0300393 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300394 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300395
Markus Rechbergera52932b2008-01-05 09:55:47 -0300396 dev = snd_pcm_substream_chip(substream);
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300397 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300398 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300399 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300400
Markus Rechbergera52932b2008-01-05 09:55:47 -0300401 return hwptr_done;
402}
403
404static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
405 unsigned long offset)
406{
407 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300408
Markus Rechbergera52932b2008-01-05 09:55:47 -0300409 return vmalloc_to_page(pageptr);
410}
411
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300412/*
413 * AC97 volume control support
414 */
415static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
416 struct snd_ctl_elem_info *info)
417{
418 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
419 info->count = 2;
420 info->value.integer.min = 0;
421 info->value.integer.max = 0x1f;
422
423 return 0;
424}
425
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300426static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
427 struct snd_ctl_elem_value *value)
428{
429 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300430 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
431 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300432 int rc;
433
434 mutex_lock(&dev->lock);
435 rc = em28xx_read_ac97(dev, kcontrol->private_value);
436 if (rc < 0)
437 goto err;
438
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300439 val |= rc & 0x8000; /* Preserve the mute flag */
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300440
441 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300442 if (rc < 0)
443 goto err;
444
445 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
446 (val & 0x8000) ? "muted " : "",
447 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
448 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300449
450err:
451 mutex_unlock(&dev->lock);
452 return rc;
453}
454
455static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
456 struct snd_ctl_elem_value *value)
457{
458 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
459 int val;
460
461 mutex_lock(&dev->lock);
462 val = em28xx_read_ac97(dev, kcontrol->private_value);
463 mutex_unlock(&dev->lock);
464 if (val < 0)
465 return val;
466
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300467 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
468 (val & 0x8000) ? "muted " : "",
469 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
470 val, (int)kcontrol->private_value);
471
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300472 value->value.integer.value[0] = 0x1f - (val & 0x1f);
473 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300474
475 return 0;
476}
477
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300478static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
479 struct snd_ctl_elem_value *value)
480{
481 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
482 u16 val = value->value.integer.value[0];
483 int rc;
484
485 mutex_lock(&dev->lock);
486 rc = em28xx_read_ac97(dev, kcontrol->private_value);
487 if (rc < 0)
488 goto err;
489
490 if (val)
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300491 rc &= 0x1f1f;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300492 else
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300493 rc |= 0x8000;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300494
495 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300496 if (rc < 0)
497 goto err;
498
499 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
500 (val & 0x8000) ? "muted " : "",
501 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
502 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300503
504err:
505 mutex_unlock(&dev->lock);
506 return rc;
507}
508
509static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
510 struct snd_ctl_elem_value *value)
511{
512 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
513 int val;
514
515 mutex_lock(&dev->lock);
516 val = em28xx_read_ac97(dev, kcontrol->private_value);
517 mutex_unlock(&dev->lock);
518 if (val < 0)
519 return val;
520
521 if (val & 0x8000)
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300522 value->value.integer.value[0] = 0;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300523 else
524 value->value.integer.value[0] = 1;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300525
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300526 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
527 (val & 0x8000) ? "muted " : "",
528 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
529 val, (int)kcontrol->private_value);
530
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300531 return 0;
532}
533
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300534static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
535
536static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
537 char *name, int id)
538{
539 int err;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300540 char ctl_name[44];
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300541 struct snd_kcontrol *kctl;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300542 struct snd_kcontrol_new tmp;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300543
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300544 memset (&tmp, 0, sizeof(tmp));
545 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
546 tmp.private_value = id,
547 tmp.name = ctl_name,
548
549 /* Add Mute Control */
550 sprintf(ctl_name, "%s Switch", name);
551 tmp.get = em28xx_vol_get_mute;
552 tmp.put = em28xx_vol_put_mute;
553 tmp.info = snd_ctl_boolean_mono_info;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300554 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300555 err = snd_ctl_add(card, kctl);
556 if (err < 0)
557 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300558 dprintk("Added control %s for ac97 volume control 0x%04x\n",
559 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300560
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300561 memset (&tmp, 0, sizeof(tmp));
562 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
563 tmp.private_value = id,
564 tmp.name = ctl_name,
565
566 /* Add Volume Control */
567 sprintf(ctl_name, "%s Volume", name);
568 tmp.get = em28xx_vol_get;
569 tmp.put = em28xx_vol_put;
570 tmp.info = em28xx_vol_info;
571 tmp.tlv.p = em28xx_db_scale,
572 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300573 err = snd_ctl_add(card, kctl);
574 if (err < 0)
575 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300576 dprintk("Added control %s for ac97 volume control 0x%04x\n",
577 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300578
579 return 0;
580}
581
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300582/*
583 * register/unregister code and data
584 */
Markus Rechbergera52932b2008-01-05 09:55:47 -0300585static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300586 .open = snd_em28xx_capture_open,
587 .close = snd_em28xx_pcm_close,
588 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300589 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300590 .hw_free = snd_em28xx_hw_capture_free,
591 .prepare = snd_em28xx_prepare,
592 .trigger = snd_em28xx_capture_trigger,
593 .pointer = snd_em28xx_capture_pointer,
594 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300595};
596
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300597static void em28xx_audio_free_urb(struct em28xx *dev)
598{
599 int i;
600
601 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
602 struct urb *urb = dev->adev.urb[i];
603
604 if (!dev->adev.urb[i])
605 continue;
606
607 usb_free_coherent(dev->udev,
608 urb->transfer_buffer_length,
609 dev->adev.transfer_buffer[i],
610 urb->transfer_dma);
611
612 usb_free_urb(urb);
613 dev->adev.urb[i] = NULL;
614 dev->adev.transfer_buffer[i] = NULL;
615 }
616}
617
Markus Rechbergera52932b2008-01-05 09:55:47 -0300618static int em28xx_audio_init(struct em28xx *dev)
619{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300620 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300621 struct snd_pcm *pcm;
622 struct snd_card *card;
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300623 struct usb_interface *intf;
624 struct usb_endpoint_descriptor *e, *ep = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300625 static int devnr;
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300626 int err, i;
627 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
628 EM28XX_AUDIO_MAX_PACKET_SIZE;
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300629 u8 alt;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300630
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300631 if (!dev->has_alsa_audio || dev->audio_ifnum < 0) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300632 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300633 the device is expecting the snd-usb-audio module or
634 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300635 return 0;
636 }
637
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300638 em28xx_info("Binding audio extension\n");
639
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300640 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
641 "Rechberger\n");
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300642 printk(KERN_INFO
643 "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300644
Takashi Iwai758021b2009-01-12 15:17:09 +0100645 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
646 &card);
647 if (err < 0)
648 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300649
650 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300651 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
652 if (err < 0) {
653 snd_card_free(card);
654 return err;
655 }
656
Markus Rechbergera52932b2008-01-05 09:55:47 -0300657 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
658 pcm->info_flags = 0;
659 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300660 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300661
662 snd_card_set_dev(card, &dev->udev->dev);
Dan Carpenter4a3eaee2010-03-23 08:40:43 -0300663 strcpy(card->driver, "Em28xx-Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300664 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300665 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300666
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300667 INIT_WORK(&dev->wq_trigger, audio_trigger);
668
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300669 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
Ezequiel GarcĂ­a67b8d032012-06-11 15:17:23 -0300670 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
671 em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
672 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
673 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
674 em28xx_cvol_new(card, dev, "CD", AC97_CD);
675 em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
676 em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300677
Ezequiel GarcĂ­a67b8d032012-06-11 15:17:23 -0300678 em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
679 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
680 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
681 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
682 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300683 }
684
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300685 if (dev->audio_ifnum)
686 alt = 1;
687 else
688 alt = 7;
689
690 intf = usb_ifnum_to_if(dev->udev, dev->audio_ifnum);
691
692 if (intf->num_altsetting <= alt) {
693 em28xx_errdev("alt %d doesn't exist on interface %d\n",
694 dev->audio_ifnum, alt);
695 return -ENODEV;
696 }
697
698 for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
699 e = &intf->altsetting[alt].endpoint[i].desc;
700 if (!usb_endpoint_dir_in(e))
701 continue;
702 if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
703 ep = e;
704 break;
705 }
706 }
707
708 if (!ep) {
709 em28xx_errdev("Couldn't find an audio endpoint");
710 return -ENODEV;
711 }
712
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300713 /* Alloc URB and transfer buffers */
714 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
715 struct urb *urb;
716 int j, k;
717 void *buf;
718
719 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
720 if (!urb) {
721 em28xx_errdev("usb_alloc_urb failed!\n");
722 em28xx_audio_free_urb(dev);
723 return -ENOMEM;
724 }
725 dev->adev.urb[i] = urb;
726
727 buf = usb_alloc_coherent(dev->udev, sb_size, GFP_ATOMIC,
728 &urb->transfer_dma);
729 if (!buf) {
730 em28xx_errdev("usb_alloc_coherent failed!\n");
731 em28xx_audio_free_urb(dev);
732 return -ENOMEM;
733 }
734 dev->adev.transfer_buffer[i] = buf;
735
736 urb->dev = dev->udev;
737 urb->context = dev;
738 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
739 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
740 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300741 urb->interval = 1 << (ep->bInterval - 1);
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300742 urb->complete = em28xx_audio_isocirq;
743 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
744 urb->transfer_buffer_length = sb_size;
745
Mauro Carvalho Chehabd2849fa2014-01-10 05:43:09 -0300746 if (!i)
747 dprintk("Will use ep 0x%02x on intf %d alt %d interval = %d (rcv isoc pipe: 0x%08x)\n",
748 EM28XX_EP_AUDIO, dev->audio_ifnum, alt,
749 urb->interval,
750 urb->pipe);
751
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300752 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
753 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
754 urb->iso_frame_desc[j].offset = k;
755 urb->iso_frame_desc[j].length =
756 EM28XX_AUDIO_MAX_PACKET_SIZE;
757 }
758 }
759
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300760 err = snd_card_register(card);
761 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300762 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300763 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300764 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300765 adev->sndcard = card;
766 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300767
Mauro Carvalho Chehab96346142013-12-26 12:41:03 -0300768 em28xx_info("Audio extension successfully initialized\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300769 return 0;
770}
771
772static int em28xx_audio_fini(struct em28xx *dev)
773{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300774 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300775 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300776
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300777 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300778 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300779 the device is expecting the snd-usb-audio module or
780 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300781 return 0;
782 }
783
Mauro Carvalho Chehabf5f894d2013-12-28 21:16:26 -0300784 em28xx_audio_free_urb(dev);
785
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300786 if (dev->adev.sndcard) {
787 snd_card_free(dev->adev.sndcard);
788 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300789 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300790
Markus Rechbergera52932b2008-01-05 09:55:47 -0300791 return 0;
792}
793
794static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300795 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300796 .name = "Em28xx Audio Extension",
797 .init = em28xx_audio_init,
798 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300799};
800
801static int __init em28xx_alsa_register(void)
802{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300803 return em28xx_register_extension(&audio_ops);
804}
805
806static void __exit em28xx_alsa_unregister(void)
807{
808 em28xx_unregister_extension(&audio_ops);
809}
810
811MODULE_LICENSE("GPL");
812MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300813MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehabd8992b02013-12-27 11:14:59 -0300814MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
815MODULE_VERSION(EM28XX_VERSION);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300816
817module_init(em28xx_alsa_register);
818module_exit(em28xx_alsa_unregister);