Skip to contentSkip to conversation

ehci-hcd: complete iso urbs ASAP for number_of_packets != (n * 8) also

PATCH New Karsten Wiese 2 participants 7 messages Nov 7, 2007 – Nov 8, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ehci-hcd: complete iso urbs ASAP for number_of_packets != (n * 8) also

Hi

I just sent attached patch to usb-devel.
It makes special workaround for iso usb communication using ehci-hcd highspeed needless.
Comments, Bug/success reports welcome!

kind regards
Karsten

Upstream (as of 2.6.23) ehci-hcd only completes iso urbs, if the last frame (= 8 uframes) they overlapped with has elapsed. That can be as late as when the following urb emits its interrupt on completion.
soundcard drivers tend to work around by only transfering iso urbs with number_of_packets = (n * 8).
Patch lets iso urbs complete asap by scanning itd always up to the elapsed uframe. An itd's last scanned uframe is stored in the new struct ehci_itd member uframe_scanned.
Itds stay kept in the hcd's schedule until the complete frame they cover has elapsed, like without patch.
To make this possible, ehci->periodic_sched is changed based on the number of active itds instead of the number of active iso urbs.

---
 drivers/usb/host/ehci-sched.c |  108 ++++++++++++++++++++---------------------
 drivers/usb/host/ehci.h       |    1 +
 2 files changed, 54 insertions(+), 55 deletions(-)
drivers/usb/host/ehci-sched.c +53 -55
index e682f23..c456b16 100644
@@ -1481,6 +1481,8 @@ itd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd)
 	itd->frame = frame;
 	wmb ();
 	ehci->periodic[frame] = cpu_to_hc32(ehci, itd->itd_dma | Q_TYPE_ITD);
+	if (unlikely (!ehci->periodic_sched++))
+		enable_periodic (ehci);
 }
 
 /* fit urb's itds into the selected schedule slot; activate as needed */
@@ -1553,30 +1555,30 @@ itd_link_urb (
 	urb->hcpriv = NULL;
 
 	timer_action (ehci, TIMER_IO_WATCHDOG);
-	if (unlikely (!ehci->periodic_sched++))
-		return enable_periodic (ehci);
 	return 0;
 }
 
 #define	ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)
 
-static unsigned
-itd_complete (
-	struct ehci_hcd	*ehci,
-	struct ehci_itd	*itd
-) {
+static bool
+itd_scan (
+	struct ehci_hcd *ehci,
+	struct ehci_itd	*itd,
+	unsigned uframe_after
+)
+{
 	struct urb				*urb = itd->urb;
 	struct usb_iso_packet_descriptor	*desc;
 	u32					t;
 	unsigned				uframe;
 	int					urb_index = -1;
 	struct ehci_iso_stream			*stream = itd->stream;
-	struct usb_device			*dev;
 
 	/* for each uframe with a packet */
-	for (uframe = 0; uframe < 8; uframe++) {
+	for (uframe = itd->uframe_scanned; uframe < uframe_after; uframe++) {
 		if (likely (itd->index[uframe] == -1))
 			continue;
+
 		urb_index = itd->index[uframe];
 		desc = &urb->iso_frame_desc [urb_index];
 
@@ -1603,45 +1605,53 @@ itd_complete (
 			desc->status = 0;
 			desc->actual_length = EHCI_ITD_LENGTH (t);
 		}
+
+		/* handle completion now? */
+		if (urb_index + 1 == urb->number_of_packets) {
+			struct usb_device *dev;
+
+			usb_put_urb (urb);
+			/* ASSERT: it's really the last itd for this urb
+			   struct ehci_itd	*stritd;
+			   list_for_each_entry (stritd, &stream->td_list, itd_list)
+			   BUG_ON (stritd != itd && stritd->urb == urb);
+			*/
+			itd->urb = NULL;
+
+			/* give urb back to the driver ... can be out-of-order */
+			dev = urb->dev;
+			ehci_urb_done (ehci, urb);
+			urb = NULL;
+
+			ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
+
+			if (unlikely (list_empty (&stream->td_list))) {
+				ehci_to_hcd(ehci)->self.bandwidth_allocated
+					-= stream->bandwidth;
+				ehci_vdbg (ehci,
+					   "deschedule devp %s ep%d%s-iso\n",
+					   dev->devpath, stream->bEndpointAddress & 0x0f,
+					   (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
+			}
+			iso_stream_put (ehci, stream);
+		}
 	}
 
-	usb_put_urb (urb);
-	itd->urb = NULL;
+	itd->uframe_scanned = uframe_after;
+	if (uframe_after < 8)
+		return false;
+
 	itd->stream = NULL;
+	itd->uframe_scanned = 0;
 	list_move (&itd->itd_list, &stream->free_list);
 	iso_stream_put (ehci, stream);
 
-	/* handle completion now? */
-	if (likely ((urb_index + 1) != urb->number_of_packets))
-		return 0;
-
-	/* ASSERT: it's really the last itd for this urb
-	list_for_each_entry (itd, &stream->td_list, itd_list)
-		BUG_ON (itd->urb == urb);
-	 */
-
-	/* give urb back to the driver ... can be out-of-order */
-	dev = urb->dev;
-	ehci_urb_done (ehci, urb);
-	urb = NULL;
-
 	/* defer stopping schedule; completion can submit */
 	ehci->periodic_sched--;
 	if (unlikely (!ehci->periodic_sched))
 		(void) disable_periodic (ehci);
-	ehci_to_hcd(ehci)->self.bandwidth_isoc_reqs--;
 
-	if (unlikely (list_empty (&stream->td_list))) {
-		ehci_to_hcd(ehci)->self.bandwidth_allocated
-				-= stream->bandwidth;
-		ehci_vdbg (ehci,
-			"deschedule devp %s ep%d%s-iso\n",
-			dev->devpath, stream->bEndpointAddress & 0x0f,
-			(stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
-	}
-	iso_stream_put (ehci, stream);
-
-	return 1;
+	return true;
 }
 
 /*-------------------------------------------------------------------------*/
@@ -2156,30 +2166,18 @@ restart:
 				q = q.fstn->fstn_next;
 				break;
 			case Q_TYPE_ITD:
-				/* skip itds for later in the frame */
 				rmb ();
-				for (uf = live ? uframes : 8; uf < 8; uf++) {
-					if (0 == (q.itd->hw_transaction [uf]
-							& ITD_ACTIVE(ehci)))
-						continue;
+				modified = itd_scan(ehci, q.itd, uf = live ? uframes : 8);
+				if (!modified) {
 					q_p = &q.itd->itd_next;
 					hw_p = &q.itd->hw_next;
-					type = Q_NEXT_TYPE(ehci,
-							q.itd->hw_next);
-					q = *q_p;
-					break;
+				} else {
+					*q_p = q.itd->itd_next;
+					*hw_p = q.itd->hw_next;
 				}
-				if (uf != 8)
-					break;
-
-				/* this one's ready ... HC won't cache the
-				 * pointer for much longer, if at all.
-				 */
-				*q_p = q.itd->itd_next;
-				*hw_p = q.itd->hw_next;
 				type = Q_NEXT_TYPE(ehci, q.itd->hw_next);
+
 				wmb();
-				modified = itd_complete (ehci, q.itd);
 				q = *q_p;
 				break;
 			case Q_TYPE_SITD:
drivers/usb/host/ehci.h +1 -0
index 951d69f..df35964 100644
@@ -581,6 +581,7 @@ struct ehci_itd {
 	struct urb		*urb;
 	struct ehci_iso_stream	*stream;	/* endpoint's queue */
 	struct list_head	itd_list;	/* list of stream's itds */
+	unsigned		uframe_scanned;
 
 	/* any/all hw_transactions here may be used by that urb */
 	unsigned		frame;		/* where scheduled */
Signature · 1 line
1.5.3.3

Upstream (as of 2.6.23) ehci-hcd only completes iso urbs, if the last frame (= 8 uframes) they overlapped with has elapsed. That can be as late as when the following urb emits its interrupt on completion.
soundcard drivers tend to work around by only transfering iso urbs with number_of_packets = (n * 8).
Patch lets iso urbs complete asap by scanning itd always up to the elapsed uframe. An itd's last scanned uframe is stored in the new struct ehci_itd member uframe_scanned.
Itds stay kept in the hcd's schedule until the complete frame they cover has elapsed, like without patch.
To make this possible, ehci->periodic_sched is changed based on the number of active itds instead of the number of active iso urbs.

---
 drivers/usb/host/ehci-sched.c |  108 ++++++++++++++++++++---------------------
 drivers/usb/host/ehci.h       |    1 +
 2 files changed, 54 insertions(+), 55 deletions(-)
drivers/usb/host/ehci-sched.c +53 -55

164 lines not on this page · raw message

drivers/usb/host/ehci.h +1 -0
index 951d69f..df35964 100644
@@ -581,6 +581,7 @@ struct ehci_itd {
 	struct urb		*urb;
 	struct ehci_iso_stream	*stream;	/* endpoint's queue */
 	struct list_head	itd_list;	/* list of stream's itds */
+	unsigned		uframe_scanned;
 
 	/* any/all hw_transactions here may be used by that urb */
 	unsigned		frame;		/* where scheduled */
Signature · 1 line
1.5.3.3

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#1 lore raw

PATCH 1/1 US-122L Driver, v0

Hi,

attached patches implement a simple driver for the tascam us-122l. Everything except midi out works i think ;-)

For USB 2.0 you also have to aply

	http://marc.info/?l=linux-usb-devel&m=119444869917392&w=2

named "ehci-hcd: complete iso urbs ASAP ..."

Will post a jack driver to use it soon.
so far you can #define DEBUG_USB_STREAM in usb_stream.c to playback what is captured.

no alsa pcm interface there yet. only a hwdep to talk to the "usb stream" thing.

A userspace plugin that works like the jack driver would be fine too. Is that easy to do? I mean that userspace driver should export all those standard alsa pcm interfaces then.

Comments bug/success reports, patches welcome.

Karsten

From 0a9a1787bad19a5f542a11f4f087e83951cd2d00 Mon Sep 17 00:00:00 2001
From: Karsten Wiese <[email protected]>
Date: Wed, 7 Nov 2007 19:36:14 +0100
Subject: [PATCH 1/1] Driver for the US-122L usb soundcard
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

uses the "usb stream" facility for pcm. The "usb stream" is made mmap()able by means of a hwdep device.
A patch to read/write audio from userspace will follow as a jackd driver.

For midi in snd-usb-lib is used. Patch for the needed quirk follows. Midi out doesn't work yet.

Works only with USB1.1 host so far. For ehci a patch is needed so "Usb stream" doesn't have to cut the urbs on 8 µframe boundaries. That patch has been posted to usb-devel & alsa-devel as "ehci-hcd: complete iso urbs ASAP for number_of_packets != (n * 8) also"

---
 include/sound/asound.h   |    3 +-
 sound/usb/Kconfig        |   11 +
 sound/usb/usbaudio.h     |    2 +-
 sound/usb/usx2y/Makefile |    2 +
 sound/usb/usx2y/us122l.c |  512 ++++++++++++++++++++++++++++++++++++++++++++++
 sound/usb/usx2y/us122l.h |   21 ++
 6 files changed, 549 insertions(+), 2 deletions(-)
 create mode 100644 sound/usb/usx2y/us122l.c
 create mode 100644 sound/usb/usx2y/us122l.h
include/sound/asound.h +2 -1
index c1621c6..f163f66 100644
@@ -92,9 +92,10 @@ enum {
 	SNDRV_HWDEP_IFACE_USX2Y_PCM,	/* Tascam US122, US224 & US428 rawusb pcm */
 	SNDRV_HWDEP_IFACE_PCXHR,	/* Digigram PCXHR */
 	SNDRV_HWDEP_IFACE_SB_RC,	/* SB Extigy/Audigy2NX remote control */
+	SNDRV_HWDEP_IFACE_USB_STREAM,	/* direct access to usb stream */
 
 	/* Don't forget to change the following: */
-	SNDRV_HWDEP_IFACE_LAST = SNDRV_HWDEP_IFACE_SB_RC
+	SNDRV_HWDEP_IFACE_LAST = SNDRV_HWDEP_IFACE_USB_STREAM
 };
 
 struct snd_hwdep_info {
sound/usb/Kconfig +11 -0
index 315360f..3da2fb4 100644
@@ -57,5 +57,16 @@ config SND_USB_CAIAQ_INPUT
 	   * Native Instruments RigKontrol2
 	   * Native Instruments Audio Kontrol 1
 
+config SND_USB_US122L
+	tristate "Tascam US-122L USB driver"
+	depends on SND && USB && (X86 || PPC || ALPHA)
+	select SND_RAWMIDI
+	help
+	  Say Y here to include support for Tascam USB Audio/MIDI
+	  interfaces or controllers US-122L.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called snd-usb-us122l.
+
 endmenu
 
sound/usb/usbaudio.h +1 -1
index 2272f45..23b13f4 100644
@@ -160,7 +160,7 @@ enum quirk_type {
 	QUIRK_AUDIO_EDIROL_UA700_UA25,
 	QUIRK_AUDIO_EDIROL_UA1000,
 	QUIRK_AUDIO_EDIROL_UA101,
-
+	QUIRK_MIDI_US122L,
 	QUIRK_TYPE_COUNT
 };
 
sound/usb/usx2y/Makefile +2 -0
index 9ac22bc..4b537ff 100644
@@ -1,3 +1,5 @@
 snd-usb-usx2y-objs := usbusx2y.o usX2Yhwdep.o usx2yhwdeppcm.o
+snd-usb-us122l-objs := us122l.o usb_stream.o
 
 obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-usx2y.o
+obj-$(CONFIG_SND_USB_US122L) += snd-usb-us122l.o
\ No newline at end of file
sound/usb/usx2y/us122l.c new file +512 -0

512 lines not on this page · raw message

sound/usb/usx2y/us122l.h new file +21 -0
new file mode 100644
index 0000000..c046943
@@ -0,0 +1,21 @@
+#ifndef US122L_H
+#define US122L_H
+
+
+struct us122l {
+	struct snd_usb_audio 	chip;
+	int			stride;
+	struct usb_stream	*usb_stream;
+
+	struct mutex		prepare_mutex;
+};
+
+
+#define US122L(c) ((struct us122l *)(c)->private_data)
+
+#define NAME_ALLCAPS "US-122L"
+
+#define USB_ID_US122L 0x800E
+#define USB_ID_US144 0x800F
+
+#endif
Signature · 1 line
1.5.3.3

From 1a4fe60461598309dac69cbcb4300244c1ae3459 Mon Sep 17 00:00:00 2001
From: Karsten Wiese <[email protected]>
Date: Tue, 6 Nov 2007 21:37:12 +0100
Subject: [PATCH 1/1] Midi in support for the US-122L usb soundcard

The US-122L sends midi in data in 9 byte long packets. Only the bytes up to before the first 0xFD are midi data.

---
 sound/usb/usbmidi.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
sound/usb/usbmidi.c +19 -0
index 99295f9..8a8e97f 100644
@@ -621,6 +621,18 @@ static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
 	snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
 }
 
+static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint* ep,
+				  uint8_t* buffer, int buffer_length)
+{
+	if (buffer_length != 9)
+		return;
+	buffer_length = 8;
+	while (buffer_length && buffer[buffer_length - 1] == 0xFD)
+		buffer_length--;
+	if (buffer_length)
+		snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
+}
+
 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
 {
 	int count;
@@ -642,6 +654,11 @@ static struct usb_protocol_ops snd_usbmidi_raw_ops = {
 	.output = snd_usbmidi_raw_output,
 };
 
+static struct usb_protocol_ops snd_usbmidi_122l_ops = {
+	.input = snd_usbmidi_us122l_input,
+	.output = snd_usbmidi_raw_output,
+};
+
 /*
  * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
  */
@@ -1661,6 +1678,8 @@ int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
 			umidi->usb_protocol_ops =
 				&snd_usbmidi_maudio_broken_running_status_ops;
 		break;
+	case QUIRK_MIDI_US122L:
+		umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
 	case QUIRK_MIDI_FIXED_ENDPOINT:
 		memcpy(&endpoints[0], quirk->data,
 		       sizeof(struct snd_usb_midi_endpoint_info));
Signature · 1 line
1.5.3.3

From 27e99f95786b5765685cbc16337b2f4e05e01a48 Mon Sep 17 00:00:00 2001
From: Karsten Wiese <[email protected]>
Date: Wed, 7 Nov 2007 19:27:14 +0100
Subject: [PATCH 1/1] "usb stream" for isochrone usb i/o

Provides a combination of an in pipe isochron stream with an out pipe one. The out pipe's urbs are scheduled with packet lengths taken from the inpipe's urbs just red.

A client reads/writes data in equally sized chunks. The chunks are specified as "no of frames per transfer" with frame_size during initialisation.

---
 sound/usb/usx2y/usb_stream.c |  676 ++++++++++++++++++++++++++++++++++++++++++
 sound/usb/usx2y/usb_stream.h |  111 +++++++
 2 files changed, 787 insertions(+), 0 deletions(-)
 create mode 100644 sound/usb/usx2y/usb_stream.c
 create mode 100644 sound/usb/usx2y/usb_stream.h
sound/usb/usx2y/usb_stream.c new file +676 -0

676 lines not on this page · raw message

sound/usb/usx2y/usb_stream.h new file +111 -0

111 lines not on this page · raw message

Signature · 1 line
1.5.3.3

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#2 lore raw

Re: [PATCH] US-122L Driver, v0

Am Mittwoch, 7. November 2007 schrieb Karsten Wiese:

Quoted 10 lines

Hi,

attached patches implement a simple driver for the tascam us-122l. Everything except midi out works i think ;-)

For USB 2.0 you also have to aply

	http://marc.info/?l=linux-usb-devel&m=119444869917392&w=2

named "ehci-hcd: complete iso urbs ASAP ..."

Will post a jack driver to use it soon.

Attached. It has to be cleaned up alot, but it works. Patch taken against jackd 0.103.

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#3 lore raw

Re: [PATCH] US-122L Driver, v0

In reply to Karsten Wiese

sound/usb/usbaudio.h enum quirk_type { · line 165

Quoted 9 lines · Karsten Wiese

attached patches implement a simple driver for the tascam us-122l.

sound/usb/usbaudio.h +1 -1
 	QUIRK_AUDIO_EDIROL_UA700_UA25,
 	QUIRK_AUDIO_EDIROL_UA1000,
 	QUIRK_AUDIO_EDIROL_UA101,
-
+	QUIRK_MIDI_US122L,
 	QUIRK_TYPE_COUNT
 };

Why not put it together with the other QUIRK_MIDI_ symbols?

sound/usb/usbmidi.c int snd_usb_create_midi_interface(struct snd_usb_audio* chip, · line 1685

Quoted 6 lines
sound/usb/usbmidi.c +2 -0
 		break;
+	case QUIRK_MIDI_US122L:
+		umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
 	case QUIRK_MIDI_FIXED_ENDPOINT:
 		memcpy(&endpoints[0], quirk->data,
 		       sizeof(struct snd_usb_midi_endpoint_info));

Aaargh! If you really want to do it this way, at least add a "/* fall through */" so that it's obvious that this is not a mistake.

Regards,
Clemens

#4 lore raw

Re: [PATCH] US-122L Driver, v0

Am Donnerstag, 8. November 2007 schrieb Clemens Ladisch:

Quoted 12 lines

Why not put it together with the other QUIRK_MIDI_ symbols?

sound/usb/usbmidi.c int snd_usb_create_midi_interface(struct snd_usb_audio* chip, · line 1685

Quoted 6 lines
sound/usb/usbmidi.c +2 -0
 		break;
+	case QUIRK_MIDI_US122L:
+		umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
 	case QUIRK_MIDI_FIXED_ENDPOINT:
 		memcpy(&endpoints[0], quirk->data,
 		       sizeof(struct snd_usb_midi_endpoint_info));

Aaargh! If you really want to do it this way, at least add a "/* fall through */" so that it's obvious that this is not a mistake.

Done here, will post again.

I'd like you to try the
"ehci-hcd: complete iso urbs ASAP for number_of_packets != (n * 8) also"
http://marc.info/?l=linux-usb-devel&m=119444869917392&w=2
with snd-usb-audio.

snd-usb-audio uses number_of_packets equalling multiples of 8 currently when transfering over USB 2.0, no?
If so, with above patch that "multiples of 8"-thing could go away and snd-usb-audio could achieve up to 8 times less userspace-activation-jitter in low latency setups like 128 frames/period, 2 periods.

thanks,
Karsten

#5 lore raw

Re: [PATCH] US-122L Driver, v0

Quoted 5 lines · Karsten Wiese

snd-usb-audio uses number_of_packets equalling multiples of 8 currently when transfering over USB 2.0, no?
If so, with above patch that "multiples of 8"-thing could go away and snd-usb-audio could achieve up to 8 times less userspace-activation-jitter in low latency setups like 128 frames/period, 2 periods.

I'm using the 8-microframes alignment because ehci-hcd configures the controller to interrupt at most every 1 ms. It has a module option to change this, but as long as snd-usb-audio cannot detect a lower setting, it would not make sense to allow shorter periods.

Regards,
Clemens

#6 lore raw

Re: [PATCH] US-122L Driver, v0

Am Donnerstag, 8. November 2007 schrieb Clemens Ladisch:

Quoted 5 lines

I'm using the 8-microframes alignment because ehci-hcd configures the controller to interrupt at most every 1 ms. It has a module option to change this, but as long as snd-usb-audio cannot detect a lower setting, it would not make sense to allow shorter periods.

For an 1ms interrupt alignment ehci-hcd must be loaded with non-default parameters.
In ehci-hcd.c as of 2.6.23:

/* Initial IRQ latency: faster than hw default */
static int log2_irq_thresh = 0; // 0 to 6
module_param (log2_irq_thresh, int, S_IRUGO); MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");

That makes a default IRQ latency of 1/8ms. This is also what I see here:
The us122l driver works with 64frames/period 2 periods at 96kHz sample rate. Thats about 1400 interupts/s. Wouldn't be possible with 1ms alignment.

kind regards,
Karsten