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
;
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
= 0;
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 /* set alternate setting for audio interface */
453 /* 1 - 48000 samples per sec */
454 mutex_lock(&dev
->lock
);
456 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 1);
458 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
459 mutex_unlock(&dev
->lock
);
461 cx231xx_errdev("failed to set alternate setting !\n");
466 runtime
->hw
= snd_cx231xx_hw_capture
;
468 mutex_lock(&dev
->lock
);
469 /* inform hardware to start streaming */
470 ret
= cx231xx_capture_start(dev
, 1, Audio
);
473 mutex_unlock(&dev
->lock
);
475 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
476 dev
->adev
.capture_pcm_substream
= substream
;
477 runtime
->private_data
= dev
;
482 static int snd_cx231xx_pcm_close(struct snd_pcm_substream
*substream
)
485 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
487 dprintk("closing device\n");
489 /* inform hardware to stop streaming */
490 mutex_lock(&dev
->lock
);
491 ret
= cx231xx_capture_start(dev
, 0, Audio
);
493 /* set alternate setting for audio interface */
494 /* 1 - 48000 samples per sec */
495 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
497 cx231xx_errdev("failed to set alternate setting !\n");
499 mutex_unlock(&dev
->lock
);
504 mutex_unlock(&dev
->lock
);
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
);
519 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream
*substream
,
520 struct snd_pcm_hw_params
*hw_params
)
524 dprintk("Setting capture parameters\n");
526 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
527 params_buffer_bytes(hw_params
));
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
;
534 format
= params_format(hw_params
);
535 rate
= params_rate(hw_params
);
536 channels
= params_channels(hw_params
);
542 static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream
*substream
)
544 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
546 dprintk("Stop capture, if needed\n");
548 if (atomic_read(&dev
->stream_started
) > 0) {
549 atomic_set(&dev
->stream_started
, 0);
550 schedule_work(&dev
->wq_trigger
);
556 static int snd_cx231xx_prepare(struct snd_pcm_substream
*substream
)
558 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
560 dev
->adev
.hwptr_done_capture
= 0;
561 dev
->adev
.capture_transfer_done
= 0;
566 static void audio_trigger(struct work_struct
*work
)
568 struct cx231xx
*dev
= container_of(work
, struct cx231xx
, wq_trigger
);
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
);
575 cx231xx_init_audio_isoc(dev
);
577 cx231xx_init_audio_bulk(dev
);
579 dprintk("stopping capture");
580 cx231xx_isoc_audio_deinit(dev
);
584 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream
*substream
,
587 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
590 if (dev
->state
& DEV_DISCONNECTED
)
593 spin_lock(&dev
->adev
.slock
);
595 case SNDRV_PCM_TRIGGER_START
:
596 atomic_set(&dev
->stream_started
, 1);
598 case SNDRV_PCM_TRIGGER_STOP
:
599 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_new(&dev
->udev
->dev
, index
[devnr
], "Cx231xx Audio",
669 THIS_MODULE
, 0, &card
);
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 strcpy(card
->driver
, "Cx231xx-Audio");
686 strcpy(card
->shortname
, "Cx231xx Audio");
687 strcpy(card
->longname
, "Conexant cx231xx Audio");
689 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
691 err
= snd_card_register(card
);
696 adev
->sndcard
= card
;
697 adev
->udev
= dev
->udev
;
699 /* compute alternate max packet sizes for Audio */
701 dev
->udev
->actconfig
->interface
[dev
->current_pcb_config
.
702 hs_config_info
[0].interface_info
.
705 adev
->end_point_addr
=
706 uif
->altsetting
[0].endpoint
[isoc_pipe
].desc
.
709 adev
->num_alt
= uif
->num_altsetting
;
710 cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
711 adev
->end_point_addr
, adev
->num_alt
);
712 adev
->alt_max_pkt_size
= kmalloc(32 * adev
->num_alt
, GFP_KERNEL
);
714 if (adev
->alt_max_pkt_size
== NULL
) {
715 cx231xx_errdev("out of memory!\n");
719 for (i
= 0; i
< adev
->num_alt
; i
++) {
721 le16_to_cpu(uif
->altsetting
[i
].endpoint
[isoc_pipe
].desc
.
723 adev
->alt_max_pkt_size
[i
] =
724 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
725 cx231xx_info("Alternate setting %i, max size= %i\n", i
,
726 adev
->alt_max_pkt_size
[i
]);
732 static int cx231xx_audio_fini(struct cx231xx
*dev
)
737 if (dev
->has_alsa_audio
!= 1) {
738 /* This device does not support the extension (in this case
739 the device is expecting the snd-usb-audio module or
740 doesn't have analog audio support at all) */
744 if (dev
->adev
.sndcard
) {
745 snd_card_free(dev
->adev
.sndcard
);
746 kfree(dev
->adev
.alt_max_pkt_size
);
747 dev
->adev
.sndcard
= NULL
;
753 static struct cx231xx_ops audio_ops
= {
755 .name
= "Cx231xx Audio Extension",
756 .init
= cx231xx_audio_init
,
757 .fini
= cx231xx_audio_fini
,
760 static int __init
cx231xx_alsa_register(void)
762 return cx231xx_register_extension(&audio_ops
);
765 static void __exit
cx231xx_alsa_unregister(void)
767 cx231xx_unregister_extension(&audio_ops
);
770 MODULE_LICENSE("GPL");
771 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
772 MODULE_DESCRIPTION("Cx231xx Audio driver");
774 module_init(cx231xx_alsa_register
);
775 module_exit(cx231xx_alsa_unregister
);