ALSA: rawmidi: Simplify error paths

Apply the standard idiom: rewrite the multiple unlocks in error paths
in the goto-error-and-single-unlock way.

Just a code refactoring, and no functional changes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2018-07-17 22:45:50 +02:00
Родитель 5bed913972
Коммит 7fdc9b0807
1 изменённых файлов: 39 добавлений и 37 удалений

Просмотреть файл

@ -338,22 +338,20 @@ int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
int mode, struct snd_rawmidi_file *rfile) int mode, struct snd_rawmidi_file *rfile)
{ {
struct snd_rawmidi *rmidi; struct snd_rawmidi *rmidi;
int err; int err = 0;
if (snd_BUG_ON(!rfile)) if (snd_BUG_ON(!rfile))
return -EINVAL; return -EINVAL;
mutex_lock(&register_mutex); mutex_lock(&register_mutex);
rmidi = snd_rawmidi_search(card, device); rmidi = snd_rawmidi_search(card, device);
if (rmidi == NULL) { if (!rmidi)
mutex_unlock(&register_mutex); err = -ENODEV;
return -ENODEV; else if (!try_module_get(rmidi->card->module))
} err = -ENXIO;
if (!try_module_get(rmidi->card->module)) {
mutex_unlock(&register_mutex);
return -ENXIO;
}
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
if (err < 0)
return err;
mutex_lock(&rmidi->open_mutex); mutex_lock(&rmidi->open_mutex);
err = rawmidi_open_priv(rmidi, subdevice, mode, rfile); err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
@ -1581,26 +1579,25 @@ int snd_rawmidi_new(struct snd_card *card, char *id, int device,
&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
SNDRV_RAWMIDI_STREAM_INPUT, SNDRV_RAWMIDI_STREAM_INPUT,
input_count); input_count);
if (err < 0) { if (err < 0)
snd_rawmidi_free(rmidi); goto error;
return err;
}
err = snd_rawmidi_alloc_substreams(rmidi, err = snd_rawmidi_alloc_substreams(rmidi,
&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
SNDRV_RAWMIDI_STREAM_OUTPUT, SNDRV_RAWMIDI_STREAM_OUTPUT,
output_count); output_count);
if (err < 0) { if (err < 0)
snd_rawmidi_free(rmidi); goto error;
return err;
}
err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops); err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
if (err < 0) { if (err < 0)
snd_rawmidi_free(rmidi); goto error;
return err;
}
if (rrawmidi) if (rrawmidi)
*rrawmidi = rmidi; *rrawmidi = rmidi;
return 0; return 0;
error:
snd_rawmidi_free(rmidi);
return err;
} }
EXPORT_SYMBOL(snd_rawmidi_new); EXPORT_SYMBOL(snd_rawmidi_new);
@ -1660,30 +1657,27 @@ static int snd_rawmidi_dev_register(struct snd_device *device)
if (rmidi->device >= SNDRV_RAWMIDI_DEVICES) if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
return -ENOMEM; return -ENOMEM;
err = 0;
mutex_lock(&register_mutex); mutex_lock(&register_mutex);
if (snd_rawmidi_search(rmidi->card, rmidi->device)) { if (snd_rawmidi_search(rmidi->card, rmidi->device))
mutex_unlock(&register_mutex); err = -EBUSY;
return -EBUSY; else
} list_add_tail(&rmidi->list, &snd_rawmidi_devices);
list_add_tail(&rmidi->list, &snd_rawmidi_devices);
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
if (err < 0)
return err;
err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI, err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
rmidi->card, rmidi->device, rmidi->card, rmidi->device,
&snd_rawmidi_f_ops, rmidi, &rmidi->dev); &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
if (err < 0) { if (err < 0) {
rmidi_err(rmidi, "unable to register\n"); rmidi_err(rmidi, "unable to register\n");
mutex_lock(&register_mutex); goto error;
list_del(&rmidi->list);
mutex_unlock(&register_mutex);
return err;
} }
if (rmidi->ops && rmidi->ops->dev_register && if (rmidi->ops && rmidi->ops->dev_register) {
(err = rmidi->ops->dev_register(rmidi)) < 0) { err = rmidi->ops->dev_register(rmidi);
snd_unregister_device(&rmidi->dev); if (err < 0)
mutex_lock(&register_mutex); goto error_unregister;
list_del(&rmidi->list);
mutex_unlock(&register_mutex);
return err;
} }
#ifdef CONFIG_SND_OSSEMUL #ifdef CONFIG_SND_OSSEMUL
rmidi->ossreg = 0; rmidi->ossreg = 0;
@ -1735,6 +1729,14 @@ static int snd_rawmidi_dev_register(struct snd_device *device)
} }
#endif #endif
return 0; return 0;
error_unregister:
snd_unregister_device(&rmidi->dev);
error:
mutex_lock(&register_mutex);
list_del(&rmidi->list);
mutex_unlock(&register_mutex);
return err;
} }
static int snd_rawmidi_dev_disconnect(struct snd_device *device) static int snd_rawmidi_dev_disconnect(struct snd_device *device)