Skip to contentSkip to conversation

Linking ALSA sequencer kernel clients to their device

PATCH Martin Koegler 2 participants 3 messages Feb 11, 2016 – Feb 12, 2016 Sound

lore
Share & tools
lore
#0 lore raw

In my domain, people use multiple identical USB-MIDI keyboards - each is assigned a different functions. They expect to kept this mapping across reboots.

The sound card number are dynamic, therefore they can change after reboots. The only stable device ID is the sysfs path of the used USB port.

This means, that I need to associate a kernel sequencer client number with a sysfs device.

rawmidi devices expose the card number via IOCTLs, which allows to find the corresponding device in sysfs.

The sequencer provides no identifing data. Chromium works around this issue by scanning rawmidi as well as sequencer devices and matching them by using assumtions, how the kernel register sequencer devices.

So I thought about exposing the missing information via sysfs:

Quoted 1 line

From 776dd02d8d7eae89c2699be27eedf81eaf690a0e Mon Sep 17 00:00:00 2001

From: Martin Koegler <[email protected]>
Date: Mon, 8 Feb 2016 22:53:42 +0100
Subject: [PATCH] Provide sequencer client number via sysfs

Provides the missing link between real hardware devices and kernel sequencer clients.

---
 sound/core/seq/seq_midi.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
sound/core/seq/seq_midi.c +38 -0
index 5dd0ee2..0d807e2 100644
@@ -271,6 +271,31 @@ static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
 	snd_midi_event_free(msynth->parser);
 }
 
+static ssize_t
+client_number_show_attr(struct device *dev,
+		     struct device_attribute *attr, char *buf)
+{
+	struct snd_seq_device *device = container_of(dev, struct snd_seq_device, dev);
+	struct snd_card *card = device->card;
+	struct seq_midisynth_client *client;
+
+	client = card ? synths[card->number] : NULL;
+	return snprintf(buf, PAGE_SIZE, "%i\n", client ? client->seq_client : -1);
+}
+
+static DEVICE_ATTR(client, S_IRUGO, client_number_show_attr, NULL);
+
+static ssize_t
+device_number_show_attr(struct device *dev,
+		     struct device_attribute *attr, char *buf)
+{
+	struct snd_seq_device *device = container_of(dev, struct snd_seq_device, dev);
+
+	return snprintf(buf, PAGE_SIZE, "%i\n", device->device);
+}
+
+static DEVICE_ATTR(device, S_IRUGO, device_number_show_attr, NULL);
+
 /* register new midi synth port */
 static int
 snd_seq_midisynth_probe(struct device *_dev)
@@ -287,6 +312,7 @@ snd_seq_midisynth_probe(struct device *_dev)
 	struct snd_card *card = dev->card;
 	int device = dev->device;
 	unsigned int input_count = 0, output_count = 0;
+	int error;
 
 	if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
 		return -EINVAL;
@@ -332,6 +358,16 @@ snd_seq_midisynth_probe(struct device *_dev)
 			kfree(info);
 			return -ENOMEM;
 		}
+		error = device_create_file(&dev->dev, &dev_attr_client);
+		if (error >= 0)
+			error = device_create_file(&dev->dev, &dev_attr_device);
+		if (error < 0) {
+			snd_seq_delete_kernel_client(client->seq_client);
+			kfree(client);
+			mutex_unlock(&register_mutex);
+			kfree(info);
+			return error;
+		}
 	}
 
 	msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
@@ -451,6 +487,8 @@ snd_seq_midisynth_remove(struct device *_dev)
 	kfree(msynth);
 	client->num_ports--;
 	if (client->num_ports <= 0) {
+		device_remove_file(&dev->dev, &dev_attr_client);
+		device_remove_file(&dev->dev, &dev_attr_device);
 		snd_seq_delete_kernel_client(client->seq_client);
 		synths[card->number] = NULL;
 		kfree(client);
Signature · 1 line
2.1.4
#1 lore raw
Quoted 11 lines · Martin Koegler

In my domain, people use multiple identical USB-MIDI keyboards - each is assigned a different functions. They expect to kept this mapping across reboots.

The sound card number are dynamic, therefore they can change after reboots. The only stable device ID is the sysfs path of the used USB port.

This means, that I need to associate a kernel sequencer client number with a sysfs device.

Quoted 6 lines

rawmidi devices expose the card number via IOCTLs, which allows to find the corresponding device in sysfs.

The sequencer provides no identifing data. Chromium works around this issue by scanning rawmidi as well as sequencer devices and matching them by using assumtions, how the kernel register sequencer devices.

This information wouldn't be wrong in the commit message.

Quoted 1 line

So I thought about exposing the missing information via sysfs:

The ioctl API (and the library API on top of it) already exists. Why do you want to introduce another kind of API that cannot be used for virtual devices?

Just add the card number to snd_seq_client_info. (For kernel drivers; user clients might have the PID here while we're at it.)

Regards,
Clemens

#2 lore raw
Quoted 7 lines · Clemens Ladisch
Quoted 3 lines · Martin Koegler

The sequencer provides no identifing data. Chromium works around this issue by scanning rawmidi as well as sequencer devices and matching them by using assumtions, how the kernel register sequencer devices.

This information wouldn't be wrong in the commit message.

Quoted 8 lines
Quoted 1 line

So I thought about exposing the missing information via sysfs:

The ioctl API (and the library API on top of it) already exists. Why do you want to introduce another kind of API that cannot be used for virtual devices?

Just add the card number to snd_seq_client_info. (For kernel drivers; user clients might have the PID here while we're at it.)

You mean something like that:

Quoted 1 line

From d2245ccf22318cc47f8dd104dee670b03b24f018 Mon Sep 17 00:00:00 2001

From: Martin Koegler <[email protected]>
Date: Thu, 11 Feb 2016 20:07:20 +0100
Subject: [PATCH] Provide card number / PID via sequencer client info

rawmidi devices expose the card number via IOCTLs, which allows to find the corresponding device in sysfs.

The sequencer provides no identifing data. Chromium works around this issue by scanning rawmidi as well as sequencer devices and matching them by using assumtions, how the kernel register sequencer devices.

This changes adds support for exposing the card number for kernel clients as well as the PID for user client.

The minor of the API version is changed to distinguish between the zero initialised reserved field and card number 0.

---
 include/uapi/sound/asequencer.h |  5 +++--
 sound/core/seq/seq_clientmgr.c  | 18 ++++++++++++++++++
 sound/core/seq/seq_clientmgr.h  |  2 ++
 3 files changed, 23 insertions(+), 2 deletions(-)
include/uapi/sound/asequencer.h +3 -2
index 5a5fa49..4726579 100644
@@ -25,7 +25,7 @@
 #include <sound/asound.h>
 
 /** version of the sequencer */
-#define SNDRV_SEQ_VERSION SNDRV_PROTOCOL_VERSION (1, 0, 1)
+#define SNDRV_SEQ_VERSION SNDRV_PROTOCOL_VERSION (1, 0, 2)
 
 /**
  * definition of sequencer event types
@@ -357,7 +357,8 @@ struct snd_seq_client_info {
 	unsigned char event_filter[32];	/* event filter bitmap */
 	int num_ports;			/* RO: number of ports */
 	int event_lost;			/* number of lost events */
-	char reserved[64];		/* for future use */
+	int owner;			/* RO: card number[kernel] / PID[user] */
+	char reserved[64 - sizeof(int)]; /* for future use */
 };
 
 
sound/core/seq/seq_clientmgr.c +18 -0
index 58e79e0..ac07ab7 100644
@@ -364,6 +364,7 @@ static int snd_seq_open(struct inode *inode, struct file *file)
 	/* fill client data */
 	user->file = file;
 	sprintf(client->name, "Client-%d", c);
+	client->data.user.owner = get_pid(task_pid(current));
 
 	/* make others aware this new client */
 	snd_seq_system_client_ev_client_start(c);
@@ -380,6 +381,7 @@ static int snd_seq_release(struct inode *inode, struct file *file)
 		seq_free_client(client);
 		if (client->data.user.fifo)
 			snd_seq_fifo_delete(&client->data.user.fifo);
+		put_pid(client->data.user.owner);
 		kfree(client);
 	}
 
@@ -1197,6 +1199,21 @@ static void get_client_info(struct snd_seq_client *cptr,
 	info->event_lost = cptr->event_lost;
 	memcpy(info->event_filter, cptr->event_filter, 32);
 	info->num_ports = cptr->num_ports;
+
+	switch (cptr->type) {
+	case USER_CLIENT:
+		info->owner = pid_vnr(cptr->data.user.owner);
+		break;
+
+	case KERNEL_CLIENT:
+		info->owner = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1;
+		break;
+
+	default:
+		info->owner = -1;
+		break;
+	}
+
 	memset(info->reserved, 0, sizeof(info->reserved));
 }
 
@@ -2271,6 +2288,7 @@ int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
 
 	client->accept_input = 1;
 	client->accept_output = 1;
+	client->data.kernel.card = card;
 		
 	va_start(args, name_fmt);
 	vsnprintf(client->name, sizeof(client->name), name_fmt, args);
sound/core/seq/seq_clientmgr.h +2 -0
index 20f0a72..031462e 100644
@@ -33,6 +33,7 @@
 struct snd_seq_user_client {
 	struct file *file;	/* file struct of client */
 	/* ... */
+	struct pid* owner;
 	
 	/* fifo */
 	struct snd_seq_fifo *fifo;	/* queue for incoming events */
@@ -41,6 +42,7 @@ struct snd_seq_user_client {
 
 struct snd_seq_kernel_client {
 	/* ... */
+	struct snd_card* card;
 };
 
 
Signature · 1 line
2.1.4