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

ALSA: hwdep: Don't embed device

Like control and PCM devices, it's better to avoid the embedded struct
device for hwdep (although it's more or less well working), too.
Change it to allocate via snd_device_alloc(), and free the memory at
the common snd_hwdep_free().

Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Signed-off-by: Curtis Malainey <cujomalainey@chromium.org>
Tested-by: Curtis Malainey <cujomalainey@chromium.org>
Link: https://lore.kernel.org/r/20230816160252.23396-5-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

+24 -20
+1 -1
include/sound/hwdep.h
··· 53 53 wait_queue_head_t open_wait; 54 54 void *private_data; 55 55 void (*private_free) (struct snd_hwdep *hwdep); 56 - struct device dev; 56 + struct device *dev; 57 57 58 58 struct mutex open_mutex; 59 59 int used; /* reference counter */
+21 -17
sound/core/hwdep.c
··· 338 338 .mmap = snd_hwdep_mmap, 339 339 }; 340 340 341 - static void release_hwdep_device(struct device *dev) 341 + static void snd_hwdep_free(struct snd_hwdep *hwdep) 342 342 { 343 - kfree(container_of(dev, struct snd_hwdep, dev)); 343 + if (!hwdep) 344 + return; 345 + if (hwdep->private_free) 346 + hwdep->private_free(hwdep); 347 + put_device(hwdep->dev); 348 + kfree(hwdep); 344 349 } 345 350 346 351 /** ··· 387 382 if (id) 388 383 strscpy(hwdep->id, id, sizeof(hwdep->id)); 389 384 390 - snd_device_initialize(&hwdep->dev, card); 391 - hwdep->dev.release = release_hwdep_device; 392 - dev_set_name(&hwdep->dev, "hwC%iD%i", card->number, device); 385 + err = snd_device_alloc(&hwdep->dev, card); 386 + if (err < 0) { 387 + snd_hwdep_free(hwdep); 388 + return err; 389 + } 390 + 391 + dev_set_name(hwdep->dev, "hwC%iD%i", card->number, device); 393 392 #ifdef CONFIG_SND_OSSEMUL 394 393 hwdep->oss_type = -1; 395 394 #endif 396 395 397 396 err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops); 398 397 if (err < 0) { 399 - put_device(&hwdep->dev); 398 + snd_hwdep_free(hwdep); 400 399 return err; 401 400 } 402 401 ··· 412 403 413 404 static int snd_hwdep_dev_free(struct snd_device *device) 414 405 { 415 - struct snd_hwdep *hwdep = device->device_data; 416 - if (!hwdep) 417 - return 0; 418 - if (hwdep->private_free) 419 - hwdep->private_free(hwdep); 420 - put_device(&hwdep->dev); 406 + snd_hwdep_free(device->device_data); 421 407 return 0; 422 408 } 423 409 ··· 430 426 list_add_tail(&hwdep->list, &snd_hwdep_devices); 431 427 err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP, 432 428 hwdep->card, hwdep->device, 433 - &snd_hwdep_f_ops, hwdep, &hwdep->dev); 429 + &snd_hwdep_f_ops, hwdep, hwdep->dev); 434 430 if (err < 0) { 435 - dev_err(&hwdep->dev, "unable to register\n"); 431 + dev_err(hwdep->dev, "unable to register\n"); 436 432 list_del(&hwdep->list); 437 433 mutex_unlock(&register_mutex); 438 434 return err; ··· 443 439 if (hwdep->oss_type >= 0) { 444 440 if (hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM && 445 441 hwdep->device) 446 - dev_warn(&hwdep->dev, 442 + dev_warn(hwdep->dev, 447 443 "only hwdep device 0 can be registered as OSS direct FM device!\n"); 448 444 else if (snd_register_oss_device(hwdep->oss_type, 449 445 card, hwdep->device, 450 446 &snd_hwdep_f_ops, hwdep) < 0) 451 - dev_warn(&hwdep->dev, 447 + dev_warn(hwdep->dev, 452 448 "unable to register OSS compatibility device\n"); 453 449 else 454 450 hwdep->ossreg = 1; ··· 475 471 if (hwdep->ossreg) 476 472 snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device); 477 473 #endif 478 - snd_unregister_device(&hwdep->dev); 474 + snd_unregister_device(hwdep->dev); 479 475 list_del_init(&hwdep->list); 480 476 mutex_unlock(&hwdep->open_mutex); 481 477 mutex_unlock(&register_mutex);
+2 -2
sound/pci/hda/hda_hwdep.c
··· 114 114 #endif 115 115 116 116 /* for sysfs */ 117 - hwdep->dev.groups = snd_hda_dev_attr_groups; 118 - dev_set_drvdata(&hwdep->dev, codec); 117 + hwdep->dev->groups = snd_hda_dev_attr_groups; 118 + dev_set_drvdata(hwdep->dev, codec); 119 119 120 120 return 0; 121 121 }