Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: Clean up snd_device_*() codes

A few code cleanups and optimizations. In addition, drop
snd_device_disconnect() that isn't used at all, and drop the return
values from snd_device_free*().

Another slight difference by this change is that now the device state
will become always SNDRV_DEV_REGISTERED no matter whether dev_register
ops is present or not. It's for better consistency. There should be
no impact for the current tree, as the state isn't checked.

Signed-off-by: Takashi Iwai <tiwai@suse.de>

+52 -86
+2 -3
include/sound/core.h
··· 309 309 void *device_data, struct snd_device_ops *ops); 310 310 int snd_device_register(struct snd_card *card, void *device_data); 311 311 int snd_device_register_all(struct snd_card *card); 312 - int snd_device_disconnect(struct snd_card *card, void *device_data); 313 312 int snd_device_disconnect_all(struct snd_card *card); 314 - int snd_device_free(struct snd_card *card, void *device_data); 315 - int snd_device_free_all(struct snd_card *card); 313 + void snd_device_free(struct snd_card *card, void *device_data); 314 + void snd_device_free_all(struct snd_card *card); 316 315 317 316 /* isadma.c */ 318 317
+49 -79
sound/core/device.c
··· 73 73 } 74 74 EXPORT_SYMBOL(snd_device_new); 75 75 76 + static int __snd_device_disconnect(struct snd_device *dev) 77 + { 78 + if (dev->state == SNDRV_DEV_REGISTERED) { 79 + if (dev->ops->dev_disconnect && 80 + dev->ops->dev_disconnect(dev)) 81 + dev_err(dev->card->dev, "device disconnect failure\n"); 82 + dev->state = SNDRV_DEV_DISCONNECTED; 83 + } 84 + return 0; 85 + } 86 + 87 + static void __snd_device_free(struct snd_device *dev) 88 + { 89 + /* unlink */ 90 + list_del(&dev->list); 91 + 92 + __snd_device_disconnect(dev); 93 + if (dev->ops->dev_free) { 94 + if (dev->ops->dev_free(dev)) 95 + dev_err(dev->card->dev, "device free failure\n"); 96 + } 97 + kfree(dev); 98 + } 99 + 76 100 static struct snd_device *look_for_dev(struct snd_card *card, void *device_data) 77 101 { 78 102 struct snd_device *dev; ··· 116 92 * Removes the device from the list on the card and invokes the 117 93 * callbacks, dev_disconnect and dev_free, corresponding to the state. 118 94 * Then release the device. 119 - * 120 - * Return: Zero if successful, or a negative error code on failure or if the 121 - * device not found. 122 95 */ 123 - int snd_device_free(struct snd_card *card, void *device_data) 96 + void snd_device_free(struct snd_card *card, void *device_data) 124 97 { 125 98 struct snd_device *dev; 126 99 127 100 if (snd_BUG_ON(!card || !device_data)) 128 - return -ENXIO; 101 + return; 129 102 dev = look_for_dev(card, device_data); 130 - if (dev) { 131 - /* unlink */ 132 - list_del(&dev->list); 133 - if (dev->state == SNDRV_DEV_REGISTERED && 134 - dev->ops->dev_disconnect) 135 - if (dev->ops->dev_disconnect(dev)) 136 - dev_err(card->dev, 137 - "device disconnect failure\n"); 138 - if (dev->ops->dev_free) { 139 - if (dev->ops->dev_free(dev)) 140 - dev_err(card->dev, "device free failure\n"); 141 - } 142 - kfree(dev); 143 - return 0; 144 - } 145 - dev_dbg(card->dev, "device free %p (from %pF), not found\n", 146 - device_data, __builtin_return_address(0)); 147 - return -ENXIO; 103 + if (dev) 104 + __snd_device_free(dev); 105 + else 106 + dev_dbg(card->dev, "device free %p (from %pF), not found\n", 107 + device_data, __builtin_return_address(0)); 148 108 } 149 109 EXPORT_SYMBOL(snd_device_free); 150 110 151 - /** 152 - * snd_device_disconnect - disconnect the device 153 - * @card: the card instance 154 - * @device_data: the data pointer to disconnect 155 - * 156 - * Turns the device into the disconnection state, invoking 157 - * dev_disconnect callback, if the device was already registered. 158 - * 159 - * Usually called from snd_card_disconnect(). 160 - * 161 - * Return: Zero if successful, or a negative error code on failure or if the 162 - * device not found. 163 - */ 164 - int snd_device_disconnect(struct snd_card *card, void *device_data) 111 + static int __snd_device_register(struct snd_device *dev) 165 112 { 166 - struct snd_device *dev; 167 - 168 - if (snd_BUG_ON(!card || !device_data)) 169 - return -ENXIO; 170 - dev = look_for_dev(card, device_data); 171 - if (dev) { 172 - if (dev->state == SNDRV_DEV_REGISTERED && 173 - dev->ops->dev_disconnect) { 174 - if (dev->ops->dev_disconnect(dev)) 175 - dev_err(card->dev, 176 - "device disconnect failure\n"); 177 - dev->state = SNDRV_DEV_DISCONNECTED; 113 + if (dev->state == SNDRV_DEV_BUILD) { 114 + if (dev->ops->dev_register) { 115 + int err = dev->ops->dev_register(dev); 116 + if (err < 0) 117 + return err; 178 118 } 179 - return 0; 119 + dev->state = SNDRV_DEV_REGISTERED; 180 120 } 181 - dev_dbg(card->dev, "device disconnect %p (from %pF), not found\n", 182 - device_data, __builtin_return_address(0)); 183 - return -ENXIO; 121 + return 0; 184 122 } 185 123 186 124 /** ··· 161 175 int snd_device_register(struct snd_card *card, void *device_data) 162 176 { 163 177 struct snd_device *dev; 164 - int err; 165 178 166 179 if (snd_BUG_ON(!card || !device_data)) 167 180 return -ENXIO; 168 181 dev = look_for_dev(card, device_data); 169 - if (dev) { 170 - if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) { 171 - if ((err = dev->ops->dev_register(dev)) < 0) 172 - return err; 173 - dev->state = SNDRV_DEV_REGISTERED; 174 - return 0; 175 - } 176 - dev_dbg(card->dev, "snd_device_register busy\n"); 177 - return -EBUSY; 178 - } 182 + if (dev) 183 + return __snd_device_register(dev); 179 184 snd_BUG(); 180 185 return -ENXIO; 181 186 } ··· 184 207 if (snd_BUG_ON(!card)) 185 208 return -ENXIO; 186 209 list_for_each_entry(dev, &card->devices, list) { 187 - if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) { 188 - if ((err = dev->ops->dev_register(dev)) < 0) 189 - return err; 190 - dev->state = SNDRV_DEV_REGISTERED; 191 - } 210 + err = __snd_device_register(dev); 211 + if (err < 0) 212 + return err; 192 213 } 193 214 return 0; 194 215 } ··· 203 228 if (snd_BUG_ON(!card)) 204 229 return -ENXIO; 205 230 list_for_each_entry_reverse(dev, &card->devices, list) { 206 - if (snd_device_disconnect(card, dev->device_data) < 0) 231 + if (__snd_device_disconnect(dev) < 0) 207 232 err = -ENXIO; 208 233 } 209 234 return err; ··· 213 238 * release all the devices on the card. 214 239 * called from init.c 215 240 */ 216 - int snd_device_free_all(struct snd_card *card) 241 + void snd_device_free_all(struct snd_card *card) 217 242 { 218 243 struct snd_device *dev, *next; 219 - int ret = 0; 220 244 221 245 if (snd_BUG_ON(!card)) 222 - return -ENXIO; 223 - list_for_each_entry_safe_reverse(dev, next, &card->devices, list) { 224 - int err = snd_device_free(card, dev->device_data); 225 - if (err < 0) 226 - ret = err; 227 - } 228 - return ret; 246 + return; 247 + list_for_each_entry_safe_reverse(dev, next, &card->devices, list) 248 + __snd_device_free(dev); 229 249 }
+1 -4
sound/core/init.c
··· 454 454 if (snd_mixer_oss_notify_callback) 455 455 snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); 456 456 #endif 457 - if (snd_device_free_all(card) < 0) { 458 - dev_err(card->dev, "unable to free all devices\n"); 459 - /* Fatal, but this situation should never occur */ 460 - } 457 + snd_device_free_all(card); 461 458 if (card->private_free) 462 459 card->private_free(card); 463 460 snd_info_free_entry(card->proc_id);