Skip to content

Commit 3bf75f9

Browse files
tiwaiJaroslav Kysela
authored andcommitted
[ALSA] Clean up PCM codes (take 2)
- Clean up initialization and destruction of substream instance Now snd_pcm_open_substream() alone does most initialization jobs. Add pcm_release callback for cleaning up at snd_pcm_release_substream() - Tidy up PCM oss code Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent bf1bbb5 commit 3bf75f9

6 files changed

Lines changed: 114 additions & 219 deletions

File tree

‎include/sound/pcm.h‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ struct snd_pcm_substream {
369369
/* -- assigned files -- */
370370
struct snd_pcm_file *file;
371371
struct file *ffile;
372+
void (*pcm_release)(struct snd_pcm_substream *);
372373
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
373374
/* -- OSS things -- */
374375
struct snd_pcm_oss_substream oss;
@@ -381,13 +382,10 @@ struct snd_pcm_substream {
381382
struct snd_info_entry *proc_prealloc_entry;
382383
/* misc flags */
383384
unsigned int no_mmap_ctrl: 1;
385+
unsigned int hw_opened: 1;
384386
};
385387

386-
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
387-
#define SUBSTREAM_BUSY(substream) ((substream)->file != NULL || ((substream)->oss.file != NULL))
388-
#else
389388
#define SUBSTREAM_BUSY(substream) ((substream)->file != NULL)
390-
#endif
391389

392390

393391
struct snd_pcm_str {
@@ -468,8 +466,12 @@ int snd_pcm_suspend(struct snd_pcm_substream *substream);
468466
int snd_pcm_suspend_all(struct snd_pcm *pcm);
469467
#endif
470468
int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
471-
int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct snd_pcm_substream **rsubstream);
469+
int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
470+
struct snd_pcm_substream **rsubstream);
472471
void snd_pcm_release_substream(struct snd_pcm_substream *substream);
472+
int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
473+
struct snd_pcm_substream **rsubstream);
474+
void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
473475
void snd_pcm_vma_notify_data(void *client, void *data);
474476
int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
475477

‎include/sound/pcm_oss.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ struct snd_pcm_oss_file {
7070
struct snd_pcm_oss_substream {
7171
unsigned oss: 1; /* oss mode */
7272
struct snd_pcm_oss_setup *setup; /* active setup */
73-
struct snd_pcm_oss_file *file;
7473
};
7574

7675
struct snd_pcm_oss_stream {

‎sound/core/oss/pcm_oss.c‎

Lines changed: 44 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,18 @@ static struct snd_pcm_oss_setup *snd_pcm_oss_look_for_setup(struct snd_pcm *pcm,
16711671
return NULL;
16721672
}
16731673

1674+
static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
1675+
{
1676+
struct snd_pcm_runtime *runtime;
1677+
runtime = substream->runtime;
1678+
vfree(runtime->oss.buffer);
1679+
runtime->oss.buffer = NULL;
1680+
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1681+
snd_pcm_oss_plugin_clear(substream);
1682+
#endif
1683+
substream->oss.oss = 0;
1684+
}
1685+
16741686
static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
16751687
struct snd_pcm_oss_setup *setup,
16761688
int minor)
@@ -1679,6 +1691,10 @@ static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
16791691

16801692
substream->oss.oss = 1;
16811693
substream->oss.setup = setup;
1694+
if (setup->nonblock)
1695+
substream->ffile->f_flags |= O_NONBLOCK;
1696+
else
1697+
substream->ffile->f_flags &= ~O_NONBLOCK;
16821698
runtime = substream->runtime;
16831699
runtime->oss.params = 1;
16841700
runtime->oss.trigger = 1;
@@ -1697,18 +1713,7 @@ static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
16971713
runtime->oss.fragshift = 0;
16981714
runtime->oss.maxfrags = 0;
16991715
runtime->oss.subdivision = 0;
1700-
}
1701-
1702-
static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
1703-
{
1704-
struct snd_pcm_runtime *runtime;
1705-
runtime = substream->runtime;
1706-
vfree(runtime->oss.buffer);
1707-
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
1708-
snd_pcm_oss_plugin_clear(substream);
1709-
#endif
1710-
substream->oss.file = NULL;
1711-
substream->oss.oss = 0;
1716+
substream->pcm_release = snd_pcm_oss_release_substream;
17121717
}
17131718

17141719
static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
@@ -1717,23 +1722,8 @@ static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
17171722
snd_assert(pcm_oss_file != NULL, return -ENXIO);
17181723
for (cidx = 0; cidx < 2; ++cidx) {
17191724
struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
1720-
struct snd_pcm_runtime *runtime;
1721-
if (substream == NULL)
1722-
continue;
1723-
runtime = substream->runtime;
1724-
1725-
snd_pcm_stream_lock_irq(substream);
1726-
if (snd_pcm_running(substream))
1727-
snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1728-
snd_pcm_stream_unlock_irq(substream);
1729-
if (substream->ffile != NULL) {
1730-
if (substream->ops->hw_free != NULL)
1731-
substream->ops->hw_free(substream);
1732-
substream->ops->close(substream);
1733-
substream->ffile = NULL;
1734-
}
1735-
snd_pcm_oss_release_substream(substream);
1736-
snd_pcm_release_substream(substream);
1725+
if (substream)
1726+
snd_pcm_release_substream(substream);
17371727
}
17381728
kfree(pcm_oss_file);
17391729
return 0;
@@ -1743,12 +1733,11 @@ static int snd_pcm_oss_open_file(struct file *file,
17431733
struct snd_pcm *pcm,
17441734
struct snd_pcm_oss_file **rpcm_oss_file,
17451735
int minor,
1746-
struct snd_pcm_oss_setup *psetup,
1747-
struct snd_pcm_oss_setup *csetup)
1736+
struct snd_pcm_oss_setup **setup)
17481737
{
1749-
int err = 0;
1738+
int idx, err;
17501739
struct snd_pcm_oss_file *pcm_oss_file;
1751-
struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
1740+
struct snd_pcm_substream *substream;
17521741
unsigned int f_mode = file->f_mode;
17531742

17541743
snd_assert(rpcm_oss_file != NULL, return -EINVAL);
@@ -1761,73 +1750,31 @@ static int snd_pcm_oss_open_file(struct file *file,
17611750
if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
17621751
(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
17631752
f_mode = FMODE_WRITE;
1764-
if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
1765-
if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1766-
&psubstream)) < 0) {
1753+
1754+
for (idx = 0; idx < 2; idx++) {
1755+
if (! setup[idx] || setup[idx]->disable)
1756+
continue;
1757+
if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
1758+
if (! (f_mode & FMODE_WRITE))
1759+
continue;
1760+
} else {
1761+
if (! (f_mode & FMODE_READ))
1762+
continue;
1763+
}
1764+
err = snd_pcm_open_substream(pcm, idx, file, &substream);
1765+
if (err < 0) {
17671766
snd_pcm_oss_release_file(pcm_oss_file);
17681767
return err;
17691768
}
1770-
pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
1771-
}
1772-
if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
1773-
if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
1774-
&csubstream)) < 0) {
1775-
if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
1776-
snd_pcm_oss_release_file(pcm_oss_file);
1777-
return err;
1778-
} else {
1779-
csubstream = NULL;
1780-
}
1781-
}
1782-
pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
1769+
1770+
pcm_oss_file->streams[idx] = substream;
1771+
snd_pcm_oss_init_substream(substream, setup[idx], minor);
17831772
}
17841773

1785-
if (psubstream == NULL && csubstream == NULL) {
1774+
if (! pcm_oss_file->streams[0] && pcm_oss_file->streams[1]) {
17861775
snd_pcm_oss_release_file(pcm_oss_file);
17871776
return -EINVAL;
17881777
}
1789-
if (psubstream != NULL) {
1790-
psubstream->oss.file = pcm_oss_file;
1791-
err = snd_pcm_hw_constraints_init(psubstream);
1792-
if (err < 0) {
1793-
snd_printd("snd_pcm_hw_constraint_init failed\n");
1794-
snd_pcm_oss_release_file(pcm_oss_file);
1795-
return err;
1796-
}
1797-
if ((err = psubstream->ops->open(psubstream)) < 0) {
1798-
snd_pcm_oss_release_file(pcm_oss_file);
1799-
return err;
1800-
}
1801-
psubstream->ffile = file;
1802-
err = snd_pcm_hw_constraints_complete(psubstream);
1803-
if (err < 0) {
1804-
snd_printd("snd_pcm_hw_constraint_complete failed\n");
1805-
snd_pcm_oss_release_file(pcm_oss_file);
1806-
return err;
1807-
}
1808-
snd_pcm_oss_init_substream(psubstream, psetup, minor);
1809-
}
1810-
if (csubstream != NULL) {
1811-
csubstream->oss.file = pcm_oss_file;
1812-
err = snd_pcm_hw_constraints_init(csubstream);
1813-
if (err < 0) {
1814-
snd_printd("snd_pcm_hw_constraint_init failed\n");
1815-
snd_pcm_oss_release_file(pcm_oss_file);
1816-
return err;
1817-
}
1818-
if ((err = csubstream->ops->open(csubstream)) < 0) {
1819-
snd_pcm_oss_release_file(pcm_oss_file);
1820-
return err;
1821-
}
1822-
csubstream->ffile = file;
1823-
err = snd_pcm_hw_constraints_complete(csubstream);
1824-
if (err < 0) {
1825-
snd_printd("snd_pcm_hw_constraint_complete failed\n");
1826-
snd_pcm_oss_release_file(pcm_oss_file);
1827-
return err;
1828-
}
1829-
snd_pcm_oss_init_substream(csubstream, csetup, minor);
1830-
}
18311778

18321779
file->private_data = pcm_oss_file;
18331780
*rpcm_oss_file = pcm_oss_file;
@@ -1852,7 +1799,7 @@ static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18521799
char task_name[32];
18531800
struct snd_pcm *pcm;
18541801
struct snd_pcm_oss_file *pcm_oss_file;
1855-
struct snd_pcm_oss_setup *psetup = NULL, *csetup = NULL;
1802+
struct snd_pcm_oss_setup *setup[2];
18561803
int nonblock;
18571804
wait_queue_t wait;
18581805

@@ -1873,23 +1820,13 @@ static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18731820
err = -EFAULT;
18741821
goto __error;
18751822
}
1823+
memset(setup, 0, sizeof(*setup));
18761824
if (file->f_mode & FMODE_WRITE)
1877-
psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
1825+
setup[0] = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
18781826
if (file->f_mode & FMODE_READ)
1879-
csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
1827+
setup[1] = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
18801828

18811829
nonblock = !!(file->f_flags & O_NONBLOCK);
1882-
if (psetup && !psetup->disable) {
1883-
if (psetup->nonblock)
1884-
nonblock = 1;
1885-
else if (psetup->block)
1886-
nonblock = 0;
1887-
} else if (csetup && !csetup->disable) {
1888-
if (csetup->nonblock)
1889-
nonblock = 1;
1890-
else if (csetup->block)
1891-
nonblock = 0;
1892-
}
18931830
if (!nonblock)
18941831
nonblock = nonblock_open;
18951832

@@ -1898,7 +1835,7 @@ static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18981835
mutex_lock(&pcm->open_mutex);
18991836
while (1) {
19001837
err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1901-
iminor(inode), psetup, csetup);
1838+
iminor(inode), setup);
19021839
if (err >= 0)
19031840
break;
19041841
if (err == -EAGAIN) {

‎sound/core/pcm.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,9 @@ static void snd_pcm_tick_timer_func(unsigned long data)
777777
snd_pcm_tick_elapsed(substream);
778778
}
779779

780-
int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
781-
struct snd_pcm_substream **rsubstream)
780+
int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream,
781+
struct file *file,
782+
struct snd_pcm_substream **rsubstream)
782783
{
783784
struct snd_pcm_str * pstr;
784785
struct snd_pcm_substream *substream;
@@ -793,7 +794,7 @@ int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
793794
*rsubstream = NULL;
794795
snd_assert(pcm != NULL, return -ENXIO);
795796
pstr = &pcm->streams[stream];
796-
if (pstr->substream == NULL)
797+
if (pstr->substream == NULL || pstr->substream_count == 0)
797798
return -ENODEV;
798799

799800
card = pcm->card;
@@ -807,8 +808,6 @@ int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
807808
}
808809
up_read(&card->controls_rwsem);
809810

810-
if (pstr->substream_count == 0)
811-
return -ENODEV;
812811
switch (stream) {
813812
case SNDRV_PCM_STREAM_PLAYBACK:
814813
if (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX) {
@@ -874,12 +873,13 @@ int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
874873

875874
substream->runtime = runtime;
876875
substream->private_data = pcm->private_data;
876+
substream->ffile = file;
877877
pstr->substream_opened++;
878878
*rsubstream = substream;
879879
return 0;
880880
}
881881

882-
void snd_pcm_release_substream(struct snd_pcm_substream *substream)
882+
void snd_pcm_detach_substream(struct snd_pcm_substream *substream)
883883
{
884884
struct snd_pcm_runtime *runtime;
885885
substream->file = NULL;

‎sound/core/pcm_lib.c‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,19 +2299,7 @@ snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const v
22992299
if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
23002300
return -EBADFD;
23012301

2302-
snd_assert(substream->ffile != NULL, return -ENXIO);
23032302
nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2304-
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2305-
if (substream->oss.oss) {
2306-
struct snd_pcm_oss_setup *setup = substream->oss.setup;
2307-
if (setup != NULL) {
2308-
if (setup->nonblock)
2309-
nonblock = 1;
2310-
else if (setup->block)
2311-
nonblock = 0;
2312-
}
2313-
}
2314-
#endif
23152303

23162304
if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
23172305
runtime->channels > 1)
@@ -2374,19 +2362,7 @@ snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
23742362
if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
23752363
return -EBADFD;
23762364

2377-
snd_assert(substream->ffile != NULL, return -ENXIO);
23782365
nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2379-
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2380-
if (substream->oss.oss) {
2381-
struct snd_pcm_oss_setup *setup = substream->oss.setup;
2382-
if (setup != NULL) {
2383-
if (setup->nonblock)
2384-
nonblock = 1;
2385-
else if (setup->block)
2386-
nonblock = 0;
2387-
}
2388-
}
2389-
#endif
23902366

23912367
if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
23922368
return -EINVAL;
@@ -2596,19 +2572,7 @@ snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __u
25962572
if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
25972573
return -EBADFD;
25982574

2599-
snd_assert(substream->ffile != NULL, return -ENXIO);
26002575
nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2601-
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2602-
if (substream->oss.oss) {
2603-
struct snd_pcm_oss_setup *setup = substream->oss.setup;
2604-
if (setup != NULL) {
2605-
if (setup->nonblock)
2606-
nonblock = 1;
2607-
else if (setup->block)
2608-
nonblock = 0;
2609-
}
2610-
}
2611-
#endif
26122576
if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
26132577
return -EINVAL;
26142578
return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
@@ -2665,20 +2629,7 @@ snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
26652629
if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
26662630
return -EBADFD;
26672631

2668-
snd_assert(substream->ffile != NULL, return -ENXIO);
26692632
nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2670-
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2671-
if (substream->oss.oss) {
2672-
struct snd_pcm_oss_setup *setup = substream->oss.setup;
2673-
if (setup != NULL) {
2674-
if (setup->nonblock)
2675-
nonblock = 1;
2676-
else if (setup->block)
2677-
nonblock = 0;
2678-
}
2679-
}
2680-
#endif
2681-
26822633
if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
26832634
return -EINVAL;
26842635
return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);

0 commit comments

Comments
 (0)