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  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include <linux/kernel.h>
24 #include <linux/usb.h>
25 #include <linux/init.h>
26 #include <linux/sound.h>
27 #include <linux/spinlock.h>
28 #include <linux/soundcard.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/proc_fs.h>
32 #include <linux/module.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/info.h>
37 #include <sound/initval.h>
38 #include <sound/control.h>
39 #include <media/v4l2-common.h>
40 #include "cx231xx.h"
41 
42 static int debug;
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug, "activates debug info");
45 
46 #define dprintk(fmt, arg...) do {					\
47 		if (debug)						\
48 			printk(KERN_INFO "cx231xx-audio %s: " fmt,	\
49 				__func__, ##arg); 			\
50 	} while (0)
51 
52 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
53 
54 static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
55 {
56 	int i;
57 
58 	dprintk("Stopping isoc\n");
59 
60 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
61 		if (dev->adev.urb[i]) {
62 			if (!irqs_disabled())
63 				usb_kill_urb(dev->adev.urb[i]);
64 			else
65 				usb_unlink_urb(dev->adev.urb[i]);
66 
67 			usb_free_urb(dev->adev.urb[i]);
68 			dev->adev.urb[i] = NULL;
69 
70 			kfree(dev->adev.transfer_buffer[i]);
71 			dev->adev.transfer_buffer[i] = NULL;
72 		}
73 	}
74 
75 	return 0;
76 }
77 
78 static int cx231xx_bulk_audio_deinit(struct cx231xx *dev)
79 {
80 	int i;
81 
82 	dprintk("Stopping bulk\n");
83 
84 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
85 		if (dev->adev.urb[i]) {
86 			if (!irqs_disabled())
87 				usb_kill_urb(dev->adev.urb[i]);
88 			else
89 				usb_unlink_urb(dev->adev.urb[i]);
90 
91 			usb_free_urb(dev->adev.urb[i]);
92 			dev->adev.urb[i] = NULL;
93 
94 			kfree(dev->adev.transfer_buffer[i]);
95 			dev->adev.transfer_buffer[i] = NULL;
96 		}
97 	}
98 
99 	return 0;
100 }
101 
102 static void cx231xx_audio_isocirq(struct urb *urb)
103 {
104 	struct cx231xx *dev = urb->context;
105 	int i;
106 	unsigned int oldptr;
107 	int period_elapsed = 0;
108 	int status;
109 	unsigned char *cp;
110 	unsigned int stride;
111 	struct snd_pcm_substream *substream;
112 	struct snd_pcm_runtime *runtime;
113 
114 	if (dev->state & DEV_DISCONNECTED)
115 		return;
116 
117 	switch (urb->status) {
118 	case 0:		/* success */
119 	case -ETIMEDOUT:	/* NAK */
120 		break;
121 	case -ECONNRESET:	/* kill */
122 	case -ENOENT:
123 	case -ESHUTDOWN:
124 		return;
125 	default:		/* error */
126 		dprintk("urb completition error %d.\n", urb->status);
127 		break;
128 	}
129 
130 	if (atomic_read(&dev->stream_started) == 0)
131 		return;
132 
133 	if (dev->adev.capture_pcm_substream) {
134 		substream = dev->adev.capture_pcm_substream;
135 		runtime = substream->runtime;
136 		stride = runtime->frame_bits >> 3;
137 
138 		for (i = 0; i < urb->number_of_packets; i++) {
139 			int length = urb->iso_frame_desc[i].actual_length /
140 				     stride;
141 			cp = (unsigned char *)urb->transfer_buffer +
142 					      urb->iso_frame_desc[i].offset;
143 
144 			if (!length)
145 				continue;
146 
147 			oldptr = dev->adev.hwptr_done_capture;
148 			if (oldptr + length >= runtime->buffer_size) {
149 				unsigned int cnt;
150 
151 				cnt = runtime->buffer_size - oldptr;
152 				memcpy(runtime->dma_area + oldptr * stride, cp,
153 				       cnt * stride);
154 				memcpy(runtime->dma_area, cp + cnt * stride,
155 				       length * stride - cnt * stride);
156 			} else {
157 				memcpy(runtime->dma_area + oldptr * stride, cp,
158 				       length * stride);
159 			}
160 
161 			snd_pcm_stream_lock(substream);
162 
163 			dev->adev.hwptr_done_capture += length;
164 			if (dev->adev.hwptr_done_capture >=
165 						runtime->buffer_size)
166 				dev->adev.hwptr_done_capture -=
167 						runtime->buffer_size;
168 
169 			dev->adev.capture_transfer_done += length;
170 			if (dev->adev.capture_transfer_done >=
171 				runtime->period_size) {
172 				dev->adev.capture_transfer_done -=
173 						runtime->period_size;
174 				period_elapsed = 1;
175 			}
176 			snd_pcm_stream_unlock(substream);
177 		}
178 		if (period_elapsed)
179 			snd_pcm_period_elapsed(substream);
180 	}
181 	urb->status = 0;
182 
183 	status = usb_submit_urb(urb, GFP_ATOMIC);
184 	if (status < 0) {
185 		cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
186 			       status);
187 	}
188 	return;
189 }
190 
191 static void cx231xx_audio_bulkirq(struct urb *urb)
192 {
193 	struct cx231xx *dev = urb->context;
194 	unsigned int oldptr;
195 	int period_elapsed = 0;
196 	int status;
197 	unsigned char *cp;
198 	unsigned int stride;
199 	struct snd_pcm_substream *substream;
200 	struct snd_pcm_runtime *runtime;
201 
202 	if (dev->state & DEV_DISCONNECTED)
203 		return;
204 
205 	switch (urb->status) {
206 	case 0:		/* success */
207 	case -ETIMEDOUT:	/* NAK */
208 		break;
209 	case -ECONNRESET:	/* kill */
210 	case -ENOENT:
211 	case -ESHUTDOWN:
212 		return;
213 	default:		/* error */
214 		dprintk("urb completition error %d.\n", urb->status);
215 		break;
216 	}
217 
218 	if (atomic_read(&dev->stream_started) == 0)
219 		return;
220 
221 	if (dev->adev.capture_pcm_substream) {
222 		substream = dev->adev.capture_pcm_substream;
223 		runtime = substream->runtime;
224 		stride = runtime->frame_bits >> 3;
225 
226 		if (1) {
227 			int length = urb->actual_length /
228 				     stride;
229 			cp = (unsigned char *)urb->transfer_buffer;
230 
231 			oldptr = dev->adev.hwptr_done_capture;
232 			if (oldptr + length >= runtime->buffer_size) {
233 				unsigned int cnt;
234 
235 				cnt = runtime->buffer_size - oldptr;
236 				memcpy(runtime->dma_area + oldptr * stride, cp,
237 				       cnt * stride);
238 				memcpy(runtime->dma_area, cp + cnt * stride,
239 				       length * stride - cnt * stride);
240 			} else {
241 				memcpy(runtime->dma_area + oldptr * stride, cp,
242 				       length * stride);
243 			}
244 
245 			snd_pcm_stream_lock(substream);
246 
247 			dev->adev.hwptr_done_capture += length;
248 			if (dev->adev.hwptr_done_capture >=
249 						runtime->buffer_size)
250 				dev->adev.hwptr_done_capture -=
251 						runtime->buffer_size;
252 
253 			dev->adev.capture_transfer_done += length;
254 			if (dev->adev.capture_transfer_done >=
255 				runtime->period_size) {
256 				dev->adev.capture_transfer_done -=
257 						runtime->period_size;
258 				period_elapsed = 1;
259 			}
260 			snd_pcm_stream_unlock(substream);
261 		}
262 		if (period_elapsed)
263 			snd_pcm_period_elapsed(substream);
264 	}
265 	urb->status = 0;
266 
267 	status = usb_submit_urb(urb, GFP_ATOMIC);
268 	if (status < 0) {
269 		cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
270 			       status);
271 	}
272 	return;
273 }
274 
275 static int cx231xx_init_audio_isoc(struct cx231xx *dev)
276 {
277 	int i, errCode;
278 	int sb_size;
279 
280 	cx231xx_info("%s: Starting ISO AUDIO transfers\n", __func__);
281 
282 	if (dev->state & DEV_DISCONNECTED)
283 		return -ENODEV;
284 
285 	sb_size = CX231XX_ISO_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
286 
287 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
288 		struct urb *urb;
289 		int j, k;
290 
291 		dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
292 		if (!dev->adev.transfer_buffer[i])
293 			return -ENOMEM;
294 
295 		memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
296 		urb = usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS, GFP_ATOMIC);
297 		if (!urb) {
298 			cx231xx_errdev("usb_alloc_urb failed!\n");
299 			for (j = 0; j < i; j++) {
300 				usb_free_urb(dev->adev.urb[j]);
301 				kfree(dev->adev.transfer_buffer[j]);
302 			}
303 			return -ENOMEM;
304 		}
305 
306 		urb->dev = dev->udev;
307 		urb->context = dev;
308 		urb->pipe = usb_rcvisocpipe(dev->udev,
309 						dev->adev.end_point_addr);
310 		urb->transfer_flags = URB_ISO_ASAP;
311 		urb->transfer_buffer = dev->adev.transfer_buffer[i];
312 		urb->interval = 1;
313 		urb->complete = cx231xx_audio_isocirq;
314 		urb->number_of_packets = CX231XX_ISO_NUM_AUDIO_PACKETS;
315 		urb->transfer_buffer_length = sb_size;
316 
317 		for (j = k = 0; j < CX231XX_ISO_NUM_AUDIO_PACKETS;
318 			j++, k += dev->adev.max_pkt_size) {
319 			urb->iso_frame_desc[j].offset = k;
320 			urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
321 		}
322 		dev->adev.urb[i] = urb;
323 	}
324 
325 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
326 		errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
327 		if (errCode < 0) {
328 			cx231xx_isoc_audio_deinit(dev);
329 			return errCode;
330 		}
331 	}
332 
333 	return errCode;
334 }
335 
336 static int cx231xx_init_audio_bulk(struct cx231xx *dev)
337 {
338 	int i, errCode;
339 	int sb_size;
340 
341 	cx231xx_info("%s: Starting BULK AUDIO transfers\n", __func__);
342 
343 	if (dev->state & DEV_DISCONNECTED)
344 		return -ENODEV;
345 
346 	sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
347 
348 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
349 		struct urb *urb;
350 		int j;
351 
352 		dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
353 		if (!dev->adev.transfer_buffer[i])
354 			return -ENOMEM;
355 
356 		memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
357 		urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
358 		if (!urb) {
359 			cx231xx_errdev("usb_alloc_urb failed!\n");
360 			for (j = 0; j < i; j++) {
361 				usb_free_urb(dev->adev.urb[j]);
362 				kfree(dev->adev.transfer_buffer[j]);
363 			}
364 			return -ENOMEM;
365 		}
366 
367 		urb->dev = dev->udev;
368 		urb->context = dev;
369 		urb->pipe = usb_rcvbulkpipe(dev->udev,
370 						dev->adev.end_point_addr);
371 		urb->transfer_flags = 0;
372 		urb->transfer_buffer = dev->adev.transfer_buffer[i];
373 		urb->complete = cx231xx_audio_bulkirq;
374 		urb->transfer_buffer_length = sb_size;
375 
376 		dev->adev.urb[i] = urb;
377 
378 	}
379 
380 	for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
381 		errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
382 		if (errCode < 0) {
383 			cx231xx_bulk_audio_deinit(dev);
384 			return errCode;
385 		}
386 	}
387 
388 	return errCode;
389 }
390 
391 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
392 					size_t size)
393 {
394 	struct snd_pcm_runtime *runtime = subs->runtime;
395 
396 	dprintk("Allocating vbuffer\n");
397 	if (runtime->dma_area) {
398 		if (runtime->dma_bytes > size)
399 			return 0;
400 
401 		vfree(runtime->dma_area);
402 	}
403 	runtime->dma_area = vmalloc(size);
404 	if (!runtime->dma_area)
405 		return -ENOMEM;
406 
407 	runtime->dma_bytes = size;
408 
409 	return 0;
410 }
411 
412 static struct snd_pcm_hardware snd_cx231xx_hw_capture = {
413 	.info = SNDRV_PCM_INFO_BLOCK_TRANSFER 	|
414 	    SNDRV_PCM_INFO_MMAP 		|
415 	    SNDRV_PCM_INFO_INTERLEAVED 		|
416 	    SNDRV_PCM_INFO_MMAP_VALID,
417 
418 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
419 
420 	.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
421 
422 	.rate_min = 48000,
423 	.rate_max = 48000,
424 	.channels_min = 2,
425 	.channels_max = 2,
426 	.buffer_bytes_max = 62720 * 8,	/* just about the value in usbaudio.c */
427 	.period_bytes_min = 64,		/* 12544/2, */
428 	.period_bytes_max = 12544,
429 	.periods_min = 2,
430 	.periods_max = 98,		/* 12544, */
431 };
432 
433 static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
434 {
435 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
436 	struct snd_pcm_runtime *runtime = substream->runtime;
437 	int ret = 0;
438 
439 	dprintk("opening device and trying to acquire exclusive lock\n");
440 
441 	if (!dev) {
442 		cx231xx_errdev("BUG: cx231xx can't find device struct."
443 			       " Can't proceed with open\n");
444 		return -ENODEV;
445 	}
446 
447 	if (dev->state & DEV_DISCONNECTED) {
448 		cx231xx_errdev("Can't open. the device was removed.\n");
449 		return -ENODEV;
450 	}
451 
452 	/* set alternate setting for audio interface */
453 	/* 1 - 48000 samples per sec */
454 	mutex_lock(&dev->lock);
455 	if (dev->USE_ISO)
456 		ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1);
457 	else
458 		ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
459 	mutex_unlock(&dev->lock);
460 	if (ret < 0) {
461 		cx231xx_errdev("failed to set alternate setting !\n");
462 
463 		return ret;
464 	}
465 
466 	runtime->hw = snd_cx231xx_hw_capture;
467 
468 	mutex_lock(&dev->lock);
469 	/* inform hardware to start streaming */
470 	ret = cx231xx_capture_start(dev, 1, Audio);
471 
472 	dev->adev.users++;
473 	mutex_unlock(&dev->lock);
474 
475 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
476 	dev->adev.capture_pcm_substream = substream;
477 	runtime->private_data = dev;
478 
479 	return 0;
480 }
481 
482 static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
483 {
484 	int ret;
485 	struct cx231xx *dev = snd_pcm_substream_chip(substream);
486 
487 	dprintk("closing device\n");
488 
489 	/* inform hardware to stop streaming */
490 	mutex_lock(&dev->lock);
491 	ret = cx231xx_capture_start(dev, 0, Audio);
492 
493 	/* set alternate setting for audio interface */
494 	/* 1 - 48000 samples per sec */
495 	ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
496 	if (ret < 0) {
497 		cx231xx_errdev("failed to set alternate setting !\n");
498 
499 		mutex_unlock(&dev->lock);
500 		return ret;
501 	}
502 
503 	dev->adev.users--;
504 	mutex_unlock(&dev->lock);
505 
506 	if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
507 		dprintk("audio users: %d\n", dev->adev.users);
508 		dprintk("disabling audio stream!\n");
509 		dev->adev.shutdown = 0;
510 		dprintk("released lock\n");
511 		if (atomic_read(&dev->stream_started) > 0) {
512 			atomic_set(&dev->stream_started, 0);
513 			schedule_work(&dev->wq_trigger);
514 		}
515 	}
516 	return 0;
517 }
518 
519 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
520 					 struct snd_pcm_hw_params *hw_params)
521 {
522 	int ret;
523 
524 	dprintk("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 	dprintk("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 		dprintk("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 		dprintk("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 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 	cx231xx_info("cx231xx-audio.c: probing for cx231xx "
666 		     "non standard usbaudio\n");
667 
668 	err = snd_card_create(index[devnr], "Cx231xx Audio", THIS_MODULE,
669 			      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 		snd_card_free(card);
677 		return err;
678 	}
679 
680 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
681 			&snd_cx231xx_pcm_capture);
682 	pcm->info_flags = 0;
683 	pcm->private_data = dev;
684 	strcpy(pcm->name, "Conexant cx231xx Capture");
685 	snd_card_set_dev(card, &dev->udev->dev);
686 	strcpy(card->driver, "Cx231xx-Audio");
687 	strcpy(card->shortname, "Cx231xx Audio");
688 	strcpy(card->longname, "Conexant cx231xx Audio");
689 
690 	INIT_WORK(&dev->wq_trigger, audio_trigger);
691 
692 	err = snd_card_register(card);
693 	if (err < 0) {
694 		snd_card_free(card);
695 		return err;
696 	}
697 	adev->sndcard = card;
698 	adev->udev = dev->udev;
699 
700 	/* compute alternate max packet sizes for Audio */
701 	uif =
702 	    dev->udev->actconfig->interface[dev->current_pcb_config.
703 					    hs_config_info[0].interface_info.
704 					    audio_index + 1];
705 
706 	adev->end_point_addr =
707 	    uif->altsetting[0].endpoint[isoc_pipe].desc.
708 			bEndpointAddress;
709 
710 	adev->num_alt = uif->num_altsetting;
711 	cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
712 		     adev->end_point_addr, adev->num_alt);
713 	adev->alt_max_pkt_size = kmalloc(32 * adev->num_alt, GFP_KERNEL);
714 
715 	if (adev->alt_max_pkt_size == NULL) {
716 		cx231xx_errdev("out of memory!\n");
717 		return -ENOMEM;
718 	}
719 
720 	for (i = 0; i < adev->num_alt; i++) {
721 		u16 tmp =
722 		    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
723 				wMaxPacketSize);
724 		adev->alt_max_pkt_size[i] =
725 		    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
726 		cx231xx_info("Alternate setting %i, max size= %i\n", i,
727 			     adev->alt_max_pkt_size[i]);
728 	}
729 
730 	return 0;
731 }
732 
733 static int cx231xx_audio_fini(struct cx231xx *dev)
734 {
735 	if (dev == NULL)
736 		return 0;
737 
738 	if (dev->has_alsa_audio != 1) {
739 		/* This device does not support the extension (in this case
740 		   the device is expecting the snd-usb-audio module or
741 		   doesn't have analog audio support at all) */
742 		return 0;
743 	}
744 
745 	if (dev->adev.sndcard) {
746 		snd_card_free(dev->adev.sndcard);
747 		kfree(dev->adev.alt_max_pkt_size);
748 		dev->adev.sndcard = NULL;
749 	}
750 
751 	return 0;
752 }
753 
754 static struct cx231xx_ops audio_ops = {
755 	.id = CX231XX_AUDIO,
756 	.name = "Cx231xx Audio Extension",
757 	.init = cx231xx_audio_init,
758 	.fini = cx231xx_audio_fini,
759 };
760 
761 static int __init cx231xx_alsa_register(void)
762 {
763 	return cx231xx_register_extension(&audio_ops);
764 }
765 
766 static void __exit cx231xx_alsa_unregister(void)
767 {
768 	cx231xx_unregister_extension(&audio_ops);
769 }
770 
771 MODULE_LICENSE("GPL");
772 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
773 MODULE_DESCRIPTION("Cx231xx Audio driver");
774 
775 module_init(cx231xx_alsa_register);
776 module_exit(cx231xx_alsa_unregister);
777