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 if (dev
->state
& DEV_DISCONNECTED
)
117 switch (urb
->status
) {
118 case 0: /* success */
119 case -ETIMEDOUT
: /* NAK */
121 case -ECONNRESET
: /* kill */
126 dprintk("urb completition error %d.\n", urb
->status
);
130 if (atomic_read(&dev
->stream_started
) == 0)
133 if (dev
->adev
.capture_pcm_substream
) {
134 substream
= dev
->adev
.capture_pcm_substream
;
135 runtime
= substream
->runtime
;
136 stride
= runtime
->frame_bits
>> 3;
138 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
139 int length
= urb
->iso_frame_desc
[i
].actual_length
/
141 cp
= (unsigned char *)urb
->transfer_buffer
+
142 urb
->iso_frame_desc
[i
].offset
;
147 oldptr
= dev
->adev
.hwptr_done_capture
;
148 if (oldptr
+ length
>= runtime
->buffer_size
) {
151 cnt
= runtime
->buffer_size
- oldptr
;
152 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
154 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
155 length
* stride
- cnt
* stride
);
157 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
161 snd_pcm_stream_lock(substream
);
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
;
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
;
176 snd_pcm_stream_unlock(substream
);
179 snd_pcm_period_elapsed(substream
);
183 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
185 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
191 static void cx231xx_audio_bulkirq(struct urb
*urb
)
193 struct cx231xx
*dev
= urb
->context
;
195 int period_elapsed
= 0;
199 struct snd_pcm_substream
*substream
;
200 struct snd_pcm_runtime
*runtime
;
202 if (dev
->state
& DEV_DISCONNECTED
)
205 switch (urb
->status
) {
206 case 0: /* success */
207 case -ETIMEDOUT
: /* NAK */
209 case -ECONNRESET
: /* kill */
214 dprintk("urb completition error %d.\n", urb
->status
);
218 if (atomic_read(&dev
->stream_started
) == 0)
221 if (dev
->adev
.capture_pcm_substream
) {
222 substream
= dev
->adev
.capture_pcm_substream
;
223 runtime
= substream
->runtime
;
224 stride
= runtime
->frame_bits
>> 3;
227 int length
= urb
->actual_length
/
229 cp
= (unsigned char *)urb
->transfer_buffer
;
231 oldptr
= dev
->adev
.hwptr_done_capture
;
232 if (oldptr
+ length
>= runtime
->buffer_size
) {
235 cnt
= runtime
->buffer_size
- oldptr
;
236 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
238 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
239 length
* stride
- cnt
* stride
);
241 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
245 snd_pcm_stream_lock(substream
);
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
;
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
;
260 snd_pcm_stream_unlock(substream
);
263 snd_pcm_period_elapsed(substream
);
267 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
269 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
275 static int cx231xx_init_audio_isoc(struct cx231xx
*dev
)
280 cx231xx_info("%s: Starting ISO AUDIO transfers\n", __func__
);
282 if (dev
->state
& DEV_DISCONNECTED
)
285 sb_size
= CX231XX_ISO_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
287 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
291 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
292 if (!dev
->adev
.transfer_buffer
[i
])
295 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
296 urb
= usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
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
]);
306 urb
->dev
= dev
->udev
;
308 urb
->pipe
= usb_rcvisocpipe(dev
->udev
,
309 dev
->adev
.end_point_addr
);
310 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
311 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
313 urb
->complete
= cx231xx_audio_isocirq
;
314 urb
->number_of_packets
= CX231XX_ISO_NUM_AUDIO_PACKETS
;
315 urb
->transfer_buffer_length
= sb_size
;
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
;
322 dev
->adev
.urb
[i
] = urb
;
325 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
326 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
328 cx231xx_isoc_audio_deinit(dev
);
336 static int cx231xx_init_audio_bulk(struct cx231xx
*dev
)
341 cx231xx_info("%s: Starting BULK AUDIO transfers\n", __func__
);
343 if (dev
->state
& DEV_DISCONNECTED
)
346 sb_size
= CX231XX_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
348 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
352 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
353 if (!dev
->adev
.transfer_buffer
[i
])
356 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
357 urb
= usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
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
]);
367 urb
->dev
= dev
->udev
;
369 urb
->pipe
= usb_rcvbulkpipe(dev
->udev
,
370 dev
->adev
.end_point_addr
);
371 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
372 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
373 urb
->complete
= cx231xx_audio_bulkirq
;
374 urb
->transfer_buffer_length
= sb_size
;
376 dev
->adev
.urb
[i
] = urb
;
380 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
381 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
383 cx231xx_bulk_audio_deinit(dev
);
391 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
394 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
396 dprintk("Allocating vbuffer\n");
397 if (runtime
->dma_area
) {
398 if (runtime
->dma_bytes
> size
)
401 vfree(runtime
->dma_area
);
403 runtime
->dma_area
= vmalloc(size
);
404 if (!runtime
->dma_area
)
407 runtime
->dma_bytes
= size
;
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
,
418 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
420 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
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,
430 .periods_max
= 98, /* 12544, */
433 static int snd_cx231xx_capture_open(struct snd_pcm_substream
*substream
)
435 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
436 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
439 dprintk("opening device and trying to acquire exclusive lock\n");
442 cx231xx_errdev("BUG: cx231xx can't find device struct."
443 " Can't proceed with open\n");
447 if (dev
->state
& DEV_DISCONNECTED
) {
448 cx231xx_errdev("Can't open. the device was removed.\n");
452 /* Sets volume, mute, etc */
455 /* set alternate setting for audio interface */
456 /* 1 - 48000 samples per sec */
457 mutex_lock(&dev
->lock
);
459 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 1);
461 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
462 mutex_unlock(&dev
->lock
);
464 cx231xx_errdev("failed to set alternate setting !\n");
469 runtime
->hw
= snd_cx231xx_hw_capture
;
471 mutex_lock(&dev
->lock
);
472 /* inform hardware to start streaming */
473 ret
= cx231xx_capture_start(dev
, 1, Audio
);
476 mutex_unlock(&dev
->lock
);
478 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
479 dev
->adev
.capture_pcm_substream
= substream
;
480 runtime
->private_data
= dev
;
485 static int snd_cx231xx_pcm_close(struct snd_pcm_substream
*substream
)
488 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
490 dprintk("closing device\n");
492 /* inform hardware to stop streaming */
493 mutex_lock(&dev
->lock
);
494 ret
= cx231xx_capture_start(dev
, 0, Audio
);
496 /* set alternate setting for audio interface */
497 /* 1 - 48000 samples per sec */
498 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
500 cx231xx_errdev("failed to set alternate setting !\n");
502 mutex_unlock(&dev
->lock
);
508 mutex_unlock(&dev
->lock
);
510 if (dev
->adev
.users
== 0 && dev
->adev
.shutdown
== 1) {
511 dprintk("audio users: %d\n", dev
->adev
.users
);
512 dprintk("disabling audio stream!\n");
513 dev
->adev
.shutdown
= 0;
514 dprintk("released lock\n");
515 if (atomic_read(&dev
->stream_started
) > 0) {
516 atomic_set(&dev
->stream_started
, 0);
517 schedule_work(&dev
->wq_trigger
);
523 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream
*substream
,
524 struct snd_pcm_hw_params
*hw_params
)
526 unsigned int channels
, rate
, format
;
529 dprintk("Setting capture parameters\n");
531 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
532 params_buffer_bytes(hw_params
));
533 format
= params_format(hw_params
);
534 rate
= params_rate(hw_params
);
535 channels
= params_channels(hw_params
);
537 /* TODO: set up cx231xx audio chip to deliver the correct audio format,
538 current default is 48000hz multiplexed => 96000hz mono
539 which shouldn't matter since analogue TV only supports mono */
543 static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream
*substream
)
545 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
547 dprintk("Stop capture, if needed\n");
549 if (atomic_read(&dev
->stream_started
) > 0) {
550 atomic_set(&dev
->stream_started
, 0);
551 schedule_work(&dev
->wq_trigger
);
557 static int snd_cx231xx_prepare(struct snd_pcm_substream
*substream
)
559 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
561 dev
->adev
.hwptr_done_capture
= 0;
562 dev
->adev
.capture_transfer_done
= 0;
567 static void audio_trigger(struct work_struct
*work
)
569 struct cx231xx
*dev
= container_of(work
, struct cx231xx
, wq_trigger
);
571 if (atomic_read(&dev
->stream_started
)) {
572 dprintk("starting capture");
573 if (is_fw_load(dev
) == 0)
574 cx25840_call(dev
, core
, load_fw
);
576 cx231xx_init_audio_isoc(dev
);
578 cx231xx_init_audio_bulk(dev
);
580 dprintk("stopping capture");
581 cx231xx_isoc_audio_deinit(dev
);
585 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream
*substream
,
588 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
591 if (dev
->state
& DEV_DISCONNECTED
)
594 spin_lock(&dev
->adev
.slock
);
596 case SNDRV_PCM_TRIGGER_START
:
597 atomic_set(&dev
->stream_started
, 1);
599 case SNDRV_PCM_TRIGGER_STOP
:
600 atomic_set(&dev
->stream_started
, 0);
605 spin_unlock(&dev
->adev
.slock
);
607 schedule_work(&dev
->wq_trigger
);
612 static snd_pcm_uframes_t
snd_cx231xx_capture_pointer(struct snd_pcm_substream
617 snd_pcm_uframes_t hwptr_done
;
619 dev
= snd_pcm_substream_chip(substream
);
621 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
622 hwptr_done
= dev
->adev
.hwptr_done_capture
;
623 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
628 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
629 unsigned long offset
)
631 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
633 return vmalloc_to_page(pageptr
);
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
,
648 static int cx231xx_audio_init(struct cx231xx
*dev
)
650 struct cx231xx_audio
*adev
= &dev
->adev
;
652 struct snd_card
*card
;
655 struct usb_interface
*uif
;
656 int i
, isoc_pipe
= 0;
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) */
665 cx231xx_info("cx231xx-audio.c: probing for cx231xx "
666 "non standard usbaudio\n");
668 err
= snd_card_create(index
[devnr
], "Cx231xx Audio", THIS_MODULE
,
673 spin_lock_init(&adev
->slock
);
674 err
= snd_pcm_new(card
, "Cx231xx Audio", 0, 0, 1, &pcm
);
680 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
681 &snd_cx231xx_pcm_capture
);
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");
690 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
692 err
= snd_card_register(card
);
697 adev
->sndcard
= card
;
698 adev
->udev
= dev
->udev
;
700 /* compute alternate max packet sizes for Audio */
702 dev
->udev
->actconfig
->interface
[dev
->current_pcb_config
.
703 hs_config_info
[0].interface_info
.
706 adev
->end_point_addr
=
707 le16_to_cpu(uif
->altsetting
[0].endpoint
[isoc_pipe
].desc
.
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
);
715 if (adev
->alt_max_pkt_size
== NULL
) {
716 cx231xx_errdev("out of memory!\n");
720 for (i
= 0; i
< adev
->num_alt
; i
++) {
722 le16_to_cpu(uif
->altsetting
[i
].endpoint
[isoc_pipe
].desc
.
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
]);
733 static int cx231xx_audio_fini(struct cx231xx
*dev
)
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) */
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
;
754 static struct cx231xx_ops audio_ops
= {
756 .name
= "Cx231xx Audio Extension",
757 .init
= cx231xx_audio_init
,
758 .fini
= cx231xx_audio_fini
,
761 static int __init
cx231xx_alsa_register(void)
763 return cx231xx_register_extension(&audio_ops
);
766 static void __exit
cx231xx_alsa_unregister(void)
768 cx231xx_unregister_extension(&audio_ops
);
771 MODULE_LICENSE("GPL");
772 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
773 MODULE_DESCRIPTION("Cx231xx Audio driver");
775 module_init(cx231xx_alsa_register
);
776 module_exit(cx231xx_alsa_unregister
);