1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Conexant Cx231xx audio extension
5 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 * Based on em28xx driver
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sound.h>
13 #include <linux/spinlock.h>
14 #include <linux/soundcard.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/info.h>
21 #include <sound/initval.h>
22 #include <sound/control.h>
23 #include <media/v4l2-common.h>
26 module_param(debug
, int, 0644);
27 MODULE_PARM_DESC(debug
, "activates debug info");
29 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
31 static int cx231xx_isoc_audio_deinit(struct cx231xx
*dev
)
35 dev_dbg(dev
->dev
, "Stopping isoc\n");
37 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
38 if (dev
->adev
.urb
[i
]) {
40 usb_kill_urb(dev
->adev
.urb
[i
]);
42 usb_unlink_urb(dev
->adev
.urb
[i
]);
44 usb_free_urb(dev
->adev
.urb
[i
]);
45 dev
->adev
.urb
[i
] = NULL
;
47 kfree(dev
->adev
.transfer_buffer
[i
]);
48 dev
->adev
.transfer_buffer
[i
] = NULL
;
55 static int cx231xx_bulk_audio_deinit(struct cx231xx
*dev
)
59 dev_dbg(dev
->dev
, "Stopping bulk\n");
61 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
62 if (dev
->adev
.urb
[i
]) {
64 usb_kill_urb(dev
->adev
.urb
[i
]);
66 usb_unlink_urb(dev
->adev
.urb
[i
]);
68 usb_free_urb(dev
->adev
.urb
[i
]);
69 dev
->adev
.urb
[i
] = NULL
;
71 kfree(dev
->adev
.transfer_buffer
[i
]);
72 dev
->adev
.transfer_buffer
[i
] = NULL
;
79 static void cx231xx_audio_isocirq(struct urb
*urb
)
81 struct cx231xx
*dev
= urb
->context
;
84 int period_elapsed
= 0;
88 struct snd_pcm_substream
*substream
;
89 struct snd_pcm_runtime
*runtime
;
91 if (dev
->state
& DEV_DISCONNECTED
)
94 switch (urb
->status
) {
96 case -ETIMEDOUT
: /* NAK */
98 case -ECONNRESET
: /* kill */
103 dev_dbg(dev
->dev
, "urb completion error %d.\n",
108 if (atomic_read(&dev
->stream_started
) == 0)
111 if (dev
->adev
.capture_pcm_substream
) {
112 substream
= dev
->adev
.capture_pcm_substream
;
113 runtime
= substream
->runtime
;
114 stride
= runtime
->frame_bits
>> 3;
116 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
118 int length
= urb
->iso_frame_desc
[i
].actual_length
/
120 cp
= (unsigned char *)urb
->transfer_buffer
+
121 urb
->iso_frame_desc
[i
].offset
;
126 oldptr
= dev
->adev
.hwptr_done_capture
;
127 if (oldptr
+ length
>= runtime
->buffer_size
) {
130 cnt
= runtime
->buffer_size
- oldptr
;
131 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
133 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
134 length
* stride
- cnt
* stride
);
136 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
140 snd_pcm_stream_lock_irqsave(substream
, flags
);
142 dev
->adev
.hwptr_done_capture
+= length
;
143 if (dev
->adev
.hwptr_done_capture
>=
144 runtime
->buffer_size
)
145 dev
->adev
.hwptr_done_capture
-=
146 runtime
->buffer_size
;
148 dev
->adev
.capture_transfer_done
+= length
;
149 if (dev
->adev
.capture_transfer_done
>=
150 runtime
->period_size
) {
151 dev
->adev
.capture_transfer_done
-=
152 runtime
->period_size
;
155 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
158 snd_pcm_period_elapsed(substream
);
162 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
165 "resubmit of audio urb failed (error=%i)\n",
171 static void cx231xx_audio_bulkirq(struct urb
*urb
)
173 struct cx231xx
*dev
= urb
->context
;
175 int period_elapsed
= 0;
179 struct snd_pcm_substream
*substream
;
180 struct snd_pcm_runtime
*runtime
;
182 if (dev
->state
& DEV_DISCONNECTED
)
185 switch (urb
->status
) {
186 case 0: /* success */
187 case -ETIMEDOUT
: /* NAK */
189 case -ECONNRESET
: /* kill */
194 dev_dbg(dev
->dev
, "urb completion error %d.\n",
199 if (atomic_read(&dev
->stream_started
) == 0)
202 if (dev
->adev
.capture_pcm_substream
) {
203 substream
= dev
->adev
.capture_pcm_substream
;
204 runtime
= substream
->runtime
;
205 stride
= runtime
->frame_bits
>> 3;
209 int length
= urb
->actual_length
/
211 cp
= (unsigned char *)urb
->transfer_buffer
;
213 oldptr
= dev
->adev
.hwptr_done_capture
;
214 if (oldptr
+ length
>= runtime
->buffer_size
) {
217 cnt
= runtime
->buffer_size
- oldptr
;
218 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
220 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
221 length
* stride
- cnt
* stride
);
223 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
227 snd_pcm_stream_lock_irqsave(substream
, flags
);
229 dev
->adev
.hwptr_done_capture
+= length
;
230 if (dev
->adev
.hwptr_done_capture
>=
231 runtime
->buffer_size
)
232 dev
->adev
.hwptr_done_capture
-=
233 runtime
->buffer_size
;
235 dev
->adev
.capture_transfer_done
+= length
;
236 if (dev
->adev
.capture_transfer_done
>=
237 runtime
->period_size
) {
238 dev
->adev
.capture_transfer_done
-=
239 runtime
->period_size
;
242 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
245 snd_pcm_period_elapsed(substream
);
249 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
252 "resubmit of audio urb failed (error=%i)\n",
258 static int cx231xx_init_audio_isoc(struct cx231xx
*dev
)
264 "%s: Starting ISO AUDIO transfers\n", __func__
);
266 if (dev
->state
& DEV_DISCONNECTED
)
269 sb_size
= CX231XX_ISO_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
271 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
275 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
276 if (!dev
->adev
.transfer_buffer
[i
])
279 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
280 urb
= usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
282 for (j
= 0; j
< i
; j
++) {
283 usb_free_urb(dev
->adev
.urb
[j
]);
284 kfree(dev
->adev
.transfer_buffer
[j
]);
289 urb
->dev
= dev
->udev
;
291 urb
->pipe
= usb_rcvisocpipe(dev
->udev
,
292 dev
->adev
.end_point_addr
);
293 urb
->transfer_flags
= URB_ISO_ASAP
;
294 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
296 urb
->complete
= cx231xx_audio_isocirq
;
297 urb
->number_of_packets
= CX231XX_ISO_NUM_AUDIO_PACKETS
;
298 urb
->transfer_buffer_length
= sb_size
;
300 for (j
= k
= 0; j
< CX231XX_ISO_NUM_AUDIO_PACKETS
;
301 j
++, k
+= dev
->adev
.max_pkt_size
) {
302 urb
->iso_frame_desc
[j
].offset
= k
;
303 urb
->iso_frame_desc
[j
].length
= dev
->adev
.max_pkt_size
;
305 dev
->adev
.urb
[i
] = urb
;
308 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
309 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
311 cx231xx_isoc_audio_deinit(dev
);
319 static int cx231xx_init_audio_bulk(struct cx231xx
*dev
)
325 "%s: Starting BULK AUDIO transfers\n", __func__
);
327 if (dev
->state
& DEV_DISCONNECTED
)
330 sb_size
= CX231XX_NUM_AUDIO_PACKETS
* dev
->adev
.max_pkt_size
;
332 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
336 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
337 if (!dev
->adev
.transfer_buffer
[i
])
340 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
341 urb
= usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
343 for (j
= 0; j
< i
; j
++) {
344 usb_free_urb(dev
->adev
.urb
[j
]);
345 kfree(dev
->adev
.transfer_buffer
[j
]);
350 urb
->dev
= dev
->udev
;
352 urb
->pipe
= usb_rcvbulkpipe(dev
->udev
,
353 dev
->adev
.end_point_addr
);
354 urb
->transfer_flags
= 0;
355 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
356 urb
->complete
= cx231xx_audio_bulkirq
;
357 urb
->transfer_buffer_length
= sb_size
;
359 dev
->adev
.urb
[i
] = urb
;
363 for (i
= 0; i
< CX231XX_AUDIO_BUFS
; i
++) {
364 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
366 cx231xx_bulk_audio_deinit(dev
);
374 static const struct snd_pcm_hardware snd_cx231xx_hw_capture
= {
375 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
376 SNDRV_PCM_INFO_MMAP
|
377 SNDRV_PCM_INFO_INTERLEAVED
|
378 SNDRV_PCM_INFO_MMAP_VALID
,
380 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
382 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
388 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
389 .period_bytes_min
= 64, /* 12544/2, */
390 .period_bytes_max
= 12544,
392 .periods_max
= 98, /* 12544, */
395 static int snd_cx231xx_capture_open(struct snd_pcm_substream
*substream
)
397 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
398 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
402 "opening device and trying to acquire exclusive lock\n");
404 if (dev
->state
& DEV_DISCONNECTED
) {
406 "Can't open. the device was removed.\n");
410 /* set alternate setting for audio interface */
411 /* 1 - 48000 samples per sec */
412 mutex_lock(&dev
->lock
);
414 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 1);
416 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
417 mutex_unlock(&dev
->lock
);
420 "failed to set alternate setting !\n");
425 runtime
->hw
= snd_cx231xx_hw_capture
;
427 mutex_lock(&dev
->lock
);
428 /* inform hardware to start streaming */
429 ret
= cx231xx_capture_start(dev
, 1, Audio
);
432 mutex_unlock(&dev
->lock
);
434 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
435 dev
->adev
.capture_pcm_substream
= substream
;
436 runtime
->private_data
= dev
;
441 static int snd_cx231xx_pcm_close(struct snd_pcm_substream
*substream
)
444 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
446 dev_dbg(dev
->dev
, "closing device\n");
448 /* inform hardware to stop streaming */
449 mutex_lock(&dev
->lock
);
450 ret
= cx231xx_capture_start(dev
, 0, Audio
);
452 /* set alternate setting for audio interface */
453 /* 1 - 48000 samples per sec */
454 ret
= cx231xx_set_alt_setting(dev
, INDEX_AUDIO
, 0);
457 "failed to set alternate setting !\n");
459 mutex_unlock(&dev
->lock
);
464 mutex_unlock(&dev
->lock
);
466 if (dev
->adev
.users
== 0 && dev
->adev
.shutdown
== 1) {
467 dev_dbg(dev
->dev
, "audio users: %d\n", dev
->adev
.users
);
468 dev_dbg(dev
->dev
, "disabling audio stream!\n");
469 dev
->adev
.shutdown
= 0;
470 dev_dbg(dev
->dev
, "released lock\n");
471 if (atomic_read(&dev
->stream_started
) > 0) {
472 atomic_set(&dev
->stream_started
, 0);
473 schedule_work(&dev
->wq_trigger
);
479 static int snd_cx231xx_prepare(struct snd_pcm_substream
*substream
)
481 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
483 dev
->adev
.hwptr_done_capture
= 0;
484 dev
->adev
.capture_transfer_done
= 0;
489 static void audio_trigger(struct work_struct
*work
)
491 struct cx231xx
*dev
= container_of(work
, struct cx231xx
, wq_trigger
);
493 if (atomic_read(&dev
->stream_started
)) {
494 dev_dbg(dev
->dev
, "starting capture");
495 if (is_fw_load(dev
) == 0)
496 cx25840_call(dev
, core
, load_fw
);
498 cx231xx_init_audio_isoc(dev
);
500 cx231xx_init_audio_bulk(dev
);
502 dev_dbg(dev
->dev
, "stopping capture");
503 cx231xx_isoc_audio_deinit(dev
);
507 static int snd_cx231xx_capture_trigger(struct snd_pcm_substream
*substream
,
510 struct cx231xx
*dev
= snd_pcm_substream_chip(substream
);
513 if (dev
->state
& DEV_DISCONNECTED
)
516 spin_lock(&dev
->adev
.slock
);
518 case SNDRV_PCM_TRIGGER_START
:
519 atomic_set(&dev
->stream_started
, 1);
521 case SNDRV_PCM_TRIGGER_STOP
:
522 atomic_set(&dev
->stream_started
, 0);
528 spin_unlock(&dev
->adev
.slock
);
530 schedule_work(&dev
->wq_trigger
);
535 static snd_pcm_uframes_t
snd_cx231xx_capture_pointer(struct snd_pcm_substream
540 snd_pcm_uframes_t hwptr_done
;
542 dev
= snd_pcm_substream_chip(substream
);
544 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
545 hwptr_done
= dev
->adev
.hwptr_done_capture
;
546 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
551 static const struct snd_pcm_ops snd_cx231xx_pcm_capture
= {
552 .open
= snd_cx231xx_capture_open
,
553 .close
= snd_cx231xx_pcm_close
,
554 .prepare
= snd_cx231xx_prepare
,
555 .trigger
= snd_cx231xx_capture_trigger
,
556 .pointer
= snd_cx231xx_capture_pointer
,
559 static int cx231xx_audio_init(struct cx231xx
*dev
)
561 struct cx231xx_audio
*adev
= &dev
->adev
;
563 struct snd_card
*card
;
566 struct usb_interface
*uif
;
567 int i
, isoc_pipe
= 0;
569 if (dev
->has_alsa_audio
!= 1) {
570 /* This device does not support the extension (in this case
571 the device is expecting the snd-usb-audio module or
572 doesn't have analog audio support at all) */
577 "probing for cx231xx non standard usbaudio\n");
579 err
= snd_card_new(dev
->dev
, index
[devnr
], "Cx231xx Audio",
580 THIS_MODULE
, 0, &card
);
584 spin_lock_init(&adev
->slock
);
585 err
= snd_pcm_new(card
, "Cx231xx Audio", 0, 0, 1, &pcm
);
589 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
,
590 &snd_cx231xx_pcm_capture
);
591 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
, NULL
, 0, 0);
593 pcm
->private_data
= dev
;
594 strscpy(pcm
->name
, "Conexant cx231xx Capture", sizeof(pcm
->name
));
595 strscpy(card
->driver
, "Cx231xx-Audio", sizeof(card
->driver
));
596 strscpy(card
->shortname
, "Cx231xx Audio", sizeof(card
->shortname
));
597 strscpy(card
->longname
, "Conexant cx231xx Audio", sizeof(card
->longname
));
599 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
601 err
= snd_card_register(card
);
605 adev
->sndcard
= card
;
606 adev
->udev
= dev
->udev
;
608 /* compute alternate max packet sizes for Audio */
610 dev
->udev
->actconfig
->interface
[dev
->current_pcb_config
.
611 hs_config_info
[0].interface_info
.
614 if (uif
->altsetting
[0].desc
.bNumEndpoints
< isoc_pipe
+ 1) {
619 adev
->end_point_addr
=
620 uif
->altsetting
[0].endpoint
[isoc_pipe
].desc
.
623 adev
->num_alt
= uif
->num_altsetting
;
625 "audio EndPoint Addr 0x%x, Alternate settings: %i\n",
626 adev
->end_point_addr
, adev
->num_alt
);
627 adev
->alt_max_pkt_size
= kmalloc_array(32, adev
->num_alt
, GFP_KERNEL
);
628 if (!adev
->alt_max_pkt_size
) {
633 for (i
= 0; i
< adev
->num_alt
; i
++) {
636 if (uif
->altsetting
[i
].desc
.bNumEndpoints
< isoc_pipe
+ 1) {
638 goto err_free_pkt_size
;
641 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[isoc_pipe
].desc
.
643 adev
->alt_max_pkt_size
[i
] =
644 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
646 "audio alternate setting %i, max size= %i\n", i
,
647 adev
->alt_max_pkt_size
[i
]);
653 kfree(adev
->alt_max_pkt_size
);
660 static int cx231xx_audio_fini(struct cx231xx
*dev
)
665 if (dev
->has_alsa_audio
!= 1) {
666 /* This device does not support the extension (in this case
667 the device is expecting the snd-usb-audio module or
668 doesn't have analog audio support at all) */
672 if (dev
->adev
.sndcard
) {
673 snd_card_free_when_closed(dev
->adev
.sndcard
);
674 kfree(dev
->adev
.alt_max_pkt_size
);
675 dev
->adev
.sndcard
= NULL
;
681 static struct cx231xx_ops audio_ops
= {
683 .name
= "Cx231xx Audio Extension",
684 .init
= cx231xx_audio_init
,
685 .fini
= cx231xx_audio_fini
,
688 static int __init
cx231xx_alsa_register(void)
690 return cx231xx_register_extension(&audio_ops
);
693 static void __exit
cx231xx_alsa_unregister(void)
695 cx231xx_unregister_extension(&audio_ops
);
698 MODULE_LICENSE("GPL");
699 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
700 MODULE_DESCRIPTION("Cx231xx Audio driver");
702 module_init(cx231xx_alsa_register
);
703 module_exit(cx231xx_alsa_unregister
);