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

ALSA: cs46xx: Use guard() for mutex locks

Replace the manual mutex lock/unlock pairs with guard() for code
simplification.

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250829144342.4290-20-tiwai@suse.de

+59 -97
+32 -55
sound/pci/cs46xx/cs46xx_lib.c
··· 1093 1093 if (snd_BUG_ON(!sample_rate)) 1094 1094 return -ENXIO; 1095 1095 1096 - mutex_lock(&chip->spos_mutex); 1096 + guard(mutex)(&chip->spos_mutex); 1097 1097 1098 - if (_cs46xx_adjust_sample_rate (chip,cpcm,sample_rate)) { 1099 - mutex_unlock(&chip->spos_mutex); 1098 + if (_cs46xx_adjust_sample_rate(chip, cpcm, sample_rate)) 1100 1099 return -ENXIO; 1101 - } 1102 1100 1103 1101 snd_BUG_ON(!cpcm->pcm_channel); 1104 - if (!cpcm->pcm_channel) { 1105 - mutex_unlock(&chip->spos_mutex); 1102 + if (!cpcm->pcm_channel) 1106 1103 return -ENXIO; 1107 - } 1108 1104 1109 - 1110 - if (cs46xx_dsp_pcm_channel_set_period (chip,cpcm->pcm_channel,period_size)) { 1111 - mutex_unlock(&chip->spos_mutex); 1105 + if (cs46xx_dsp_pcm_channel_set_period(chip, cpcm->pcm_channel, period_size)) 1112 1106 return -EINVAL; 1113 - } 1114 1107 1115 1108 dev_dbg(chip->card->dev, 1116 1109 "period_size (%d), periods (%d) buffer_size(%d)\n", ··· 1137 1144 if (runtime->dma_area == cpcm->hw_buf.area) 1138 1145 snd_pcm_set_runtime_buffer(substream, NULL); 1139 1146 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); 1140 - if (err < 0) { 1141 - #ifdef CONFIG_SND_CS46XX_NEW_DSP 1142 - mutex_unlock(&chip->spos_mutex); 1143 - #endif 1147 + if (err < 0) 1144 1148 return err; 1145 - } 1146 1149 1147 1150 #ifdef CONFIG_SND_CS46XX_NEW_DSP 1148 1151 if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) { ··· 1157 1168 #endif 1158 1169 1159 1170 } 1160 - 1161 - #ifdef CONFIG_SND_CS46XX_NEW_DSP 1162 - mutex_unlock(&chip->spos_mutex); 1163 - #endif 1164 1171 1165 1172 return 0; 1166 1173 } ··· 1480 1495 1481 1496 cpcm->substream = substream; 1482 1497 #ifdef CONFIG_SND_CS46XX_NEW_DSP 1483 - mutex_lock(&chip->spos_mutex); 1484 - cpcm->pcm_channel = NULL; 1485 - cpcm->pcm_channel_id = pcm_channel_id; 1486 - 1498 + scoped_guard(mutex, &chip->spos_mutex) { 1499 + cpcm->pcm_channel = NULL; 1500 + cpcm->pcm_channel_id = pcm_channel_id; 1501 + } 1487 1502 1488 1503 snd_pcm_hw_constraint_list(runtime, 0, 1489 1504 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 1490 1505 &hw_constraints_period_sizes); 1491 - 1492 - mutex_unlock(&chip->spos_mutex); 1493 1506 #else 1494 1507 chip->playback_pcm = cpcm; /* HACK */ 1495 1508 #endif ··· 1524 1541 1525 1542 dev_dbg(chip->card->dev, "open raw iec958 channel\n"); 1526 1543 1527 - mutex_lock(&chip->spos_mutex); 1528 - cs46xx_iec958_pre_open (chip); 1529 - mutex_unlock(&chip->spos_mutex); 1544 + scoped_guard(mutex, &chip->spos_mutex) { 1545 + cs46xx_iec958_pre_open(chip); 1546 + } 1530 1547 1531 1548 return _cs46xx_playback_open_channel(substream,DSP_IEC958_CHANNEL); 1532 1549 } ··· 1542 1559 1543 1560 err = snd_cs46xx_playback_close(substream); 1544 1561 1545 - mutex_lock(&chip->spos_mutex); 1546 - cs46xx_iec958_post_close (chip); 1547 - mutex_unlock(&chip->spos_mutex); 1562 + scoped_guard(mutex, &chip->spos_mutex) { 1563 + cs46xx_iec958_post_close(chip); 1564 + } 1548 1565 1549 1566 return err; 1550 1567 } ··· 1585 1602 if (!cpcm) return -ENXIO; 1586 1603 1587 1604 #ifdef CONFIG_SND_CS46XX_NEW_DSP 1588 - mutex_lock(&chip->spos_mutex); 1589 - if (cpcm->pcm_channel) { 1590 - cs46xx_dsp_destroy_pcm_channel(chip,cpcm->pcm_channel); 1591 - cpcm->pcm_channel = NULL; 1605 + scoped_guard(mutex, &chip->spos_mutex) { 1606 + if (cpcm->pcm_channel) { 1607 + cs46xx_dsp_destroy_pcm_channel(chip, cpcm->pcm_channel); 1608 + cpcm->pcm_channel = NULL; 1609 + } 1592 1610 } 1593 - mutex_unlock(&chip->spos_mutex); 1594 1611 #else 1595 1612 chip->playback_pcm = NULL; 1596 1613 #endif ··· 1965 1982 1966 1983 switch (kcontrol->private_value) { 1967 1984 case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT: 1968 - mutex_lock(&chip->spos_mutex); 1969 - change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED); 1970 - if (ucontrol->value.integer.value[0] && !change) 1971 - cs46xx_dsp_enable_spdif_out(chip); 1972 - else if (change && !ucontrol->value.integer.value[0]) 1973 - cs46xx_dsp_disable_spdif_out(chip); 1985 + scoped_guard(mutex, &chip->spos_mutex) { 1986 + change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED); 1987 + if (ucontrol->value.integer.value[0] && !change) 1988 + cs46xx_dsp_enable_spdif_out(chip); 1989 + else if (change && !ucontrol->value.integer.value[0]) 1990 + cs46xx_dsp_disable_spdif_out(chip); 1974 1991 1975 - res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED)); 1976 - mutex_unlock(&chip->spos_mutex); 1992 + res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED)); 1993 + } 1977 1994 break; 1978 1995 case CS46XX_MIXER_SPDIF_INPUT_ELEMENT: 1979 1996 change = chip->dsp_spos_instance->spdif_status_in; ··· 2114 2131 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol); 2115 2132 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 2116 2133 2117 - mutex_lock(&chip->spos_mutex); 2134 + guard(mutex)(&chip->spos_mutex); 2118 2135 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff); 2119 2136 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff); 2120 2137 ucontrol->value.iec958.status[2] = 0; 2121 2138 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff); 2122 - mutex_unlock(&chip->spos_mutex); 2123 2139 2124 2140 return 0; 2125 2141 } ··· 2131 2149 unsigned int val; 2132 2150 int change; 2133 2151 2134 - mutex_lock(&chip->spos_mutex); 2152 + guard(mutex)(&chip->spos_mutex); 2135 2153 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) | 2136 2154 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) | 2137 2155 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) | ··· 2144 2162 2145 2163 if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) ) 2146 2164 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val); 2147 - 2148 - mutex_unlock(&chip->spos_mutex); 2149 2165 2150 2166 return change; 2151 2167 } ··· 2164 2184 struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol); 2165 2185 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 2166 2186 2167 - mutex_lock(&chip->spos_mutex); 2187 + guard(mutex)(&chip->spos_mutex); 2168 2188 ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff); 2169 2189 ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff); 2170 2190 ucontrol->value.iec958.status[2] = 0; 2171 2191 ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff); 2172 - mutex_unlock(&chip->spos_mutex); 2173 2192 2174 2193 return 0; 2175 2194 } ··· 2181 2202 unsigned int val; 2182 2203 int change; 2183 2204 2184 - mutex_lock(&chip->spos_mutex); 2205 + guard(mutex)(&chip->spos_mutex); 2185 2206 val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) | 2186 2207 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) | 2187 2208 ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) | ··· 2194 2215 2195 2216 if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN ) 2196 2217 cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val); 2197 - 2198 - mutex_unlock(&chip->spos_mutex); 2199 2218 2200 2219 return change; 2201 2220 }
+26 -40
sound/pci/cs46xx/dsp_spos.c
··· 283 283 if (snd_BUG_ON(!ins)) 284 284 return; 285 285 286 - mutex_lock(&chip->spos_mutex); 286 + guard(mutex)(&chip->spos_mutex); 287 287 for (i = 0; i < ins->nscb; ++i) { 288 288 if (ins->scbs[i].deleted) continue; 289 289 ··· 297 297 vfree(ins->symbol_table.symbols); 298 298 kfree(ins->modules); 299 299 kfree(ins); 300 - mutex_unlock(&chip->spos_mutex); 301 300 } 302 301 303 302 static int dsp_load_parameter(struct snd_cs46xx *chip, ··· 524 525 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 525 526 int i,j; 526 527 527 - mutex_lock(&chip->spos_mutex); 528 + guard(mutex)(&chip->spos_mutex); 528 529 snd_iprintf(buffer, "MODULES:\n"); 529 530 for ( i = 0; i < ins->nmodules; ++i ) { 530 531 snd_iprintf(buffer, "\n%s:\n", ins->modules[i].module_name); ··· 537 538 desc->segment_type,desc->offset, desc->size); 538 539 } 539 540 } 540 - mutex_unlock(&chip->spos_mutex); 541 541 } 542 542 543 543 static void cs46xx_dsp_proc_task_tree_read (struct snd_info_entry *entry, ··· 547 549 int i, j, col; 548 550 void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET; 549 551 550 - mutex_lock(&chip->spos_mutex); 552 + guard(mutex)(&chip->spos_mutex); 551 553 snd_iprintf(buffer, "TASK TREES:\n"); 552 554 for ( i = 0; i < ins->ntask; ++i) { 553 555 snd_iprintf(buffer,"\n%04x %s:\n",ins->tasks[i].address,ins->tasks[i].task_name); ··· 564 566 } 565 567 566 568 snd_iprintf(buffer,"\n"); 567 - mutex_unlock(&chip->spos_mutex); 568 569 } 569 570 570 571 static void cs46xx_dsp_proc_scb_read (struct snd_info_entry *entry, ··· 573 576 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 574 577 int i; 575 578 576 - mutex_lock(&chip->spos_mutex); 579 + guard(mutex)(&chip->spos_mutex); 577 580 snd_iprintf(buffer, "SCB's:\n"); 578 581 for ( i = 0; i < ins->nscb; ++i) { 579 582 if (ins->scbs[i].deleted) ··· 596 599 } 597 600 598 601 snd_iprintf(buffer,"\n"); 599 - mutex_unlock(&chip->spos_mutex); 600 602 } 601 603 602 604 static void cs46xx_dsp_proc_parameter_dump_read (struct snd_info_entry *entry, ··· 827 831 snd_info_set_text_ops(entry, chip, 828 832 cs46xx_dsp_proc_scb_read); 829 833 830 - mutex_lock(&chip->spos_mutex); 834 + guard(mutex)(&chip->spos_mutex); 831 835 /* register/update SCB's entries on proc */ 832 836 for (i = 0; i < ins->nscb; ++i) { 833 837 if (ins->scbs[i].deleted) continue; 834 838 835 839 cs46xx_dsp_proc_register_scb_desc (chip, (ins->scbs + i)); 836 840 } 837 - mutex_unlock(&chip->spos_mutex); 838 841 839 842 return 0; 840 843 } ··· 846 851 if (!ins) 847 852 return 0; 848 853 849 - mutex_lock(&chip->spos_mutex); 850 - for (i = 0; i < ins->nscb; ++i) { 851 - if (ins->scbs[i].deleted) continue; 852 - cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) ); 854 + scoped_guard(mutex, &chip->spos_mutex) { 855 + for (i = 0; i < ins->nscb; ++i) { 856 + if (ins->scbs[i].deleted) 857 + continue; 858 + cs46xx_dsp_proc_free_scb_desc((ins->scbs + i)); 859 + } 853 860 } 854 - mutex_unlock(&chip->spos_mutex); 855 861 856 862 snd_info_free_entry(ins->proc_dsp_dir); 857 863 ins->proc_dsp_dir = NULL; ··· 1673 1677 if (snd_BUG_ON(!ins->spdif_in_src)) 1674 1678 return -EINVAL; 1675 1679 1676 - mutex_lock(&chip->spos_mutex); 1680 + guard(mutex)(&chip->spos_mutex); 1677 1681 1678 1682 if ( ! (ins->spdif_status_out & DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED) ) { 1679 1683 /* time countdown enable */ ··· 1717 1721 1718 1722 /* monitor state */ 1719 1723 ins->spdif_status_in = 1; 1720 - mutex_unlock(&chip->spos_mutex); 1721 1724 1722 1725 return 0; 1723 1726 } ··· 1730 1735 if (snd_BUG_ON(!ins->spdif_in_src)) 1731 1736 return -EINVAL; 1732 1737 1733 - mutex_lock(&chip->spos_mutex); 1738 + scoped_guard(mutex, &chip->spos_mutex) { 1739 + /* Remove the asynchronous receiver SCB */ 1740 + cs46xx_dsp_remove_scb(chip, ins->asynch_rx_scb); 1741 + ins->asynch_rx_scb = NULL; 1734 1742 1735 - /* Remove the asynchronous receiver SCB */ 1736 - cs46xx_dsp_remove_scb (chip,ins->asynch_rx_scb); 1737 - ins->asynch_rx_scb = NULL; 1743 + cs46xx_src_unlink(chip, ins->spdif_in_src); 1738 1744 1739 - cs46xx_src_unlink(chip,ins->spdif_in_src); 1740 - 1741 - /* monitor state */ 1742 - ins->spdif_status_in = 0; 1743 - mutex_unlock(&chip->spos_mutex); 1745 + /* monitor state */ 1746 + ins->spdif_status_in = 0; 1747 + } 1744 1748 1745 1749 /* restore amplifier */ 1746 1750 chip->active_ctrl(chip, -1); ··· 1757 1763 if (snd_BUG_ON(!ins->ref_snoop_scb)) 1758 1764 return -EINVAL; 1759 1765 1760 - mutex_lock(&chip->spos_mutex); 1766 + guard(mutex)(&chip->spos_mutex); 1761 1767 ins->pcm_input = cs46xx_add_record_source(chip,ins->ref_snoop_scb,PCMSERIALIN_PCM_SCB_ADDR, 1762 1768 "PCMSerialInput_Wave"); 1763 - mutex_unlock(&chip->spos_mutex); 1764 1769 1765 1770 return 0; 1766 1771 } ··· 1771 1778 if (snd_BUG_ON(!ins->pcm_input)) 1772 1779 return -EINVAL; 1773 1780 1774 - mutex_lock(&chip->spos_mutex); 1781 + guard(mutex)(&chip->spos_mutex); 1775 1782 cs46xx_dsp_remove_scb (chip,ins->pcm_input); 1776 1783 ins->pcm_input = NULL; 1777 - mutex_unlock(&chip->spos_mutex); 1778 1784 1779 1785 return 0; 1780 1786 } ··· 1787 1795 if (snd_BUG_ON(!ins->codec_in_scb)) 1788 1796 return -EINVAL; 1789 1797 1790 - mutex_lock(&chip->spos_mutex); 1798 + guard(mutex)(&chip->spos_mutex); 1791 1799 ins->adc_input = cs46xx_add_record_source(chip,ins->codec_in_scb,PCMSERIALIN_SCB_ADDR, 1792 1800 "PCMSerialInput_ADC"); 1793 - mutex_unlock(&chip->spos_mutex); 1794 1801 1795 1802 return 0; 1796 1803 } ··· 1801 1810 if (snd_BUG_ON(!ins->adc_input)) 1802 1811 return -EINVAL; 1803 1812 1804 - mutex_lock(&chip->spos_mutex); 1813 + guard(mutex)(&chip->spos_mutex); 1805 1814 cs46xx_dsp_remove_scb (chip,ins->adc_input); 1806 1815 ins->adc_input = NULL; 1807 - mutex_unlock(&chip->spos_mutex); 1808 1816 1809 1817 return 0; 1810 1818 } ··· 1851 1861 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 1852 1862 struct dsp_scb_descriptor * scb; 1853 1863 1854 - mutex_lock(&chip->spos_mutex); 1864 + guard(mutex)(&chip->spos_mutex); 1855 1865 1856 1866 /* main output */ 1857 1867 scb = ins->master_mix_scb->sub_list_ptr; ··· 1870 1880 ins->dac_volume_left = left; 1871 1881 ins->dac_volume_right = right; 1872 1882 1873 - mutex_unlock(&chip->spos_mutex); 1874 - 1875 1883 return 0; 1876 1884 } 1877 1885 ··· 1877 1889 { 1878 1890 struct dsp_spos_instance * ins = chip->dsp_spos_instance; 1879 1891 1880 - mutex_lock(&chip->spos_mutex); 1892 + guard(mutex)(&chip->spos_mutex); 1881 1893 1882 1894 if (ins->asynch_rx_scb != NULL) 1883 1895 cs46xx_dsp_scb_set_volume (chip,ins->asynch_rx_scb, ··· 1885 1897 1886 1898 ins->spdif_input_volume_left = left; 1887 1899 ins->spdif_input_volume_right = right; 1888 - 1889 - mutex_unlock(&chip->spos_mutex); 1890 1900 1891 1901 return 0; 1892 1902 }
+1 -2
sound/pci/cs46xx/dsp_spos_scb_lib.c
··· 63 63 int j,col; 64 64 void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET; 65 65 66 - mutex_lock(&chip->spos_mutex); 66 + guard(mutex)(&chip->spos_mutex); 67 67 snd_iprintf(buffer,"%04x %s:\n",scb->address,scb->scb_name); 68 68 69 69 for (col = 0,j = 0;j < 0x10; j++,col++) { ··· 91 91 scb->task_entry->address); 92 92 93 93 snd_iprintf(buffer,"index [%d] ref_count [%d]\n",scb->index,scb->ref_count); 94 - mutex_unlock(&chip->spos_mutex); 95 94 } 96 95 #endif 97 96