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