blob: 4df6d32884248a95c88029aea03c60349eb0c18f [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 Chehab6d794682008-01-05 09:57:31 -03006 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
9 *
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>
Markus Rechbergera52932b2008-01-05 09:55:47 -030044#include <media/v4l2-common.h>
45#include "em28xx.h"
46
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030047static int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "activates debug info");
50
51#define dprintk(fmt, arg...) do { \
52 if (debug) \
53 printk(KERN_INFO "em28xx-audio %s: " fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030054 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030055 } while (0)
56
Markus Rechbergera52932b2008-01-05 09:55:47 -030057static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030058
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030059static int em28xx_isoc_audio_deinit(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030060{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030061 int i;
62
63 dprintk("Stopping isoc\n");
64 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030065 usb_unlink_urb(dev->adev.urb[i]);
66 usb_free_urb(dev->adev.urb[i]);
67 dev->adev.urb[i] = NULL;
Robert Krakora53d12e52009-01-16 11:23:25 -030068
69 kfree(dev->adev.transfer_buffer[i]);
70 dev->adev.transfer_buffer[i] = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -030071 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030072
Markus Rechbergera52932b2008-01-05 09:55:47 -030073 return 0;
74}
75
Markus Rechbergera52932b2008-01-05 09:55:47 -030076static void em28xx_audio_isocirq(struct urb *urb)
77{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030078 struct em28xx *dev = urb->context;
79 int i;
80 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030081 int period_elapsed = 0;
82 int status;
83 unsigned char *cp;
84 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030085 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030086 struct snd_pcm_runtime *runtime;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030087 if (dev->adev.capture_pcm_substream) {
88 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -030089 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -030090 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030091
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -030092 for (i = 0; i < urb->number_of_packets; i++) {
93 int length =
94 urb->iso_frame_desc[i].actual_length / stride;
95 cp = (unsigned char *)urb->transfer_buffer +
96 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -030097
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -030098 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -030099 continue;
100
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300101 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300102 if (oldptr + length >= runtime->buffer_size) {
103 unsigned int cnt =
104 runtime->buffer_size - oldptr;
105 memcpy(runtime->dma_area + oldptr * stride, cp,
106 cnt * stride);
107 memcpy(runtime->dma_area, cp + cnt * stride,
108 length * stride - cnt * stride);
109 } else {
110 memcpy(runtime->dma_area + oldptr * stride, cp,
111 length * stride);
112 }
113
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300114 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300115
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300116 dev->adev.hwptr_done_capture += length;
117 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300118 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300119 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300120 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300121
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300122 dev->adev.capture_transfer_done += length;
123 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300124 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300125 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300126 runtime->period_size;
127 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300128 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300129
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300130 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300131 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300132 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300133 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300134 }
135 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300136
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300137 if (dev->adev.shutdown)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300138 return;
139
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300140 status = usb_submit_urb(urb, GFP_ATOMIC);
141 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300142 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
143 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300144 }
145 return;
146}
147
Markus Rechbergera52932b2008-01-05 09:55:47 -0300148static int em28xx_init_audio_isoc(struct em28xx *dev)
149{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300150 int i, errCode;
151 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
152 EM28XX_AUDIO_MAX_PACKET_SIZE;
153
154 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300155
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300156 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300157 struct urb *urb;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300158 int j, k;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300159
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300160 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
161 if (!dev->adev.transfer_buffer[i])
Markus Rechbergera52932b2008-01-05 09:55:47 -0300162 return -ENOMEM;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300163
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300164 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300165 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300166 if (!urb) {
167 em28xx_errdev("usb_alloc_urb failed!\n");
168 for (j = 0; j < i; j++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300169 usb_free_urb(dev->adev.urb[j]);
170 kfree(dev->adev.transfer_buffer[j]);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300171 }
Markus Rechbergera52932b2008-01-05 09:55:47 -0300172 return -ENOMEM;
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300173 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300174
175 urb->dev = dev->udev;
176 urb->context = dev;
177 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
178 urb->transfer_flags = URB_ISO_ASAP;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300179 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300180 urb->interval = 1;
181 urb->complete = em28xx_audio_isocirq;
182 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
183 urb->transfer_buffer_length = sb_size;
184
185 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
186 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
187 urb->iso_frame_desc[j].offset = k;
188 urb->iso_frame_desc[j].length =
189 EM28XX_AUDIO_MAX_PACKET_SIZE;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300190 }
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300191 dev->adev.urb[i] = urb;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300192 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300193
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300194 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300195 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300196 if (errCode) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300197 em28xx_isoc_audio_deinit(dev);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300198
Markus Rechbergera52932b2008-01-05 09:55:47 -0300199 return errCode;
200 }
201 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300202
Markus Rechbergera52932b2008-01-05 09:55:47 -0300203 return 0;
204}
205
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300206static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300207{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300208 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300209 "stop" : "start");
210
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300211 switch (cmd) {
212 case EM28XX_CAPTURE_STREAM_EN:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300213 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
214 dev->adev.capture_stream = STREAM_ON;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300215 em28xx_init_audio_isoc(dev);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300216 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
217 dev->adev.capture_stream = STREAM_OFF;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300218 em28xx_isoc_audio_deinit(dev);
219 } else {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300220 printk(KERN_ERR "An underrun very likely occurred. "
221 "Ignoring it.\n");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300222 }
223 return 0;
224 default:
225 return -EINVAL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300226 }
227}
228
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300229static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
230 size_t size)
231{
232 struct snd_pcm_runtime *runtime = subs->runtime;
233
234 dprintk("Alocating vbuffer\n");
235 if (runtime->dma_area) {
236 if (runtime->dma_bytes > size)
237 return 0;
238
239 vfree(runtime->dma_area);
240 }
241 runtime->dma_area = vmalloc(size);
242 if (!runtime->dma_area)
243 return -ENOMEM;
244
245 runtime->dma_bytes = size;
246
247 return 0;
248}
249
250static struct snd_pcm_hardware snd_em28xx_hw_capture = {
251 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
252 SNDRV_PCM_INFO_MMAP |
253 SNDRV_PCM_INFO_INTERLEAVED |
254 SNDRV_PCM_INFO_MMAP_VALID,
255
256 .formats = SNDRV_PCM_FMTBIT_S16_LE,
257
258 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
259
260 .rate_min = 48000,
261 .rate_max = 48000,
262 .channels_min = 2,
263 .channels_max = 2,
264 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
265 .period_bytes_min = 64, /* 12544/2, */
266 .period_bytes_max = 12544,
267 .periods_min = 2,
268 .periods_max = 98, /* 12544, */
269};
270
271static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
272{
273 struct em28xx *dev = snd_pcm_substream_chip(substream);
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 int ret = 0;
276
277 dprintk("opening device and trying to acquire exclusive lock\n");
278
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300279 if (!dev) {
280 printk(KERN_ERR "BUG: em28xx can't find device struct."
281 " Can't proceed with open\n");
282 return -ENODEV;
283 }
284
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300285 /* Sets volume, mute, etc */
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300286
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300287 dev->mute = 0;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300288 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300289 ret = em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300290 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300291 if (ret < 0)
292 goto err;
293
294 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300295 if (dev->alt == 0 && dev->adev.users == 0) {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300296 int errCode;
297 dev->alt = 7;
298 errCode = usb_set_interface(dev->udev, 0, 7);
299 dprintk("changing alternate number to 7\n");
300 }
301
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300302 dev->adev.users++;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300303
304 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300305 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300306 runtime->private_data = dev;
307
308 return 0;
309err:
310 printk(KERN_ERR "Error while configuring em28xx mixer\n");
311 return ret;
312}
313
314static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
315{
316 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300317 dev->adev.users--;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300318
319 dprintk("closing device\n");
320
321 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300322 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300323 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300324 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300325
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300326 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
327 dprintk("audio users: %d\n", dev->adev.users);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300328 dprintk("disabling audio stream!\n");
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300329 dev->adev.shutdown = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300330 dprintk("released lock\n");
331 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
332 }
333 return 0;
334}
335
336static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
337 struct snd_pcm_hw_params *hw_params)
338{
339 unsigned int channels, rate, format;
340 int ret;
341
342 dprintk("Setting capture parameters\n");
343
344 ret = snd_pcm_alloc_vmalloc_buffer(substream,
345 params_buffer_bytes(hw_params));
346 format = params_format(hw_params);
347 rate = params_rate(hw_params);
348 channels = params_channels(hw_params);
349
350 /* TODO: set up em28xx audio chip to deliver the correct audio format,
351 current default is 48000hz multiplexed => 96000hz mono
352 which shouldn't matter since analogue TV only supports mono */
353 return 0;
354}
355
356static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
357{
358 struct em28xx *dev = snd_pcm_substream_chip(substream);
359
360 dprintk("Stop capture, if needed\n");
361
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300362 if (dev->adev.capture_stream == STREAM_ON)
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300363 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
364
365 return 0;
366}
367
368static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
369{
370 return 0;
371}
372
373static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
374 int cmd)
375{
376 struct em28xx *dev = snd_pcm_substream_chip(substream);
377
378 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
379 "start": "stop");
380 switch (cmd) {
381 case SNDRV_PCM_TRIGGER_START:
382 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
383 return 0;
384 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300385 dev->adev.shutdown = 1;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300386 return 0;
387 default:
388 return -EINVAL;
389 }
390}
391
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300392static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
393 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300394{
Robert Krakora53d12e52009-01-16 11:23:25 -0300395 unsigned long flags;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300396
Robert Krakora53d12e52009-01-16 11:23:25 -0300397 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300398 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300399
Markus Rechbergera52932b2008-01-05 09:55:47 -0300400 dev = snd_pcm_substream_chip(substream);
Robert Krakora53d12e52009-01-16 11:23:25 -0300401 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300402 hwptr_done = dev->adev.hwptr_done_capture;
Robert Krakora53d12e52009-01-16 11:23:25 -0300403 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300404
Markus Rechbergera52932b2008-01-05 09:55:47 -0300405 return hwptr_done;
406}
407
408static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
409 unsigned long offset)
410{
411 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300412
Markus Rechbergera52932b2008-01-05 09:55:47 -0300413 return vmalloc_to_page(pageptr);
414}
415
416static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300417 .open = snd_em28xx_capture_open,
418 .close = snd_em28xx_pcm_close,
419 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300420 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300421 .hw_free = snd_em28xx_hw_capture_free,
422 .prepare = snd_em28xx_prepare,
423 .trigger = snd_em28xx_capture_trigger,
424 .pointer = snd_em28xx_capture_pointer,
425 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300426};
427
Markus Rechbergera52932b2008-01-05 09:55:47 -0300428static int em28xx_audio_init(struct em28xx *dev)
429{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300430 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300431 struct snd_pcm *pcm;
432 struct snd_card *card;
433 static int devnr;
Devin Heitmueller65c9fd42008-11-12 02:04:53 -0300434 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300435
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300436 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300437 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300438 the device is expecting the snd-usb-audio module or
439 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300440 return 0;
441 }
442
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300443 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
444 "non standard usbaudio\n");
445 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
446 "Rechberger\n");
447
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300448 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300449 if (card == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300450 return -ENOMEM;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300451
452 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300453 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
454 if (err < 0) {
455 snd_card_free(card);
456 return err;
457 }
458
Markus Rechbergera52932b2008-01-05 09:55:47 -0300459 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
460 pcm->info_flags = 0;
461 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300462 strcpy(pcm->name, "Empia 28xx Capture");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300463 strcpy(card->driver, "Empia Em28xx Audio");
464 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300465 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300466
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300467 err = snd_card_register(card);
468 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300469 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300470 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300471 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300472 adev->sndcard = card;
473 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300474
Markus Rechbergera52932b2008-01-05 09:55:47 -0300475 return 0;
476}
477
478static int em28xx_audio_fini(struct em28xx *dev)
479{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300480 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300481 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300482
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300483 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300484 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300485 the device is expecting the snd-usb-audio module or
486 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300487 return 0;
488 }
489
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300490 if (dev->adev.sndcard) {
491 snd_card_free(dev->adev.sndcard);
492 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300493 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300494
Markus Rechbergera52932b2008-01-05 09:55:47 -0300495 return 0;
496}
497
498static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300499 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300500 .name = "Em28xx Audio Extension",
501 .init = em28xx_audio_init,
502 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300503};
504
505static int __init em28xx_alsa_register(void)
506{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300507 return em28xx_register_extension(&audio_ops);
508}
509
510static void __exit em28xx_alsa_unregister(void)
511{
512 em28xx_unregister_extension(&audio_ops);
513}
514
515MODULE_LICENSE("GPL");
516MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300517MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300518MODULE_DESCRIPTION("Em28xx Audio driver");
519
520module_init(em28xx_alsa_register);
521module_exit(em28xx_alsa_unregister);