Skip to contentSkip to conversation

stateful reconnect with snd-usb-audio

DISCUSSION Sam Revitch 3 participants 22 messages Jun 12, 2006 – Jun 22, 2006

lore
Share & tools
lore
#0 lore raw

Hi everybody,

I'd like to solicit the ALSA brain trust for tips and opinions on how one might go about implementing a new feature. With some (most?) of the ALSA driver code base, it is possible, or at least someone intended for it to be possible to leave an MP3 player or such running across a suspend-to-RAM or suspend-to-disk operation, and have it pick up exactly where it left off on resume. To see this in action is just cool, there's no getting around it. A notable exception to this is the snd-usb-audio driver, under which devices seem to be disconnected and re-enumerated as entirely new cards, rather than having their state explicitly backed up and restored with the assumption that the hardware will be present on resume.

I searched hard and couldn't find any prior efforts to address this problem, so it seemed worthwhile to ask about.

There are two possibilities that seem promising:

1. Rework snd-usb-audio to support a timely reconnect operation. If a card is unplugged and reconnected within some period of time, its prior state will be restored and it will be reassociated with open file handles. Active playback/capture operations may continue without reopening the device, with only the reconnection/reconfiguration delay.

2. Support reconnection at the alsa-lib layer. I couldn't find anything that does this already. Somehow all playback/capture file descriptors would need to be closed on command and reopened/reconfigured when a replacement device is identified. Anybody know of an easy way to do this with dmix? Otherwise it seems like alsa-lib would be stuck funneling playback through an aserver/jackd process and that would be undesirable.

Opinions? Does one seem like it would be more robust, more reusable, or more work than the other?

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Thanks.
-Sam Revitch

#1 lore raw
Quoted 2 lines · Sam Revitch

Opinions? Does one seem like it would be more robust, more reusable, or more work than the other?

How other drivers work? Network, video? I've not checked the code, but I guess they don't care, too.

Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Do you suggest a better solution? The original idea was to free slot (card number) before a next probe to guarantee that reconnect will grab this slot.

Thanks,
Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

#2 lore raw
In reply to Sam Revitch

At Mon, 12 Jun 2006 14:05:15 -0700,

Quoted 35 lines · Sam Revitch

Hi everybody,

I'd like to solicit the ALSA brain trust for tips and opinions on how one might go about implementing a new feature. With some (most?) of the ALSA driver code base, it is possible, or at least someone intended for it to be possible to leave an MP3 player or such running across a suspend-to-RAM or suspend-to-disk operation, and have it pick up exactly where it left off on resume. To see this in action is just cool, there's no getting around it. A notable exception to this is the snd-usb-audio driver, under which devices seem to be disconnected and re-enumerated as entirely new cards, rather than having their state explicitly backed up and restored with the assumption that the hardware will be present on resume.

I searched hard and couldn't find any prior efforts to address this problem, so it seemed worthwhile to ask about.

There are two possibilities that seem promising:

1. Rework snd-usb-audio to support a timely reconnect operation. If a card is unplugged and reconnected within some period of time, its prior state will be restored and it will be reassociated with open file handles. Active playback/capture operations may continue without reopening the device, with only the reconnection/reconfiguration delay.

2. Support reconnection at the alsa-lib layer. I couldn't find anything that does this already. Somehow all playback/capture file descriptors would need to be closed on command and reopened/reconfigured when a replacement device is identified. Anybody know of an easy way to do this with dmix? Otherwise it seems like alsa-lib would be stuck funneling playback through an aserver/jackd process and that would be undesirable.

Quoted 2 lines

Opinions? Does one seem like it would be more robust, more reusable, or more work than the other?

The recent usb drivers have suspend/resume callbacks. So, just implementing them might suffice.

Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Takashi

#3 lore raw
Quoted 11 lines · Takashi Iwai
Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Quoted 2 lines

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Skipping the wait means calling snd_card_free() in last fops->release(), but as I mentioned, the card slot is still in use in this time. So it is not guaranteed that the reconnected devices will have same IDs. I think that best way is to fix applications to handle such situations as fast as possible and eventually add an event mechanism (if missing) to notify applications as fast as possible that device is no more available.

BTW: It's good that the USB layer handles susped/resume in midlevel.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

#4 lore raw

At Wed, 14 Jun 2006 13:37:14 +0200 (CEST),

Quoted 14 lines · Jaroslav Kysela
Quoted 11 lines · Takashi Iwai
Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Quoted 4 lines
Quoted 2 lines

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Skipping the wait means calling snd_card_free() in last fops->release(),

No, snd_card_free() is called in disconnect callback.

Takashi

#5 lore raw

At Wed, 14 Jun 2006 14:19:14 +0200,

Quoted 17 lines · I wrote:

At Wed, 14 Jun 2006 13:37:14 +0200 (CEST),

Quoted 14 lines · Jaroslav Kysela
Quoted 11 lines · Takashi Iwai
Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Skipping the wait means calling snd_card_free() in last fops->release(),

No, snd_card_free() is called in disconnect callback.

I was too desnse. You meant "a part of jobs of snd_card_free()" is going to be executed in the last release?

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

Takashi

#6 lore raw
Quoted 22 lines · Takashi Iwai

At Wed, 14 Jun 2006 14:19:14 +0200,

Quoted 20 lines · I wrote:

At Wed, 14 Jun 2006 13:37:14 +0200 (CEST),

Quoted 17 lines · Jaroslav Kysela
Quoted 13 lines · Takashi Iwai
Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Skipping the wait means calling snd_card_free() in last fops->release(),

No, snd_card_free() is called in disconnect callback.

I was too desnse. You meant "a part of jobs of snd_card_free()" is going to be executed in the last release?

Yes, exactly.

Quoted 3 lines

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

Yes, but we have a problem when an user remove an hotplug device, if apps do not check the state of file descriptor. After disconnect, read()/write() syscalls return immediately with -ENODEV and poll() with POLLERR | POLLNVAL, thus it should be possible to close the disconnected devices ASAP.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

#7 lore raw

At Wed, 14 Jun 2006 15:51:01 +0200 (CEST),

Quoted 38 lines · Jaroslav Kysela
Quoted 22 lines · Takashi Iwai

At Wed, 14 Jun 2006 14:19:14 +0200,

Quoted 20 lines · I wrote:

At Wed, 14 Jun 2006 13:37:14 +0200 (CEST),

Quoted 17 lines · Jaroslav Kysela
Quoted 13 lines · Takashi Iwai
Quoted 10 lines

As another individual pointed out a few months ago, there are some other problems with snd-usb-audio similar to those described above. If a certain usb audio device has open file handles and is disconnected or the system is suspended, snd_usb_audio_disconnect()->snd_card_free() will block in the khubd context while ALSA waits for the file handles to be closed, preventing khubd from doing anything else useful like processing probe requests. Using snd_card_free_in_thread() can be just as bad as it uses schedule_work() and blocks one of the "events" threads. A hang in either context can cause problems for a resume-from-RAM operation.

Hm, this has to be fixed. Just skipping the wait for file close will solve the problem?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Skipping the wait means calling snd_card_free() in last fops->release(),

No, snd_card_free() is called in disconnect callback.

I was too desnse. You meant "a part of jobs of snd_card_free()" is going to be executed in the last release?

Yes, exactly.

Quoted 3 lines

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

Quoted 5 lines

Yes, but we have a problem when an user remove an hotplug device, if apps do not check the state of file descriptor. After disconnect, read()/write() syscalls return immediately with -ENODEV and poll() with POLLERR | POLLNVAL, thus it should be possible to close the disconnected devices ASAP.

Then such a thing should be implemented in alsa-lib, not in app codes.

Takashi

#8 lore raw
Quoted 8 lines · Takashi Iwai
Quoted 5 lines

Yes, but we have a problem when an user remove an hotplug device, if apps do not check the state of file descriptor. After disconnect, read()/write() syscalls return immediately with -ENODEV and poll() with POLLERR | POLLNVAL, thus it should be possible to close the disconnected devices ASAP.

Then such a thing should be implemented in alsa-lib, not in app codes.

I'm not sure how alsa-lib can decide if device has been disconnected permanently or temporary. But if you mean "closing the file descriptor in the first time when -ENODEV occurs", it makes a lot of sense for alsa-lib and yes, it should be there.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

#9 lore raw
Quoted 8 lines

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

I'm not sure how alsa-lib can decide if device has been disconnected permanently or temporary. But if you mean "closing the file descriptor in the first time when -ENODEV occurs", it makes a lot of sense for alsa-lib and yes, it should be there.

Thanks for replies.

The problem with usb_audio_disconnect() getting stuck isn't specific to suspend, although the part of it that I care about right now is. That function shouldn't be waiting for handles to be closed, as somebody can execute a DoS attack on khubd by sending SIGSTOP to any process that has the sound driver open.

Perhaps the snd_device dev_disconnect and dev_unregister routines for the various abstract layers need to be changed so that sysfs/procfs entries are removed in dev_disconnect? This way it might be possible to release a slot without freeing anything that might be in use.

As for implementing suspend/resume methods for snd-usb-audio, doesn't that require explicit support from the device for retaining state in some sort of low power 0.5mA mode? I don't know whether the card I'm using even supports that.

-Sam Revitch

#10 lore raw

For fun, here's a rough proof of concept patch to resolve the snd-usb-audio khubd hang. It alters the behavior of snd_device dev_disconnect methods for abstract layers control, info, and pcm to also unregister sysfs/procfs entries. It adds dev_disconnect methods to layers hwdep and timer to similar effect, and alters the behavior in the pcm_oss and mixer_oss modules through their special interfaces. Omitted are the seq and rawmidi layers, which I haven't looked at. It moves the calls to snd_info_unregister() and snd_info_card_free() from snd_card_free() to snd_card_disconnect(). Finally, it breaks snd_card_free() into smaller functions, and leaves the wait-for-last-close semantic intact, but adds an snd_card_free_when_closed() function that causes the interesting parts of snd_card_free() to be called in the context of snd_card_file_remove().

Thanks.
-Sam Revitch

core/control.c +11 -9
diff -r 08577d0b45ef core/control.c
@@ -1375,6 +1375,11 @@ static int snd_ctl_dev_disconnect(struct
 	struct snd_card *card = device->device_data;
 	struct list_head *flist;
 	struct snd_ctl_file *ctl;
+	int err, cardnum;
+
+	snd_assert(card != NULL, return -ENXIO);
+	cardnum = card->number;
+	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
 
 	down_read(&card->controls_rwsem);
 	list_for_each(flist, &card->ctl_files) {
@@ -1383,6 +1388,10 @@ static int snd_ctl_dev_disconnect(struct
 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
 	}
 	up_read(&card->controls_rwsem);
+
+	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
+					 card, -1)) < 0)
+		return err;
 	return 0;
 }
 
@@ -1408,15 +1417,8 @@ static int snd_ctl_dev_free(struct snd_d
  */
 static int snd_ctl_dev_unregister(struct snd_device *device)
 {
-	struct snd_card *card = device->device_data;
-	int err, cardnum;
-
-	snd_assert(card != NULL, return -ENXIO);
-	cardnum = card->number;
-	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
-	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
-					 card, -1)) < 0)
-		return err;
+	if (device->state == SNDRV_DEV_REGISTERED)
+		(void) snd_ctl_dev_disconnect(device);
 	return snd_ctl_dev_free(device);
 }
 
core/hwdep.c +12 -2
diff -r 08577d0b45ef core/hwdep.c
@@ -42,6 +42,7 @@ static int snd_hwdep_free(struct snd_hwd
 static int snd_hwdep_free(struct snd_hwdep *hwdep);
 static int snd_hwdep_dev_free(struct snd_device *device);
 static int snd_hwdep_dev_register(struct snd_device *device);
+static int snd_hwdep_dev_disconnect(struct snd_device *device);
 static int snd_hwdep_dev_unregister(struct snd_device *device);
 
 
@@ -353,6 +354,7 @@ int snd_hwdep_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_hwdep_dev_free,
 		.dev_register = snd_hwdep_dev_register,
+		.dev_disconnect = snd_hwdep_dev_disconnect,
 		.dev_unregister = snd_hwdep_dev_unregister
 	};
 
@@ -439,7 +441,7 @@ static int snd_hwdep_dev_register(struct
 	return 0;
 }
 
-static int snd_hwdep_dev_unregister(struct snd_device *device)
+static int snd_hwdep_dev_disconnect(struct snd_device *device)
 {
 	struct snd_hwdep *hwdep = device->device_data;
 
@@ -454,8 +456,16 @@ static int snd_hwdep_dev_unregister(stru
 		snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
 #endif
 	snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
-	list_del(&hwdep->list);
+	list_del_init(&hwdep->list);
 	mutex_unlock(&register_mutex);
+	return 0;
+}
+
+static int snd_hwdep_dev_unregister(struct snd_device *device)
+{
+	struct snd_hwdep *hwdep = device->device_data;
+	snd_assert(hwdep != NULL, return -ENXIO);
+	(void) snd_hwdep_dev_disconnect(device);
 	return snd_hwdep_free(hwdep);
 }
 
core/info.c +17 -10
diff -r 08577d0b45ef core/info.c
@@ -820,6 +820,21 @@ struct snd_info_entry *snd_info_create_c
 
 EXPORT_SYMBOL(snd_info_create_card_entry);
 
+static int snd_info_disconnect(struct snd_info_entry *entry)
+{
+	struct proc_dir_entry *root;
+	if (!entry->disconnected) {
+		snd_assert(entry->p != NULL, return -ENXIO);
+		root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
+		snd_assert(root, return -ENXIO);
+		mutex_lock(&info_mutex);
+		snd_remove_proc_entry(root, entry->p);
+		mutex_unlock(&info_mutex);
+		entry->disconnected = 1;
+	}
+	return 0;
+}
+
 static int snd_info_dev_free_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
@@ -836,8 +851,7 @@ static int snd_info_dev_disconnect_entry
 static int snd_info_dev_disconnect_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
-	entry->disconnected = 1;
-	return 0;
+	return snd_info_disconnect(entry);
 }
 
 static int snd_info_dev_unregister_entry(struct snd_device *device)
@@ -952,16 +966,9 @@ EXPORT_SYMBOL(snd_info_register);
  */
 int snd_info_unregister(struct snd_info_entry * entry)
 {
-	struct proc_dir_entry *root;
-
 	if (! entry)
 		return 0;
-	snd_assert(entry->p != NULL, return -ENXIO);
-	root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
-	snd_assert(root, return -ENXIO);
-	mutex_lock(&info_mutex);
-	snd_remove_proc_entry(root, entry->p);
-	mutex_unlock(&info_mutex);
+	(void) snd_info_disconnect(entry);
 	snd_info_free_entry(entry);
 	return 0;
 }
core/init.c +92 -52

177 lines not on this page · raw message

core/oss/mixer_oss.c +8 -10
diff -r 08577d0b45ef core/oss/mixer_oss.c
@@ -1313,22 +1313,20 @@ static int snd_mixer_oss_notify_handler(
 		card->mixer_oss = mixer;
 		snd_mixer_oss_build(mixer);
 		snd_mixer_oss_proc_init(mixer);
-	} else if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) {
-		mixer = card->mixer_oss;
-		if (mixer == NULL || !mixer->oss_dev_alloc)
-			return 0;
-		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		mixer->oss_dev_alloc = 0;
-	} else {		/* free */
+	} else {
 		mixer = card->mixer_oss;
 		if (mixer == NULL)
 			return 0;
+		if (mixer->oss_dev_alloc) {
 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
-		snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
+			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
 #endif
-		if (mixer->oss_dev_alloc)
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		snd_mixer_oss_proc_done(mixer);
+			snd_mixer_oss_proc_done(mixer);
+			mixer->oss_dev_alloc = 0;
+		}
+		if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
+			return 0;
 		return snd_mixer_oss_free1(mixer);
 	}
 	return 0;
core/oss/pcm_oss.c +6 -8
diff -r 08577d0b45ef core/oss/pcm_oss.c
@@ -2929,14 +2929,6 @@ static int snd_pcm_oss_disconnect_minor(
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
 						  pcm->card, 1);
 		}
-	}
-	return 0;
-}
-
-static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
-{
-	snd_pcm_oss_disconnect_minor(pcm);
-	if (pcm->oss.reg) {
 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
@@ -2945,6 +2937,12 @@ static int snd_pcm_oss_unregister_minor(
 		pcm->oss.reg = 0;
 		snd_pcm_oss_proc_done(pcm);
 	}
+	return 0;
+}
+
+static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
+{
+	snd_pcm_oss_disconnect_minor(pcm);
 	return 0;
 }
 
core/pcm.c +45 -27

94 lines not on this page · raw message

core/timer.c +11 -0
diff -r 08577d0b45ef core/timer.c
@@ -88,6 +88,7 @@ static int snd_timer_free(struct snd_tim
 static int snd_timer_free(struct snd_timer *timer);
 static int snd_timer_dev_free(struct snd_device *device);
 static int snd_timer_dev_register(struct snd_device *device);
+static int snd_timer_dev_disconnect(struct snd_device *device);
 static int snd_timer_dev_unregister(struct snd_device *device);
 
 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
@@ -772,6 +773,7 @@ int snd_timer_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_timer_dev_free,
 		.dev_register = snd_timer_dev_register,
+		.dev_disconnect = snd_timer_dev_disconnect,
 		.dev_unregister = snd_timer_dev_unregister
 	};
 
@@ -884,6 +886,15 @@ static int snd_timer_unregister(struct s
 	list_del(&timer->device_list);
 	mutex_unlock(&register_mutex);
 	return snd_timer_free(timer);
+}
+
+static int snd_timer_dev_disconnect(struct snd_device *device)
+{
+	struct snd_timer *timer = device->device_data;
+	mutex_lock(&register_mutex);
+	list_del_init(&timer->device_list);
+	mutex_unlock(&register_mutex);
+	return 0;
 }
 
 static int snd_timer_dev_unregister(struct snd_device *device)
include/core.h +2 -0
diff -r 08577d0b45ef include/core.h
@@ -131,6 +131,7 @@ struct snd_card {
 								state */
 	spinlock_t files_lock;		/* lock the files for this card */
 	int shutdown;			/* this card is going down */
+	int free_on_last_close;		/* free in context of file_release */
 	wait_queue_head_t shutdown_sleep;
 	struct work_struct free_workq;	/* for free in workqueue */
 	struct device *dev;
@@ -246,6 +247,7 @@ struct snd_card *snd_card_new(int idx, c
 			 struct module *module, int extra_size);
 int snd_card_disconnect(struct snd_card *card);
 int snd_card_free(struct snd_card *card);
+int snd_card_free_when_closed(struct snd_card *card);
 int snd_card_free_in_thread(struct snd_card *card);
 int snd_card_register(struct snd_card *card);
 int snd_card_info_init(void);
usb/usbaudio.c +1 -1
diff -r 08577d0b45ef usb/usbaudio.c
@@ -3469,7 +3469,7 @@ static void snd_usb_audio_disconnect(str
 		}
 		usb_chip[chip->index] = NULL;
 		mutex_unlock(&register_mutex);
-		snd_card_free(card);
+		snd_card_free_when_closed(card);
 	} else {
 		mutex_unlock(&register_mutex);
 	}

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#11 lore raw

At Thu, 15 Jun 2006 03:04:08 -0700,

Quoted 15 lines · Sam Revitch

For fun, here's a rough proof of concept patch to resolve the snd-usb-audio khubd hang. It alters the behavior of snd_device dev_disconnect methods for abstract layers control, info, and pcm to also unregister sysfs/procfs entries. It adds dev_disconnect methods to layers hwdep and timer to similar effect, and alters the behavior in the pcm_oss and mixer_oss modules through their special interfaces.
Omitted are the seq and rawmidi layers, which I haven't looked at.
It moves the calls to snd_info_unregister() and snd_info_card_free() from snd_card_free() to snd_card_disconnect(). Finally, it breaks snd_card_free() into smaller functions, and leaves the wait-for-last-close semantic intact, but adds an snd_card_free_when_closed() function that causes the interesting parts of snd_card_free() to be called in the context of snd_card_file_remove().

The patch looks fine to me (as I also wanted to implement the same stuff right now :) Did you check whether it works with usb-audio actually?

If it's confirmed to work, we can commit it together with the changes for other components. Then snd_card_free_in_thread() should be replaced with snd_card_free_when_closed().

thanks,

Takashi

Quoted 567 lines

Thanks.
-Sam Revitch
[2 disconnect.patch <text/x-patch; ISO-8859-1 (base64)>] diff -r 08577d0b45ef core/control.c

core/control.c +11 -9
@@ -1375,6 +1375,11 @@ static int snd_ctl_dev_disconnect(struct
 	struct snd_card *card = device->device_data;
 	struct list_head *flist;
 	struct snd_ctl_file *ctl;
+	int err, cardnum;
+
+	snd_assert(card != NULL, return -ENXIO);
+	cardnum = card->number;
+	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
 
 	down_read(&card->controls_rwsem);
 	list_for_each(flist, &card->ctl_files) {
@@ -1383,6 +1388,10 @@ static int snd_ctl_dev_disconnect(struct
 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
 	}
 	up_read(&card->controls_rwsem);
+
+	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
+					 card, -1)) < 0)
+		return err;
 	return 0;
 }
 
@@ -1408,15 +1417,8 @@ static int snd_ctl_dev_free(struct snd_d
  */
 static int snd_ctl_dev_unregister(struct snd_device *device)
 {
-	struct snd_card *card = device->device_data;
-	int err, cardnum;
-
-	snd_assert(card != NULL, return -ENXIO);
-	cardnum = card->number;
-	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
-	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
-					 card, -1)) < 0)
-		return err;
+	if (device->state == SNDRV_DEV_REGISTERED)
+		(void) snd_ctl_dev_disconnect(device);
 	return snd_ctl_dev_free(device);
 }
 

diff -r 08577d0b45ef core/hwdep.c

core/hwdep.c +12 -2
@@ -42,6 +42,7 @@ static int snd_hwdep_free(struct snd_hwd
 static int snd_hwdep_free(struct snd_hwdep *hwdep);
 static int snd_hwdep_dev_free(struct snd_device *device);
 static int snd_hwdep_dev_register(struct snd_device *device);
+static int snd_hwdep_dev_disconnect(struct snd_device *device);
 static int snd_hwdep_dev_unregister(struct snd_device *device);
 
 
@@ -353,6 +354,7 @@ int snd_hwdep_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_hwdep_dev_free,
 		.dev_register = snd_hwdep_dev_register,
+		.dev_disconnect = snd_hwdep_dev_disconnect,
 		.dev_unregister = snd_hwdep_dev_unregister
 	};
 
@@ -439,7 +441,7 @@ static int snd_hwdep_dev_register(struct
 	return 0;
 }
 
-static int snd_hwdep_dev_unregister(struct snd_device *device)
+static int snd_hwdep_dev_disconnect(struct snd_device *device)
 {
 	struct snd_hwdep *hwdep = device->device_data;
 
@@ -454,8 +456,16 @@ static int snd_hwdep_dev_unregister(stru
 		snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
 #endif
 	snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
-	list_del(&hwdep->list);
+	list_del_init(&hwdep->list);
 	mutex_unlock(&register_mutex);
+	return 0;
+}
+
+static int snd_hwdep_dev_unregister(struct snd_device *device)
+{
+	struct snd_hwdep *hwdep = device->device_data;
+	snd_assert(hwdep != NULL, return -ENXIO);
+	(void) snd_hwdep_dev_disconnect(device);
 	return snd_hwdep_free(hwdep);
 }
 

diff -r 08577d0b45ef core/info.c

core/info.c +17 -10
@@ -820,6 +820,21 @@ struct snd_info_entry *snd_info_create_c
 
 EXPORT_SYMBOL(snd_info_create_card_entry);
 
+static int snd_info_disconnect(struct snd_info_entry *entry)
+{
+	struct proc_dir_entry *root;
+	if (!entry->disconnected) {
+		snd_assert(entry->p != NULL, return -ENXIO);
+		root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
+		snd_assert(root, return -ENXIO);
+		mutex_lock(&info_mutex);
+		snd_remove_proc_entry(root, entry->p);
+		mutex_unlock(&info_mutex);
+		entry->disconnected = 1;
+	}
+	return 0;
+}
+
 static int snd_info_dev_free_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
@@ -836,8 +851,7 @@ static int snd_info_dev_disconnect_entry
 static int snd_info_dev_disconnect_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
-	entry->disconnected = 1;
-	return 0;
+	return snd_info_disconnect(entry);
 }
 
 static int snd_info_dev_unregister_entry(struct snd_device *device)
@@ -952,16 +966,9 @@ EXPORT_SYMBOL(snd_info_register);
  */
 int snd_info_unregister(struct snd_info_entry * entry)
 {
-	struct proc_dir_entry *root;
-
 	if (! entry)
 		return 0;
-	snd_assert(entry->p != NULL, return -ENXIO);
-	root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
-	snd_assert(root, return -ENXIO);
-	mutex_lock(&info_mutex);
-	snd_remove_proc_entry(root, entry->p);
-	mutex_unlock(&info_mutex);
+	(void) snd_info_disconnect(entry);
 	snd_info_free_entry(entry);
 	return 0;
 }

diff -r 08577d0b45ef core/init.c

core/init.c +92 -52
@@ -310,70 +310,104 @@ int snd_card_disconnect(struct snd_card 
 	if (err < 0)
 		snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
 
-	return 0;	
-}
-
-EXPORT_SYMBOL(snd_card_disconnect);
-
-/**
- *  snd_card_free - frees given soundcard structure
- *  @card: soundcard structure
- *
- *  This function releases the soundcard structure and the all assigned
- *  devices automatically.  That is, you don't have to release the devices
- *  by yourself.
- *
- *  Returns zero. Frees all associated devices and frees the control
- *  interface associated to given soundcard.
- */
-int snd_card_free(struct snd_card *card)
-{
-	struct snd_shutdown_f_ops *s_f_ops;
-
-	if (card == NULL)
-		return -EINVAL;
-	mutex_lock(&snd_card_mutex);
-	snd_cards[card->number] = NULL;
-	mutex_unlock(&snd_card_mutex);
-
-#ifdef CONFIG_PM
-	wake_up(&card->power_sleep);
-#endif
-	/* wait, until all devices are ready for the free operation */
-	wait_event(card->shutdown_sleep, card->files == NULL);
-
-#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
-	if (snd_mixer_oss_notify_callback)
-		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
-#endif
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (pre)\n");
-		/* Fatal, but this situation should never occur */
-	}
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (normal)\n");
-		/* Fatal, but this situation should never occur */
-	}
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (post)\n");
-		/* Fatal, but this situation should never occur */
-	}
-	if (card->private_free)
-		card->private_free(card);
 	snd_info_unregister(card->proc_id);
 	if (snd_info_card_free(card) < 0) {
 		snd_printk(KERN_WARNING "unable to free card info\n");
 		/* Not fatal error */
 	}
+
+	return 0;	
+}
+
+EXPORT_SYMBOL(snd_card_disconnect);
+
+/**
+ *  snd_card_free - frees given soundcard structure
+ *  @card: soundcard structure
+ *
+ *  This function releases the soundcard structure and the all assigned
+ *  devices automatically.  That is, you don't have to release the devices
+ *  by yourself.
+ *
+ *  Returns zero. Frees all associated devices and frees the control
+ *  interface associated to given soundcard.
+ */
+static int snd_card_do_free(struct snd_card *card)
+{
+	struct snd_shutdown_f_ops *s_f_ops;
+
+#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
+	if (snd_mixer_oss_notify_callback)
+		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
+#endif
+	if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
+		snd_printk(KERN_ERR "unable to free all devices (pre)\n");
+		/* Fatal, but this situation should never occur */
+	}
+	if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
+		snd_printk(KERN_ERR "unable to free all devices (normal)\n");
+		/* Fatal, but this situation should never occur */
+	}
+	if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
+		snd_printk(KERN_ERR "unable to free all devices (post)\n");
+		/* Fatal, but this situation should never occur */
+	}
+	if (card->private_free)
+		card->private_free(card);
 	while (card->s_f_ops) {
 		s_f_ops = card->s_f_ops;
 		card->s_f_ops = s_f_ops->next;
 		kfree(s_f_ops);
 	}
+	kfree(card);
+	return 0;
+}
+
+static int snd_card_free_prepare(struct snd_card *card)
+{
+	if (card == NULL)
+		return -EINVAL;
+	(void) snd_card_disconnect(card);
 	mutex_lock(&snd_card_mutex);
+	snd_cards[card->number] = NULL;
 	snd_cards_lock &= ~(1 << card->number);
 	mutex_unlock(&snd_card_mutex);
-	kfree(card);
+#ifdef CONFIG_PM
+	wake_up(&card->power_sleep);
+#endif
+	return 0;
+}
+
+int snd_card_free_when_closed(struct snd_card *card)
+{
+	int free_now = 0;
+	int ret = snd_card_free_prepare(card);
+	if (ret)
+		return ret;
+
+	spin_lock(&card->files_lock);
+	if (card->files == NULL)
+		free_now = 1;
+	else
+		card->free_on_last_close = 1;
+	spin_unlock(&card->files_lock);
+
+	if (free_now)
+		snd_card_do_free(card);
+	return 0;
+}
+
+EXPORT_SYMBOL(snd_card_free_when_closed);
+
+int snd_card_free(struct snd_card *card)
+{
+	int ret = snd_card_free_prepare(card);
+	if (ret)
+		return ret;
+
+	/* wait, until all devices are ready for the free operation */
+	wait_event(card->shutdown_sleep, card->files == NULL);
+	snd_card_do_free(card);
 	return 0;
 }
 
@@ -717,6 +751,7 @@ int snd_card_file_remove(struct snd_card
 int snd_card_file_remove(struct snd_card *card, struct file *file)
 {
 	struct snd_monitor_file *mfile, *pfile = NULL;
+	int last_close = 0;
 
 	spin_lock(&card->files_lock);
 	mfile = card->files;
@@ -731,9 +766,14 @@ int snd_card_file_remove(struct snd_card
 		pfile = mfile;
 		mfile = mfile->next;
 	}
+	if (card->files == NULL)
+		last_close = 1;
 	spin_unlock(&card->files_lock);
-	if (card->files == NULL)
+	if (last_close) {
 		wake_up(&card->shutdown_sleep);
+		if (card->free_on_last_close)
+			snd_card_do_free(card);
+	}
 	if (!mfile) {
 		snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
 		return -ENOENT;

diff -r 08577d0b45ef core/oss/mixer_oss.c

core/oss/mixer_oss.c +8 -10
@@ -1313,22 +1313,20 @@ static int snd_mixer_oss_notify_handler(
 		card->mixer_oss = mixer;
 		snd_mixer_oss_build(mixer);
 		snd_mixer_oss_proc_init(mixer);
-	} else if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) {
-		mixer = card->mixer_oss;
-		if (mixer == NULL || !mixer->oss_dev_alloc)
-			return 0;
-		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		mixer->oss_dev_alloc = 0;
-	} else {		/* free */
+	} else {
 		mixer = card->mixer_oss;
 		if (mixer == NULL)
 			return 0;
+		if (mixer->oss_dev_alloc) {
 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
-		snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
+			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
 #endif
-		if (mixer->oss_dev_alloc)
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		snd_mixer_oss_proc_done(mixer);
+			snd_mixer_oss_proc_done(mixer);
+			mixer->oss_dev_alloc = 0;
+		}
+		if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
+			return 0;
 		return snd_mixer_oss_free1(mixer);
 	}
 	return 0;

diff -r 08577d0b45ef core/oss/pcm_oss.c

core/oss/pcm_oss.c +6 -8
@@ -2929,14 +2929,6 @@ static int snd_pcm_oss_disconnect_minor(
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
 						  pcm->card, 1);
 		}
-	}
-	return 0;
-}
-
-static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
-{
-	snd_pcm_oss_disconnect_minor(pcm);
-	if (pcm->oss.reg) {
 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
@@ -2945,6 +2937,12 @@ static int snd_pcm_oss_unregister_minor(
 		pcm->oss.reg = 0;
 		snd_pcm_oss_proc_done(pcm);
 	}
+	return 0;
+}
+
+static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
+{
+	snd_pcm_oss_disconnect_minor(pcm);
 	return 0;
 }
 

diff -r 08577d0b45ef core/pcm.c

core/pcm.c +45 -27
@@ -968,39 +968,29 @@ static int snd_pcm_dev_register(struct s
 	return 0;
 }
 
-static int snd_pcm_dev_disconnect(struct snd_device *device)
-{
-	struct snd_pcm *pcm = device->device_data;
+static int snd_pcm_do_disconnect(struct snd_pcm *pcm, int notify)
+{
 	struct list_head *list;
 	struct snd_pcm_substream *substream;
-	int cidx;
-
-	mutex_lock(&register_mutex);
+	int cidx, devtype;
+
+	if (list_empty(&pcm->list))
+		return 0;
+
 	list_del_init(&pcm->list);
 	for (cidx = 0; cidx < 2; cidx++)
 		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
 			if (substream->runtime)
 				substream->runtime->status->state = SNDRV_PCM_STATE_DISCONNECTED;
-	list_for_each(list, &snd_pcm_notify_list) {
-		struct snd_pcm_notify *notify;
-		notify = list_entry(list, struct snd_pcm_notify, list);
-		notify->n_disconnect(pcm);
-	}
-	mutex_unlock(&register_mutex);
-	return 0;
-}
-
-static int snd_pcm_dev_unregister(struct snd_device *device)
-{
-	int cidx, devtype;
-	struct snd_pcm_substream *substream;
-	struct list_head *list;
-	struct snd_pcm *pcm = device->device_data;
-
-	snd_assert(pcm != NULL, return -ENXIO);
-	mutex_lock(&register_mutex);
-	list_del(&pcm->list);
+	if (notify) {
+		list_for_each(list, &snd_pcm_notify_list) {
+			struct snd_pcm_notify *notify;
+			notify = list_entry(list, struct snd_pcm_notify, list);
+			notify->n_disconnect(pcm);
+		}
+	}
 	for (cidx = 0; cidx < 2; cidx++) {
+		int err;
 		devtype = -1;
 		switch (cidx) {
 		case SNDRV_PCM_STREAM_PLAYBACK:
@@ -1010,10 +1000,38 @@ static int snd_pcm_dev_unregister(struct
 			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
 			break;
 		}
-		snd_unregister_device(devtype, pcm->card, pcm->device);
+		err = snd_unregister_device(devtype, pcm->card, pcm->device);
+		if (err < 0)
+			snd_printk(KERN_ERR "snd_unregister_device %d/%d = %i\n",
+					pcm->card->number, devtype, err);
+	}
+	return 0;
+}
+
+static int snd_pcm_dev_disconnect(struct snd_device *device)
+{
+	struct snd_pcm *pcm = device->device_data;
+	int err = 0;
+
+	mutex_lock(&register_mutex);
+	err = snd_pcm_do_disconnect(pcm, 1);
+	mutex_unlock(&register_mutex);
+	return err;
+}
+
+static int snd_pcm_dev_unregister(struct snd_device *device)
+{
+	int cidx;
+	struct snd_pcm_substream *substream;
+	struct list_head *list;
+	struct snd_pcm *pcm = device->device_data;
+
+	snd_assert(pcm != NULL, return -ENXIO);
+	mutex_lock(&register_mutex);
+	snd_pcm_do_disconnect(pcm, 0);
+	for (cidx = 0; cidx < 2; cidx++)
 		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
 			snd_pcm_timer_done(substream);
-	}
 	list_for_each(list, &snd_pcm_notify_list) {
 		struct snd_pcm_notify *notify;
 		notify = list_entry(list, struct snd_pcm_notify, list);

diff -r 08577d0b45ef core/timer.c

core/timer.c +11 -0
@@ -88,6 +88,7 @@ static int snd_timer_free(struct snd_tim
 static int snd_timer_free(struct snd_timer *timer);
 static int snd_timer_dev_free(struct snd_device *device);
 static int snd_timer_dev_register(struct snd_device *device);
+static int snd_timer_dev_disconnect(struct snd_device *device);
 static int snd_timer_dev_unregister(struct snd_device *device);
 
 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
@@ -772,6 +773,7 @@ int snd_timer_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_timer_dev_free,
 		.dev_register = snd_timer_dev_register,
+		.dev_disconnect = snd_timer_dev_disconnect,
 		.dev_unregister = snd_timer_dev_unregister
 	};
 
@@ -884,6 +886,15 @@ static int snd_timer_unregister(struct s
 	list_del(&timer->device_list);
 	mutex_unlock(&register_mutex);
 	return snd_timer_free(timer);
+}
+
+static int snd_timer_dev_disconnect(struct snd_device *device)
+{
+	struct snd_timer *timer = device->device_data;
+	mutex_lock(&register_mutex);
+	list_del_init(&timer->device_list);
+	mutex_unlock(&register_mutex);
+	return 0;
 }
 
 static int snd_timer_dev_unregister(struct snd_device *device)

diff -r 08577d0b45ef include/core.h

include/core.h +2 -0
@@ -131,6 +131,7 @@ struct snd_card {
 								state */
 	spinlock_t files_lock;		/* lock the files for this card */
 	int shutdown;			/* this card is going down */
+	int free_on_last_close;		/* free in context of file_release */
 	wait_queue_head_t shutdown_sleep;
 	struct work_struct free_workq;	/* for free in workqueue */
 	struct device *dev;
@@ -246,6 +247,7 @@ struct snd_card *snd_card_new(int idx, c
 			 struct module *module, int extra_size);
 int snd_card_disconnect(struct snd_card *card);
 int snd_card_free(struct snd_card *card);
+int snd_card_free_when_closed(struct snd_card *card);
 int snd_card_free_in_thread(struct snd_card *card);
 int snd_card_register(struct snd_card *card);
 int snd_card_info_init(void);

diff -r 08577d0b45ef usb/usbaudio.c

usb/usbaudio.c +1 -1
@@ -3469,7 +3469,7 @@ static void snd_usb_audio_disconnect(str
 		}
 		usb_chip[chip->index] = NULL;
 		mutex_unlock(&register_mutex);
-		snd_card_free(card);
+		snd_card_free_when_closed(card);
 	} else {
 		mutex_unlock(&register_mutex);
 	}

[3 <text/plain; us-ascii (7bit)>]

[4 <text/plain; us-ascii (7bit)>]
_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#12 lore raw
Quoted 7 lines · Takashi Iwai

The patch looks fine to me (as I also wanted to implement the same stuff right now :) Did you check whether it works with usb-audio actually?

If it's confirmed to work, we can commit it together with the changes for other components. Then snd_card_free_in_thread() should be replaced with snd_card_free_when_closed().

Apparently the last patch had some problems tearing down the /proc/asound/cardX/pcmYZ entries at disconnect time. The attached one should fix that. The original one was developed on the driver bundled with a recent -mm kernel that didn't seem to create those entries. Anyway, it seems to work as advertised with the usb-audio driver, at least in my configuration. The whole /proc/asound/cardX tree goes away immediately when the card is unplugged without hanging khubd, and with an old handle held open, playback seems to work through the new device instance on the same slot when the card is plugged back in.

Now, there has got to be some way to make usb-audio retain its snd_card structure at disconnect time. Perhaps it could optionally leave things hanging for some period of time before calling snd_card_disconnect(), leaving a window for the same device to be reconnected...? Does this sound too kludgy?

Thanks..
-Sam Revitch

core/control.c +11 -9
diff -r 08577d0b45ef core/control.c
@@ -1375,6 +1375,11 @@ static int snd_ctl_dev_disconnect(struct
 	struct snd_card *card = device->device_data;
 	struct list_head *flist;
 	struct snd_ctl_file *ctl;
+	int err, cardnum;
+
+	snd_assert(card != NULL, return -ENXIO);
+	cardnum = card->number;
+	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
 
 	down_read(&card->controls_rwsem);
 	list_for_each(flist, &card->ctl_files) {
@@ -1383,6 +1388,10 @@ static int snd_ctl_dev_disconnect(struct
 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
 	}
 	up_read(&card->controls_rwsem);
+
+	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
+					 card, -1)) < 0)
+		return err;
 	return 0;
 }
 
@@ -1408,15 +1417,8 @@ static int snd_ctl_dev_free(struct snd_d
  */
 static int snd_ctl_dev_unregister(struct snd_device *device)
 {
-	struct snd_card *card = device->device_data;
-	int err, cardnum;
-
-	snd_assert(card != NULL, return -ENXIO);
-	cardnum = card->number;
-	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
-	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
-					 card, -1)) < 0)
-		return err;
+	if (device->state == SNDRV_DEV_REGISTERED)
+		(void) snd_ctl_dev_disconnect(device);
 	return snd_ctl_dev_free(device);
 }
 
core/hwdep.c +12 -2
diff -r 08577d0b45ef core/hwdep.c
@@ -42,6 +42,7 @@ static int snd_hwdep_free(struct snd_hwd
 static int snd_hwdep_free(struct snd_hwdep *hwdep);
 static int snd_hwdep_dev_free(struct snd_device *device);
 static int snd_hwdep_dev_register(struct snd_device *device);
+static int snd_hwdep_dev_disconnect(struct snd_device *device);
 static int snd_hwdep_dev_unregister(struct snd_device *device);
 
 
@@ -353,6 +354,7 @@ int snd_hwdep_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_hwdep_dev_free,
 		.dev_register = snd_hwdep_dev_register,
+		.dev_disconnect = snd_hwdep_dev_disconnect,
 		.dev_unregister = snd_hwdep_dev_unregister
 	};
 
@@ -439,7 +441,7 @@ static int snd_hwdep_dev_register(struct
 	return 0;
 }
 
-static int snd_hwdep_dev_unregister(struct snd_device *device)
+static int snd_hwdep_dev_disconnect(struct snd_device *device)
 {
 	struct snd_hwdep *hwdep = device->device_data;
 
@@ -454,8 +456,16 @@ static int snd_hwdep_dev_unregister(stru
 		snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
 #endif
 	snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
-	list_del(&hwdep->list);
+	list_del_init(&hwdep->list);
 	mutex_unlock(&register_mutex);
+	return 0;
+}
+
+static int snd_hwdep_dev_unregister(struct snd_device *device)
+{
+	struct snd_hwdep *hwdep = device->device_data;
+	snd_assert(hwdep != NULL, return -ENXIO);
+	(void) snd_hwdep_dev_disconnect(device);
 	return snd_hwdep_free(hwdep);
 }
 
core/info.c +19 -10
diff -r 08577d0b45ef core/info.c
@@ -820,6 +820,23 @@ struct snd_info_entry *snd_info_create_c
 
 EXPORT_SYMBOL(snd_info_create_card_entry);
 
+int snd_info_disconnect(struct snd_info_entry *entry)
+{
+	struct proc_dir_entry *root;
+	if (!entry->disconnected) {
+		snd_assert(entry->p != NULL, return -ENXIO);
+		root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
+		snd_assert(root, return -ENXIO);
+		mutex_lock(&info_mutex);
+		snd_remove_proc_entry(root, entry->p);
+		mutex_unlock(&info_mutex);
+		entry->disconnected = 1;
+	}
+	return 0;
+}
+
+EXPORT_SYMBOL(snd_info_disconnect);
+
 static int snd_info_dev_free_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
@@ -836,8 +853,7 @@ static int snd_info_dev_disconnect_entry
 static int snd_info_dev_disconnect_entry(struct snd_device *device)
 {
 	struct snd_info_entry *entry = device->device_data;
-	entry->disconnected = 1;
-	return 0;
+	return snd_info_disconnect(entry);
 }
 
 static int snd_info_dev_unregister_entry(struct snd_device *device)
@@ -952,16 +968,9 @@ EXPORT_SYMBOL(snd_info_register);
  */
 int snd_info_unregister(struct snd_info_entry * entry)
 {
-	struct proc_dir_entry *root;
-
 	if (! entry)
 		return 0;
-	snd_assert(entry->p != NULL, return -ENXIO);
-	root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
-	snd_assert(root, return -ENXIO);
-	mutex_lock(&info_mutex);
-	snd_remove_proc_entry(root, entry->p);
-	mutex_unlock(&info_mutex);
+	(void) snd_info_disconnect(entry);
 	snd_info_free_entry(entry);
 	return 0;
 }
core/init.c +92 -52

177 lines not on this page · raw message

core/oss/mixer_oss.c +8 -10
diff -r 08577d0b45ef core/oss/mixer_oss.c
@@ -1313,22 +1313,20 @@ static int snd_mixer_oss_notify_handler(
 		card->mixer_oss = mixer;
 		snd_mixer_oss_build(mixer);
 		snd_mixer_oss_proc_init(mixer);
-	} else if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) {
-		mixer = card->mixer_oss;
-		if (mixer == NULL || !mixer->oss_dev_alloc)
-			return 0;
-		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		mixer->oss_dev_alloc = 0;
-	} else {		/* free */
+	} else {
 		mixer = card->mixer_oss;
 		if (mixer == NULL)
 			return 0;
+		if (mixer->oss_dev_alloc) {
 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
-		snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
+			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
 #endif
-		if (mixer->oss_dev_alloc)
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		snd_mixer_oss_proc_done(mixer);
+			snd_mixer_oss_proc_done(mixer);
+			mixer->oss_dev_alloc = 0;
+		}
+		if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
+			return 0;
 		return snd_mixer_oss_free1(mixer);
 	}
 	return 0;
core/oss/pcm_oss.c +6 -8
diff -r 08577d0b45ef core/oss/pcm_oss.c
@@ -2929,14 +2929,6 @@ static int snd_pcm_oss_disconnect_minor(
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
 						  pcm->card, 1);
 		}
-	}
-	return 0;
-}
-
-static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
-{
-	snd_pcm_oss_disconnect_minor(pcm);
-	if (pcm->oss.reg) {
 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
@@ -2945,6 +2937,12 @@ static int snd_pcm_oss_unregister_minor(
 		pcm->oss.reg = 0;
 		snd_pcm_oss_proc_done(pcm);
 	}
+	return 0;
+}
+
+static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
+{
+	snd_pcm_oss_disconnect_minor(pcm);
 	return 0;
 }
 
core/pcm.c +79 -27

141 lines not on this page · raw message

core/pcm_memory.c +10 -0
diff -r 08577d0b45ef core/pcm_memory.c
@@ -255,6 +255,16 @@ int snd_pcm_lib_preallocate_pages(struct
 
 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
 
+
+void snd_pcm_lib_disconnect(struct snd_pcm_substream *substream)
+{
+#ifdef CONFIG_SND_VERBOSE_PROCFS
+	if (substream->proc_prealloc_entry)
+		snd_info_disconnect(substream->proc_prealloc_entry);
+#endif
+}
+
+
 /**
  * snd_pcm_lib_preallocate_pages_for_all - pre-allocation for continous memory type (all substreams)
  * @pcm: the pcm instance
core/timer.c +11 -0
diff -r 08577d0b45ef core/timer.c
@@ -88,6 +88,7 @@ static int snd_timer_free(struct snd_tim
 static int snd_timer_free(struct snd_timer *timer);
 static int snd_timer_dev_free(struct snd_device *device);
 static int snd_timer_dev_register(struct snd_device *device);
+static int snd_timer_dev_disconnect(struct snd_device *device);
 static int snd_timer_dev_unregister(struct snd_device *device);
 
 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
@@ -772,6 +773,7 @@ int snd_timer_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_timer_dev_free,
 		.dev_register = snd_timer_dev_register,
+		.dev_disconnect = snd_timer_dev_disconnect,
 		.dev_unregister = snd_timer_dev_unregister
 	};
 
@@ -884,6 +886,15 @@ static int snd_timer_unregister(struct s
 	list_del(&timer->device_list);
 	mutex_unlock(&register_mutex);
 	return snd_timer_free(timer);
+}
+
+static int snd_timer_dev_disconnect(struct snd_device *device)
+{
+	struct snd_timer *timer = device->device_data;
+	mutex_lock(&register_mutex);
+	list_del_init(&timer->device_list);
+	mutex_unlock(&register_mutex);
+	return 0;
 }
 
 static int snd_timer_dev_unregister(struct snd_device *device)
include/core.h +2 -0
diff -r 08577d0b45ef include/core.h
@@ -131,6 +131,7 @@ struct snd_card {
 								state */
 	spinlock_t files_lock;		/* lock the files for this card */
 	int shutdown;			/* this card is going down */
+	int free_on_last_close;		/* free in context of file_release */
 	wait_queue_head_t shutdown_sleep;
 	struct work_struct free_workq;	/* for free in workqueue */
 	struct device *dev;
@@ -246,6 +247,7 @@ struct snd_card *snd_card_new(int idx, c
 			 struct module *module, int extra_size);
 int snd_card_disconnect(struct snd_card *card);
 int snd_card_free(struct snd_card *card);
+int snd_card_free_when_closed(struct snd_card *card);
 int snd_card_free_in_thread(struct snd_card *card);
 int snd_card_register(struct snd_card *card);
 int snd_card_info_init(void);
include/info.h +1 -0
diff -r 08577d0b45ef include/info.h
@@ -123,6 +123,7 @@ int snd_info_card_register(struct snd_ca
 int snd_info_card_register(struct snd_card * card);
 int snd_info_card_free(struct snd_card * card);
 int snd_info_register(struct snd_info_entry * entry);
+int snd_info_disconnect(struct snd_info_entry * entry);
 int snd_info_unregister(struct snd_info_entry * entry);
 
 /* for card drivers */
usb/usbaudio.c +1 -1
diff -r 08577d0b45ef usb/usbaudio.c
@@ -3469,7 +3469,7 @@ static void snd_usb_audio_disconnect(str
 		}
 		usb_chip[chip->index] = NULL;
 		mutex_unlock(&register_mutex);
-		snd_card_free(card);
+		snd_card_free_when_closed(card);
 	} else {
 		mutex_unlock(&register_mutex);
 	}

_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#14 lore raw

At Sat, 17 Jun 2006 02:01:39 -0700,

Quoted 6 lines · Sam Revitch

The last patch has other problems -- races in mixer_oss/pcm_oss, build failures with disable-verbose-procfs, etc.. :-( To avoid spamming this list with another file attachment the latest one is posted here:

http://lsb.blogdns.net/files/snd-disconnect.patch

The patch still has a problem with ac97_codec.c that has also own lowlevel device. opl3 and opl4 have to be fixed.

Looking through the patch, I found that unregister callback can be replaced simply with disconnect + free. This will remove the complexity a bit. I'll try to make another patch based on yours.

Takashi

#15 lore raw

At Tue, 20 Jun 2006 14:21:52 +0200,

Quoted 8 lines · I wrote:

At Sat, 17 Jun 2006 02:01:39 -0700,

Quoted 5 lines · Sam Revitch

The last patch has other problems -- races in mixer_oss/pcm_oss, build failures with disable-verbose-procfs, etc.. :-( To avoid spamming this list with another file attachment the latest one is posted here:

Quoted 8 lines
Quoted 1 line

http://lsb.blogdns.net/files/snd-disconnect.patch

The patch still has a problem with ac97_codec.c that has also own lowlevel device. opl3 and opl4 have to be fixed.

Looking through the patch, I found that unregister callback can be replaced simply with disconnect + free. This will remove the complexity a bit. I'll try to make another patch based on yours.

The resultant patch is below.

The following additional items are changed:

- dev_unregister is removed and replaced with dev_disconnect + dev_free.

- snd_info_unregister() is removed, replaced with snd_info_free_entry(). The NULL checks in the caller is optimized out in many places.

- linked list is introduced in struct snd_info_entry so that a single disconnect call does all disconnections of the proc tree. (Thus snd_info_disconnect() is changed to static)

- info->disconnect flag is removed. info->p is checked instead.

Takashi

core/control.c +9 -18
diff -r 333ca526b6e8 core/control.c
@@ -1375,6 +1375,11 @@ static int snd_ctl_dev_disconnect(struct
 	struct snd_card *card = device->device_data;
 	struct list_head *flist;
 	struct snd_ctl_file *ctl;
+	int err, cardnum;
+
+	snd_assert(card != NULL, return -ENXIO);
+	cardnum = card->number;
+	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
 
 	down_read(&card->controls_rwsem);
 	list_for_each(flist, &card->ctl_files) {
@@ -1383,6 +1388,10 @@ static int snd_ctl_dev_disconnect(struct
 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
 	}
 	up_read(&card->controls_rwsem);
+
+	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
+					 card, -1)) < 0)
+		return err;
 	return 0;
 }
 
@@ -1404,23 +1413,6 @@ static int snd_ctl_dev_free(struct snd_d
 }
 
 /*
- * de-registration of the control device
- */
-static int snd_ctl_dev_unregister(struct snd_device *device)
-{
-	struct snd_card *card = device->device_data;
-	int err, cardnum;
-
-	snd_assert(card != NULL, return -ENXIO);
-	cardnum = card->number;
-	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
-	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
-					 card, -1)) < 0)
-		return err;
-	return snd_ctl_dev_free(device);
-}
-
-/*
  * create control core:
  * called from init.c
  */
@@ -1430,7 +1422,6 @@ int snd_ctl_create(struct snd_card *card
 		.dev_free = snd_ctl_dev_free,
 		.dev_register =	snd_ctl_dev_register,
 		.dev_disconnect = snd_ctl_dev_disconnect,
-		.dev_unregister = snd_ctl_dev_unregister
 	};
 
 	snd_assert(card != NULL, return -ENXIO);
core/device.c +9 -11
diff -r 333ca526b6e8 core/device.c
@@ -71,7 +71,7 @@ EXPORT_SYMBOL(snd_device_new);
  * @device_data: the data pointer to release
  *
  * Removes the device from the list on the card and invokes the
- * callback, dev_unregister or dev_free, corresponding to the state.
+ * callbacks, dev_disconnect and dev_free, corresponding to the state.
  * Then release the device.
  *
  * Returns zero if successful, or a negative error code on failure or if the
@@ -90,16 +90,14 @@ int snd_device_free(struct snd_card *car
 			continue;
 		/* unlink */
 		list_del(&dev->list);
-		if ((dev->state == SNDRV_DEV_REGISTERED ||
-		     dev->state == SNDRV_DEV_DISCONNECTED) &&
-		    dev->ops->dev_unregister) {
-			if (dev->ops->dev_unregister(dev))
-				snd_printk(KERN_ERR "device unregister failure\n");
-		} else {
-			if (dev->ops->dev_free) {
-				if (dev->ops->dev_free(dev))
-					snd_printk(KERN_ERR "device free failure\n");
-			}
+		if (dev->state == SNDRV_DEV_REGISTERED &&
+		    dev->ops->dev_disconnect)
+			if (dev->ops->dev_disconnect(dev))
+				snd_printk(KERN_ERR
+					   "device disconnect failure\n");
+		if (dev->ops->dev_free) {
+			if (dev->ops->dev_free(dev))
+				snd_printk(KERN_ERR "device free failure\n");
 		}
 		kfree(dev);
 		return 0;
core/hwdep.c +6 -6
diff -r 333ca526b6e8 core/hwdep.c
@@ -42,7 +42,7 @@ static int snd_hwdep_free(struct snd_hwd
 static int snd_hwdep_free(struct snd_hwdep *hwdep);
 static int snd_hwdep_dev_free(struct snd_device *device);
 static int snd_hwdep_dev_register(struct snd_device *device);
-static int snd_hwdep_dev_unregister(struct snd_device *device);
+static int snd_hwdep_dev_disconnect(struct snd_device *device);
 
 
 static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
@@ -353,7 +353,7 @@ int snd_hwdep_new(struct snd_card *card,
 	static struct snd_device_ops ops = {
 		.dev_free = snd_hwdep_dev_free,
 		.dev_register = snd_hwdep_dev_register,
-		.dev_unregister = snd_hwdep_dev_unregister
+		.dev_disconnect = snd_hwdep_dev_disconnect,
 	};
 
 	snd_assert(rhwdep != NULL, return -EINVAL);
@@ -439,7 +439,7 @@ static int snd_hwdep_dev_register(struct
 	return 0;
 }
 
-static int snd_hwdep_dev_unregister(struct snd_device *device)
+static int snd_hwdep_dev_disconnect(struct snd_device *device)
 {
 	struct snd_hwdep *hwdep = device->device_data;
 
@@ -454,9 +454,9 @@ static int snd_hwdep_dev_unregister(stru
 		snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
 #endif
 	snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
-	list_del(&hwdep->list);
+	list_del_init(&hwdep->list);
 	mutex_unlock(&register_mutex);
-	return snd_hwdep_free(hwdep);
+	return 0;
 }
 
 #ifdef CONFIG_PROC_FS
@@ -497,7 +497,7 @@ static void __init snd_hwdep_proc_init(v
 
 static void __exit snd_hwdep_proc_done(void)
 {
-	snd_info_unregister(snd_hwdep_proc_entry);
+	snd_info_free_entry(snd_hwdep_proc_entry);
 }
 #else /* !CONFIG_PROC_FS */
 #define snd_hwdep_proc_init()
core/info.c +51 -53

181 lines not on this page · raw message

core/info_oss.c +2 -4
diff -r 333ca526b6e8 core/info_oss.c
@@ -131,10 +131,8 @@ int snd_info_minor_register(void)
 
 int snd_info_minor_unregister(void)
 {
-	if (snd_sndstat_proc_entry) {
-		snd_info_unregister(snd_sndstat_proc_entry);
-		snd_sndstat_proc_entry = NULL;
-	}
+	snd_info_free_entry(snd_sndstat_proc_entry);
+	snd_sndstat_proc_entry = NULL;
 	return 0;
 }
 
core/init.c +58 -18

126 lines not on this page · raw message

core/oss/mixer_oss.c +9 -13
diff -r 333ca526b6e8 core/oss/mixer_oss.c
@@ -1194,10 +1194,8 @@ static void snd_mixer_oss_proc_init(stru
 
 static void snd_mixer_oss_proc_done(struct snd_mixer_oss *mixer)
 {
-	if (mixer->proc_entry) {
-		snd_info_unregister(mixer->proc_entry);
-		mixer->proc_entry = NULL;
-	}
+	snd_info_free_entry(mixer->proc_entry);
+	mixer->proc_entry = NULL;
 }
 #else /* !CONFIG_PROC_FS */
 #define snd_mixer_oss_proc_init(mix)
@@ -1313,21 +1311,19 @@ static int snd_mixer_oss_notify_handler(
 		card->mixer_oss = mixer;
 		snd_mixer_oss_build(mixer);
 		snd_mixer_oss_proc_init(mixer);
-	} else if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT) {
-		mixer = card->mixer_oss;
-		if (mixer == NULL || !mixer->oss_dev_alloc)
-			return 0;
-		snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
-		mixer->oss_dev_alloc = 0;
-	} else {		/* free */
+	} else {
 		mixer = card->mixer_oss;
 		if (mixer == NULL)
 			return 0;
+		if (mixer->oss_dev_alloc) {
 #ifdef SNDRV_OSS_INFO_DEV_MIXERS
-		snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
+			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIXERS, mixer->card->number);
 #endif
-		if (mixer->oss_dev_alloc)
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER, mixer->card, 0);
+			mixer->oss_dev_alloc = 0;
+		}
+		if (cmd == SND_MIXER_OSS_NOTIFY_DISCONNECT)
+			return 0;
 		snd_mixer_oss_proc_done(mixer);
 		return snd_mixer_oss_free1(mixer);
 	}
core/oss/pcm_oss.c +11 -15
diff -r 333ca526b6e8 core/oss/pcm_oss.c
@@ -2844,11 +2844,9 @@ static void snd_pcm_oss_proc_done(struct
 	int stream;
 	for (stream = 0; stream < 2; ++stream) {
 		struct snd_pcm_str *pstr = &pcm->streams[stream];
-		if (pstr->oss.proc_entry) {
-			snd_info_unregister(pstr->oss.proc_entry);
-			pstr->oss.proc_entry = NULL;
-			snd_pcm_oss_proc_free_setup_list(pstr);
-		}
+		snd_info_free_entry(pstr->oss.proc_entry);
+		pstr->oss.proc_entry = NULL;
+		snd_pcm_oss_proc_free_setup_list(pstr);
 	}
 }
 #else /* !CONFIG_SND_VERBOSE_PROCFS */
@@ -2929,22 +2927,20 @@ static int snd_pcm_oss_disconnect_minor(
 			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
 						  pcm->card, 1);
 		}
-	}
-	return 0;
-}
-
-static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
-{
-	snd_pcm_oss_disconnect_minor(pcm);
-	if (pcm->oss.reg) {
 		if (dsp_map[pcm->card->number] == (int)pcm->device) {
 #ifdef SNDRV_OSS_INFO_DEV_AUDIO
 			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
 #endif
 		}
 		pcm->oss.reg = 0;
-		snd_pcm_oss_proc_done(pcm);
-	}
+	}
+	return 0;
+}
+
+static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
+{
+	snd_pcm_oss_disconnect_minor(pcm);
+	snd_pcm_oss_proc_done(pcm);
 	return 0;
 }
 
core/pcm.c +37 -66

178 lines not on this page · raw message

core/pcm_memory.c +1 -1
diff -r 333ca526b6e8 core/pcm_memory.c
@@ -101,7 +101,7 @@ int snd_pcm_lib_preallocate_free(struct 
 {
 	snd_pcm_lib_preallocate_dma_free(substream);
 #ifdef CONFIG_SND_VERBOSE_PROCFS
-	snd_info_unregister(substream->proc_prealloc_entry);
+	snd_info_free_entry(substream->proc_prealloc_entry);
 	substream->proc_prealloc_entry = NULL;
 #endif
 	return 0;
core/rawmidi.c +9 -26
diff -r 333ca526b6e8 core/rawmidi.c
@@ -55,7 +55,6 @@ static int snd_rawmidi_dev_free(struct s
 static int snd_rawmidi_dev_free(struct snd_device *device);
 static int snd_rawmidi_dev_register(struct snd_device *device);
 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
-static int snd_rawmidi_dev_unregister(struct snd_device *device);
 
 static LIST_HEAD(snd_rawmidi_devices);
 static DEFINE_MUTEX(register_mutex);
@@ -1426,7 +1425,6 @@ int snd_rawmidi_new(struct snd_card *car
 		.dev_free = snd_rawmidi_dev_free,
 		.dev_register = snd_rawmidi_dev_register,
 		.dev_disconnect = snd_rawmidi_dev_disconnect,
-		.dev_unregister = snd_rawmidi_dev_unregister
 	};
 
 	snd_assert(rrawmidi != NULL, return -EINVAL);
@@ -1479,6 +1477,14 @@ static int snd_rawmidi_free(struct snd_r
 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
 {
 	snd_assert(rmidi != NULL, return -ENXIO);	
+
+	snd_info_free_entry(rmidi->proc_entry);
+	rmidi->proc_entry = NULL;
+	mutex_lock(&register_mutex);
+	if (rmidi->ops && rmidi->ops->dev_unregister)
+		rmidi->ops->dev_unregister(rmidi);
+	mutex_unlock(&register_mutex);
+
 	snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
 	snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
 	if (rmidi->private_free)
@@ -1587,21 +1593,6 @@ static int snd_rawmidi_dev_disconnect(st
 
 	mutex_lock(&register_mutex);
 	list_del_init(&rmidi->list);
-	mutex_unlock(&register_mutex);
-	return 0;
-}
-
-static int snd_rawmidi_dev_unregister(struct snd_device *device)
-{
-	struct snd_rawmidi *rmidi = device->device_data;
-
-	snd_assert(rmidi != NULL, return -ENXIO);
-	mutex_lock(&register_mutex);
-	list_del(&rmidi->list);
-	if (rmidi->proc_entry) {
-		snd_info_unregister(rmidi->proc_entry);
-		rmidi->proc_entry = NULL;
-	}
 #ifdef CONFIG_SND_OSSEMUL
 	if (rmidi->ossreg) {
 		if ((int)rmidi->device == midi_map[rmidi->card->number]) {
@@ -1615,17 +1606,9 @@ static int snd_rawmidi_dev_unregister(st
 		rmidi->ossreg = 0;
 	}
 #endif /* CONFIG_SND_OSSEMUL */
-	if (rmidi->ops && rmidi->ops->dev_unregister)
-		rmidi->ops->dev_unregister(rmidi);
 	snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
 	mutex_unlock(&register_mutex);
-#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
-	if (rmidi->seq_dev) {
-		snd_device_free(rmidi->card, rmidi->seq_dev);
-		rmidi->seq_dev = NULL;
-	}
-#endif
-	return snd_rawmidi_free(rmidi);
+	return 0;
 }
 
 /**
core/rtctimer.c +1 -1
diff -r 333ca526b6e8 core/rtctimer.c
@@ -156,7 +156,7 @@ static void __exit rtctimer_exit(void)
 static void __exit rtctimer_exit(void)
 {
 	if (rtctimer) {
-		snd_timer_global_unregister(rtctimer);
+		snd_timer_global_free(rtctimer);
 		rtctimer = NULL;
 	}
 }
core/seq/oss/seq_oss.c +1 -2
diff -r 333ca526b6e8 core/seq/oss/seq_oss.c
@@ -303,8 +303,7 @@ static void
 static void
 unregister_proc(void)
 {
-	if (info_entry)
-		snd_info_unregister(info_entry);
+	snd_info_free_entry(info_entry);
 	info_entry = NULL;
 }
 #endif /* CONFIG_PROC_FS */
core/seq/seq_device.c +1 -12
diff -r 333ca526b6e8 core/seq/seq_device.c
@@ -90,7 +90,6 @@ static int snd_seq_device_dev_free(struc
 static int snd_seq_device_dev_free(struct snd_device *device);
 static int snd_seq_device_dev_register(struct snd_device *device);
 static int snd_seq_device_dev_disconnect(struct snd_device *device);
-static int snd_seq_device_dev_unregister(struct snd_device *device);
 
 static int init_device(struct snd_seq_device *dev, struct ops_list *ops);
 static int free_device(struct snd_seq_device *dev, struct ops_list *ops);
@@ -189,7 +188,6 @@ int snd_seq_device_new(struct snd_card *
 		.dev_free = snd_seq_device_dev_free,
 		.dev_register = snd_seq_device_dev_register,
 		.dev_disconnect = snd_seq_device_dev_disconnect,
-		.dev_unregister = snd_seq_device_dev_unregister
 	};
 
 	if (result)
@@ -306,15 +304,6 @@ static int snd_seq_device_dev_disconnect
 
 	unlock_driver(ops);
 	return 0;
-}
-
-/*
- * unregister the existing device
- */
-static int snd_seq_device_dev_unregister(struct snd_device *device)
-{
-	struct snd_seq_device *dev = device->device_data;
-	return snd_seq_device_free(dev);
 }
 
 /*
@@ -568,7 +557,7 @@ static void __exit alsa_seq_device_exit(
 {
 	remove_drivers();
 #ifdef CONFIG_PROC_FS
-	snd_info_unregister(info_entry);
+	snd_info_free_entry(info_entry);
 #endif
 	if (num_ops)
 		snd_printk(KERN_ERR "drivers not released (%d)\n", num_ops);
core/seq/seq_info.c +3 -3
diff -r 333ca526b6e8 core/seq/seq_info.c
@@ -64,9 +64,9 @@ int __init snd_seq_info_init(void)
 
 int __exit snd_seq_info_done(void)
 {
-	snd_info_unregister(queues_entry);
-	snd_info_unregister(clients_entry);
-	snd_info_unregister(timer_entry);
+	snd_info_free_entry(queues_entry);
+	snd_info_free_entry(clients_entry);
+	snd_info_free_entry(timer_entry);
 	return 0;
 }
 #endif
core/sound.c +1 -2
diff -r 333ca526b6e8 core/sound.c
@@ -399,8 +399,7 @@ int __init snd_minor_info_init(void)
 
 int __exit snd_minor_info_done(void)
 {
-	if (snd_minor_info_entry)
-		snd_info_unregister(snd_minor_info_entry);
+	snd_info_free_entry(snd_minor_info_entry);
 	return 0;
 }
 #endif /* CONFIG_PROC_FS */
core/sound_oss.c +1 -2
diff -r 333ca526b6e8 core/sound_oss.c
@@ -270,8 +270,7 @@ int __init snd_minor_info_oss_init(void)
 
 int __exit snd_minor_info_oss_done(void)
 {
-	if (snd_minor_info_oss_entry)
-		snd_info_unregister(snd_minor_info_oss_entry);
+	snd_info_free_entry(snd_minor_info_oss_entry);
 	return 0;
 }
 #endif /* CONFIG_PROC_FS */
core/timer.c +24 -32

104 lines not on this page · raw message

drivers/opl4/opl4_proc.c +1 -2
diff -r 333ca526b6e8 drivers/opl4/opl4_proc.c
@@ -159,8 +159,7 @@ int snd_opl4_create_proc(struct snd_opl4
 
 void snd_opl4_free_proc(struct snd_opl4 *opl4)
 {
-	if (opl4->proc_entry)
-		snd_info_unregister(opl4->proc_entry);
+	snd_info_free_entry(opl4->proc_entry);
 }
 
 #endif /* CONFIG_PROC_FS */
include/core.h +2 -1
diff -r 333ca526b6e8 include/core.h
@@ -71,7 +71,6 @@ struct snd_device_ops {
 	int (*dev_free)(struct snd_device *dev);
 	int (*dev_register)(struct snd_device *dev);
 	int (*dev_disconnect)(struct snd_device *dev);
-	int (*dev_unregister)(struct snd_device *dev);
 };
 
 struct snd_device {
@@ -131,6 +130,7 @@ struct snd_card {
 								state */
 	spinlock_t files_lock;		/* lock the files for this card */
 	int shutdown;			/* this card is going down */
+	int free_on_last_close;		/* free in context of file_release */
 	wait_queue_head_t shutdown_sleep;
 	struct work_struct free_workq;	/* for free in workqueue */
 	struct device *dev;
@@ -246,6 +246,7 @@ struct snd_card *snd_card_new(int idx, c
 			 struct module *module, int extra_size);
 int snd_card_disconnect(struct snd_card *card);
 int snd_card_free(struct snd_card *card);
+int snd_card_free_when_closed(struct snd_card *card);
 int snd_card_free_in_thread(struct snd_card *card);
 int snd_card_register(struct snd_card *card);
 int snd_card_info_init(void);
include/info.h +4 -3
diff -r 333ca526b6e8 include/info.h
@@ -71,7 +71,6 @@ struct snd_info_entry {
 	mode_t mode;
 	long size;
 	unsigned short content;
-	unsigned short disconnected: 1;
 	union {
 		struct snd_info_entry_text text;
 		struct snd_info_entry_ops *ops;
@@ -83,6 +82,8 @@ struct snd_info_entry {
 	void (*private_free)(struct snd_info_entry *entry);
 	struct proc_dir_entry *p;
 	struct mutex access;
+	struct list_head children;
+	struct list_head list;
 };
 
 #if defined(CONFIG_SND_OSSEMUL) && defined(CONFIG_PROC_FS)
@@ -122,8 +123,8 @@ int snd_info_card_create(struct snd_card
 int snd_info_card_create(struct snd_card * card);
 int snd_info_card_register(struct snd_card * card);
 int snd_info_card_free(struct snd_card * card);
+void snd_info_card_disconnect(struct snd_card * card);
 int snd_info_register(struct snd_info_entry * entry);
-int snd_info_unregister(struct snd_info_entry * entry);
 
 /* for card drivers */
 int snd_card_proc_new(struct snd_card *card, const char *name, struct snd_info_entry **entryp);
@@ -156,8 +157,8 @@ static inline int snd_info_card_create(s
 static inline int snd_info_card_create(struct snd_card * card) { return 0; }
 static inline int snd_info_card_register(struct snd_card * card) { return 0; }
 static inline int snd_info_card_free(struct snd_card * card) { return 0; }
+static inline void snd_info_card_disconnect(struct snd_card * card) { }
 static inline int snd_info_register(struct snd_info_entry * entry) { return 0; }
-static inline int snd_info_unregister(struct snd_info_entry * entry) { return 0; }
 
 static inline int snd_card_proc_new(struct snd_card *card, const char *name,
 				    struct snd_info_entry **entryp) { return -EINVAL; }
include/timer.h +0 -1
diff -r 333ca526b6e8 include/timer.h
@@ -129,7 +129,6 @@ int snd_timer_global_new(char *id, int d
 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer);
 int snd_timer_global_free(struct snd_timer *timer);
 int snd_timer_global_register(struct snd_timer *timer);
-int snd_timer_global_unregister(struct snd_timer *timer);
 
 int snd_timer_open(struct snd_timer_instance **ti, char *owner, struct snd_timer_id *tid, unsigned int slave_id);
 int snd_timer_close(struct snd_timer_instance *timeri);
pci/ac97/ac97_codec.c +4 -4
diff -r 333ca526b6e8 pci/ac97/ac97_codec.c
@@ -1817,13 +1817,13 @@ static int snd_ac97_dev_register(struct 
 	return 0;
 }
 
-/* unregister ac97 codec */
-static int snd_ac97_dev_unregister(struct snd_device *device)
+/* disconnect ac97 codec */
+static int snd_ac97_dev_disconnect(struct snd_device *device)
 {
 	struct snd_ac97 *ac97 = device->device_data;
 	if (ac97->dev.bus)
 		device_unregister(&ac97->dev);
-	return snd_ac97_free(ac97);
+	return 0;
 }
 
 /* build_ops to do nothing */
@@ -1860,7 +1860,7 @@ int snd_ac97_mixer(struct snd_ac97_bus *
 	static struct snd_device_ops ops = {
 		.dev_free =	snd_ac97_dev_free,
 		.dev_register =	snd_ac97_dev_register,
-		.dev_unregister =	snd_ac97_dev_unregister,
+		.dev_disconnect =	snd_ac97_dev_disconnect,
 	};
 
 	snd_assert(rac97 != NULL, return -EINVAL);
pci/ac97/ac97_proc.c +7 -13
diff -r 333ca526b6e8 pci/ac97/ac97_proc.c
@@ -457,14 +457,10 @@ void snd_ac97_proc_init(struct snd_ac97 
 
 void snd_ac97_proc_done(struct snd_ac97 * ac97)
 {
-	if (ac97->proc_regs) {
-		snd_info_unregister(ac97->proc_regs);
-		ac97->proc_regs = NULL;
-	}
-	if (ac97->proc) {
-		snd_info_unregister(ac97->proc);
-		ac97->proc = NULL;
-	}
+	snd_info_free_entry(ac97->proc_regs);
+	ac97->proc_regs = NULL;
+	snd_info_free_entry(ac97->proc);
+	ac97->proc = NULL;
 }
 
 void snd_ac97_bus_proc_init(struct snd_ac97_bus * bus)
@@ -485,8 +481,6 @@ void snd_ac97_bus_proc_init(struct snd_a
 
 void snd_ac97_bus_proc_done(struct snd_ac97_bus * bus)
 {
-	if (bus->proc) {
-		snd_info_unregister(bus->proc);
-		bus->proc = NULL;
-	}
-}
+	snd_info_free_entry(bus->proc);
+	bus->proc = NULL;
+}
pci/cs46xx/dsp_spos.c +19 -33

64 lines not on this page · raw message

pci/cs46xx/dsp_spos_scb_lib.c +1 -1
diff -r 333ca526b6e8 pci/cs46xx/dsp_spos_scb_lib.c
@@ -232,7 +232,7 @@ void cs46xx_dsp_proc_free_scb_desc (stru
 
 		snd_printdd("cs46xx_dsp_proc_free_scb_desc: freeing %s\n",scb->scb_name);
 
-		snd_info_unregister(scb->proc_info);
+		snd_info_free_entry(scb->proc_info);
 		scb->proc_info = NULL;
 
 		snd_assert (scb_info != NULL, return);
synth/emux/emux_proc.c +2 -4
diff -r 333ca526b6e8 synth/emux/emux_proc.c
@@ -128,10 +128,8 @@ void snd_emux_proc_init(struct snd_emux 
 
 void snd_emux_proc_free(struct snd_emux *emu)
 {
-	if (emu->proc) {
-		snd_info_unregister(emu->proc);
-		emu->proc = NULL;
-	}
+	snd_info_free_entry(emu->proc);
+	emu->proc = NULL;
 }
 
 #endif /* CONFIG_PROC_FS */
usb/usbaudio.c +1 -1
diff -r 333ca526b6e8 usb/usbaudio.c
@@ -3469,7 +3469,7 @@ static void snd_usb_audio_disconnect(str
 		}
 		usb_chip[chip->index] = NULL;
 		mutex_unlock(&register_mutex);
-		snd_card_free(card);
+		snd_card_free_when_closed(card);
 	} else {
 		mutex_unlock(&register_mutex);
 	}
#16 lore raw

At Tue, 20 Jun 2006 17:27:12 +0200,

Quoted 13 lines · I wrote:

At Tue, 20 Jun 2006 14:21:52 +0200,

Quoted 10 lines · I wrote:

At Sat, 17 Jun 2006 02:01:39 -0700,

Quoted 6 lines · Sam Revitch

The last patch has other problems -- races in mixer_oss/pcm_oss, build failures with disable-verbose-procfs, etc.. :-( To avoid spamming this list with another file attachment the latest one is posted here:

http://lsb.blogdns.net/files/snd-disconnect.patch

Quoted 8 lines
Quoted 6 lines

The patch still has a problem with ac97_codec.c that has also own lowlevel device. opl3 and opl4 have to be fixed.

Looking through the patch, I found that unregister callback can be replaced simply with disconnect + free. This will remove the complexity a bit. I'll try to make another patch based on yours.

The resultant patch is below.

Also the below is to alsa-driver tree.

Takashi

acore/memory_debug.c +1 -2
diff -r c272885c5162 acore/memory_debug.c
@@ -169,7 +169,6 @@ int __init snd_memory_info_init(void)
 
 void __exit snd_memory_info_done(void)
 {
-	if (snd_memory_info_entry)
-		snd_info_unregister(snd_memory_info_entry);
+	snd_info_free_entry(snd_memory_info_entry);
 }
 #endif
usb/usbaudio.patch +14 -24
diff -r c272885c5162 usb/usbaudio.patch
@@ -1,5 +1,5 @@
---- ../alsa-kernel/usb/usbaudio.c	2006-03-14 12:20:41.000000000 +0100
-+++ usbaudio.c	2006-03-14 12:37:54.000000000 +0100
+--- ../alsa-kernel/usb/usbaudio.c	2006-06-20 12:49:23.000000000 +0200
++++ usbaudio.c	2006-06-20 12:51:01.000000000 +0200
 @@ -1,3 +1,4 @@
 +#include "usbaudio.inc"
  /*
@@ -137,7 +137,7 @@
  			continue;
  		/* must be isochronous */
  		if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
-@@ -2644,7 +2683,11 @@
+@@ -2645,7 +2684,11 @@
  		fp->altset_idx = i;
  		fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  		fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
@@ -149,7 +149,7 @@
  		if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  			fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  					* (fp->maxpacksize & 0x7ff);
-@@ -2925,7 +2968,11 @@
+@@ -2926,7 +2969,11 @@
  	fp->iface = altsd->bInterfaceNumber;
  	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
@@ -161,7 +161,7 @@
  
  	switch (fp->maxpacksize) {
  	case 0x120:
-@@ -2993,7 +3040,11 @@
+@@ -2994,7 +3041,11 @@
  	fp->iface = altsd->bInterfaceNumber;
  	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
@@ -173,7 +173,7 @@
  	fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
  
  	stream = (fp->endpoint & USB_DIR_IN)
-@@ -3058,8 +3109,13 @@
+@@ -3059,8 +3110,13 @@
  	struct usb_host_config *config = dev->actconfig;
  	int err;
  
@@ -187,7 +187,7 @@
  		snd_printdd("sending Extigy boot sequence...\n");
  		/* Send message to force it to reconnect with full interface. */
  		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
-@@ -3071,8 +3127,13 @@
+@@ -3072,8 +3128,13 @@
  		if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
  		err = usb_reset_configuration(dev);
  		if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
@@ -201,7 +201,7 @@
  		return -ENODEV; /* quit this anyway */
  	}
  	return 0;
-@@ -3080,6 +3141,8 @@
+@@ -3081,6 +3142,8 @@
  
  static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
  {
@@ -210,7 +210,7 @@
  	u8 buf = 1;
  
  	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
-@@ -3091,6 +3154,7 @@
+@@ -3092,6 +3155,7 @@
  				1, 2000, NULL, 0, 1000);
  		return -ENODEV;
  	}
@@ -218,7 +218,7 @@
  	return 0;
  }
  
-@@ -3260,8 +3324,13 @@
+@@ -3261,8 +3325,13 @@
  	chip->index = idx;
  	chip->dev = dev;
  	chip->card = card;
@@ -232,7 +232,7 @@
  	INIT_LIST_HEAD(&chip->pcm_list);
  	INIT_LIST_HEAD(&chip->midi_list);
  	INIT_LIST_HEAD(&chip->mixer_list);
-@@ -3346,8 +3415,12 @@
+@@ -3347,8 +3416,12 @@
  
  	alts = &intf->altsetting[0];
  	ifnum = get_iface_desc(alts)->bInterfaceNumber;
@@ -245,17 +245,7 @@
  
  	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  		goto __err_val;
-@@ -3468,12 +3541,17 @@
- 		}
- 		usb_chip[chip->index] = NULL;
- 		mutex_unlock(&register_mutex);
-+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
- 		snd_card_free(card);
-+#else
-+		snd_card_free_in_thread(card);
-+#endif
- 	} else {
- 		mutex_unlock(&register_mutex);
+@@ -3475,6 +3548,7 @@
  	}
  }
  
@@ -263,7 +253,7 @@
  /*
   * new 2.5 USB kernel API
   */
-@@ -3494,6 +3572,8 @@
+@@ -3495,6 +3569,8 @@
  	snd_usb_audio_disconnect(interface_to_usbdev(intf),
  				 dev_get_drvdata(&intf->dev));
  }
@@ -272,7 +262,7 @@
  
  
  static int __init snd_usb_audio_init(void)
-@@ -3514,3 +3594,5 @@
+@@ -3515,3 +3591,5 @@
  
  module_init(snd_usb_audio_init);
  module_exit(snd_usb_audio_cleanup);
#17 lore raw
In reply to Takashi Iwai
Quoted 6 lines · Takashi Iwai

The patch still has a problem with ac97_codec.c that has also own lowlevel device. opl3 and opl4 have to be fixed.

Looking through the patch, I found that unregister callback can be replaced simply with disconnect + free. This will remove the complexity a bit. I'll try to make another patch based on yours.

Thanks for looking at this and fixing it, and sorry to take so long to get back to ya. I tried out the patch that you sent out earlier and it definitely behaves correctly when pulling cables with my USB card and also seems to pass the alsasound restart loop test. Eliminating dev_unregister seems appropriate, as dev_disconnect was being called every time anyway and the former certainly became redundant. IMHO the patch you sent is complete or very close.

-Sam Revitch

All the advantages of Linux Managed Hosting--Without the Cost and Risk! Fully trained technicians. The highest number of Red Hat certifications in the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642

#18 lore raw

At Wed, 21 Jun 2006 23:20:29 -0700,

Quoted 9 lines · Sam Revitch
Quoted 6 lines · Takashi Iwai

The patch still has a problem with ac97_codec.c that has also own lowlevel device. opl3 and opl4 have to be fixed.

Looking through the patch, I found that unregister callback can be replaced simply with disconnect + free. This will remove the complexity a bit. I'll try to make another patch based on yours.

Quoted 7 lines

Thanks for looking at this and fixing it, and sorry to take so long to get back to ya. I tried out the patch that you sent out earlier and it definitely behaves correctly when pulling cables with my USB card and also seems to pass the alsasound restart loop test. Eliminating dev_unregister seems appropriate, as dev_disconnect was being called every time anyway and the former certainly became redundant. IMHO the patch you sent is complete or very close.

Good. I'll commit it (splitted to separate patches) to HG repo later after releasing 1.0.12rc1. Also snd_card_free_in_thread() will be replaced with snd_card_free_when_closed().

thanks,

Takashi

All the advantages of Linux Managed Hosting--Without the Cost and Risk! Fully trained technicians. The highest number of Red Hat certifications in the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642

#19 lore raw
In reply to Sam Revitch

At Sat, 17 Jun 2006 01:23:23 -0700,

Quoted 6 lines · Sam Revitch

Now, there has got to be some way to make usb-audio retain its snd_card structure at disconnect time. Perhaps it could optionally leave things hanging for some period of time before calling snd_card_disconnect(), leaving a window for the same device to be reconnected...? Does this sound too kludgy?

The principle question about this is how to determine "some period". One hour, one day, one week or one month? It cannot be decided uniquely.

Also, from the architecture POV, the determination of suspend/resume should be done in the usb core side rather than in each driver. Each driver can implement suspend/resume callback easily.

Takashi

#20 lore raw
In reply to Sam Revitch

At Wed, 14 Jun 2006 10:20:57 -0700,

Quoted 18 lines · Sam Revitch
Quoted 9 lines

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

I'm not sure how alsa-lib can decide if device has been disconnected permanently or temporary. But if you mean "closing the file descriptor in the first time when -ENODEV occurs", it makes a lot of sense for alsa-lib and yes, it should be there.

Thanks for replies.

The problem with usb_audio_disconnect() getting stuck isn't specific to suspend, although the part of it that I care about right now is. That function shouldn't be waiting for handles to be closed, as somebody can execute a DoS attack on khubd by sending SIGSTOP to any process that has the sound driver open.

Quoted 4 lines

Perhaps the snd_device dev_disconnect and dev_unregister routines for the various abstract layers need to be changed so that sysfs/procfs entries are removed in dev_disconnect? This way it might be possible to release a slot without freeing anything that might be in use.

Yes, it sounds feasible.

Quoted 4 lines

As for implementing suspend/resume methods for snd-usb-audio, doesn't that require explicit support from the device for retaining state in some sort of low power 0.5mA mode? I don't know whether the card I'm using even supports that.

I don't think the low-poer mode is a requirement although it would be nice to have. The main purpose of suspend/resume is the stop of PCM streams and save/restore the mixer status.

Takashi

#21 lore raw

At Fri, 16 Jun 2006 12:04:04 +0200,

Quoted 28 lines · I wrote:

At Wed, 14 Jun 2006 10:20:57 -0700,

Quoted 18 lines · Sam Revitch
Quoted 9 lines

Anyway, re-reading the quote above, it sounds more like a problem happening only via suspend. Then, this might be solved automatically if the proper suspend/resume is implemented on usb-audio driver.

I'm not sure how alsa-lib can decide if device has been disconnected permanently or temporary. But if you mean "closing the file descriptor in the first time when -ENODEV occurs", it makes a lot of sense for alsa-lib and yes, it should be there.

Thanks for replies.

The problem with usb_audio_disconnect() getting stuck isn't specific to suspend, although the part of it that I care about right now is. That function shouldn't be waiting for handles to be closed, as somebody can execute a DoS attack on khubd by sending SIGSTOP to any process that has the sound driver open.

Quoted 4 lines

Perhaps the snd_device dev_disconnect and dev_unregister routines for the various abstract layers need to be changed so that sysfs/procfs entries are removed in dev_disconnect? This way it might be possible to release a slot without freeing anything that might be in use.

Yes, it sounds feasible.

Quoted 8 lines
Quoted 4 lines

As for implementing suspend/resume methods for snd-usb-audio, doesn't that require explicit support from the device for retaining state in some sort of low power 0.5mA mode? I don't know whether the card I'm using even supports that.

I don't think the low-poer mode is a requirement although it would be nice to have. The main purpose of suspend/resume is the stop of PCM streams and save/restore the mixer status.

A short test didn't work, so looks like you are right...

The fix for snd_card_free() is the way to go right now. (Though, the proper support of suspend/resume would be best, of
course.)

Takashi