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.
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>
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "activates debug info");
41 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
43 static int cx231xx_isoc_audio_deinit(struct cx231xx
*dev
)
47 dev_dbg(dev
->dev
, "Stopping isoc\n");
49 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
50 if (dev
->adev
.urb
[i
]) {
52 usb_kill_urb(dev
->adev
.urb
[i
]);
54 usb_unlink_urb(dev
->adev
.urb
[i
]);
56 usb_free_urb(dev
->adev
.urb
[i
]);
57 dev
->adev
.urb
[i
] = NULL
;
59 kfree(dev
->adev
.transfer_buffer
[i
]);
60 dev
->adev
.transfer_buffer
[i
] = NULL
;
67 static int cx231xx_bulk_audio_deinit(struct cx231xx
*dev
)
71 dev_dbg(dev
->dev
, "Stopping bulk\n");
73 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
74 if (dev
->adev
.urb
[i
]) {
76 usb_kill_urb(dev
->adev
.urb
[i
]);
78 usb_unlink_urb(dev
->adev
.urb
[i
]);
80 usb_free_urb(dev
->adev
.urb
[i
]);
81 dev
->adev
.urb
[i
] = NULL
;
83 kfree(dev
->adev
.transfer_buffer
[i
]);
84 dev
->adev
.transfer_buffer
[i
] = NULL
;
91 static void cx231xx_audio_isocirq(struct urb
*urb
)
93 struct cx231xx
*dev
= urb
->context
;
96 int period_elapsed
= 0;
100 struct snd_pcm_substream
*substream
;
101 struct snd_pcm_runtime
*runtime
;
103 if (dev
->state
& DEV_DISCONNECTED
)
106 switch (urb
->status
) {
107 case 0: /* success */
108 case -ETIMEDOUT
: /* NAK */
110 case -ECONNRESET
: /* kill */
115 dev_dbg(dev
->dev
, "urb completition error %d.\n",
120 if (atomic_read(&dev
->stream_started
) == 0)
123 if (dev
->adev
.capture_pcm_substream
) {
124 substream
= dev
->adev
.capture_pcm_substream
;
125 runtime
= substream
->runtime
;
126 stride
= runtime
->frame_bits
>> 3;
128 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
129 int length
= urb
->iso_frame_desc
[i
].actual_length
/
131 cp
= (unsigned char *)urb
->transfer_buffer
+
132 urb
->iso_frame_desc
[i
].offset
;
137 oldptr
= dev
->adev
.hwptr_done_capture
;
138 if (oldptr
+ length
>= runtime
->buffer_size
) {
141 cnt
= runtime
->buffer_size
- oldptr
;
142 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
144 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
145 length
* stride
- cnt
* stride
);
147 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
151 snd_pcm_stream_lock(substream
);
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
;
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
;
166 snd_pcm_stream_unlock(substream
);
169 snd_pcm_period_elapsed(substream
);
173 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
176 "resubmit of audio urb failed (error=%i)\n",
182 static void cx231xx_audio_bulkirq(struct urb
*urb
)
184 struct cx231xx
*dev
= urb
->context
;
186 int period_elapsed
= 0;
190 struct snd_pcm_substream
*substream
;
191 struct snd_pcm_runtime
*runtime
;
193 if (dev
->state
& DEV_DISCONNECTED
)
196 switch (urb
->status
) {
197 case 0: /* success */
198 case -ETIMEDOUT
: /* NAK */
200 case -ECONNRESET
: /* kill */
205 dev_dbg(dev
->dev
, "urb completition error %d.\n",
210 if (atomic_read(&dev
->stream_started
) == 0)
213 if (dev
->adev
.capture_pcm_substream
) {
214 substream
= dev
->adev
.capture_pcm_substream
;
215 runtime
= substream
->runtime
;
216 stride
= runtime
->frame_bits
>> 3;
219 int length
= urb
->actual_length
/
221 cp
= (unsigned char *)urb
->transfer_buffer
;
223 oldptr
= dev
->adev
.hwptr_done_capture
;
224 if (oldptr
+ length
>= runtime
->buffer_size
) {
227 cnt
= runtime
->buffer_size
- oldptr
;
228 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
230 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
231 length
* stride
- cnt
* stride
);
233 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
237 snd_pcm_stream_lock(substream
);
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
;
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
;
252 snd_pcm_stream_unlock(substream
);
255 snd_pcm_period_elapsed(substream
);
259 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
262 "resubmit of audio urb failed (error=%i)\n",
268 static int cx231xx_init_audio_isoc(struct cx231xx
*dev
)
274 "%s: Starting ISO AUDIO transfers\n", __func__
);
276 if (dev
->state
& DEV_DISCONNECTED
)
279 sb_size
= CX231XX_ISO_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
281 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
285 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
286 if (!dev
->adev
.transfer_buffer
[i
])
289 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
290 urb
= usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
292 for (j
= 0; j
< i
; j
++) {
293 usb_free_urb(dev
->adev
.urb
[j
]);
294 kfree(dev
->adev
.transfer_buffer
[j
]);
299 urb
->dev
= dev
->udev
;
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
];
306 urb
->complete
= cx231xx_audio_isocirq
;
307 urb
->number_of_packets
= CX231XX_ISO_NUM_AUDIO_PACKETS
;
308 urb
->transfer_buffer_length
= sb_size
;
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
;
315 dev
->adev
.urb
[i
] = urb
;
318 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
319 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
321 cx231xx_isoc_audio_deinit(dev
);
329 static int cx231xx_init_audio_bulk(struct cx231xx
*dev
)
335 "%s: Starting BULK AUDIO transfers\n", __func__
);
337 if (dev
->state
& DEV_DISCONNECTED
)
340 sb_size
= CX231XX_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
342 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
346 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
347 if (!dev
->adev
.transfer_buffer
[i
])
350 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
351 urb
= usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
353 for (j
= 0; j
< i
; j
++) {
354 usb_free_urb(dev
->adev
.urb
[j
]);
355 kfree(dev
->adev
.transfer_buffer
[j
]);
360 urb
->dev
= dev
->udev
;
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
;
369 dev
->adev
.urb
[i
] = urb
;
373 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
374 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
376 cx231xx_bulk_audio_deinit(dev
);
384 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
387 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
388 struct cx231xx
*dev
= snd_pcm_substream_chip(subs
);
390 dev_dbg(dev
->dev
, "Allocating vbuffer\n");
391 if (runtime
->dma_area
) {
392 if (runtime
->dma_bytes
> size
)
395 vfree(runtime
->dma_area
);
397 runtime
->dma_area
= vmalloc(size
);
398 if (!runtime
->dma_area
)
401 runtime
->dma_bytes
= size
;
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
,
412 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
414 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
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,
424 .periods_max
= 98, /* 12544, */
427 static int snd_cx231xx_capture_open(struct snd_pcm_substream
*substream
)
429 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
430 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
434 "opening device and trying to acquire exclusive lock\n");
436 if (dev
->state
& DEV_DISCONNECTED
) {
438 "Can't open. the device was removed.\n");
442 /* set alternate setting for audio interface */
443 /* 1 - 48000 samples per sec */
444 mutex_lock(&dev
->lock
);
446 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 1);
448 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
449 mutex_unlock(&dev
->lock
);
452 "failed to set alternate setting !\n");
457 runtime
->hw
= snd_cx231xx_hw_capture
;
459 mutex_lock(&dev
->lock
);
460 /* inform hardware to start streaming */
461 ret
= cx231xx_capture_start(dev
, 1, Audio
);
464 mutex_unlock(&dev
->lock
);
466 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
467 dev
->adev
.capture_pcm_substream
= substream
;
468 runtime
->private_data
= dev
;
473 static int snd_cx231xx_pcm_close(struct snd_pcm_substream
*substream
)
476 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
478 dev_dbg(dev
->dev
, "closing device\n");
480 /* inform hardware to stop streaming */
481 mutex_lock(&dev
->lock
);
482 ret
= cx231xx_capture_start(dev
, 0, Audio
);
484 /* set alternate setting for audio interface */
485 /* 1 - 48000 samples per sec */
486 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
489 "failed to set alternate setting !\n");
491 mutex_unlock(&dev
->lock
);
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
;
501 mutex_unlock(&dev
->lock
);
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
);
516 static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream
*substream
,
517 struct snd_pcm_hw_params
*hw_params
)
519 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
522 dev_dbg(dev
->dev
, "Setting capture parameters\n");
524 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
525 params_buffer_bytes(hw_params
));
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
;
532 format
= params_format(hw_params
);
533 rate
= params_rate(hw_params
);
534 channels
= params_channels(hw_params
);
540 static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream
*substream
)
542 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
544 dev_dbg(dev
->dev
, "Stop capture, if needed\n");
546 if (atomic_read(&dev
->stream_started
) > 0) {
547 atomic_set(&dev
->stream_started
, 0);
548 schedule_work(&dev
->wq_trigger
);
554 static int snd_cx231xx_prepare(struct snd_pcm_substream
*substream
)
556 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
558 dev
->adev
.hwptr_done_capture
= 0;
559 dev
->adev
.capture_transfer_done
= 0;
564 static void audio_trigger(struct work_struct
*work
)
566 struct cx231xx
*dev
= container_of(work
, struct cx231xx
, wq_trigger
);
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
);
573 cx231xx_init_audio_isoc(dev
);
575 cx231xx_init_audio_bulk(dev
);
577 dev_dbg(dev
->dev
, "stopping capture");
578 cx231xx_isoc_audio_deinit(dev
);
582 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream
*substream
,
585 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
588 if (dev
->state
& DEV_DISCONNECTED
)
591 spin_lock(&dev
->adev
.slock
);
593 case SNDRV_PCM_TRIGGER_START
:
594 atomic_set(&dev
->stream_started
, 1);
596 case SNDRV_PCM_TRIGGER_STOP
:
597 atomic_set(&dev
->stream_started
, 0);
603 spin_unlock(&dev
->adev
.slock
);
605 schedule_work(&dev
->wq_trigger
);
610 static snd_pcm_uframes_t
snd_cx231xx_capture_pointer(struct snd_pcm_substream
615 snd_pcm_uframes_t hwptr_done
;
617 dev
= snd_pcm_substream_chip(substream
);
619 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
620 hwptr_done
= dev
->adev
.hwptr_done_capture
;
621 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
626 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
627 unsigned long offset
)
629 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
631 return vmalloc_to_page(pageptr
);
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
,
646 static int cx231xx_audio_init(struct cx231xx
*dev
)
648 struct cx231xx_audio
*adev
= &dev
->adev
;
650 struct snd_card
*card
;
653 struct usb_interface
*uif
;
654 int i
, isoc_pipe
= 0;
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) */
664 "probing for cx231xx non standard usbaudio\n");
666 err
= snd_card_new(dev
->dev
, index
[devnr
], "Cx231xx Audio",
667 THIS_MODULE
, 0, &card
);
671 spin_lock_init(&adev
->slock
);
672 err
= snd_pcm_new(card
, "Cx231xx Audio", 0, 0, 1, &pcm
);
676 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
677 &snd_cx231xx_pcm_capture
);
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");
685 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
687 err
= snd_card_register(card
);
691 adev
->sndcard
= card
;
692 adev
->udev
= dev
->udev
;
694 /* compute alternate max packet sizes for Audio */
696 dev
->udev
->actconfig
->interface
[dev
->current_pcb_config
.
697 hs_config_info
[0].interface_info
.
700 if (uif
->altsetting
[0].desc
.bNumEndpoints
< isoc_pipe
+ 1) {
705 adev
->end_point_addr
=
706 uif
->altsetting
[0].endpoint
[isoc_pipe
].desc
.
709 adev
->num_alt
= uif
->num_altsetting
;
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
) {
719 for (i
= 0; i
< adev
->num_alt
; i
++) {
722 if (uif
->altsetting
[i
].desc
.bNumEndpoints
< isoc_pipe
+ 1) {
724 goto err_free_pkt_size
;
727 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[isoc_pipe
].desc
.
729 adev
->alt_max_pkt_size
[i
] =
730 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
732 "audio alternate setting %i, max size= %i\n", i
,
733 adev
->alt_max_pkt_size
[i
]);
739 kfree(adev
->alt_max_pkt_size
);
746 static int cx231xx_audio_fini(struct cx231xx
*dev
)
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) */
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
;
767 static struct cx231xx_ops audio_ops
= {
769 .name
= "Cx231xx Audio Extension",
770 .init
= cx231xx_audio_init
,
771 .fini
= cx231xx_audio_fini
,
774 static int __init
cx231xx_alsa_register(void)
776 return cx231xx_register_extension(&audio_ops
);
779 static void __exit
cx231xx_alsa_unregister(void)
781 cx231xx_unregister_extension(&audio_ops
);
784 MODULE_LICENSE("GPL");
785 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
786 MODULE_DESCRIPTION("Cx231xx Audio driver");
788 module_init(cx231xx_alsa_register
);
789 module_exit(cx231xx_alsa_unregister
);