em28xx-audio.c 13.6 KB
Newer Older
1 2 3 4 5
/*
 *  Empiatech em28x1 audio extension
 *
 *  Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
 *
6 7 8 9
 *  Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
 *	- Port to work with the in-kernel driver
 *	- Several cleanups
 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *  This driver is based on my previous au600 usb pstn audio driver
 *  and inherits all the copyrights
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/init.h>
#include <linux/sound.h>
#include <linux/spinlock.h>
#include <linux/soundcard.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/proc_fs.h>
37
#include <linux/module.h>
38 39 40 41 42 43 44 45 46
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/info.h>
#include <sound/initval.h>
#include <sound/control.h>
#include <media/v4l2-common.h>
#include "em28xx.h"

47 48 49
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "activates debug info");
50

51 52 53
#define dprintk(fmt, arg...) do {					\
	    if (debug)							\
		printk(KERN_INFO "em28xx-audio %s: " fmt,		\
54
				  __func__, ##arg); 		\
55
	} while (0)
56

57
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
58

59
static int em28xx_deinit_isoc_audio(struct em28xx *dev)
60
{
61
	int i;
62

63
	dprintk("Stopping isoc\n");
64
	for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
65 66 67 68
		if (!irqs_disabled())
			usb_kill_urb(dev->adev.urb[i]);
		else
			usb_unlink_urb(dev->adev.urb[i]);
69

70 71
		usb_free_urb(dev->adev.urb[i]);
		dev->adev.urb[i] = NULL;
72

73 74
		kfree(dev->adev.transfer_buffer[i]);
		dev->adev.transfer_buffer[i] = NULL;
75 76 77 78 79 80 81
	}

	return 0;
}

static void em28xx_audio_isocirq(struct urb *urb)
{
82 83 84 85 86 87 88
	struct em28xx            *dev = urb->context;
	int                      i;
	unsigned int             oldptr;
	int                      period_elapsed = 0;
	int                      status;
	unsigned char            *cp;
	unsigned int             stride;
89
	struct snd_pcm_substream *substream;
90
	struct snd_pcm_runtime   *runtime;
91 92 93 94 95 96 97 98 99 100 101 102 103 104

	switch (urb->status) {
	case 0:             /* success */
	case -ETIMEDOUT:    /* NAK */
		break;
	case -ECONNRESET:   /* kill */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	default:            /* error */
		dprintk("urb completition error %d.\n", urb->status);
		break;
	}

105 106
	if (dev->adev.capture_pcm_substream) {
		substream = dev->adev.capture_pcm_substream;
107
		runtime = substream->runtime;
108
		stride = runtime->frame_bits >> 3;
109

110 111 112 113 114
		for (i = 0; i < urb->number_of_packets; i++) {
			int length =
			    urb->iso_frame_desc[i].actual_length / stride;
			cp = (unsigned char *)urb->transfer_buffer +
			    urb->iso_frame_desc[i].offset;
115

116
			if (!length)
117 118
				continue;

119
			oldptr = dev->adev.hwptr_done_capture;
120 121 122 123 124 125 126 127 128 129 130 131 132 133
			if (oldptr + length >= runtime->buffer_size) {
				unsigned int cnt =
				    runtime->buffer_size - oldptr;
				memcpy(runtime->dma_area + oldptr * stride, cp,
				       cnt * stride);
				memcpy(runtime->dma_area, cp + cnt * stride,
				       length * stride - cnt * stride);
			} else {
				memcpy(runtime->dma_area + oldptr * stride, cp,
				       length * stride);
			}

			snd_pcm_stream_lock(substream);

134 135
			dev->adev.hwptr_done_capture += length;
			if (dev->adev.hwptr_done_capture >=
136
			    runtime->buffer_size)
137
				dev->adev.hwptr_done_capture -=
138
				    runtime->buffer_size;
139

140 141
			dev->adev.capture_transfer_done += length;
			if (dev->adev.capture_transfer_done >=
142
			    runtime->period_size) {
143
				dev->adev.capture_transfer_done -=
144 145
				    runtime->period_size;
				period_elapsed = 1;
146
			}
147

148
			snd_pcm_stream_unlock(substream);
149
		}
150
		if (period_elapsed)
151 152 153
			snd_pcm_period_elapsed(substream);
	}
	urb->status = 0;
154

155 156
	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status < 0) {
157 158
		em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
			      status);
159 160 161 162 163 164
	}
	return;
}

static int em28xx_init_audio_isoc(struct em28xx *dev)
{
165 166 167 168 169
	int       i, errCode;
	const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
			    EM28XX_AUDIO_MAX_PACKET_SIZE;

	dprintk("Starting isoc transfers\n");
170

171
	for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
172
		struct urb *urb;
173
		int j, k;
174

175 176
		dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
		if (!dev->adev.transfer_buffer[i])
177
			return -ENOMEM;
178

179
		memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
180
		urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
181 182 183
		if (!urb) {
			em28xx_errdev("usb_alloc_urb failed!\n");
			for (j = 0; j < i; j++) {
184 185
				usb_free_urb(dev->adev.urb[j]);
				kfree(dev->adev.transfer_buffer[j]);
186
			}
187
			return -ENOMEM;
188
		}
189 190 191 192 193

		urb->dev = dev->udev;
		urb->context = dev;
		urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
		urb->transfer_flags = URB_ISO_ASAP;
194
		urb->transfer_buffer = dev->adev.transfer_buffer[i];
195 196 197 198 199 200 201 202 203 204
		urb->interval = 1;
		urb->complete = em28xx_audio_isocirq;
		urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
		urb->transfer_buffer_length = sb_size;

		for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
			     j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
			urb->iso_frame_desc[j].offset = k;
			urb->iso_frame_desc[j].length =
			    EM28XX_AUDIO_MAX_PACKET_SIZE;
205
		}
206
		dev->adev.urb[i] = urb;
207
	}
208

209
	for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
210
		errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
211
		if (errCode) {
212
			em28xx_deinit_isoc_audio(dev);
213 214 215
			return errCode;
		}
	}
216

217 218 219
	return 0;
}

220
static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
221
{
222
	dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
223 224
				 "stop" : "start");

225 226
	switch (cmd) {
	case EM28XX_CAPTURE_STREAM_EN:
227
		if (dev->adev.capture_stream == STREAM_OFF && arg == EM28XX_START_AUDIO) {
228
			dev->adev.capture_stream = STREAM_ON;
229
			em28xx_init_audio_isoc(dev);
230
		} else if (dev->adev.capture_stream == STREAM_ON && arg == EM28XX_STOP_AUDIO) {
231
			dev->adev.capture_stream = STREAM_OFF;
232
			em28xx_deinit_isoc_audio(dev);
233
		} else {
234
			em28xx_errdev("An underrun very likely occurred. "
235
					"Ignoring it.\n");
236 237 238 239
		}
		return 0;
	default:
		return -EINVAL;
240 241 242
	}
}

243 244 245 246 247
static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
					size_t size)
{
	struct snd_pcm_runtime *runtime = subs->runtime;

248
	dprintk("Allocating vbuffer\n");
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	if (runtime->dma_area) {
		if (runtime->dma_bytes > size)
			return 0;

		vfree(runtime->dma_area);
	}
	runtime->dma_area = vmalloc(size);
	if (!runtime->dma_area)
		return -ENOMEM;

	runtime->dma_bytes = size;

	return 0;
}

static struct snd_pcm_hardware snd_em28xx_hw_capture = {
	.info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
		SNDRV_PCM_INFO_MMAP           |
		SNDRV_PCM_INFO_INTERLEAVED    |
		SNDRV_PCM_INFO_MMAP_VALID,

	.formats = SNDRV_PCM_FMTBIT_S16_LE,

	.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,

	.rate_min = 48000,
	.rate_max = 48000,
	.channels_min = 2,
	.channels_max = 2,
	.buffer_bytes_max = 62720 * 8,	/* just about the value in usbaudio.c */
	.period_bytes_min = 64,		/* 12544/2, */
	.period_bytes_max = 12544,
	.periods_min = 2,
	.periods_max = 98,		/* 12544, */
};

static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
{
	struct em28xx *dev = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	int ret = 0;

	dprintk("opening device and trying to acquire exclusive lock\n");

293 294 295 296 297 298
	if (!dev) {
		printk(KERN_ERR "BUG: em28xx can't find device struct."
				" Can't proceed with open\n");
		return -ENODEV;
	}

299
	/* Sets volume, mute, etc */
300

301
	dev->mute = 0;
302
	mutex_lock(&dev->lock);
303
	ret = em28xx_audio_analog_set(dev);
304
	mutex_unlock(&dev->lock);
305 306 307 308
	if (ret < 0)
		goto err;

	runtime->hw = snd_em28xx_hw_capture;
309
	if (dev->alt == 0 && dev->adev.users == 0) {
310 311 312 313 314 315
		int errCode;
		dev->alt = 7;
		errCode = usb_set_interface(dev->udev, 0, 7);
		dprintk("changing alternate number to 7\n");
	}

316
	mutex_lock(&dev->lock);
317
	dev->adev.users++;
318
	mutex_unlock(&dev->lock);
319 320

	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
321
	dev->adev.capture_pcm_substream = substream;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	runtime->private_data = dev;

	return 0;
err:
	printk(KERN_ERR "Error while configuring em28xx mixer\n");
	return ret;
}

static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
{
	struct em28xx *dev = snd_pcm_substream_chip(substream);

	dprintk("closing device\n");

	dev->mute = 1;
337
	mutex_lock(&dev->lock);
338
	dev->adev.users--;
339
	em28xx_audio_analog_set(dev);
340
	mutex_unlock(&dev->lock);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

	return 0;
}

static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
					struct snd_pcm_hw_params *hw_params)
{
	unsigned int channels, rate, format;
	int ret;

	dprintk("Setting capture parameters\n");

	ret = snd_pcm_alloc_vmalloc_buffer(substream,
				params_buffer_bytes(hw_params));
	format = params_format(hw_params);
	rate = params_rate(hw_params);
	channels = params_channels(hw_params);

	/* TODO: set up em28xx audio chip to deliver the correct audio format,
	   current default is 48000hz multiplexed => 96000hz mono
	   which shouldn't matter since analogue TV only supports mono */
	return 0;
}

static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
{
	struct em28xx *dev = snd_pcm_substream_chip(substream);

	dprintk("Stop capture, if needed\n");

371
	if (dev->adev.capture_stream == STREAM_ON)
372
		em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
373 374 375 376 377 378 379 380 381 382 383 384 385

	return 0;
}

static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
{
	return 0;
}

static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
				      int cmd)
{
	struct em28xx *dev = snd_pcm_substream_chip(substream);
386
	int retval;
387 388 389

	dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
				       "start": "stop");
390 391

	spin_lock(&dev->adev.slock);
392 393
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
394
		em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_START_AUDIO);
395 396
		retval = 0;
		break;
397
	case SNDRV_PCM_TRIGGER_STOP:
398
		em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
399 400
		retval = 0;
		break;
401
	default:
402
		retval = -EINVAL;
403
	}
404 405 406

	spin_unlock(&dev->adev.slock);
	return retval;
407 408
}

409 410
static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
						    *substream)
411
{
412
	unsigned long flags;
413
	struct em28xx *dev;
414
	snd_pcm_uframes_t hwptr_done;
415

416
	dev = snd_pcm_substream_chip(substream);
417
	spin_lock_irqsave(&dev->adev.slock, flags);
418
	hwptr_done = dev->adev.hwptr_done_capture;
419
	spin_unlock_irqrestore(&dev->adev.slock, flags);
420

421 422 423 424 425 426 427
	return hwptr_done;
}

static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
					     unsigned long offset)
{
	void *pageptr = subs->runtime->dma_area + offset;
428

429 430 431 432
	return vmalloc_to_page(pageptr);
}

static struct snd_pcm_ops snd_em28xx_pcm_capture = {
433 434 435
	.open      = snd_em28xx_capture_open,
	.close     = snd_em28xx_pcm_close,
	.ioctl     = snd_pcm_lib_ioctl,
436
	.hw_params = snd_em28xx_hw_capture_params,
437 438 439 440 441
	.hw_free   = snd_em28xx_hw_capture_free,
	.prepare   = snd_em28xx_prepare,
	.trigger   = snd_em28xx_capture_trigger,
	.pointer   = snd_em28xx_capture_pointer,
	.page      = snd_pcm_get_vmalloc_page,
442 443 444 445
};

static int em28xx_audio_init(struct em28xx *dev)
{
446
	struct em28xx_audio *adev = &dev->adev;
447 448 449
	struct snd_pcm      *pcm;
	struct snd_card     *card;
	static int          devnr;
450
	int                 err;
451

452
	if (dev->has_alsa_audio != 1) {
453
		/* This device does not support the extension (in this case
454 455
		   the device is expecting the snd-usb-audio module or
		   doesn't have analog audio support at all) */
456 457 458
		return 0;
	}

459 460 461 462 463
	printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
			 "non standard usbaudio\n");
	printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
			 "Rechberger\n");

464 465 466 467
	err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
			      &card);
	if (err < 0)
		return err;
468 469

	spin_lock_init(&adev->slock);
470 471 472 473 474 475
	err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
	if (err < 0) {
		snd_card_free(card);
		return err;
	}

476 477 478
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
	pcm->info_flags = 0;
	pcm->private_data = dev;
479
	strcpy(pcm->name, "Empia 28xx Capture");
480 481

	snd_card_set_dev(card, &dev->udev->dev);
482 483
	strcpy(card->driver, "Empia Em28xx Audio");
	strcpy(card->shortname, "Em28xx Audio");
484
	strcpy(card->longname, "Empia Em28xx Audio");
485

486 487
	err = snd_card_register(card);
	if (err < 0) {
488
		snd_card_free(card);
489
		return err;
490
	}
491 492
	adev->sndcard = card;
	adev->udev = dev->udev;
493

494 495 496 497 498
	return 0;
}

static int em28xx_audio_fini(struct em28xx *dev)
{
499
	if (dev == NULL)
500
		return 0;
501

502
	if (dev->has_alsa_audio != 1) {
503
		/* This device does not support the extension (in this case
504 505
		   the device is expecting the snd-usb-audio module or
		   doesn't have analog audio support at all) */
506 507 508
		return 0;
	}

509 510 511
	if (dev->adev.sndcard) {
		snd_card_free(dev->adev.sndcard);
		dev->adev.sndcard = NULL;
512
	}
513

514 515 516 517
	return 0;
}

static struct em28xx_ops audio_ops = {
518
	.id   = EM28XX_AUDIO,
519 520 521
	.name = "Em28xx Audio Extension",
	.init = em28xx_audio_init,
	.fini = em28xx_audio_fini,
522 523 524 525 526 527 528 529 530 531 532 533 534 535
};

static int __init em28xx_alsa_register(void)
{
	return em28xx_register_extension(&audio_ops);
}

static void __exit em28xx_alsa_unregister(void)
{
	em28xx_unregister_extension(&audio_ops);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
536
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
537 538 539 540
MODULE_DESCRIPTION("Em28xx Audio driver");

module_init(em28xx_alsa_register);
module_exit(em28xx_alsa_unregister);