blob: cee64879c4f00e1bd4a160f543460b89fcb473b0 [file] [log] [blame]
Sri Deevie0d3baf2009-03-03 14:37:50 -03001/*
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#include "cx231xx-pcb-config.h"
42
43static int debug;
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug, "activates debug info");
46
47#define dprintk(fmt, arg...) do { \
48 if (debug) \
49 printk(KERN_INFO "cx231xx-audio %s: " fmt, \
50 __func__, ##arg); \
51 } while (0)
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54
55static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
56{
57 int i;
58
59 dprintk("Stopping isoc\n");
60
Sri Deevie0d3baf2009-03-03 14:37:50 -030061 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030062 if (dev->adev.urb[i]) {
63 if (!irqs_disabled())
64 usb_kill_urb(dev->adev.urb[i]);
65 else
66 usb_unlink_urb(dev->adev.urb[i]);
Sri Deevie0d3baf2009-03-03 14:37:50 -030067
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030068 usb_free_urb(dev->adev.urb[i]);
69 dev->adev.urb[i] = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -030070
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030071 kfree(dev->adev.transfer_buffer[i]);
72 dev->adev.transfer_buffer[i] = NULL;
Sri Deevie0d3baf2009-03-03 14:37:50 -030073
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030074 }
Sri Deevie0d3baf2009-03-03 14:37:50 -030075 }
76
77 return 0;
78}
79
80static void cx231xx_audio_isocirq(struct urb *urb)
81{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030082 struct cx231xx *dev = urb->context;
83 int i;
84 unsigned int oldptr;
85 int period_elapsed = 0;
86 int status;
87 unsigned char *cp;
88 unsigned int stride;
Sri Deevie0d3baf2009-03-03 14:37:50 -030089 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030090 struct snd_pcm_runtime *runtime;
Sri Deevie0d3baf2009-03-03 14:37:50 -030091
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -030092 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;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300103 }
104
105 if (dev->adev.capture_pcm_substream) {
106 substream = dev->adev.capture_pcm_substream;
107 runtime = substream->runtime;
108 stride = runtime->frame_bits >> 3;
109
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;
115
116 if (!length)
117 continue;
118
119 oldptr = dev->adev.hwptr_done_capture;
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
132 snd_pcm_stream_lock(substream);
133
134 dev->adev.hwptr_done_capture += length;
135 if (dev->adev.hwptr_done_capture >=
136 runtime->buffer_size)
137 dev->adev.hwptr_done_capture -=
138 runtime->buffer_size;
139
140 dev->adev.capture_transfer_done += length;
141 if (dev->adev.capture_transfer_done >=
142 runtime->period_size) {
143 dev->adev.capture_transfer_done -=
144 runtime->period_size;
145 period_elapsed = 1;
146 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300147 snd_pcm_stream_unlock(substream);
148 }
149 if (period_elapsed)
150 snd_pcm_period_elapsed(substream);
151 }
152 urb->status = 0;
153
154 status = usb_submit_urb(urb, GFP_ATOMIC);
155 if (status < 0) {
156 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300157 status);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300158 }
159 return;
160}
161
162static int cx231xx_init_audio_isoc(struct cx231xx *dev)
163{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300164 int i, errCode;
165 int sb_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300166
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300167 cx231xx_info("%s: Starting AUDIO transfers\n", __func__);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300168
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300169 sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300170
171 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
172 struct urb *urb;
173 int j, k;
174
175 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
176 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(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
181 if (!urb) {
182 cx231xx_errdev("usb_alloc_urb failed!\n");
183 for (j = 0; j < i; j++) {
184 usb_free_urb(dev->adev.urb[j]);
185 kfree(dev->adev.transfer_buffer[j]);
186 }
187 return -ENOMEM;
188 }
189
190 urb->dev = dev->udev;
191 urb->context = dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300192 urb->pipe =
193 usb_rcvisocpipe(dev->udev, dev->adev.end_point_addr);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300194 urb->transfer_flags = URB_ISO_ASAP;
195 urb->transfer_buffer = dev->adev.transfer_buffer[i];
196 urb->interval = 1;
197 urb->complete = cx231xx_audio_isocirq;
198 urb->number_of_packets = CX231XX_NUM_AUDIO_PACKETS;
199 urb->transfer_buffer_length = sb_size;
200
201 for (j = k = 0; j < CX231XX_NUM_AUDIO_PACKETS;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300202 j++, k += dev->adev.max_pkt_size) {
Sri Deevie0d3baf2009-03-03 14:37:50 -0300203 urb->iso_frame_desc[j].offset = k;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300204 urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300205 }
206 dev->adev.urb[i] = urb;
207 }
208
209 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
210 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
211 if (errCode < 0) {
212 cx231xx_isoc_audio_deinit(dev);
213 return errCode;
214 }
215 }
216
217 return errCode;
218}
219
220static int cx231xx_cmd(struct cx231xx *dev, int cmd, int arg)
221{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300222 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
223 "stop" : "start");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300224
225 switch (cmd) {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300226 case CX231XX_CAPTURE_STREAM_EN:
Sri Deevie0d3baf2009-03-03 14:37:50 -0300227 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
228 dev->adev.capture_stream = STREAM_ON;
229 cx231xx_init_audio_isoc(dev);
230 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
231 dev->adev.capture_stream = STREAM_OFF;
232 cx231xx_isoc_audio_deinit(dev);
233 } else {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300234 cx231xx_errdev("An underrun very likely occurred. "
235 "Ignoring it.\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300236 }
237 return 0;
238 default:
239 return -EINVAL;
240 }
241}
242
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
248 dprintk("Allocating vbuffer\n");
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_cx231xx_hw_capture = {
265 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300266 SNDRV_PCM_INFO_MMAP |
267 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300268
269 .formats = SNDRV_PCM_FMTBIT_S16_LE,
270
271 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
272
273 .rate_min = 48000,
274 .rate_max = 48000,
275 .channels_min = 2,
276 .channels_max = 2,
277 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300278 .period_bytes_min = 64, /* 12544/2, */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300279 .period_bytes_max = 12544,
280 .periods_min = 2,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300281 .periods_max = 98, /* 12544, */
Sri Deevie0d3baf2009-03-03 14:37:50 -0300282};
283
284static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
285{
286 struct cx231xx *dev = snd_pcm_substream_chip(substream);
287 struct snd_pcm_runtime *runtime = substream->runtime;
288 int ret = 0;
289
290 dprintk("opening device and trying to acquire exclusive lock\n");
291
292 if (!dev) {
293 cx231xx_errdev("BUG: cx231xx can't find device struct."
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300294 " Can't proceed with open\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300295 return -ENODEV;
296 }
297
298 /* Sets volume, mute, etc */
299 dev->mute = 0;
300
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300301 /* set alternate setting for audio interface */
302 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1); /* 1 - 48000 samples per sec */
303 if (ret < 0) {
304 cx231xx_errdev("failed to set alternate setting !\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300305
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300306 return ret;
307 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300308
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300309 /* inform hardware to start streaming */
310 ret = cx231xx_capture_start(dev, 1, Audio);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300311
312 runtime->hw = snd_cx231xx_hw_capture;
313
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300314 mutex_lock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300315 dev->adev.users++;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300316 mutex_unlock(&dev->lock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300317
318 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
319 dev->adev.capture_pcm_substream = substream;
320 runtime->private_data = dev;
321
322 return 0;
323}
324
325static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
326{
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300327 int ret;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300328 struct cx231xx *dev = snd_pcm_substream_chip(substream);
329
Sri Deevie0d3baf2009-03-03 14:37:50 -0300330 dprintk("closing device\n");
331
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300332 /* set alternate setting for audio interface */
333 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0); /* 1 - 48000 samples per sec */
334 if (ret < 0) {
335 cx231xx_errdev("failed to set alternate setting !\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300336
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300337 return ret;
338 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300339
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300340 /* inform hardware to start streaming */
341 ret = cx231xx_capture_start(dev, 0, Audio);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300342
343 dev->mute = 1;
344 mutex_lock(&dev->lock);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300345 dev->adev.users--;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300346 mutex_unlock(&dev->lock);
347
348 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
349 dprintk("audio users: %d\n", dev->adev.users);
350 dprintk("disabling audio stream!\n");
351 dev->adev.shutdown = 0;
352 dprintk("released lock\n");
353 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, 0);
354 }
355 return 0;
356}
357
358static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300359 struct snd_pcm_hw_params *hw_params)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300360{
361 unsigned int channels, rate, format;
362 int ret;
363
364 dprintk("Setting capture parameters\n");
365
366 ret = snd_pcm_alloc_vmalloc_buffer(substream,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300367 params_buffer_bytes(hw_params));
Sri Deevie0d3baf2009-03-03 14:37:50 -0300368 format = params_format(hw_params);
369 rate = params_rate(hw_params);
370 channels = params_channels(hw_params);
371
372 /* TODO: set up cx231xx audio chip to deliver the correct audio format,
373 current default is 48000hz multiplexed => 96000hz mono
374 which shouldn't matter since analogue TV only supports mono */
375 return 0;
376}
377
378static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
379{
380 struct cx231xx *dev = snd_pcm_substream_chip(substream);
381
382 dprintk("Stop capture, if needed\n");
383
384 if (dev->adev.capture_stream == STREAM_ON)
385 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
386
387 return 0;
388}
389
390static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
391{
392 return 0;
393}
394
395static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300396 int cmd)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300397{
398 struct cx231xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300399 int retval;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300400
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300401 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START) ?
402 "start" : "stop");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300403
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300404 spin_lock(&dev->adev.slock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300405 switch (cmd) {
406 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300407 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN,
408 CX231XX_START_AUDIO);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300409 retval = 0;
410 break;
411 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300412 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300413 retval = 0;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300414 break;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300415 default:
416 retval = -EINVAL;
417 }
418
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300419 spin_unlock(&dev->adev.slock);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300420 return retval;
421}
422
423static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300424 *substream)
Sri Deevie0d3baf2009-03-03 14:37:50 -0300425{
426 struct cx231xx *dev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300427 unsigned long flags;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300428 snd_pcm_uframes_t hwptr_done;
429
430 dev = snd_pcm_substream_chip(substream);
431
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300432 spin_lock_irqsave(&dev->adev.slock, flags);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300433 hwptr_done = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300434 spin_unlock_irqrestore(&dev->adev.slock, flags);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300435
436 return hwptr_done;
437}
438
439static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
440 unsigned long offset)
441{
442 void *pageptr = subs->runtime->dma_area + offset;
443
444 return vmalloc_to_page(pageptr);
445}
446
447static struct snd_pcm_ops snd_cx231xx_pcm_capture = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300448 .open = snd_cx231xx_capture_open,
449 .close = snd_cx231xx_pcm_close,
450 .ioctl = snd_pcm_lib_ioctl,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300451 .hw_params = snd_cx231xx_hw_capture_params,
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300452 .hw_free = snd_cx231xx_hw_capture_free,
453 .prepare = snd_cx231xx_prepare,
454 .trigger = snd_cx231xx_capture_trigger,
455 .pointer = snd_cx231xx_capture_pointer,
456 .page = snd_pcm_get_vmalloc_page,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300457};
458
459static int cx231xx_audio_init(struct cx231xx *dev)
460{
461 struct cx231xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300462 struct snd_pcm *pcm;
463 struct snd_card *card;
464 static int devnr;
465 int err;
466 struct usb_interface *uif;
467 int i, isoc_pipe = 0;
Sri Deevie0d3baf2009-03-03 14:37:50 -0300468
469 if (dev->has_alsa_audio != 1) {
470 /* This device does not support the extension (in this case
471 the device is expecting the snd-usb-audio module or
472 doesn't have analog audio support at all) */
473 return 0;
474 }
475
476 cx231xx_info("cx231xx-audio.c: probing for cx231xx "
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300477 "non standard usbaudio\n");
Sri Deevie0d3baf2009-03-03 14:37:50 -0300478
479 card = snd_card_new(index[devnr], "Cx231xx Audio", THIS_MODULE, 0);
480 if (card == NULL) {
481 return -ENOMEM;
482 }
483
484 spin_lock_init(&adev->slock);
485 err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
486 if (err < 0) {
487 snd_card_free(card);
488 return err;
489 }
490
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300491 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
492 &snd_cx231xx_pcm_capture);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300493 pcm->info_flags = 0;
494 pcm->private_data = dev;
495 strcpy(pcm->name, "Conexant cx231xx Capture");
496 strcpy(card->driver, "Conexant cx231xx Audio");
497 strcpy(card->shortname, "Cx231xx Audio");
498 strcpy(card->longname, "Conexant cx231xx Audio");
499
500 err = snd_card_register(card);
501 if (err < 0) {
502 snd_card_free(card);
503 return err;
504 }
505 adev->sndcard = card;
506 adev->udev = dev->udev;
507
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300508 /* compute alternate max packet sizes for Audio */
509 uif =
510 dev->udev->actconfig->interface[dev->current_pcb_config.
511 hs_config_info[0].interface_info.
512 audio_index + 1];
Sri Deevie0d3baf2009-03-03 14:37:50 -0300513
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300514 adev->end_point_addr =
515 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
516 bEndpointAddress);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300517
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300518 adev->num_alt = uif->num_altsetting;
519 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
520 adev->end_point_addr, adev->num_alt);
521 adev->alt_max_pkt_size = kmalloc(32 * adev->num_alt, GFP_KERNEL);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300522
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300523 if (adev->alt_max_pkt_size == NULL) {
524 cx231xx_errdev("out of memory!\n");
525 return -ENOMEM;
526 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300527
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300528 for (i = 0; i < adev->num_alt; i++) {
529 u16 tmp =
530 le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
531 wMaxPacketSize);
532 adev->alt_max_pkt_size[i] =
533 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
534 cx231xx_info("Alternate setting %i, max size= %i\n", i,
535 adev->alt_max_pkt_size[i]);
536 }
Sri Deevie0d3baf2009-03-03 14:37:50 -0300537
538 return 0;
539}
540
541static int cx231xx_audio_fini(struct cx231xx *dev)
542{
543 if (dev == NULL)
544 return 0;
545
546 if (dev->has_alsa_audio != 1) {
547 /* This device does not support the extension (in this case
548 the device is expecting the snd-usb-audio module or
549 doesn't have analog audio support at all) */
550 return 0;
551 }
552
553 if (dev->adev.sndcard) {
554 snd_card_free(dev->adev.sndcard);
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300555 kfree(dev->adev.alt_max_pkt_size);
Sri Deevie0d3baf2009-03-03 14:37:50 -0300556 dev->adev.sndcard = NULL;
557 }
558
559 return 0;
560}
561
562static struct cx231xx_ops audio_ops = {
Mauro Carvalho Chehab84b5dbf2009-03-03 06:14:34 -0300563 .id = CX231XX_AUDIO,
Sri Deevie0d3baf2009-03-03 14:37:50 -0300564 .name = "Cx231xx Audio Extension",
565 .init = cx231xx_audio_init,
566 .fini = cx231xx_audio_fini,
567};
568
569static int __init cx231xx_alsa_register(void)
570{
571 return cx231xx_register_extension(&audio_ops);
572}
573
574static void __exit cx231xx_alsa_unregister(void)
575{
576 cx231xx_unregister_extension(&audio_ops);
577}
578
579MODULE_LICENSE("GPL");
580MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
581MODULE_DESCRIPTION("Cx231xx Audio driver");
582
583module_init(cx231xx_alsa_register);
584module_exit(cx231xx_alsa_unregister);