Skip to contentSkip to conversation

snd-bcd2000: add audio support and control interface

PATCH In review Mario Kicherer 2 participants 8 messages Jan 31, 2015 – Feb 1, 2015 Sound

lore
Share & tools
lore
#0 lore raw

cover snd-bcd2000: add audio support and control interface

The current version of the driver only provides a MIDI interface to read controller status and switch LEDs. These patches add audio playback and capture support as well as a control interface to switch between Phono A and Mic input.

#1 lore raw

PATCH 1/3 snd-bcd2000: move MIDI part into own files

---
 sound/usb/bcd2000/Makefile  |   2 +-
 sound/usb/bcd2000/bcd2000.c | 353 +++-----------------------------------------
 sound/usb/bcd2000/bcd2000.h |  25 ++++
 sound/usb/bcd2000/midi.c    | 324 ++++++++++++++++++++++++++++++++++++++++
 sound/usb/bcd2000/midi.h    |  31 ++++
 5 files changed, 404 insertions(+), 331 deletions(-)
 create mode 100644 sound/usb/bcd2000/bcd2000.h
 create mode 100644 sound/usb/bcd2000/midi.c
 create mode 100644 sound/usb/bcd2000/midi.h
sound/usb/bcd2000/Makefile +1 -1
index f09ccc0..bc64a21 100644
@@ -1,3 +1,3 @@
-snd-bcd2000-y := bcd2000.o
+snd-bcd2000-y := bcd2000.o midi.o
 
 obj-$(CONFIG_SND_BCD2000) += snd-bcd2000.o
\ No newline at end of file
sound/usb/bcd2000/bcd2000.c +23 -330

413 lines not on this page · raw message

sound/usb/bcd2000/bcd2000.h new file +25 -0
new file mode 100644
index 0000000..a0e27c9
@@ -0,0 +1,25 @@
+#ifndef BCD2000_H
+#define BCD2000_H
+
+#include <linux/usb.h>
+#include <linux/usb/audio.h>
+#include <sound/core.h>
+#include <sound/initval.h>
+
+#define DEVICENAME "BCD2000"
+#define PREFIX "snd-bcd2000: "
+
+#include "midi.h"
+
+struct bcd2000 {
+	struct usb_device *dev;
+	struct snd_card *card;
+	struct usb_interface *intf;
+	int card_index;
+
+	struct bcd2000_midi midi;
+};
+
+void bcd2000_dump_buffer(const char *prefix, const char *buf, int len);
+
+#endif
sound/usb/bcd2000/midi.c new file +324 -0

324 lines not on this page · raw message

sound/usb/bcd2000/midi.h new file +31 -0
new file mode 100644
index 0000000..a4d9fd8
@@ -0,0 +1,31 @@
+#ifndef MIDI_H
+#define MIDI_H
+
+#include <sound/rawmidi.h>
+
+#define MIDI_URB_BUFSIZE 64
+#define MIDI_CMD_PREFIX_INIT {0x03, 0x00}
+
+struct bcd2000;
+
+struct bcd2000_midi {
+	struct bcd2000 *bcd2k;
+
+	int out_active;
+	struct snd_rawmidi *rmidi;
+	struct snd_rawmidi_substream *receive_substream;
+	struct snd_rawmidi_substream *send_substream;
+
+	unsigned char in_buffer[MIDI_URB_BUFSIZE];
+	unsigned char out_buffer[MIDI_URB_BUFSIZE];
+
+	struct urb *out_urb;
+	struct urb *in_urb;
+
+	struct usb_anchor anchor;
+};
+
+int bcd2000_init_midi(struct bcd2000 *bcd2k);
+void bcd2000_free_midi(struct bcd2000 *bcd2k);
+
+#endif
Signature · 1 line
2.0.5
#2 lore raw

PATCH 2/3 snd-bcd2000: add playback and capture support

In reply to Mario Kicherer
---
 sound/usb/bcd2000/Makefile  |   2 +-
 sound/usb/bcd2000/audio.c   | 632 ++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/bcd2000/audio.h   |  58 ++++
 sound/usb/bcd2000/bcd2000.c |   6 +
 sound/usb/bcd2000/bcd2000.h |   2 +
 5 files changed, 699 insertions(+), 1 deletion(-)
 create mode 100644 sound/usb/bcd2000/audio.c
 create mode 100644 sound/usb/bcd2000/audio.h
sound/usb/bcd2000/Makefile +1 -1
index bc64a21..d424dff 100644
@@ -1,3 +1,3 @@
-snd-bcd2000-y := bcd2000.o midi.o
+snd-bcd2000-y := bcd2000.o audio.o midi.o
 
 obj-$(CONFIG_SND_BCD2000) += snd-bcd2000.o
\ No newline at end of file
sound/usb/bcd2000/audio.c new file +632 -0

632 lines not on this page · raw message

sound/usb/bcd2000/audio.h new file +58 -0

58 lines not on this page · raw message

sound/usb/bcd2000/bcd2000.c +6 -0
index 0f22bd9..f7ba9ad 100644
@@ -59,6 +59,8 @@ static void bcd2000_disconnect(struct usb_interface *interface)
 	/* make sure that userspace cannot create new requests */
 	snd_card_disconnect(bcd2k->card);
 
+	bcd2000_free_audio(bcd2k);
+
 	bcd2000_free_midi(bcd2k);
 
 	if (bcd2k->intf) {
@@ -120,6 +122,10 @@ static int bcd2000_probe(struct usb_interface *interface,
 	if (err < 0)
 		goto probe_error;
 
+	err = bcd2000_init_audio(bcd2k);
+	if (err < 0)
+		goto probe_error;
+
 	err = snd_card_register(card);
 	if (err < 0)
 		goto probe_error;
sound/usb/bcd2000/bcd2000.h +2 -0
index a0e27c9..802685b 100644
@@ -9,6 +9,7 @@
 #define DEVICENAME "BCD2000"
 #define PREFIX "snd-bcd2000: "
 
+#include "audio.h"
 #include "midi.h"
 
 struct bcd2000 {
@@ -18,6 +19,7 @@ struct bcd2000 {
 	int card_index;
 
 	struct bcd2000_midi midi;
+	struct bcd2000_pcm pcm;
 };
 
 void bcd2000_dump_buffer(const char *prefix, const char *buf, int len);
Signature · 1 line
2.0.5
#5 lore raw

PATCH 3/3 snd-bcd2000: add control interface

In reply to Mario Kicherer
---
 sound/usb/bcd2000/Makefile  |   2 +-
 sound/usb/bcd2000/bcd2000.c |   6 +++
 sound/usb/bcd2000/bcd2000.h |   2 +
 sound/usb/bcd2000/control.c | 119 ++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/bcd2000/control.h |  15 ++++++
 5 files changed, 143 insertions(+), 1 deletion(-)
 create mode 100644 sound/usb/bcd2000/control.c
 create mode 100644 sound/usb/bcd2000/control.h
sound/usb/bcd2000/Makefile +1 -1
index d424dff..176324f 100644
@@ -1,3 +1,3 @@
-snd-bcd2000-y := bcd2000.o audio.o midi.o
+snd-bcd2000-y := bcd2000.o audio.o control.o midi.o
 
 obj-$(CONFIG_SND_BCD2000) += snd-bcd2000.o
\ No newline at end of file
sound/usb/bcd2000/bcd2000.c +6 -0
index f7ba9ad..1bd5111 100644
@@ -59,6 +59,8 @@ static void bcd2000_disconnect(struct usb_interface *interface)
 	/* make sure that userspace cannot create new requests */
 	snd_card_disconnect(bcd2k->card);
 
+	bcd2000_free_control(bcd2k);
+
 	bcd2000_free_audio(bcd2k);
 
 	bcd2000_free_midi(bcd2k);
@@ -126,6 +128,10 @@ static int bcd2000_probe(struct usb_interface *interface,
 	if (err < 0)
 		goto probe_error;
 
+	err = bcd2000_init_control(bcd2k);
+	if (err < 0)
+		goto probe_error;
+
 	err = snd_card_register(card);
 	if (err < 0)
 		goto probe_error;
sound/usb/bcd2000/bcd2000.h +2 -0
index 802685b..96d93f5 100644
@@ -10,6 +10,7 @@
 #define PREFIX "snd-bcd2000: "
 
 #include "audio.h"
+#include "control.h"
 #include "midi.h"
 
 struct bcd2000 {
@@ -18,6 +19,7 @@ struct bcd2000 {
 	struct usb_interface *intf;
 	int card_index;
 
+	struct bcd2000_control control;
 	struct bcd2000_midi midi;
 	struct bcd2000_pcm pcm;
 };
sound/usb/bcd2000/control.c new file +119 -0

119 lines not on this page · raw message

sound/usb/bcd2000/control.h new file +15 -0
new file mode 100644
index 0000000..79cc053
@@ -0,0 +1,15 @@
+#ifndef CONTROL_H
+#define CONTROL_H
+
+struct bcd2000;
+
+struct bcd2000_control {
+	struct bcd2000 *bcd2k;
+
+	bool phono_mic_switch;
+};
+
+int bcd2000_init_control(struct bcd2000 *bcd2k);
+void bcd2000_free_control(struct bcd2000 *bcd2k);
+
+#endif
Signature · 1 line
2.0.5
#7 lore raw
In reply to Mario Kicherer

At Sat, 31 Jan 2015 22:34:22 +0100,

Quoted 5 lines · Mario Kicherer

The current version of the driver only provides a MIDI interface to read controller status and switch LEDs. These patches add audio playback and capture support as well as a control interface to switch between Phono A and Mic input.

The patches are almost good but there are a few issues, especially the biggest problem is the empty changelog. Usually the empty changlog already indicates something wrong, except for trivial fixes or conversions. If you have some words in the cover letter, such details can be put into each patch changelog, too, at least.

thanks,

Takashi

#3 lore raw

Re: [PATCH 2/3] snd-bcd2000: add playback and capture support

In reply to Mario Kicherer

At Sat, 31 Jan 2015 22:34:24 +0100,

sound/usb/bcd2000/audio.c line 105

Quoted 13 lines · Mario Kicherer
sound/usb/bcd2000/audio.c new file +12 -0
+/* handle incoming URB with captured data */
+static void bcd2000_pcm_in_urb_handler(struct urb *usb_urb)
+{
+	struct bcd2000_urb *bcd2k_urb = usb_urb->context;
+	struct bcd2000_pcm *pcm = &bcd2k_urb->bcd2k->pcm;
+	struct bcd2000_substream *stream = bcd2k_urb->stream;
+	unsigned long flags;
+	int ret, k, period_bytes;
+	struct usb_iso_packet_descriptor *packet;
+
+	if (pcm->panic || stream->state == STREAM_STOPPING)
+		return;

Don't you need protection for these flags and state variables?

sound/usb/bcd2000/audio.c line 112

Quoted 7 lines
sound/usb/bcd2000/audio.c new file +7 -0
+
+	if (unlikely(usb_urb->status == -ENOENT ||	/* unlinked */
+		usb_urb->status == -ENODEV ||		/* device removed */
+		usb_urb->status == -ECONNRESET ||	/* unlinked */
+		usb_urb->status == -ESHUTDOWN))		/* device disabled */
+	{
+		goto out_fail;

This isn't always an error, e.g. active URBS at disconnection.

Quoted 24 lines
sound/usb/bcd2000/audio.c new file +24 -0
+	}
+
+	if (stream->state == STREAM_STARTING) {
+		stream->wait_cond = true;
+		wake_up(&stream->wait_queue);
+	}
+
+	if (stream->active) {
+		spin_lock_irqsave(&stream->lock, flags);
+
+		/* copy captured data into ALSA buffer */
+		bcd2000_pcm_capture(stream, bcd2k_urb);
+
+		period_bytes = snd_pcm_lib_period_bytes(stream->instance);
+
+		/* do we have enough data for one period? */
+		if (stream->period_off > period_bytes) {
+			stream->period_off %= period_bytes;
+
+			spin_unlock_irqrestore(&stream->lock, flags);
+
+			/*
+			 * call this only once even if multiple periods
+			 * are ready

sound/usb/bcd2000/audio.c line 146

Quoted 10 lines
sound/usb/bcd2000/audio.c new file +10 -0
+			 */
+			snd_pcm_period_elapsed(stream->instance);
+
+			memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);
+		} else {
+			spin_unlock_irqrestore(&stream->lock, flags);
+			memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);
+		}
+	} else {
+		memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);

We see three same lines of memset... It shows something to improve.

Quoted 12 lines
sound/usb/bcd2000/audio.c new file +12 -0
+	}
+
+	/* reset URB data */
+	for (k = 0; k < USB_N_PACKETS_PER_URB; k++) {
+		packet = &bcd2k_urb->packets[k];
+		packet->offset = k * USB_PACKET_SIZE;
+		packet->length = USB_PACKET_SIZE;
+		packet->actual_length = 0;
+		packet->status = 0;
+	}
+	bcd2k_urb->instance.number_of_packets = USB_N_PACKETS_PER_URB;
+

sound/usb/bcd2000/audio.c line 168

Quoted 10 lines
sound/usb/bcd2000/audio.c new file +10 -0
+	/* send the URB back to the BCD2000 */
+	ret = usb_submit_urb(&bcd2k_urb->instance, GFP_ATOMIC);
+	if (ret < 0)
+		goto out_fail;
+
+	return;
+
+out_fail:
+	dev_info(&bcd2k_urb->bcd2k->dev->dev, PREFIX "error in in_urb handler");
+	pcm->panic = true;

How is pcm->panic recovered?
I see it's cleared in bcd2000_pcm_stream_start(). But then some doubts:

- who will refill / resubmit urbs at restart?

- the flag is common to both playback and capture, and it's cleared in the trigger for one direction?

Also, does the disconnection while playing/recording works reliably? snd_card_disconnect() doesn't close the PCM stream by itself, it merely triggers STOP and sets the state to DISCONNECTED. That is, with your code, the URBs are still pending, and it's killed after disconnection callback returns.

BTW, a similar (but basically more severe) problem exists for the MIDI. You shouldn't release the resources right after snd_card_disconnect(). The streams might be still alive. Usually the resources have to be released in the free callback, not at disconnection.

Takashi

#6 lore raw

Re: [PATCH 3/3] snd-bcd2000: add control interface

In reply to Mario Kicherer

At Sat, 31 Jan 2015 22:34:25 +0100,

sound/usb/bcd2000/control.c line 87

Quoted 5 lines · Mario Kicherer
sound/usb/bcd2000/control.c new file +4 -0
+static struct snd_kcontrol_new elements[] = {
+	{
+		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
+		.name = "Phono A / Mic Capture Switch",

Don't use "Switch" suffix for an enum control.

Quoted 14 lines
sound/usb/bcd2000/control.c new file +14 -0
+		.index = 0,
+		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
+		.info = bcd2000_control_phono_mic_sw_info,
+		.get = bcd2000_control_phono_mic_sw_get,
+		.put = bcd2000_control_phono_mic_sw_put
+	},
+	{}
+};
+
+int bcd2000_init_control(struct bcd2000 *bcd2k)
+{
+	int i, ret;
+
+	bcd2k->control.bcd2k = bcd2k;

sound/usb/bcd2000/control.c line 111

Quoted 10 lines
sound/usb/bcd2000/control.c new file +10 -0
+
+	i = 0;
+	while (elements[i].name) {
+		ret = snd_ctl_add(bcd2k->card, snd_ctl_new1(&elements[i],
+							    &bcd2k->control));
+		if (ret < 0) {
+			dev_err(&bcd2k->dev->dev, "cannot add control\n");
+			return ret;
+		}
+		i++;

This is written usually with a for loop.

thanks,

Takashi

#4 lore raw

Re: [PATCH 2/3] snd-bcd2000: add playback and capture support

In reply to Takashi Iwai

At Sun, 01 Feb 2015 10:29:24 +0100,

Quoted 96 lines · Takashi Iwai

At Sat, 31 Jan 2015 22:34:24 +0100,

sound/usb/bcd2000/audio.c line 105

Quoted 13 lines · Mario Kicherer
sound/usb/bcd2000/audio.c new file +12 -0
+/* handle incoming URB with captured data */
+static void bcd2000_pcm_in_urb_handler(struct urb *usb_urb)
+{
+	struct bcd2000_urb *bcd2k_urb = usb_urb->context;
+	struct bcd2000_pcm *pcm = &bcd2k_urb->bcd2k->pcm;
+	struct bcd2000_substream *stream = bcd2k_urb->stream;
+	unsigned long flags;
+	int ret, k, period_bytes;
+	struct usb_iso_packet_descriptor *packet;
+
+	if (pcm->panic || stream->state == STREAM_STOPPING)
+		return;

Don't you need protection for these flags and state variables?

sound/usb/bcd2000/audio.c line 112

Quoted 7 lines
sound/usb/bcd2000/audio.c new file +7 -0
+
+	if (unlikely(usb_urb->status == -ENOENT ||	/* unlinked */
+		usb_urb->status == -ENODEV ||		/* device removed */
+		usb_urb->status == -ECONNRESET ||	/* unlinked */
+		usb_urb->status == -ESHUTDOWN))		/* device disabled */
+	{
+		goto out_fail;

This isn't always an error, e.g. active URBS at disconnection.

Quoted 24 lines
sound/usb/bcd2000/audio.c new file +24 -0
+	}
+
+	if (stream->state == STREAM_STARTING) {
+		stream->wait_cond = true;
+		wake_up(&stream->wait_queue);
+	}
+
+	if (stream->active) {
+		spin_lock_irqsave(&stream->lock, flags);
+
+		/* copy captured data into ALSA buffer */
+		bcd2000_pcm_capture(stream, bcd2k_urb);
+
+		period_bytes = snd_pcm_lib_period_bytes(stream->instance);
+
+		/* do we have enough data for one period? */
+		if (stream->period_off > period_bytes) {
+			stream->period_off %= period_bytes;
+
+			spin_unlock_irqrestore(&stream->lock, flags);
+
+			/*
+			 * call this only once even if multiple periods
+			 * are ready

sound/usb/bcd2000/audio.c line 146

Quoted 10 lines
sound/usb/bcd2000/audio.c new file +10 -0
+			 */
+			snd_pcm_period_elapsed(stream->instance);
+
+			memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);
+		} else {
+			spin_unlock_irqrestore(&stream->lock, flags);
+			memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);
+		}
+	} else {
+		memset(bcd2k_urb->buffer, 0, USB_BUFFER_SIZE);

We see three same lines of memset... It shows something to improve.

Quoted 12 lines
sound/usb/bcd2000/audio.c new file +12 -0
+	}
+
+	/* reset URB data */
+	for (k = 0; k < USB_N_PACKETS_PER_URB; k++) {
+		packet = &bcd2k_urb->packets[k];
+		packet->offset = k * USB_PACKET_SIZE;
+		packet->length = USB_PACKET_SIZE;
+		packet->actual_length = 0;
+		packet->status = 0;
+	}
+	bcd2k_urb->instance.number_of_packets = USB_N_PACKETS_PER_URB;
+

sound/usb/bcd2000/audio.c line 168

Quoted 10 lines
sound/usb/bcd2000/audio.c new file +10 -0
+	/* send the URB back to the BCD2000 */
+	ret = usb_submit_urb(&bcd2k_urb->instance, GFP_ATOMIC);
+	if (ret < 0)
+		goto out_fail;
+
+	return;
+
+out_fail:
+	dev_info(&bcd2k_urb->bcd2k->dev->dev, PREFIX "error in in_urb handler");
+	pcm->panic = true;

How is pcm->panic recovered?
I see it's cleared in bcd2000_pcm_stream_start(). But then some doubts:

- who will refill / resubmit urbs at restart?

Quoted 8 lines

- the flag is common to both playback and capture, and it's cleared in the trigger for one direction?

Also, does the disconnection while playing/recording works reliably? snd_card_disconnect() doesn't close the PCM stream by itself, it merely triggers STOP and sets the state to DISCONNECTED. That is, with your code, the URBs are still pending, and it's killed after disconnection callback returns.

After reading through this patch, I now wonder whether the standard usb-audio would work with a simple quirk for the PCM of this device. If so, it'd be easier to merge bcd2000 stuff into usb-audio, as a quirk for MIDI and additional controls, where already some devices do.

Writing the whole stuff from the scratch is error-prone. With the merger, you'll get the suspend/resume, auto PM, and the proper disconnection with gratis.

thanks,

Takashi