ALSA: Simplify snd_device_register() variants

Now that all callers have been replaced with
snd_device_register_for_dev(), let's drop the obsolete device
registration code and concentrate only on the code handling struct
device directly.  That said,

- remove the old snd_device_register(),
- rename snd_device_register_for_dev() with snd_device_register(),
- drop superfluous arguments from snd_device_register(),
- change snd_unregister_device() to pass the device pointer directly

Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2015-01-30 08:34:58 +01:00
parent 04c5d5a430
commit 40a4b26385
9 changed files with 55 additions and 121 deletions

View File

@ -187,7 +187,6 @@ struct snd_minor {
int type; /* SNDRV_DEVICE_TYPE_XXX */ int type; /* SNDRV_DEVICE_TYPE_XXX */
int card; /* card number */ int card; /* card number */
int device; /* device number */ int device; /* device number */
bool created;
const struct file_operations *f_ops; /* file operations */ const struct file_operations *f_ops; /* file operations */
void *private_data; /* private data for f_ops->open */ void *private_data; /* private data for f_ops->open */
struct device *dev; /* device for sysfs */ struct device *dev; /* device for sysfs */
@ -210,40 +209,10 @@ void snd_request_card(int card);
void snd_device_initialize(struct device *dev, struct snd_card *card); void snd_device_initialize(struct device *dev, struct snd_card *card);
int snd_register_device_for_dev(int type, struct snd_card *card, int dev, int snd_register_device(int type, struct snd_card *card, int dev,
const struct file_operations *f_ops, const struct file_operations *f_ops,
void *private_data, struct device *device, void *private_data, struct device *device);
struct device *parent, const char *name); int snd_unregister_device(struct device *dev);
/**
* snd_register_device - Register the ALSA device file for the card
* @type: the device type, SNDRV_DEVICE_TYPE_XXX
* @card: the card instance
* @dev: the device index
* @f_ops: the file operations
* @private_data: user pointer for f_ops->open()
* @name: the device file name
*
* Registers an ALSA device file for the given card.
* The operators have to be set in reg parameter.
*
* This function uses the card's device pointer to link to the
* correct &struct device.
*
* Return: Zero if successful, or a negative error code on failure.
*/
static inline int snd_register_device(int type, struct snd_card *card, int dev,
const struct file_operations *f_ops,
void *private_data,
const char *name)
{
return snd_register_device_for_dev(type, card, dev, f_ops,
private_data, NULL,
snd_card_get_device_link(card),
name);
}
int snd_unregister_device(int type, struct snd_card *card, int dev);
void *snd_lookup_minor_data(unsigned int minor, int type); void *snd_lookup_minor_data(unsigned int minor, int type);
struct device *snd_get_device(int type, struct snd_card *card, int dev); struct device *snd_get_device(int type, struct snd_card *card, int dev);

View File

@ -871,10 +871,9 @@ static int snd_compress_dev_register(struct snd_device *device)
pr_debug("reg %s for device %s, direction %d\n", str, compr->name, pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
compr->direction); compr->direction);
/* register compressed device */ /* register compressed device */
ret = snd_register_device_for_dev(SNDRV_DEVICE_TYPE_COMPRESS, ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
compr->card, compr->device, compr->card, compr->device,
&snd_compr_file_ops, compr, &snd_compr_file_ops, compr, &compr->dev);
&compr->dev, NULL, NULL);
if (ret < 0) { if (ret < 0) {
pr_err("snd_register_device failed\n %d", ret); pr_err("snd_register_device failed\n %d", ret);
return ret; return ret;
@ -888,8 +887,7 @@ static int snd_compress_dev_disconnect(struct snd_device *device)
struct snd_compr *compr; struct snd_compr *compr;
compr = device->device_data; compr = device->device_data;
snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, snd_unregister_device(&compr->dev);
compr->device);
return 0; return 0;
} }

View File

@ -1661,9 +1661,8 @@ static int snd_ctl_dev_register(struct snd_device *device)
{ {
struct snd_card *card = device->device_data; struct snd_card *card = device->device_data;
return snd_register_device_for_dev(SNDRV_DEVICE_TYPE_CONTROL, card, return snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
-1, &snd_ctl_f_ops, card, &snd_ctl_f_ops, card, &card->ctl_dev);
&card->ctl_dev, NULL, NULL);
} }
/* /*
@ -1681,7 +1680,7 @@ static int snd_ctl_dev_disconnect(struct snd_device *device)
} }
read_unlock(&card->ctl_files_rwlock); read_unlock(&card->ctl_files_rwlock);
return snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1); return snd_unregister_device(&card->ctl_dev);
} }
/* /*

View File

@ -432,10 +432,9 @@ static int snd_hwdep_dev_register(struct snd_device *device)
return -EBUSY; return -EBUSY;
} }
list_add_tail(&hwdep->list, &snd_hwdep_devices); list_add_tail(&hwdep->list, &snd_hwdep_devices);
err = snd_register_device_for_dev(SNDRV_DEVICE_TYPE_HWDEP, err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
hwdep->card, hwdep->device, hwdep->card, hwdep->device,
&snd_hwdep_f_ops, hwdep, &snd_hwdep_f_ops, hwdep, &hwdep->dev);
&hwdep->dev, NULL, NULL);
if (err < 0) { if (err < 0) {
dev_err(&hwdep->dev, "unable to register\n"); dev_err(&hwdep->dev, "unable to register\n");
list_del(&hwdep->list); list_del(&hwdep->list);
@ -480,7 +479,7 @@ static int snd_hwdep_dev_disconnect(struct snd_device *device)
if (hwdep->ossreg) if (hwdep->ossreg)
snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device); snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
#endif #endif
snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device); snd_unregister_device(&hwdep->dev);
list_del_init(&hwdep->list); list_del_init(&hwdep->list);
mutex_unlock(&hwdep->open_mutex); mutex_unlock(&hwdep->open_mutex);
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);

View File

@ -1105,11 +1105,9 @@ static int snd_pcm_dev_register(struct snd_device *device)
break; break;
} }
/* register pcm */ /* register pcm */
err = snd_register_device_for_dev(devtype, pcm->card, err = snd_register_device(devtype, pcm->card, pcm->device,
pcm->device, &snd_pcm_f_ops[cidx], pcm,
&snd_pcm_f_ops[cidx], &pcm->streams[cidx].dev);
pcm, &pcm->streams[cidx].dev,
NULL, NULL);
if (err < 0) { if (err < 0) {
list_del(&pcm->list); list_del(&pcm->list);
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
@ -1132,7 +1130,7 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
struct snd_pcm *pcm = device->device_data; struct snd_pcm *pcm = device->device_data;
struct snd_pcm_notify *notify; struct snd_pcm_notify *notify;
struct snd_pcm_substream *substream; struct snd_pcm_substream *substream;
int cidx, devtype; int cidx;
mutex_lock(&register_mutex); mutex_lock(&register_mutex);
if (list_empty(&pcm->list)) if (list_empty(&pcm->list))
@ -1155,16 +1153,7 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
notify->n_disconnect(pcm); notify->n_disconnect(pcm);
} }
for (cidx = 0; cidx < 2; cidx++) { for (cidx = 0; cidx < 2; cidx++) {
devtype = -1; snd_unregister_device(&pcm->streams[cidx].dev);
switch (cidx) {
case SNDRV_PCM_STREAM_PLAYBACK:
devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
break;
case SNDRV_PCM_STREAM_CAPTURE:
devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
break;
}
snd_unregister_device(devtype, pcm->card, pcm->device);
if (pcm->streams[cidx].chmap_kctl) { if (pcm->streams[cidx].chmap_kctl) {
snd_ctl_remove(pcm->card, pcm->streams[cidx].chmap_kctl); snd_ctl_remove(pcm->card, pcm->streams[cidx].chmap_kctl);
pcm->streams[cidx].chmap_kctl = NULL; pcm->streams[cidx].chmap_kctl = NULL;

View File

@ -1581,10 +1581,9 @@ static int snd_rawmidi_dev_register(struct snd_device *device)
return -EBUSY; return -EBUSY;
} }
list_add_tail(&rmidi->list, &snd_rawmidi_devices); list_add_tail(&rmidi->list, &snd_rawmidi_devices);
err = snd_register_device_for_dev(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, &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
&rmidi->dev, NULL, NULL);
if (err < 0) { if (err < 0) {
rmidi_err(rmidi, "unable to register\n"); rmidi_err(rmidi, "unable to register\n");
list_del(&rmidi->list); list_del(&rmidi->list);
@ -1593,7 +1592,7 @@ static int snd_rawmidi_dev_register(struct snd_device *device)
} }
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)) < 0) {
snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device); snd_unregister_device(&rmidi->dev);
list_del(&rmidi->list); list_del(&rmidi->list);
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
return err; return err;
@ -1681,7 +1680,7 @@ static int snd_rawmidi_dev_disconnect(struct snd_device *device)
rmidi->ossreg = 0; rmidi->ossreg = 0;
} }
#endif /* CONFIG_SND_OSSEMUL */ #endif /* CONFIG_SND_OSSEMUL */
snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device); snd_unregister_device(&rmidi->dev);
mutex_unlock(&rmidi->open_mutex); mutex_unlock(&rmidi->open_mutex);
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
return 0; return 0;

View File

@ -2586,9 +2586,8 @@ int __init snd_sequencer_device_init(void)
if (mutex_lock_interruptible(&register_mutex)) if (mutex_lock_interruptible(&register_mutex))
return -ERESTARTSYS; return -ERESTARTSYS;
err = snd_register_device_for_dev(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0, err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
&snd_seq_f_ops, NULL, &snd_seq_f_ops, NULL, &seq_dev);
&seq_dev, NULL, NULL);
if (err < 0) { if (err < 0) {
mutex_unlock(&register_mutex); mutex_unlock(&register_mutex);
put_device(&seq_dev); put_device(&seq_dev);
@ -2607,6 +2606,6 @@ int __init snd_sequencer_device_init(void)
*/ */
void __exit snd_sequencer_device_done(void) void __exit snd_sequencer_device_done(void)
{ {
snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0); snd_unregister_device(&seq_dev);
put_device(&seq_dev); put_device(&seq_dev);
} }

View File

@ -242,30 +242,30 @@ static int snd_kernel_minor(int type, struct snd_card *card, int dev)
#endif #endif
/** /**
* snd_register_device_for_dev - Register the ALSA device file for the card * snd_register_device - Register the ALSA device file for the card
* @type: the device type, SNDRV_DEVICE_TYPE_XXX * @type: the device type, SNDRV_DEVICE_TYPE_XXX
* @card: the card instance * @card: the card instance
* @dev: the device index * @dev: the device index
* @f_ops: the file operations * @f_ops: the file operations
* @private_data: user pointer for f_ops->open() * @private_data: user pointer for f_ops->open()
* @device: the device to register, NULL to create a new one * @device: the device to register
* @parent: the &struct device to link this new device to (only for device=NULL)
* @name: the device file name (only for device=NULL)
* *
* Registers an ALSA device file for the given card. * Registers an ALSA device file for the given card.
* The operators have to be set in reg parameter. * The operators have to be set in reg parameter.
* *
* Return: Zero if successful, or a negative error code on failure. * Return: Zero if successful, or a negative error code on failure.
*/ */
int snd_register_device_for_dev(int type, struct snd_card *card, int dev, int snd_register_device(int type, struct snd_card *card, int dev,
const struct file_operations *f_ops, const struct file_operations *f_ops,
void *private_data, struct device *device, void *private_data, struct device *device)
struct device *parent, const char *name)
{ {
int minor; int minor;
int err = 0; int err = 0;
struct snd_minor *preg; struct snd_minor *preg;
if (snd_BUG_ON(!device))
return -EINVAL;
preg = kmalloc(sizeof *preg, GFP_KERNEL); preg = kmalloc(sizeof *preg, GFP_KERNEL);
if (preg == NULL) if (preg == NULL)
return -ENOMEM; return -ENOMEM;
@ -288,19 +288,9 @@ int snd_register_device_for_dev(int type, struct snd_card *card, int dev,
goto error; goto error;
} }
if (device) { preg->dev = device;
preg->created = false; device->devt = MKDEV(major, minor);
preg->dev = device; err = device_add(device);
device->devt = MKDEV(major, minor);
err = device_add(device);
} else {
preg->created = true;
preg->dev = device_create(sound_class, parent,
MKDEV(major, minor), private_data,
"%s", name);
if (IS_ERR(preg->dev))
err = PTR_ERR(preg->dev);
}
if (err < 0) if (err < 0)
goto error; goto error;
@ -311,8 +301,7 @@ int snd_register_device_for_dev(int type, struct snd_card *card, int dev,
kfree(preg); kfree(preg);
return err; return err;
} }
EXPORT_SYMBOL(snd_register_device);
EXPORT_SYMBOL(snd_register_device_for_dev);
/* find the matching minor record /* find the matching minor record
* return the index of snd_minor, or -1 if not found * return the index of snd_minor, or -1 if not found
@ -334,39 +323,33 @@ static int find_snd_minor(int type, struct snd_card *card, int dev)
/** /**
* snd_unregister_device - unregister the device on the given card * snd_unregister_device - unregister the device on the given card
* @type: the device type, SNDRV_DEVICE_TYPE_XXX * @dev: the device instance
* @card: the card instance
* @dev: the device index
* *
* Unregisters the device file already registered via * Unregisters the device file already registered via
* snd_register_device(). * snd_register_device().
* *
* Return: Zero if successful, or a negative error code on failure. * Return: Zero if successful, or a negative error code on failure.
*/ */
int snd_unregister_device(int type, struct snd_card *card, int dev) int snd_unregister_device(struct device *dev)
{ {
int minor; int minor;
struct snd_minor *preg; struct snd_minor *preg;
mutex_lock(&sound_mutex); mutex_lock(&sound_mutex);
minor = find_snd_minor(type, card, dev); for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
if (minor < 0) { preg = snd_minors[minor];
mutex_unlock(&sound_mutex); if (preg && preg->dev == dev) {
return -EINVAL; snd_minors[minor] = NULL;
device_del(dev);
kfree(preg);
break;
}
} }
preg = snd_minors[minor];
if (preg && !preg->created)
device_del(preg->dev);
else
device_destroy(sound_class, MKDEV(major, minor));
kfree(snd_minors[minor]);
snd_minors[minor] = NULL;
mutex_unlock(&sound_mutex); mutex_unlock(&sound_mutex);
if (minor >= ARRAY_SIZE(snd_minors))
return -ENOENT;
return 0; return 0;
} }
EXPORT_SYMBOL(snd_unregister_device); EXPORT_SYMBOL(snd_unregister_device);
/** /**

View File

@ -1976,9 +1976,8 @@ static int __init alsa_timer_init(void)
return err; return err;
} }
err = snd_register_device_for_dev(SNDRV_DEVICE_TYPE_TIMER, NULL, 0, err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
&snd_timer_f_ops, NULL, &snd_timer_f_ops, NULL, &timer_dev);
&timer_dev, NULL, NULL);
if (err < 0) { if (err < 0) {
pr_err("ALSA: unable to register timer device (%i)\n", err); pr_err("ALSA: unable to register timer device (%i)\n", err);
snd_timer_free_all(); snd_timer_free_all();
@ -1992,7 +1991,7 @@ static int __init alsa_timer_init(void)
static void __exit alsa_timer_exit(void) static void __exit alsa_timer_exit(void)
{ {
snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0); snd_unregister_device(&timer_dev);
snd_timer_free_all(); snd_timer_free_all();
put_device(&timer_dev); put_device(&timer_dev);
snd_timer_proc_done(); snd_timer_proc_done();