| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * fireworks.c - a part of driver for Fireworks based devices |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2009-2010 Clemens Ladisch |
|---|
| 5 | 6 | * Copyright (c) 2013-2014 Takashi Sakamoto |
|---|
| 6 | | - * |
|---|
| 7 | | - * Licensed under the terms of the GNU General Public License, version 2. |
|---|
| 8 | 7 | */ |
|---|
| 9 | 8 | |
|---|
| 10 | 9 | /* |
|---|
| .. | .. |
|---|
| 184 | 183 | return err; |
|---|
| 185 | 184 | } |
|---|
| 186 | 185 | |
|---|
| 187 | | -static void efw_free(struct snd_efw *efw) |
|---|
| 188 | | -{ |
|---|
| 189 | | - snd_efw_stream_destroy_duplex(efw); |
|---|
| 190 | | - snd_efw_transaction_remove_instance(efw); |
|---|
| 191 | | - fw_unit_put(efw->unit); |
|---|
| 192 | | - |
|---|
| 193 | | - kfree(efw->resp_buf); |
|---|
| 194 | | - |
|---|
| 195 | | - mutex_destroy(&efw->mutex); |
|---|
| 196 | | - kfree(efw); |
|---|
| 197 | | -} |
|---|
| 198 | | - |
|---|
| 199 | | -/* |
|---|
| 200 | | - * This module releases the FireWire unit data after all ALSA character devices |
|---|
| 201 | | - * are released by applications. This is for releasing stream data or finishing |
|---|
| 202 | | - * transactions safely. Thus at returning from .remove(), this module still keep |
|---|
| 203 | | - * references for the unit. |
|---|
| 204 | | - */ |
|---|
| 205 | 186 | static void |
|---|
| 206 | 187 | efw_card_free(struct snd_card *card) |
|---|
| 207 | 188 | { |
|---|
| 208 | 189 | struct snd_efw *efw = card->private_data; |
|---|
| 209 | 190 | |
|---|
| 210 | | - if (efw->card_index >= 0) { |
|---|
| 211 | | - mutex_lock(&devices_mutex); |
|---|
| 212 | | - clear_bit(efw->card_index, devices_used); |
|---|
| 213 | | - mutex_unlock(&devices_mutex); |
|---|
| 214 | | - } |
|---|
| 191 | + mutex_lock(&devices_mutex); |
|---|
| 192 | + clear_bit(efw->card_index, devices_used); |
|---|
| 193 | + mutex_unlock(&devices_mutex); |
|---|
| 215 | 194 | |
|---|
| 216 | | - efw_free(card->private_data); |
|---|
| 195 | + snd_efw_stream_destroy_duplex(efw); |
|---|
| 196 | + snd_efw_transaction_remove_instance(efw); |
|---|
| 217 | 197 | } |
|---|
| 218 | 198 | |
|---|
| 219 | 199 | static void |
|---|
| .. | .. |
|---|
| 226 | 206 | if (efw->registered) |
|---|
| 227 | 207 | return; |
|---|
| 228 | 208 | |
|---|
| 229 | | - mutex_lock(&devices_mutex); |
|---|
| 230 | | - |
|---|
| 231 | 209 | /* check registered cards */ |
|---|
| 210 | + mutex_lock(&devices_mutex); |
|---|
| 232 | 211 | for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) { |
|---|
| 233 | 212 | if (!test_bit(card_index, devices_used) && enable[card_index]) |
|---|
| 234 | 213 | break; |
|---|
| .. | .. |
|---|
| 244 | 223 | mutex_unlock(&devices_mutex); |
|---|
| 245 | 224 | return; |
|---|
| 246 | 225 | } |
|---|
| 226 | + set_bit(card_index, devices_used); |
|---|
| 227 | + mutex_unlock(&devices_mutex); |
|---|
| 228 | + |
|---|
| 229 | + efw->card->private_free = efw_card_free; |
|---|
| 230 | + efw->card->private_data = efw; |
|---|
| 247 | 231 | |
|---|
| 248 | 232 | /* prepare response buffer */ |
|---|
| 249 | 233 | snd_efw_resp_buf_size = clamp(snd_efw_resp_buf_size, |
|---|
| 250 | 234 | SND_EFW_RESPONSE_MAXIMUM_BYTES, 4096U); |
|---|
| 251 | | - efw->resp_buf = kzalloc(snd_efw_resp_buf_size, GFP_KERNEL); |
|---|
| 252 | | - if (efw->resp_buf == NULL) { |
|---|
| 235 | + efw->resp_buf = devm_kzalloc(&efw->card->card_dev, |
|---|
| 236 | + snd_efw_resp_buf_size, GFP_KERNEL); |
|---|
| 237 | + if (!efw->resp_buf) { |
|---|
| 253 | 238 | err = -ENOMEM; |
|---|
| 254 | 239 | goto error; |
|---|
| 255 | 240 | } |
|---|
| .. | .. |
|---|
| 284 | 269 | if (err < 0) |
|---|
| 285 | 270 | goto error; |
|---|
| 286 | 271 | |
|---|
| 287 | | - set_bit(card_index, devices_used); |
|---|
| 288 | | - mutex_unlock(&devices_mutex); |
|---|
| 289 | | - |
|---|
| 290 | | - /* |
|---|
| 291 | | - * After registered, efw instance can be released corresponding to |
|---|
| 292 | | - * releasing the sound card instance. |
|---|
| 293 | | - */ |
|---|
| 294 | | - efw->card->private_free = efw_card_free; |
|---|
| 295 | | - efw->card->private_data = efw; |
|---|
| 296 | 272 | efw->registered = true; |
|---|
| 297 | 273 | |
|---|
| 298 | 274 | return; |
|---|
| 299 | 275 | error: |
|---|
| 300 | | - mutex_unlock(&devices_mutex); |
|---|
| 301 | | - snd_efw_transaction_remove_instance(efw); |
|---|
| 302 | | - snd_efw_stream_destroy_duplex(efw); |
|---|
| 303 | 276 | snd_card_free(efw->card); |
|---|
| 304 | | - kfree(efw->resp_buf); |
|---|
| 305 | | - efw->resp_buf = NULL; |
|---|
| 306 | 277 | dev_info(&efw->unit->device, |
|---|
| 307 | 278 | "Sound card registration failed: %d\n", err); |
|---|
| 308 | 279 | } |
|---|
| .. | .. |
|---|
| 312 | 283 | { |
|---|
| 313 | 284 | struct snd_efw *efw; |
|---|
| 314 | 285 | |
|---|
| 315 | | - efw = kzalloc(sizeof(struct snd_efw), GFP_KERNEL); |
|---|
| 286 | + efw = devm_kzalloc(&unit->device, sizeof(struct snd_efw), GFP_KERNEL); |
|---|
| 316 | 287 | if (efw == NULL) |
|---|
| 317 | 288 | return -ENOMEM; |
|---|
| 318 | | - |
|---|
| 319 | 289 | efw->unit = fw_unit_get(unit); |
|---|
| 320 | 290 | dev_set_drvdata(&unit->device, efw); |
|---|
| 321 | 291 | |
|---|
| .. | .. |
|---|
| 363 | 333 | cancel_delayed_work_sync(&efw->dwork); |
|---|
| 364 | 334 | |
|---|
| 365 | 335 | if (efw->registered) { |
|---|
| 366 | | - /* No need to wait for releasing card object in this context. */ |
|---|
| 367 | | - snd_card_free_when_closed(efw->card); |
|---|
| 368 | | - } else { |
|---|
| 369 | | - /* Don't forget this case. */ |
|---|
| 370 | | - efw_free(efw); |
|---|
| 336 | + // Block till all of ALSA character devices are released. |
|---|
| 337 | + snd_card_free(efw->card); |
|---|
| 371 | 338 | } |
|---|
| 339 | + |
|---|
| 340 | + mutex_destroy(&efw->mutex); |
|---|
| 341 | + fw_unit_put(efw->unit); |
|---|
| 372 | 342 | } |
|---|
| 373 | 343 | |
|---|
| 374 | 344 | static const struct ieee1394_device_id efw_id_table[] = { |
|---|
| .. | .. |
|---|
| 392 | 362 | static struct fw_driver efw_driver = { |
|---|
| 393 | 363 | .driver = { |
|---|
| 394 | 364 | .owner = THIS_MODULE, |
|---|
| 395 | | - .name = "snd-fireworks", |
|---|
| 365 | + .name = KBUILD_MODNAME, |
|---|
| 396 | 366 | .bus = &fw_bus_type, |
|---|
| 397 | 367 | }, |
|---|
| 398 | 368 | .probe = efw_probe, |
|---|