V4L/DVB (10525): em28xx: Coding style fixes and a typo correction
[deliverable/linux.git] / drivers / media / video / em28xx / em28xx-audio.c
CommitLineData
a52932b4
MR
1/*
2 * Empiatech em28x1 audio extension
3 *
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5 *
6d79468d
MCC
6 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
9 *
a52932b4
MR
10 * This driver is based on my previous au600 usb pstn audio driver
11 * and inherits all the copyrights
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <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>
6d79468d 37#include <linux/module.h>
a52932b4
MR
38#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>
a52932b4
MR
44#include <media/v4l2-common.h>
45#include "em28xx.h"
46
6d79468d
MCC
47static int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "activates debug info");
a52932b4 50
6d79468d
MCC
51#define dprintk(fmt, arg...) do { \
52 if (debug) \
53 printk(KERN_INFO "em28xx-audio %s: " fmt, \
d80e134d 54 __func__, ##arg); \
6d79468d 55 } while (0)
a52932b4 56
6d79468d 57static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
a52932b4 58
aa5a1821 59static int em28xx_deinit_isoc_audio(struct em28xx *dev)
a52932b4 60{
6d79468d 61 int i;
a52932b4 62
6d79468d 63 dprintk("Stopping isoc\n");
3e099baf 64 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
9c06210b
RK
65 if (!irqs_disabled())
66 usb_kill_urb(dev->adev.urb[i]);
67 else
68 usb_unlink_urb(dev->adev.urb[i]);
aa5a1821 69
9baed99e
MCC
70 usb_free_urb(dev->adev.urb[i]);
71 dev->adev.urb[i] = NULL;
53d12e5a 72
9c06210b
RK
73 kfree(dev->adev.transfer_buffer[i]);
74 dev->adev.transfer_buffer[i] = NULL;
a52932b4 75 }
a52932b4 76
a52932b4
MR
77 return 0;
78}
79
a52932b4
MR
80static void em28xx_audio_isocirq(struct urb *urb)
81{
6d79468d
MCC
82 struct em28xx *dev = urb->context;
83 int i;
84 unsigned int oldptr;
6d79468d
MCC
85 int period_elapsed = 0;
86 int status;
87 unsigned char *cp;
88 unsigned int stride;
a52932b4 89 struct snd_pcm_substream *substream;
6d79468d 90 struct snd_pcm_runtime *runtime;
aa5a1821
RK
91
92 switch (urb->status) {
93 case 0: /* success */
94 case -ETIMEDOUT: /* NAK */
95 break;
96 case -ECONNRESET: /* kill */
97 case -ENOENT:
98 case -ESHUTDOWN:
99 return;
100 default: /* error */
101 dprintk("urb completition error %d.\n", urb->status);
102 break;
103 }
104
9baed99e
MCC
105 if (dev->adev.capture_pcm_substream) {
106 substream = dev->adev.capture_pcm_substream;
1a6f11e0 107 runtime = substream->runtime;
a52932b4 108 stride = runtime->frame_bits >> 3;
6d79468d 109
1a6f11e0
MCC
110 for (i = 0; i < urb->number_of_packets; i++) {
111 int length =
112 urb->iso_frame_desc[i].actual_length / stride;
113 cp = (unsigned char *)urb->transfer_buffer +
114 urb->iso_frame_desc[i].offset;
a52932b4 115
1a6f11e0 116 if (!length)
a52932b4
MR
117 continue;
118
9baed99e 119 oldptr = dev->adev.hwptr_done_capture;
50f3beb5
MCC
120 if (oldptr + length >= runtime->buffer_size) {
121 unsigned int cnt =
122 runtime->buffer_size - oldptr;
123 memcpy(runtime->dma_area + oldptr * stride, cp,
124 cnt * stride);
125 memcpy(runtime->dma_area, cp + cnt * stride,
126 length * stride - cnt * stride);
127 } else {
128 memcpy(runtime->dma_area + oldptr * stride, cp,
129 length * stride);
130 }
131
50f3beb5 132 snd_pcm_stream_lock(substream);
50f3beb5 133
9baed99e
MCC
134 dev->adev.hwptr_done_capture += length;
135 if (dev->adev.hwptr_done_capture >=
1a6f11e0 136 runtime->buffer_size)
9baed99e 137 dev->adev.hwptr_done_capture -=
1a6f11e0 138 runtime->buffer_size;
a52932b4 139
9baed99e
MCC
140 dev->adev.capture_transfer_done += length;
141 if (dev->adev.capture_transfer_done >=
1a6f11e0 142 runtime->period_size) {
9baed99e 143 dev->adev.capture_transfer_done -=
1a6f11e0
MCC
144 runtime->period_size;
145 period_elapsed = 1;
a52932b4 146 }
6d79468d 147
50f3beb5 148 snd_pcm_stream_unlock(substream);
a52932b4 149 }
6d79468d 150 if (period_elapsed)
a52932b4 151 snd_pcm_period_elapsed(substream);
a52932b4
MR
152 }
153 urb->status = 0;
1a6f11e0 154
6d79468d
MCC
155 status = usb_submit_urb(urb, GFP_ATOMIC);
156 if (status < 0) {
1a6f11e0
MCC
157 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
158 status);
a52932b4
MR
159 }
160 return;
161}
162
a52932b4
MR
163static int em28xx_init_audio_isoc(struct em28xx *dev)
164{
6d79468d
MCC
165 int i, errCode;
166 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
167 EM28XX_AUDIO_MAX_PACKET_SIZE;
168
169 dprintk("Starting isoc transfers\n");
a52932b4 170
1a6f11e0 171 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
a52932b4 172 struct urb *urb;
1a6f11e0 173 int j, k;
6d79468d 174
9baed99e
MCC
175 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
176 if (!dev->adev.transfer_buffer[i])
a52932b4 177 return -ENOMEM;
6d79468d 178
9baed99e 179 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
1a6f11e0 180 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
ff9b3e43
DSL
181 if (!urb) {
182 em28xx_errdev("usb_alloc_urb failed!\n");
183 for (j = 0; j < i; j++) {
9baed99e
MCC
184 usb_free_urb(dev->adev.urb[j]);
185 kfree(dev->adev.transfer_buffer[j]);
ff9b3e43 186 }
a52932b4 187 return -ENOMEM;
ff9b3e43 188 }
6d79468d
MCC
189
190 urb->dev = dev->udev;
191 urb->context = dev;
192 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
193 urb->transfer_flags = URB_ISO_ASAP;
9baed99e 194 urb->transfer_buffer = dev->adev.transfer_buffer[i];
6d79468d
MCC
195 urb->interval = 1;
196 urb->complete = em28xx_audio_isocirq;
197 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
198 urb->transfer_buffer_length = sb_size;
199
200 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
201 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
202 urb->iso_frame_desc[j].offset = k;
203 urb->iso_frame_desc[j].length =
204 EM28XX_AUDIO_MAX_PACKET_SIZE;
a52932b4 205 }
9baed99e 206 dev->adev.urb[i] = urb;
a52932b4 207 }
6d79468d 208
1a6f11e0 209 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
9baed99e 210 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
1a6f11e0 211 if (errCode) {
aa5a1821 212 em28xx_deinit_isoc_audio(dev);
a52932b4
MR
213 return errCode;
214 }
215 }
6d79468d 216
a52932b4
MR
217 return 0;
218}
219
1a6f11e0 220static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
a52932b4 221{
9baed99e 222 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
6d79468d
MCC
223 "stop" : "start");
224
1a6f11e0
MCC
225 switch (cmd) {
226 case EM28XX_CAPTURE_STREAM_EN:
22cff7b3 227 if (dev->adev.capture_stream == STREAM_OFF && arg == EM28XX_START_AUDIO) {
9baed99e 228 dev->adev.capture_stream = STREAM_ON;
1a6f11e0 229 em28xx_init_audio_isoc(dev);
22cff7b3 230 } else if (dev->adev.capture_stream == STREAM_ON && arg == EM28XX_STOP_AUDIO) {
9baed99e 231 dev->adev.capture_stream = STREAM_OFF;
aa5a1821 232 em28xx_deinit_isoc_audio(dev);
1a6f11e0 233 } else {
5c030de4 234 em28xx_errdev("An underrun very likely occurred. "
6d79468d 235 "Ignoring it.\n");
1a6f11e0
MCC
236 }
237 return 0;
238 default:
239 return -EINVAL;
a52932b4
MR
240 }
241}
242
6d79468d
MCC
243static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
244 size_t size)
245{
246 struct snd_pcm_runtime *runtime = subs->runtime;
247
a1a6ee74 248 dprintk("Allocating vbuffer\n");
6d79468d
MCC
249 if (runtime->dma_area) {
250 if (runtime->dma_bytes > size)
251 return 0;
252
253 vfree(runtime->dma_area);
254 }
255 runtime->dma_area = vmalloc(size);
256 if (!runtime->dma_area)
257 return -ENOMEM;
258
259 runtime->dma_bytes = size;
260
261 return 0;
262}
263
264static struct snd_pcm_hardware snd_em28xx_hw_capture = {
265 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
266 SNDRV_PCM_INFO_MMAP |
267 SNDRV_PCM_INFO_INTERLEAVED |
268 SNDRV_PCM_INFO_MMAP_VALID,
269
270 .formats = SNDRV_PCM_FMTBIT_S16_LE,
271
272 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
273
274 .rate_min = 48000,
275 .rate_max = 48000,
276 .channels_min = 2,
277 .channels_max = 2,
278 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
279 .period_bytes_min = 64, /* 12544/2, */
280 .period_bytes_max = 12544,
281 .periods_min = 2,
282 .periods_max = 98, /* 12544, */
283};
284
285static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
286{
287 struct em28xx *dev = snd_pcm_substream_chip(substream);
288 struct snd_pcm_runtime *runtime = substream->runtime;
289 int ret = 0;
290
291 dprintk("opening device and trying to acquire exclusive lock\n");
292
83ee87a3
MCC
293 if (!dev) {
294 printk(KERN_ERR "BUG: em28xx can't find device struct."
295 " Can't proceed with open\n");
296 return -ENODEV;
297 }
298
6d79468d 299 /* Sets volume, mute, etc */
92ea42f4 300
6d79468d 301 dev->mute = 0;
92ea42f4 302 mutex_lock(&dev->lock);
6d79468d 303 ret = em28xx_audio_analog_set(dev);
92ea42f4 304 mutex_unlock(&dev->lock);
6d79468d
MCC
305 if (ret < 0)
306 goto err;
307
308 runtime->hw = snd_em28xx_hw_capture;
9baed99e 309 if (dev->alt == 0 && dev->adev.users == 0) {
6d79468d
MCC
310 int errCode;
311 dev->alt = 7;
312 errCode = usb_set_interface(dev->udev, 0, 7);
313 dprintk("changing alternate number to 7\n");
314 }
315
bf510ac3 316 mutex_lock(&dev->lock);
9baed99e 317 dev->adev.users++;
bf510ac3 318 mutex_unlock(&dev->lock);
6d79468d
MCC
319
320 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
9baed99e 321 dev->adev.capture_pcm_substream = substream;
6d79468d
MCC
322 runtime->private_data = dev;
323
324 return 0;
325err:
326 printk(KERN_ERR "Error while configuring em28xx mixer\n");
327 return ret;
328}
329
330static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
331{
332 struct em28xx *dev = snd_pcm_substream_chip(substream);
6d79468d
MCC
333
334 dprintk("closing device\n");
335
336 dev->mute = 1;
92ea42f4 337 mutex_lock(&dev->lock);
bf510ac3 338 dev->adev.users--;
6d79468d 339 em28xx_audio_analog_set(dev);
92ea42f4 340 mutex_unlock(&dev->lock);
6d79468d 341
6d79468d
MCC
342 return 0;
343}
344
345static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
346 struct snd_pcm_hw_params *hw_params)
347{
348 unsigned int channels, rate, format;
349 int ret;
350
351 dprintk("Setting capture parameters\n");
352
353 ret = snd_pcm_alloc_vmalloc_buffer(substream,
354 params_buffer_bytes(hw_params));
355 format = params_format(hw_params);
356 rate = params_rate(hw_params);
357 channels = params_channels(hw_params);
358
359 /* TODO: set up em28xx audio chip to deliver the correct audio format,
360 current default is 48000hz multiplexed => 96000hz mono
361 which shouldn't matter since analogue TV only supports mono */
362 return 0;
363}
364
365static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
366{
367 struct em28xx *dev = snd_pcm_substream_chip(substream);
368
369 dprintk("Stop capture, if needed\n");
370
9baed99e 371 if (dev->adev.capture_stream == STREAM_ON)
22cff7b3 372 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
6d79468d
MCC
373
374 return 0;
375}
376
377static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
378{
379 return 0;
380}
381
382static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
383 int cmd)
384{
385 struct em28xx *dev = snd_pcm_substream_chip(substream);
df39ca64 386 int retval;
6d79468d
MCC
387
388 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
389 "start": "stop");
df39ca64
DSL
390
391 spin_lock(&dev->adev.slock);
6d79468d
MCC
392 switch (cmd) {
393 case SNDRV_PCM_TRIGGER_START:
22cff7b3 394 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_START_AUDIO);
df39ca64
DSL
395 retval = 0;
396 break;
6d79468d 397 case SNDRV_PCM_TRIGGER_STOP:
22cff7b3 398 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
df39ca64
DSL
399 retval = 0;
400 break;
6d79468d 401 default:
df39ca64 402 retval = -EINVAL;
6d79468d 403 }
df39ca64
DSL
404
405 spin_unlock(&dev->adev.slock);
406 return retval;
6d79468d
MCC
407}
408
1a6f11e0
MCC
409static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
410 *substream)
a52932b4 411{
a1a6ee74 412 unsigned long flags;
53d12e5a 413 struct em28xx *dev;
a52932b4 414 snd_pcm_uframes_t hwptr_done;
53d12e5a 415
a52932b4 416 dev = snd_pcm_substream_chip(substream);
00bc0645 417 spin_lock_irqsave(&dev->adev.slock, flags);
9baed99e 418 hwptr_done = dev->adev.hwptr_done_capture;
00bc0645 419 spin_unlock_irqrestore(&dev->adev.slock, flags);
6d79468d 420
a52932b4
MR
421 return hwptr_done;
422}
423
424static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
425 unsigned long offset)
426{
427 void *pageptr = subs->runtime->dma_area + offset;
6d79468d 428
a52932b4
MR
429 return vmalloc_to_page(pageptr);
430}
431
432static struct snd_pcm_ops snd_em28xx_pcm_capture = {
6d79468d
MCC
433 .open = snd_em28xx_capture_open,
434 .close = snd_em28xx_pcm_close,
435 .ioctl = snd_pcm_lib_ioctl,
a52932b4 436 .hw_params = snd_em28xx_hw_capture_params,
6d79468d
MCC
437 .hw_free = snd_em28xx_hw_capture_free,
438 .prepare = snd_em28xx_prepare,
439 .trigger = snd_em28xx_capture_trigger,
440 .pointer = snd_em28xx_capture_pointer,
441 .page = snd_pcm_get_vmalloc_page,
a52932b4
MR
442};
443
a52932b4
MR
444static int em28xx_audio_init(struct em28xx *dev)
445{
9baed99e 446 struct em28xx_audio *adev = &dev->adev;
6d79468d
MCC
447 struct snd_pcm *pcm;
448 struct snd_card *card;
449 static int devnr;
65c9fd46 450 int err;
6d79468d 451
24a613e4 452 if (dev->has_alsa_audio != 1) {
df619181 453 /* This device does not support the extension (in this case
24a613e4
DH
454 the device is expecting the snd-usb-audio module or
455 doesn't have analog audio support at all) */
df619181
DH
456 return 0;
457 }
458
6d79468d
MCC
459 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
460 "non standard usbaudio\n");
461 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
462 "Rechberger\n");
463
758021bf
TI
464 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
465 &card);
466 if (err < 0)
467 return err;
a52932b4
MR
468
469 spin_lock_init(&adev->slock);
1f6340bd
MCC
470 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
471 if (err < 0) {
472 snd_card_free(card);
473 return err;
474 }
475
a52932b4
MR
476 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
477 pcm->info_flags = 0;
478 pcm->private_data = dev;
1a6f11e0 479 strcpy(pcm->name, "Empia 28xx Capture");
7662b00c
NS
480
481 snd_card_set_dev(card, &dev->udev->dev);
a52932b4
MR
482 strcpy(card->driver, "Empia Em28xx Audio");
483 strcpy(card->shortname, "Em28xx Audio");
1a6f11e0 484 strcpy(card->longname, "Empia Em28xx Audio");
a52932b4 485
6d79468d
MCC
486 err = snd_card_register(card);
487 if (err < 0) {
a52932b4 488 snd_card_free(card);
1f6340bd 489 return err;
a52932b4 490 }
1a6f11e0
MCC
491 adev->sndcard = card;
492 adev->udev = dev->udev;
6d79468d 493
a52932b4
MR
494 return 0;
495}
496
497static int em28xx_audio_fini(struct em28xx *dev)
498{
1a6f11e0 499 if (dev == NULL)
a52932b4 500 return 0;
6d79468d 501
24a613e4 502 if (dev->has_alsa_audio != 1) {
df619181 503 /* This device does not support the extension (in this case
24a613e4
DH
504 the device is expecting the snd-usb-audio module or
505 doesn't have analog audio support at all) */
df619181
DH
506 return 0;
507 }
508
9baed99e
MCC
509 if (dev->adev.sndcard) {
510 snd_card_free(dev->adev.sndcard);
511 dev->adev.sndcard = NULL;
a52932b4 512 }
6d79468d 513
a52932b4
MR
514 return 0;
515}
516
517static struct em28xx_ops audio_ops = {
6d79468d 518 .id = EM28XX_AUDIO,
1a6f11e0
MCC
519 .name = "Em28xx Audio Extension",
520 .init = em28xx_audio_init,
521 .fini = em28xx_audio_fini,
a52932b4
MR
522};
523
524static int __init em28xx_alsa_register(void)
525{
a52932b4
MR
526 return em28xx_register_extension(&audio_ops);
527}
528
529static void __exit em28xx_alsa_unregister(void)
530{
531 em28xx_unregister_extension(&audio_ops);
532}
533
534MODULE_LICENSE("GPL");
535MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
6d79468d 536MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
a52932b4
MR
537MODULE_DESCRIPTION("Em28xx Audio driver");
538
539module_init(em28xx_alsa_register);
540module_exit(em28xx_alsa_unregister);
This page took 0.209091 seconds and 5 git commands to generate.