2 * Conexant Cx231xx audio extension
4 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 * Based on em28xx driver
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.
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.
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.
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>
43 module_param(debug
, int, 0644);
44 MODULE_PARM_DESC(debug
, "activates debug info");
46 #define dprintk(fmt, arg...) do { \
48 printk(KERN_INFO "cx231xx-audio %s: " fmt, \
52 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
54 static int cx231xx_isoc_audio_deinit(struct cx231xx
*dev
)
58 dprintk("Stopping isoc\n");
60 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
61 if (dev
->adev
.urb
[i
]) {
63 usb_kill_urb(dev
->adev
.urb
[i
]);
65 usb_unlink_urb(dev
->adev
.urb
[i
]);
67 usb_free_urb(dev
->adev
.urb
[i
]);
68 dev
->adev
.urb
[i
] = NULL
;
70 kfree(dev
->adev
.transfer_buffer
[i
]);
71 dev
->adev
.transfer_buffer
[i
] = NULL
;
78 static int cx231xx_bulk_audio_deinit(struct cx231xx
*dev
)
82 dprintk("Stopping bulk\n");
84 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
85 if (dev
->adev
.urb
[i
]) {
87 usb_kill_urb(dev
->adev
.urb
[i
]);
89 usb_unlink_urb(dev
->adev
.urb
[i
]);
91 usb_free_urb(dev
->adev
.urb
[i
]);
92 dev
->adev
.urb
[i
] = NULL
;
94 kfree(dev
->adev
.transfer_buffer
[i
]);
95 dev
->adev
.transfer_buffer
[i
] = NULL
;
102 static void cx231xx_audio_isocirq(struct urb
*urb
)
104 struct cx231xx
*dev
= urb
->context
;
107 int period_elapsed
= 0;
111 struct snd_pcm_substream
*substream
;
112 struct snd_pcm_runtime
*runtime
;
114 switch (urb
->status
) {
115 case 0: /* success */
116 case -ETIMEDOUT
: /* NAK */
118 case -ECONNRESET
: /* kill */
123 dprintk("urb completition error %d.\n", urb
->status
);
127 if (atomic_read(&dev
->stream_started
) == 0)
130 if (dev
->adev
.capture_pcm_substream
) {
131 substream
= dev
->adev
.capture_pcm_substream
;
132 runtime
= substream
->runtime
;
133 stride
= runtime
->frame_bits
>> 3;
135 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
136 int length
= urb
->iso_frame_desc
[i
].actual_length
/
138 cp
= (unsigned char *)urb
->transfer_buffer
+
139 urb
->iso_frame_desc
[i
].offset
;
144 oldptr
= dev
->adev
.hwptr_done_capture
;
145 if (oldptr
+ length
>= runtime
->buffer_size
) {
148 cnt
= runtime
->buffer_size
- oldptr
;
149 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
151 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
152 length
* stride
- cnt
* stride
);
154 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
158 snd_pcm_stream_lock(substream
);
160 dev
->adev
.hwptr_done_capture
+= length
;
161 if (dev
->adev
.hwptr_done_capture
>=
162 runtime
->buffer_size
)
163 dev
->adev
.hwptr_done_capture
-=
164 runtime
->buffer_size
;
166 dev
->adev
.capture_transfer_done
+= length
;
167 if (dev
->adev
.capture_transfer_done
>=
168 runtime
->period_size
) {
169 dev
->adev
.capture_transfer_done
-=
170 runtime
->period_size
;
173 snd_pcm_stream_unlock(substream
);
176 snd_pcm_period_elapsed(substream
);
180 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
182 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
188 static void cx231xx_audio_bulkirq(struct urb
*urb
)
190 struct cx231xx
*dev
= urb
->context
;
192 int period_elapsed
= 0;
196 struct snd_pcm_substream
*substream
;
197 struct snd_pcm_runtime
*runtime
;
199 switch (urb
->status
) {
200 case 0: /* success */
201 case -ETIMEDOUT
: /* NAK */
203 case -ECONNRESET
: /* kill */
208 dprintk("urb completition error %d.\n", urb
->status
);
212 if (atomic_read(&dev
->stream_started
) == 0)
215 if (dev
->adev
.capture_pcm_substream
) {
216 substream
= dev
->adev
.capture_pcm_substream
;
217 runtime
= substream
->runtime
;
218 stride
= runtime
->frame_bits
>> 3;
221 int length
= urb
->actual_length
/
223 cp
= (unsigned char *)urb
->transfer_buffer
;
225 oldptr
= dev
->adev
.hwptr_done_capture
;
226 if (oldptr
+ length
>= runtime
->buffer_size
) {
229 cnt
= runtime
->buffer_size
- oldptr
;
230 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
232 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
233 length
* stride
- cnt
* stride
);
235 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
239 snd_pcm_stream_lock(substream
);
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
;
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
;
254 snd_pcm_stream_unlock(substream
);
257 snd_pcm_period_elapsed(substream
);
261 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
263 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
269 static int cx231xx_init_audio_isoc(struct cx231xx
*dev
)
274 cx231xx_info("%s: Starting ISO AUDIO transfers\n", __func__
);
276 sb_size
= CX231XX_ISO_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
278 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
282 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
283 if (!dev
->adev
.transfer_buffer
[i
])
286 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
287 urb
= usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
289 cx231xx_errdev("usb_alloc_urb failed!\n");
290 for (j
= 0; j
< i
; j
++) {
291 usb_free_urb(dev
->adev
.urb
[j
]);
292 kfree(dev
->adev
.transfer_buffer
[j
]);
297 urb
->dev
= dev
->udev
;
299 urb
->pipe
= usb_rcvisocpipe(dev
->udev
,
300 dev
->adev
.end_point_addr
);
301 urb
->transfer_flags
= URB_ISO_ASAP
;
302 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
304 urb
->complete
= cx231xx_audio_isocirq
;
305 urb
->number_of_packets
= CX231XX_ISO_NUM_AUDIO_PACKETS
;
306 urb
->transfer_buffer_length
= sb_size
;
308 for (j
= k
= 0; j
< CX231XX_ISO_NUM_AUDIO_PACKETS
;
309 j
++, k
+= dev
->adev
.max_pkt_size
) {
310 urb
->iso_frame_desc
[j
].offset
= k
;
311 urb
->iso_frame_desc
[j
].length
= dev
->adev
.max_pkt_size
;
313 dev
->adev
.urb
[i
] = urb
;
316 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
317 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
319 cx231xx_isoc_audio_deinit(dev
);
327 static int cx231xx_init_audio_bulk(struct cx231xx
*dev
)
332 cx231xx_info("%s: Starting BULK AUDIO transfers\n", __func__
);
334 sb_size
= CX231XX_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
336 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
340 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
341 if (!dev
->adev
.transfer_buffer
[i
])
344 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
345 urb
= usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
347 cx231xx_errdev("usb_alloc_urb failed!\n");
348 for (j
= 0; j
< i
; j
++) {
349 usb_free_urb(dev
->adev
.urb
[j
]);
350 kfree(dev
->adev
.transfer_buffer
[j
]);
355 urb
->dev
= dev
->udev
;
357 urb
->pipe
= usb_rcvbulkpipe(dev
->udev
,
358 dev
->adev
.end_point_addr
);
359 urb
->transfer_flags
= 0;
360 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
361 urb
->complete
= cx231xx_audio_bulkirq
;
362 urb
->transfer_buffer_length
= sb_size
;
364 dev
->adev
.urb
[i
] = urb
;
368 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
369 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
371 cx231xx_bulk_audio_deinit(dev
);
379 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
382 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
384 dprintk("Allocating vbuffer\n");
385 if (runtime
->dma_area
) {
386 if (runtime
->dma_bytes
> size
)
389 vfree(runtime
->dma_area
);
391 runtime
->dma_area
= vmalloc(size
);
392 if (!runtime
->dma_area
)
395 runtime
->dma_bytes
= size
;
400 static struct snd_pcm_hardware snd_cx231xx_hw_capture
= {
401 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
402 SNDRV_PCM_INFO_MMAP
|
403 SNDRV_PCM_INFO_INTERLEAVED
|
404 SNDRV_PCM_INFO_MMAP_VALID
,
406 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
408 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
414 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
415 .period_bytes_min
= 64, /* 12544/2, */
416 .period_bytes_max
= 12544,
418 .periods_max
= 98, /* 12544, */
421 static int snd_cx231xx_capture_open(struct snd_pcm_substream
*substream
)
423 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
424 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
427 dprintk("opening device and trying to acquire exclusive lock\n");
430 cx231xx_errdev("BUG: cx231xx can't find device struct."
431 " Can't proceed with open\n");
435 /* Sets volume, mute, etc */
438 /* set alternate setting for audio interface */
439 /* 1 - 48000 samples per sec */
440 mutex_lock(&dev
->lock
);
442 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 1);
444 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
445 mutex_unlock(&dev
->lock
);
447 cx231xx_errdev("failed to set alternate setting !\n");
452 runtime
->hw
= snd_cx231xx_hw_capture
;
454 mutex_lock(&dev
->lock
);
455 /* inform hardware to start streaming */
456 ret
= cx231xx_capture_start(dev
, 1, Audio
);
459 mutex_unlock(&dev
->lock
);
461 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
462 dev
->adev
.capture_pcm_substream
= substream
;
463 runtime
->private_data
= dev
;
468 static int snd_cx231xx_pcm_close(struct snd_pcm_substream
*substream
)
471 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
473 dprintk("closing device\n");
475 /* inform hardware to stop streaming */
476 mutex_lock(&dev
->lock
);
477 ret
= cx231xx_capture_start(dev
, 0, Audio
);
479 /* set alternate setting for audio interface */
480 /* 1 - 48000 samples per sec */
481 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
483 cx231xx_errdev("failed to set alternate setting !\n");
485 mutex_unlock(&dev
->lock
);
491 mutex_unlock(&dev
->lock
);
493 if (dev
->adev
.users
== 0 && dev
->adev
.shutdown
== 1) {
494 dprintk("audio users: %d\n", dev
->adev
.users
);
495 dprintk("disabling audio stream!\n");
496 dev
->adev
.shutdown
= 0;
497 dprintk("released lock\n");
498 if (atomic_read(&dev
->stream_started
) > 0) {
499 atomic_set(&dev
->stream_started
, 0);
500 schedule_work(&dev
->wq_trigger
);
506 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream
*substream
,
507 struct snd_pcm_hw_params
*hw_params
)
509 unsigned int channels
, rate
, format
;
512 dprintk("Setting capture parameters\n");
514 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
515 params_buffer_bytes(hw_params
));
516 format
= params_format(hw_params
);
517 rate
= params_rate(hw_params
);
518 channels
= params_channels(hw_params
);
520 /* TODO: set up cx231xx audio chip to deliver the correct audio format,
521 current default is 48000hz multiplexed => 96000hz mono
522 which shouldn't matter since analogue TV only supports mono */
526 static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream
*substream
)
528 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
530 dprintk("Stop capture, if needed\n");
532 if (atomic_read(&dev
->stream_started
) > 0) {
533 atomic_set(&dev
->stream_started
, 0);
534 schedule_work(&dev
->wq_trigger
);
540 static int snd_cx231xx_prepare(struct snd_pcm_substream
*substream
)
542 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
544 dev
->adev
.hwptr_done_capture
= 0;
545 dev
->adev
.capture_transfer_done
= 0;
550 static void audio_trigger(struct work_struct
*work
)
552 struct cx231xx
*dev
= container_of(work
, struct cx231xx
, wq_trigger
);
554 if (atomic_read(&dev
->stream_started
)) {
555 dprintk("starting capture");
556 if (is_fw_load(dev
) == 0)
557 cx25840_call(dev
, core
, load_fw
);
559 cx231xx_init_audio_isoc(dev
);
561 cx231xx_init_audio_bulk(dev
);
563 dprintk("stopping capture");
564 cx231xx_isoc_audio_deinit(dev
);
568 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream
*substream
,
571 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
574 spin_lock(&dev
->adev
.slock
);
576 case SNDRV_PCM_TRIGGER_START
:
577 atomic_set(&dev
->stream_started
, 1);
579 case SNDRV_PCM_TRIGGER_STOP
:
580 atomic_set(&dev
->stream_started
, 0);
585 spin_unlock(&dev
->adev
.slock
);
587 schedule_work(&dev
->wq_trigger
);
592 static snd_pcm_uframes_t
snd_cx231xx_capture_pointer(struct snd_pcm_substream
597 snd_pcm_uframes_t hwptr_done
;
599 dev
= snd_pcm_substream_chip(substream
);
601 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
602 hwptr_done
= dev
->adev
.hwptr_done_capture
;
603 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
608 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
609 unsigned long offset
)
611 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
613 return vmalloc_to_page(pageptr
);
616 static struct snd_pcm_ops snd_cx231xx_pcm_capture
= {
617 .open
= snd_cx231xx_capture_open
,
618 .close
= snd_cx231xx_pcm_close
,
619 .ioctl
= snd_pcm_lib_ioctl
,
620 .hw_params
= snd_cx231xx_hw_capture_params
,
621 .hw_free
= snd_cx231xx_hw_capture_free
,
622 .prepare
= snd_cx231xx_prepare
,
623 .trigger
= snd_cx231xx_capture_trigger
,
624 .pointer
= snd_cx231xx_capture_pointer
,
625 .page
= snd_pcm_get_vmalloc_page
,
628 static int cx231xx_audio_init(struct cx231xx
*dev
)
630 struct cx231xx_audio
*adev
= &dev
->adev
;
632 struct snd_card
*card
;
635 struct usb_interface
*uif
;
636 int i
, isoc_pipe
= 0;
638 if (dev
->has_alsa_audio
!= 1) {
639 /* This device does not support the extension (in this case
640 the device is expecting the snd-usb-audio module or
641 doesn't have analog audio support at all) */
645 cx231xx_info("cx231xx-audio.c: probing for cx231xx "
646 "non standard usbaudio\n");
648 err
= snd_card_create(index
[devnr
], "Cx231xx Audio", THIS_MODULE
,
653 spin_lock_init(&adev
->slock
);
654 err
= snd_pcm_new(card
, "Cx231xx Audio", 0, 0, 1, &pcm
);
660 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
661 &snd_cx231xx_pcm_capture
);
663 pcm
->private_data
= dev
;
664 strcpy(pcm
->name
, "Conexant cx231xx Capture");
665 snd_card_set_dev(card
, &dev
->udev
->dev
);
666 strcpy(card
->driver
, "Cx231xx-Audio");
667 strcpy(card
->shortname
, "Cx231xx Audio");
668 strcpy(card
->longname
, "Conexant cx231xx Audio");
670 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
672 err
= snd_card_register(card
);
677 adev
->sndcard
= card
;
678 adev
->udev
= dev
->udev
;
680 /* compute alternate max packet sizes for Audio */
682 dev
->udev
->actconfig
->interface
[dev
->current_pcb_config
.
683 hs_config_info
[0].interface_info
.
686 adev
->end_point_addr
=
687 le16_to_cpu(uif
->altsetting
[0].endpoint
[isoc_pipe
].desc
.
690 adev
->num_alt
= uif
->num_altsetting
;
691 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
692 adev
->end_point_addr
, adev
->num_alt
);
693 adev
->alt_max_pkt_size
= kmalloc(32 * adev
->num_alt
, GFP_KERNEL
);
695 if (adev
->alt_max_pkt_size
== NULL
) {
696 cx231xx_errdev("out of memory!\n");
700 for (i
= 0; i
< adev
->num_alt
; i
++) {
702 le16_to_cpu(uif
->altsetting
[i
].endpoint
[isoc_pipe
].desc
.
704 adev
->alt_max_pkt_size
[i
] =
705 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
706 cx231xx_info("Alternate setting %i, max size= %i\n", i
,
707 adev
->alt_max_pkt_size
[i
]);
713 static int cx231xx_audio_fini(struct cx231xx
*dev
)
718 if (dev
->has_alsa_audio
!= 1) {
719 /* This device does not support the extension (in this case
720 the device is expecting the snd-usb-audio module or
721 doesn't have analog audio support at all) */
725 if (dev
->adev
.sndcard
) {
726 snd_card_free(dev
->adev
.sndcard
);
727 kfree(dev
->adev
.alt_max_pkt_size
);
728 dev
->adev
.sndcard
= NULL
;
734 static struct cx231xx_ops audio_ops
= {
736 .name
= "Cx231xx Audio Extension",
737 .init
= cx231xx_audio_init
,
738 .fini
= cx231xx_audio_fini
,
741 static int __init
cx231xx_alsa_register(void)
743 return cx231xx_register_extension(&audio_ops
);
746 static void __exit
cx231xx_alsa_unregister(void)
748 cx231xx_unregister_extension(&audio_ops
);
751 MODULE_LICENSE("GPL");
752 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
753 MODULE_DESCRIPTION("Cx231xx Audio driver");
755 module_init(cx231xx_alsa_register
);
756 module_exit(cx231xx_alsa_unregister
);