xref: /linux/drivers/media/usb/cx231xx/cx231xx-audio.c (revision 4ab5a5d2a4a2289c2af07accbec7170ca5671f41)
1 /*
2  *  Conexant Cx231xx audio extension
3  *
4  *  Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5  *       Based on em28xx driver
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  */
18 
19 #include "cx231xx.h"
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sound.h>
23 #include <linux/spinlock.h>
24 #include <linux/soundcard.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/proc_fs.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/info.h>
33 #include <sound/initval.h>
34 #include <sound/control.h>
35 #include <media/v4l2-common.h>
36 
37 static int debug;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "activates debug info");
40 
41 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
42 
43 static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
44 {
45 	int i;
46 
47 	dev_dbg(dev->dev, "Stopping isoc\n");
48 
49 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
50 		if (dev->adev.urb[i]) {
51 			if (!irqs_disabled())
52 				usb_kill_urb(dev->adev.urb[i]);
53 			else
54 				usb_unlink_urb(dev->adev.urb[i]);
55 
56 			usb_free_urb(dev->adev.urb[i]);
57 			dev->adev.urb[i] = NULL;
58 
59 			kfree(dev->adev.transfer_buffer[i]);
60 			dev->adev.transfer_buffer[i] = NULL;
61 		}
62 	}
63 
64 	return 0;
65 }
66 
67 static int cx231xx_bulk_audio_deinit(struct cx231xx *dev)
68 {
69 	int i;
70 
71 	dev_dbg(dev->dev, "Stopping bulk\n");
72 
73 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
74 		if (dev->adev.urb[i]) {
75 			if (!irqs_disabled())
76 				usb_kill_urb(dev->adev.urb[i]);
77 			else
78 				usb_unlink_urb(dev->adev.urb[i]);
79 
80 			usb_free_urb(dev->adev.urb[i]);
81 			dev->adev.urb[i] = NULL;
82 
83 			kfree(dev->adev.transfer_buffer[i]);
84 			dev->adev.transfer_buffer[i] = NULL;
85 		}
86 	}
87 
88 	return 0;
89 }
90 
91 static void cx231xx_audio_isocirq(struct urb *urb)
92 {
93 	struct cx231xx *dev = urb->context;
94 	int i;
95 	unsigned int oldptr;
96 	int period_elapsed = 0;
97 	int status;
98 	unsigned char *cp;
99 	unsigned int stride;
100 	struct snd_pcm_substream *substream;
101 	struct snd_pcm_runtime *runtime;
102 
103 	if (dev->state & DEV_DISCONNECTED)
104 		return;
105 
106 	switch (urb->status) {
107 	case 0:		/* success */
108 	case -ETIMEDOUT:	/* NAK */
109 		break;
110 	case -ECONNRESET:	/* kill */
111 	case -ENOENT:
112 	case -ESHUTDOWN:
113 		return;
114 	default:		/* error */
115 		dev_dbg(dev->dev, "urb completion error %d.\n",
116 			urb->status);
117 		break;
118 	}
119 
120 	if (atomic_read(&dev->stream_started) == 0)
121 		return;
122 
123 	if (dev->adev.capture_pcm_substream) {
124 		substream = dev->adev.capture_pcm_substream;
125 		runtime = substream->runtime;
126 		stride = runtime->frame_bits >> 3;
127 
128 		for (i = 0; i < urb->number_of_packets; i++) {
129 			unsigned long flags;
130 			int length = urb->iso_frame_desc[i].actual_length /
131 				     stride;
132 			cp = (unsigned char *)urb->transfer_buffer +
133 					      urb->iso_frame_desc[i].offset;
134 
135 			if (!length)
136 				continue;
137 
138 			oldptr = dev->adev.hwptr_done_capture;
139 			if (oldptr + length >= runtime->buffer_size) {
140 				unsigned int cnt;
141 
142 				cnt = runtime->buffer_size - oldptr;
143 				memcpy(runtime->dma_area + oldptr * stride, cp,
144 				       cnt * stride);
145 				memcpy(runtime->dma_area, cp + cnt * stride,
146 				       length * stride - cnt * stride);
147 			} else {
148 				memcpy(runtime->dma_area + oldptr * stride, cp,
149 				       length * stride);
150 			}
151 
152 			snd_pcm_stream_lock_irqsave(substream, flags);
153 
154 			dev->adev.hwptr_done_capture += length;
155 			if (dev->adev.hwptr_done_capture >=
156 						runtime->buffer_size)
157 				dev->adev.hwptr_done_capture -=
158 						runtime->buffer_size;
159 
160 			dev->adev.capture_transfer_done += length;
161 			if (dev->adev.capture_transfer_done >=
162 				runtime->period_size) {
163 				dev->adev.capture_transfer_done -=
164 						runtime->period_size;
165 				period_elapsed = 1;
166 			}
167 			snd_pcm_stream_unlock_irqrestore(substream, flags);
168 		}
169 		if (period_elapsed)
170 			snd_pcm_period_elapsed(substream);
171 	}
172 	urb->status = 0;
173 
174 	status = usb_submit_urb(urb, GFP_ATOMIC);
175 	if (status < 0) {
176 		dev_err(dev->dev,
177 			"resubmit of audio urb failed (error=%i)\n",
178 			status);
179 	}
180 	return;
181 }
182 
183 static void cx231xx_audio_bulkirq(struct urb *urb)
184 {
185 	struct cx231xx *dev = urb->context;
186 	unsigned int oldptr;
187 	int period_elapsed = 0;
188 	int status;
189 	unsigned char *cp;
190 	unsigned int stride;
191 	struct snd_pcm_substream *substream;
192 	struct snd_pcm_runtime *runtime;
193 
194 	if (dev->state & DEV_DISCONNECTED)
195 		return;
196 
197 	switch (urb->status) {
198 	case 0:		/* success */
199 	case -ETIMEDOUT:	/* NAK */
200 		break;
201 	case -ECONNRESET:	/* kill */
202 	case -ENOENT:
203 	case -ESHUTDOWN:
204 		return;
205 	default:		/* error */
206 		dev_dbg(dev->dev, "urb completion error %d.\n",
207 			urb->status);
208 		break;
209 	}
210 
211 	if (atomic_read(&dev->stream_started) == 0)
212 		return;
213 
214 	if (dev->adev.capture_pcm_substream) {
215 		substream = dev->adev.capture_pcm_substream;
216 		runtime = substream->runtime;
217 		stride = runtime->frame_bits >> 3;
218 
219 		if (1) {
220 			unsigned long flags;
221 			int length = urb->actual_length /
222 				     stride;
223 			cp = (unsigned char *)urb->transfer_buffer;
224 
225 			oldptr = dev->adev.hwptr_done_capture;
226 			if (oldptr + length >= runtime->buffer_size) {
227 				unsigned int cnt;
228 
229 				cnt = runtime->buffer_size - oldptr;
230 				memcpy(runtime->dma_area + oldptr * stride, cp,
231 				       cnt * stride);
232 				memcpy(runtime->dma_area, cp + cnt * stride,
233 				       length * stride - cnt * stride);
234 			} else {
235 				memcpy(runtime->dma_area + oldptr * stride, cp,
236 				       length * stride);
237 			}
238 
239 			snd_pcm_stream_lock_irqsave(substream, flags);
240 
241 			dev->adev.hwptr_done_capture += length;
242 			if (dev->adev.hwptr_done_capture >=
243 						runtime->buffer_size)
244 				dev->adev.hwptr_done_capture -=
245 						runtime->buffer_size;
246 
247 			dev->adev.capture_transfer_done += length;
248 			if (dev->adev.capture_transfer_done >=
249 				runtime->period_size) {
250 				dev->adev.capture_transfer_done -=
251 						runtime->period_size;
252 				period_elapsed = 1;
253 			}
254 			snd_pcm_stream_unlock_irqrestore(substream, flags);
255 		}
256 		if (period_elapsed)
257 			snd_pcm_period_elapsed(substream);
258 	}
259 	urb->status = 0;
260 
261 	status = usb_submit_urb(urb, GFP_ATOMIC);
262 	if (status < 0) {
263 		dev_err(dev->dev,
264 			"resubmit of audio urb failed (error=%i)\n",
265 			status);
266 	}
267 	return;
268 }
269 
270 static int cx231xx_init_audio_isoc(struct cx231xx *dev)
271 {
272 	int i, errCode;
273 	int sb_size;
274 
275 	dev_dbg(dev->dev,
276 		"%s: Starting ISO AUDIO transfers\n", __func__);
277 
278 	if (dev->state & DEV_DISCONNECTED)
279 		return -ENODEV;
280 
281 	sb_size = CX231XX_ISO_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
282 
283 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
284 		struct urb *urb;
285 		int j, k;
286 
287 		dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
288 		if (!dev->adev.transfer_buffer[i])
289 			return -ENOMEM;
290 
291 		memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
292 		urb = usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS, GFP_ATOMIC);
293 		if (!urb) {
294 			for (j = 0; j < i; j++) {
295 				usb_free_urb(dev->adev.urb[j]);
296 				kfree(dev->adev.transfer_buffer[j]);
297 			}
298 			return -ENOMEM;
299 		}
300 
301 		urb->dev = dev->udev;
302 		urb->context = dev;
303 		urb->pipe = usb_rcvisocpipe(dev->udev,
304 						dev->adev.end_point_addr);
305 		urb->transfer_flags = URB_ISO_ASAP;
306 		urb->transfer_buffer = dev->adev.transfer_buffer[i];
307 		urb->interval = 1;
308 		urb->complete = cx231xx_audio_isocirq;
309 		urb->number_of_packets = CX231XX_ISO_NUM_AUDIO_PACKETS;
310 		urb->transfer_buffer_length = sb_size;
311 
312 		for (j = k = 0; j < CX231XX_ISO_NUM_AUDIO_PACKETS;
313 			j++, k += dev->adev.max_pkt_size) {
314 			urb->iso_frame_desc[j].offset = k;
315 			urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
316 		}
317 		dev->adev.urb[i] = urb;
318 	}
319 
320 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
321 		errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
322 		if (errCode < 0) {
323 			cx231xx_isoc_audio_deinit(dev);
324 			return errCode;
325 		}
326 	}
327 
328 	return errCode;
329 }
330 
331 static int cx231xx_init_audio_bulk(struct cx231xx *dev)
332 {
333 	int i, errCode;
334 	int sb_size;
335 
336 	dev_dbg(dev->dev,
337 		"%s: Starting BULK AUDIO transfers\n", __func__);
338 
339 	if (dev->state & DEV_DISCONNECTED)
340 		return -ENODEV;
341 
342 	sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
343 
344 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
345 		struct urb *urb;
346 		int j;
347 
348 		dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
349 		if (!dev->adev.transfer_buffer[i])
350 			return -ENOMEM;
351 
352 		memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
353 		urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
354 		if (!urb) {
355 			for (j = 0; j < i; j++) {
356 				usb_free_urb(dev->adev.urb[j]);
357 				kfree(dev->adev.transfer_buffer[j]);
358 			}
359 			return -ENOMEM;
360 		}
361 
362 		urb->dev = dev->udev;
363 		urb->context = dev;
364 		urb->pipe = usb_rcvbulkpipe(dev->udev,
365 						dev->adev.end_point_addr);
366 		urb->transfer_flags = 0;
367 		urb->transfer_buffer = dev->adev.transfer_buffer[i];
368 		urb->complete = cx231xx_audio_bulkirq;
369 		urb->transfer_buffer_length = sb_size;
370 
371 		dev->adev.urb[i] = urb;
372 
373 	}
374 
375 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
376 		errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
377 		if (errCode < 0) {
378 			cx231xx_bulk_audio_deinit(dev);
379 			return errCode;
380 		}
381 	}
382 
383 	return errCode;
384 }
385 
386 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
387 					size_t size)
388 {
389 	struct snd_pcm_runtime *runtime = subs->runtime;
390 	struct cx231xx *dev = snd_pcm_substream_chip(subs);
391 
392 	dev_dbg(dev->dev, "Allocating vbuffer\n");
393 	if (runtime->dma_area) {
394 		if (runtime->dma_bytes > size)
395 			return 0;
396 
397 		vfree(runtime->dma_area);
398 	}
399 	runtime->dma_area = vmalloc(size);
400 	if (!runtime->dma_area)
401 		return -ENOMEM;
402 
403 	runtime->dma_bytes = size;
404 
405 	return 0;
406 }
407 
408 static const struct snd_pcm_hardware snd_cx231xx_hw_capture = {
409 	.info = SNDRV_PCM_INFO_BLOCK_TRANSFER	|
410 	    SNDRV_PCM_INFO_MMAP			|
411 	    SNDRV_PCM_INFO_INTERLEAVED		|
412 	    SNDRV_PCM_INFO_MMAP_VALID,
413 
414 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
415 
416 	.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
417 
418 	.rate_min = 48000,
419 	.rate_max = 48000,
420 	.channels_min = 2,
421 	.channels_max = 2,
422 	.buffer_bytes_max = 62720 * 8,	/* just about the value in usbaudio.c */
423 	.period_bytes_min = 64,		/* 12544/2, */
424 	.period_bytes_max = 12544,
425 	.periods_min = 2,
426 	.periods_max = 98,		/* 12544, */
427 };
428 
429 static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
430 {
431 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
432 	struct snd_pcm_runtime *runtime = substream->runtime;
433 	int ret = 0;
434 
435 	dev_dbg(dev->dev,
436 		"opening device and trying to acquire exclusive lock\n");
437 
438 	if (dev->state & DEV_DISCONNECTED) {
439 		dev_err(dev->dev,
440 			"Can't open. the device was removed.\n");
441 		return -ENODEV;
442 	}
443 
444 	/* set alternate setting for audio interface */
445 	/* 1 - 48000 samples per sec */
446 	mutex_lock(&dev->lock);
447 	if (dev->USE_ISO)
448 		ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1);
449 	else
450 		ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
451 	mutex_unlock(&dev->lock);
452 	if (ret < 0) {
453 		dev_err(dev->dev,
454 			"failed to set alternate setting !\n");
455 
456 		return ret;
457 	}
458 
459 	runtime->hw = snd_cx231xx_hw_capture;
460 
461 	mutex_lock(&dev->lock);
462 	/* inform hardware to start streaming */
463 	ret = cx231xx_capture_start(dev, 1, Audio);
464 
465 	dev->adev.users++;
466 	mutex_unlock(&dev->lock);
467 
468 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
469 	dev->adev.capture_pcm_substream = substream;
470 	runtime->private_data = dev;
471 
472 	return 0;
473 }
474 
475 static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
476 {
477 	int ret;
478 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
479 
480 	dev_dbg(dev->dev, "closing device\n");
481 
482 	/* inform hardware to stop streaming */
483 	mutex_lock(&dev->lock);
484 	ret = cx231xx_capture_start(dev, 0, Audio);
485 
486 	/* set alternate setting for audio interface */
487 	/* 1 - 48000 samples per sec */
488 	ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
489 	if (ret < 0) {
490 		dev_err(dev->dev,
491 			"failed to set alternate setting !\n");
492 
493 		mutex_unlock(&dev->lock);
494 		return ret;
495 	}
496 
497 	dev->adev.users--;
498 	if (substream->runtime->dma_area) {
499 		dev_dbg(dev->dev, "freeing\n");
500 		vfree(substream->runtime->dma_area);
501 		substream->runtime->dma_area = NULL;
502 	}
503 	mutex_unlock(&dev->lock);
504 
505 	if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
506 		dev_dbg(dev->dev, "audio users: %d\n", dev->adev.users);
507 		dev_dbg(dev->dev, "disabling audio stream!\n");
508 		dev->adev.shutdown = 0;
509 		dev_dbg(dev->dev, "released lock\n");
510 		if (atomic_read(&dev->stream_started) > 0) {
511 			atomic_set(&dev->stream_started, 0);
512 			schedule_work(&dev->wq_trigger);
513 		}
514 	}
515 	return 0;
516 }
517 
518 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
519 					 struct snd_pcm_hw_params *hw_params)
520 {
521 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
522 	int ret;
523 
524 	dev_dbg(dev->dev, "Setting capture parameters\n");
525 
526 	ret = snd_pcm_alloc_vmalloc_buffer(substream,
527 					   params_buffer_bytes(hw_params));
528 #if 0
529 	/* TODO: set up cx231xx audio chip to deliver the correct audio format,
530 	   current default is 48000hz multiplexed => 96000hz mono
531 	   which shouldn't matter since analogue TV only supports mono */
532 	unsigned int channels, rate, format;
533 
534 	format = params_format(hw_params);
535 	rate = params_rate(hw_params);
536 	channels = params_channels(hw_params);
537 #endif
538 
539 	return ret;
540 }
541 
542 static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
543 {
544 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
545 
546 	dev_dbg(dev->dev, "Stop capture, if needed\n");
547 
548 	if (atomic_read(&dev->stream_started) > 0) {
549 		atomic_set(&dev->stream_started, 0);
550 		schedule_work(&dev->wq_trigger);
551 	}
552 
553 	return 0;
554 }
555 
556 static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
557 {
558 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
559 
560 	dev->adev.hwptr_done_capture = 0;
561 	dev->adev.capture_transfer_done = 0;
562 
563 	return 0;
564 }
565 
566 static void audio_trigger(struct work_struct *work)
567 {
568 	struct cx231xx *dev = container_of(work, struct cx231xx, wq_trigger);
569 
570 	if (atomic_read(&dev->stream_started)) {
571 		dev_dbg(dev->dev, "starting capture");
572 		if (is_fw_load(dev) == 0)
573 			cx25840_call(dev, core, load_fw);
574 		if (dev->USE_ISO)
575 			cx231xx_init_audio_isoc(dev);
576 		else
577 			cx231xx_init_audio_bulk(dev);
578 	} else {
579 		dev_dbg(dev->dev, "stopping capture");
580 		cx231xx_isoc_audio_deinit(dev);
581 	}
582 }
583 
584 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
585 				       int cmd)
586 {
587 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
588 	int retval = 0;
589 
590 	if (dev->state & DEV_DISCONNECTED)
591 		return -ENODEV;
592 
593 	spin_lock(&dev->adev.slock);
594 	switch (cmd) {
595 	case SNDRV_PCM_TRIGGER_START:
596 		atomic_set(&dev->stream_started, 1);
597 		break;
598 	case SNDRV_PCM_TRIGGER_STOP:
599 		atomic_set(&dev->stream_started, 0);
600 		break;
601 	default:
602 		retval = -EINVAL;
603 		break;
604 	}
605 	spin_unlock(&dev->adev.slock);
606 
607 	schedule_work(&dev->wq_trigger);
608 
609 	return retval;
610 }
611 
612 static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
613 						     *substream)
614 {
615 	struct cx231xx *dev;
616 	unsigned long flags;
617 	snd_pcm_uframes_t hwptr_done;
618 
619 	dev = snd_pcm_substream_chip(substream);
620 
621 	spin_lock_irqsave(&dev->adev.slock, flags);
622 	hwptr_done = dev->adev.hwptr_done_capture;
623 	spin_unlock_irqrestore(&dev->adev.slock, flags);
624 
625 	return hwptr_done;
626 }
627 
628 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
629 					     unsigned long offset)
630 {
631 	void *pageptr = subs->runtime->dma_area + offset;
632 
633 	return vmalloc_to_page(pageptr);
634 }
635 
636 static const struct snd_pcm_ops snd_cx231xx_pcm_capture = {
637 	.open = snd_cx231xx_capture_open,
638 	.close = snd_cx231xx_pcm_close,
639 	.ioctl = snd_pcm_lib_ioctl,
640 	.hw_params = snd_cx231xx_hw_capture_params,
641 	.hw_free = snd_cx231xx_hw_capture_free,
642 	.prepare = snd_cx231xx_prepare,
643 	.trigger = snd_cx231xx_capture_trigger,
644 	.pointer = snd_cx231xx_capture_pointer,
645 	.page = snd_pcm_get_vmalloc_page,
646 };
647 
648 static int cx231xx_audio_init(struct cx231xx *dev)
649 {
650 	struct cx231xx_audio *adev = &dev->adev;
651 	struct snd_pcm *pcm;
652 	struct snd_card *card;
653 	static int devnr;
654 	int err;
655 	struct usb_interface *uif;
656 	int i, isoc_pipe = 0;
657 
658 	if (dev->has_alsa_audio != 1) {
659 		/* This device does not support the extension (in this case
660 		   the device is expecting the snd-usb-audio module or
661 		   doesn't have analog audio support at all) */
662 		return 0;
663 	}
664 
665 	dev_dbg(dev->dev,
666 		"probing for cx231xx non standard usbaudio\n");
667 
668 	err = snd_card_new(dev->dev, index[devnr], "Cx231xx Audio",
669 			   THIS_MODULE, 0, &card);
670 	if (err < 0)
671 		return err;
672 
673 	spin_lock_init(&adev->slock);
674 	err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
675 	if (err < 0)
676 		goto err_free_card;
677 
678 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
679 			&snd_cx231xx_pcm_capture);
680 	pcm->info_flags = 0;
681 	pcm->private_data = dev;
682 	strscpy(pcm->name, "Conexant cx231xx Capture", sizeof(pcm->name));
683 	strscpy(card->driver, "Cx231xx-Audio", sizeof(card->driver));
684 	strscpy(card->shortname, "Cx231xx Audio", sizeof(card->shortname));
685 	strscpy(card->longname, "Conexant cx231xx Audio", sizeof(card->longname));
686 
687 	INIT_WORK(&dev->wq_trigger, audio_trigger);
688 
689 	err = snd_card_register(card);
690 	if (err < 0)
691 		goto err_free_card;
692 
693 	adev->sndcard = card;
694 	adev->udev = dev->udev;
695 
696 	/* compute alternate max packet sizes for Audio */
697 	uif =
698 	    dev->udev->actconfig->interface[dev->current_pcb_config.
699 					    hs_config_info[0].interface_info.
700 					    audio_index + 1];
701 
702 	if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1) {
703 		err = -ENODEV;
704 		goto err_free_card;
705 	}
706 
707 	adev->end_point_addr =
708 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
709 			bEndpointAddress;
710 
711 	adev->num_alt = uif->num_altsetting;
712 	dev_info(dev->dev,
713 		"audio EndPoint Addr 0x%x, Alternate settings: %i\n",
714 		adev->end_point_addr, adev->num_alt);
715 	adev->alt_max_pkt_size = kmalloc_array(32, adev->num_alt, GFP_KERNEL);
716 	if (!adev->alt_max_pkt_size) {
717 		err = -ENOMEM;
718 		goto err_free_card;
719 	}
720 
721 	for (i = 0; i < adev->num_alt; i++) {
722 		u16 tmp;
723 
724 		if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1) {
725 			err = -ENODEV;
726 			goto err_free_pkt_size;
727 		}
728 
729 		tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
730 				wMaxPacketSize);
731 		adev->alt_max_pkt_size[i] =
732 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
733 		dev_dbg(dev->dev,
734 			"audio alternate setting %i, max size= %i\n", i,
735 			adev->alt_max_pkt_size[i]);
736 	}
737 
738 	return 0;
739 
740 err_free_pkt_size:
741 	kfree(adev->alt_max_pkt_size);
742 err_free_card:
743 	snd_card_free(card);
744 
745 	return err;
746 }
747 
748 static int cx231xx_audio_fini(struct cx231xx *dev)
749 {
750 	if (dev == NULL)
751 		return 0;
752 
753 	if (dev->has_alsa_audio != 1) {
754 		/* This device does not support the extension (in this case
755 		   the device is expecting the snd-usb-audio module or
756 		   doesn't have analog audio support at all) */
757 		return 0;
758 	}
759 
760 	if (dev->adev.sndcard) {
761 		snd_card_free(dev->adev.sndcard);
762 		kfree(dev->adev.alt_max_pkt_size);
763 		dev->adev.sndcard = NULL;
764 	}
765 
766 	return 0;
767 }
768 
769 static struct cx231xx_ops audio_ops = {
770 	.id = CX231XX_AUDIO,
771 	.name = "Cx231xx Audio Extension",
772 	.init = cx231xx_audio_init,
773 	.fini = cx231xx_audio_fini,
774 };
775 
776 static int __init cx231xx_alsa_register(void)
777 {
778 	return cx231xx_register_extension(&audio_ops);
779 }
780 
781 static void __exit cx231xx_alsa_unregister(void)
782 {
783 	cx231xx_unregister_extension(&audio_ops);
784 }
785 
786 MODULE_LICENSE("GPL");
787 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
788 MODULE_DESCRIPTION("Cx231xx Audio driver");
789 
790 module_init(cx231xx_alsa_register);
791 module_exit(cx231xx_alsa_unregister);
792