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

ALSA: echoaudio: Allocate resources with device-managed APIs

This patch converts the resource management in PCI echoaudio drivers
with devres as a clean up. Each manual resource management is
converted with the corresponding devres helper, the page allocations
are done with the devres helper, and the card object release is
managed now via card->private_free instead of a lowlevel snd_device.
The irq handler is still managed manually because it's re-acquired at
PM suspend/resume.

This should give no user-visible functional changes.

Link: https://lore.kernel.org/r/20210715075941.23332-33-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

+48 -122
+47 -121
sound/pci/echoaudio/echoaudio.c
··· 1882 1882 Module construction / destruction 1883 1883 ******************************************************************************/ 1884 1884 1885 - static int snd_echo_free(struct echoaudio *chip) 1885 + static void snd_echo_free(struct snd_card *card) 1886 1886 { 1887 + struct echoaudio *chip = card->private_data; 1888 + 1887 1889 if (chip->comm_page) 1888 1890 rest_in_peace(chip); 1889 1891 1890 1892 if (chip->irq >= 0) 1891 1893 free_irq(chip->irq, chip); 1892 1894 1893 - if (chip->comm_page) 1894 - snd_dma_free_pages(&chip->commpage_dma_buf); 1895 - 1896 - iounmap(chip->dsp_registers); 1897 - release_and_free_resource(chip->iores); 1898 - pci_disable_device(chip->pci); 1899 - 1900 1895 /* release chip data */ 1901 1896 free_firmware_cache(chip); 1902 - kfree(chip); 1903 - return 0; 1904 1897 } 1905 - 1906 - 1907 - 1908 - static int snd_echo_dev_free(struct snd_device *device) 1909 - { 1910 - struct echoaudio *chip = device->device_data; 1911 - 1912 - return snd_echo_free(chip); 1913 - } 1914 - 1915 - 1916 1898 1917 1899 /* <--snd_echo_probe() */ 1918 1900 static int snd_echo_create(struct snd_card *card, 1919 - struct pci_dev *pci, 1920 - struct echoaudio **rchip) 1901 + struct pci_dev *pci) 1921 1902 { 1922 - struct echoaudio *chip; 1903 + struct echoaudio *chip = card->private_data; 1923 1904 int err; 1924 1905 size_t sz; 1925 - static const struct snd_device_ops ops = { 1926 - .dev_free = snd_echo_dev_free, 1927 - }; 1928 - 1929 - *rchip = NULL; 1930 1906 1931 1907 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0); 1932 1908 1933 - err = pci_enable_device(pci); 1909 + err = pcim_enable_device(pci); 1934 1910 if (err < 0) 1935 1911 return err; 1936 1912 pci_set_master(pci); 1937 1913 1938 1914 /* Allocate chip if needed */ 1939 - if (!*rchip) { 1940 - chip = kzalloc(sizeof(*chip), GFP_KERNEL); 1941 - if (!chip) { 1942 - pci_disable_device(pci); 1943 - return -ENOMEM; 1944 - } 1945 - dev_dbg(card->dev, "chip=%p\n", chip); 1946 - spin_lock_init(&chip->lock); 1947 - chip->card = card; 1948 - chip->pci = pci; 1949 - chip->irq = -1; 1950 - chip->opencount = 0; 1951 - mutex_init(&chip->mode_mutex); 1952 - chip->can_set_rate = 1; 1953 - } else { 1954 - /* If this was called from the resume function, chip is 1955 - * already allocated and it contains current card settings. 1956 - */ 1957 - chip = *rchip; 1958 - } 1915 + spin_lock_init(&chip->lock); 1916 + chip->card = card; 1917 + chip->pci = pci; 1918 + chip->irq = -1; 1919 + chip->opencount = 0; 1920 + mutex_init(&chip->mode_mutex); 1921 + chip->can_set_rate = 1; 1959 1922 1960 1923 /* PCI resource allocation */ 1924 + err = pci_request_regions(pci, ECHOCARD_NAME); 1925 + if (err < 0) 1926 + return err; 1927 + 1961 1928 chip->dsp_registers_phys = pci_resource_start(pci, 0); 1962 1929 sz = pci_resource_len(pci, 0); 1963 1930 if (sz > PAGE_SIZE) 1964 1931 sz = PAGE_SIZE; /* We map only the required part */ 1965 1932 1966 - chip->iores = request_mem_region(chip->dsp_registers_phys, sz, 1967 - ECHOCARD_NAME); 1968 - if (!chip->iores) { 1969 - dev_err(chip->card->dev, "cannot get memory region\n"); 1970 - snd_echo_free(chip); 1971 - return -EBUSY; 1972 - } 1973 - chip->dsp_registers = ioremap(chip->dsp_registers_phys, sz); 1933 + chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz); 1974 1934 if (!chip->dsp_registers) { 1975 1935 dev_err(chip->card->dev, "ioremap failed\n"); 1976 - snd_echo_free(chip); 1977 1936 return -ENOMEM; 1978 1937 } 1979 1938 1980 1939 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED, 1981 1940 KBUILD_MODNAME, chip)) { 1982 1941 dev_err(chip->card->dev, "cannot grab irq\n"); 1983 - snd_echo_free(chip); 1984 1942 return -EBUSY; 1985 1943 } 1986 1944 chip->irq = pci->irq; ··· 1946 1988 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n", 1947 1989 chip->pci, chip->irq, chip->pci->subsystem_device); 1948 1990 1991 + card->private_free = snd_echo_free; 1992 + 1949 1993 /* Create the DSP comm page - this is the area of memory used for most 1950 1994 of the communication with the DSP, which accesses it via bus mastering */ 1951 - if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 1952 - sizeof(struct comm_page), 1953 - &chip->commpage_dma_buf) < 0) { 1954 - dev_err(chip->card->dev, "cannot allocate the comm page\n"); 1955 - snd_echo_free(chip); 1995 + chip->commpage_dma_buf = 1996 + snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV, 1997 + sizeof(struct comm_page)); 1998 + if (!chip->commpage_dma_buf) 1956 1999 return -ENOMEM; 1957 - } 1958 - chip->comm_page_phys = chip->commpage_dma_buf.addr; 1959 - chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area; 2000 + chip->comm_page_phys = chip->commpage_dma_buf->addr; 2001 + chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area; 1960 2002 1961 2003 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device); 1962 2004 if (err >= 0) 1963 2005 err = set_mixer_defaults(chip); 1964 2006 if (err < 0) { 1965 2007 dev_err(card->dev, "init_hw err=%d\n", err); 1966 - snd_echo_free(chip); 1967 2008 return err; 1968 2009 } 1969 2010 1970 - err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 1971 - if (err < 0) { 1972 - snd_echo_free(chip); 1973 - return err; 1974 - } 1975 - *rchip = chip; 1976 - /* Init done ! */ 1977 2011 return 0; 1978 2012 } 1979 - 1980 - 1981 2013 1982 2014 /* constructor */ 1983 2015 static int snd_echo_probe(struct pci_dev *pci, ··· 1988 2040 } 1989 2041 1990 2042 i = 0; 1991 - err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 1992 - 0, &card); 2043 + err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 2044 + sizeof(*chip), &card); 1993 2045 if (err < 0) 1994 2046 return err; 2047 + chip = card->private_data; 1995 2048 1996 - chip = NULL; /* Tells snd_echo_create to allocate chip */ 1997 - err = snd_echo_create(card, pci, &chip); 1998 - if (err < 0) { 1999 - snd_card_free(card); 2049 + err = snd_echo_create(card, pci); 2050 + if (err < 0) 2000 2051 return err; 2001 - } 2002 2052 2003 2053 strcpy(card->driver, "Echo_" ECHOCARD_NAME); 2004 2054 strcpy(card->shortname, chip->card_name); ··· 2012 2066 err = snd_echo_new_pcm(chip); 2013 2067 if (err < 0) { 2014 2068 dev_err(chip->card->dev, "new pcm error %d\n", err); 2015 - snd_card_free(card); 2016 2069 return err; 2017 2070 } 2018 2071 ··· 2020 2075 err = snd_echo_midi_create(card, chip); 2021 2076 if (err < 0) { 2022 2077 dev_err(chip->card->dev, "new midi error %d\n", err); 2023 - snd_card_free(card); 2024 2078 return err; 2025 2079 } 2026 2080 } ··· 2029 2085 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip); 2030 2086 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip)); 2031 2087 if (err < 0) 2032 - goto ctl_error; 2088 + return err; 2033 2089 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN 2034 2090 err = snd_ctl_add(chip->card, 2035 2091 snd_ctl_new1(&snd_echo_line_output_gain, chip)); 2036 2092 if (err < 0) 2037 - goto ctl_error; 2093 + return err; 2038 2094 #endif 2039 2095 #else /* ECHOCARD_HAS_VMIXER */ 2040 2096 err = snd_ctl_add(chip->card, 2041 2097 snd_ctl_new1(&snd_echo_pcm_output_gain, chip)); 2042 2098 if (err < 0) 2043 - goto ctl_error; 2099 + return err; 2044 2100 #endif /* ECHOCARD_HAS_VMIXER */ 2045 2101 2046 2102 #ifdef ECHOCARD_HAS_INPUT_GAIN 2047 2103 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip)); 2048 2104 if (err < 0) 2049 - goto ctl_error; 2105 + return err; 2050 2106 #endif 2051 2107 2052 2108 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL 2053 2109 if (!chip->hasnt_input_nominal_level) { 2054 2110 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip)); 2055 2111 if (err < 0) 2056 - goto ctl_error; 2112 + return err; 2057 2113 } 2058 2114 #endif 2059 2115 2060 2116 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL 2061 2117 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip)); 2062 2118 if (err < 0) 2063 - goto ctl_error; 2119 + return err; 2064 2120 #endif 2065 2121 2066 2122 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip)); 2067 2123 if (err < 0) 2068 - goto ctl_error; 2124 + return err; 2069 2125 2070 2126 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip)); 2071 2127 if (err < 0) 2072 - goto ctl_error; 2128 + return err; 2073 2129 2074 2130 #ifdef ECHOCARD_HAS_MONITOR 2075 2131 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip); 2076 2132 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip)); 2077 2133 if (err < 0) 2078 - goto ctl_error; 2134 + return err; 2079 2135 #endif 2080 2136 2081 2137 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE 2082 2138 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip)); 2083 2139 if (err < 0) 2084 - goto ctl_error; 2140 + return err; 2085 2141 #endif 2086 2142 2087 2143 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip)); 2088 2144 if (err < 0) 2089 - goto ctl_error; 2145 + return err; 2090 2146 2091 2147 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH 2092 2148 /* Creates a list of available digital modes */ ··· 2097 2153 2098 2154 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip)); 2099 2155 if (err < 0) 2100 - goto ctl_error; 2156 + return err; 2101 2157 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */ 2102 2158 2103 2159 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK ··· 2111 2167 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip); 2112 2168 err = snd_ctl_add(chip->card, chip->clock_src_ctl); 2113 2169 if (err < 0) 2114 - goto ctl_error; 2170 + return err; 2115 2171 } 2116 2172 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */ 2117 2173 2118 2174 #ifdef ECHOCARD_HAS_DIGITAL_IO 2119 2175 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip)); 2120 2176 if (err < 0) 2121 - goto ctl_error; 2177 + return err; 2122 2178 #endif 2123 2179 2124 2180 #ifdef ECHOCARD_HAS_PHANTOM_POWER 2125 2181 if (chip->has_phantom_power) { 2126 2182 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip)); 2127 2183 if (err < 0) 2128 - goto ctl_error; 2184 + return err; 2129 2185 } 2130 2186 #endif 2131 2187 2132 2188 err = snd_card_register(card); 2133 2189 if (err < 0) 2134 - goto ctl_error; 2190 + return err; 2135 2191 dev_info(card->dev, "Card registered: %s\n", card->longname); 2136 2192 2137 2193 pci_set_drvdata(pci, chip); 2138 2194 dev++; 2139 2195 return 0; 2140 - 2141 - ctl_error: 2142 - dev_err(card->dev, "new control error %d\n", err); 2143 - snd_card_free(card); 2144 - return err; 2145 2196 } 2146 2197 2147 2198 ··· 2238 2299 #define SND_ECHO_PM_OPS NULL 2239 2300 #endif /* CONFIG_PM_SLEEP */ 2240 2301 2241 - 2242 - static void snd_echo_remove(struct pci_dev *pci) 2243 - { 2244 - struct echoaudio *chip; 2245 - 2246 - chip = pci_get_drvdata(pci); 2247 - if (chip) 2248 - snd_card_free(chip->card); 2249 - } 2250 - 2251 - 2252 - 2253 2302 /****************************************************************************** 2254 2303 Everything starts and ends here 2255 2304 ******************************************************************************/ ··· 2247 2320 .name = KBUILD_MODNAME, 2248 2321 .id_table = snd_echo_ids, 2249 2322 .probe = snd_echo_probe, 2250 - .remove = snd_echo_remove, 2251 2323 .driver = { 2252 2324 .pm = SND_ECHO_PM_OPS, 2253 2325 },
+1 -1
sound/pci/echoaudio/echoaudio.h
··· 348 348 struct pci_dev *pci; 349 349 unsigned long dsp_registers_phys; 350 350 struct resource *iores; 351 - struct snd_dma_buffer commpage_dma_buf; 351 + struct snd_dma_buffer *commpage_dma_buf; 352 352 int irq; 353 353 #ifdef ECHOCARD_HAS_MIDI 354 354 struct snd_rawmidi *rmidi;