CS5535 AUDIO ALSA driver
Subsystems (3)
[email protected]
maintainerPATCH 1/1 CS5535 AUDIO ALSA driver
Hi Jaroslav, Takashi, Martin, alsa and kernel folk,
Appended is my patch adding support for the CS5535 Audio device. I've fixed up some errors as per Takashi's advice from the thread:
http://lkml.org/lkml/2005/9/15/119
This patch is against 2.6.13.1. Please let me know if there are any issues.
Thanks,
Jaya Kumar
---
CREDITS | 1 MAINTAINERS | 5 include/linux/pci_ids.h | 4 sound/pci/Kconfig | 13 sound/pci/Makefile | 1 sound/pci/cs5535audio/Makefile | 8 sound/pci/cs5535audio/cs5535audio.c | 412 ++++++++++++++++++++++++++++++ sound/pci/cs5535audio/cs5535audio.h | 124 +++++++++ sound/pci/cs5535audio/cs5535audio_pcm.c | 431 ++++++++++++++++++++++++++++++++ 9 files changed, 999 insertions(+)
CREDITS +1 -0
@@ -1884,6 +1884,7 @@ N: Jaya Kumar E: [email protected] W: http://www.intworks.biz D: Arc monochrome LCD framebuffer driver, x86 reboot fixups +D: pirq addr, CS5535 alsa audio driver S: Gurgaon, India S: Kuala Lumpur, Malaysia
include/linux/pci_ids.h +4 -0
@@ -392,6 +392,10 @@#define PCI_DEVICE_ID_NS_87560_USB 0x0012#define PCI_DEVICE_ID_NS_83815 0x0020#define PCI_DEVICE_ID_NS_83820 0x0022 +#define PCI_DEVICE_ID_NS_CS5535_IDE 0x002d +#define PCI_DEVICE_ID_NS_CS5535_AUDIO 0x002e +#define PCI_DEVICE_ID_NS_CS5535_USB 0x002f +#define PCI_DEVICE_ID_NS_CS5535_VIDEO 0x0030#define PCI_DEVICE_ID_NS_SCx200_BRIDGE 0x0500#define PCI_DEVICE_ID_NS_SCx200_SMI 0x0501#define PCI_DEVICE_ID_NS_SCx200_IDE 0x0502
MAINTAINERS +5 -0
@@ -621,6 +621,11 @@ M: [email protected] L: [email protected] S: Maintained +CS5535 Audio ALSA driver +P: Jaya Kumar +M: [email protected] +S: Maintained + CYBERPRO FB DRIVER P: Russell King M: [email protected]
sound/pci/cs5535audio/cs5535audio.c +412 -0
412 lines not on this page · raw message
sound/pci/cs5535audio/cs5535audio.h +124 -0
124 lines not on this page · raw message
sound/pci/cs5535audio/cs5535audio_pcm.c +431 -0
431 lines not on this page · raw message
sound/pci/cs5535audio/Makefile +8 -0
@@ -0,0 +1,8 @@ +# +# Makefile for cs5535audio +# + +snd-cs5535audio-objs := cs5535audio.o cs5535audio_pcm.o + +# Toplevel Module Dependency +obj-$(CONFIG_SND_CS5535AUDIO) += snd-cs5535audio.o
sound/pci/Kconfig +13 -0
@@ -350,6 +350,19 @@ config SND_ENS1370 To compile this driver as a module, choose M here: the module will be called snd-ens1370. +config SND_CS5535AUDIO + tristate "CS5535 Audio" + depends on SND + select SND_PCM + select SND_AC97_CODEC + help + Say Y here to include support for audio on CS5535 chips. It is + referred to as NS CS5535 IO or AMD CS5535 IO companion in + various literature. + + To compile this driver as a module, choose M here: the module + will be called snd-cs5535audio. + config SND_ENS1371 tristate "(Creative) Ensoniq AudioPCI 1371/1373" depends on SND
sound/pci/Makefile +1 -0
@@ -52,6 +52,7 @@ obj-$(CONFIG_SND) += \ au88x0/ \ ca0106/ \ cs46xx/ \ + cs5535audio/ \ emu10k1/ \ hda/ \ ice1712/ \
Quoted 6 lines · [email protected] wrote:
Hi Jaroslav, Takashi, Martin, alsa and kernel folk,
Appended is my patch adding support for the CS5535 Audio device. I've fixed up some errors as per Takashi's advice from the thread:
Some triviata:
MAINTAINERS M: [email protected] · line 628
Quoted 10 lines
+++ linux-2.6.13.1/MAINTAINERS 2005-09-15 15:00:19.000000000 +0800
MAINTAINERS +5
-0@@ -621,6 +621,11 @@ M: [email protected] L: [email protected] S: Maintained +CS5535 Audio ALSA driver +P: Jaya Kumar +M: [email protected] +S: Maintained +
Neat, thanks.
Quoted 7 lines
sound/pci/cs5535audio/cs5535audio.c +7
-0+ +#include <asm/io.h> +#include <linux/delay.h> +#include <linux/interrupt.h> +#include <linux/init.h> +#include <linux/pci.h> +#include <linux/slab.h>
sound/pci/cs5535audio/cs5535audio.c line 39
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+#include <linux/moduleparam.h> +#include <sound/driver.h> +#include <sound/core.h> +#include <sound/control.h> +#include <sound/pcm.h> +#include <sound/rawmidi.h> +#include <sound/ac97_codec.h> +#include <sound/initval.h> +#include <sound/asoundef.h> +#include "cs5535audio.h"
By convention we put the asm/ includes after the linux/ includes.
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static unsigned short snd_cs5535audio_codec_read(cs5535audio_t *cs5535au,
typedefs are unpopular in-kernel. We generally don't get too fussed if a driver maintainer really wants them there. The main objection is that we now have two names for the same thing. Plus they cannot be used when forward-declaring a structure.
sound/pci/cs5535audio/cs5535audio.c line 78
Quoted 11 lines
sound/pci/cs5535audio/cs5535audio.c +11
-0+ + timeout =500 ; +do { + tmp =cs_readl (ACC_CODEC_CNTL ); +if (!(tmp &CMD_NEW )) +break ; +msleep (10 ); + }while (timeout--); +if (!timeout) { +snd_printk (KERN_ERR "Failure writing to cs5535 codec\n" ); + }
This isn't right. `timeout' will have a value of -1 if we timed out. (several instances).
sound/pci/cs5535audio/cs5535audio.c line 90
Quoted 11 lines
sound/pci/cs5535audio/cs5535audio.c +11
-0+ timeout =50 ; +do { + val =cs_readl (ACC_CODEC_STATUS ); +if ( (val &STS_NEW ) && + ((unsigned long ) reg == ((0xFF000000 & val)>>24 )) ) +break ; +msleep (10 ); + }while (timeout--); +if (!timeout) { +snd_printk (KERN_ERR "Failure reading cs5535 codec\n" ); + }
Ditto. Plus we prefer to not put braces around a single statement like this.
sound/pci/cs5535audio/cs5535audio.c line 118
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+ timeout =50 ; +do { + tmp =cs_readl (ACC_CODEC_CNTL ); +if (!(tmp &CMD_NEW )) +break ; +msleep (10 ); + }while (timeout--); +if (!timeout) { +snd_printk (KERN_ERR "Failure writing to cs5535 codec\n" ); + }
Again. Perhaps a helper function so this code (and its bug) don't get duplicated so much?
sound/pci/cs5535audio/cs5535audio.c line 125
Quoted 1 line
sound/pci/cs5535audio/cs5535audio.c +1
-0+snd_cs5535audio_codec_write (cs5535au,reg,val);
We normally put a space after commas.
sound/pci/cs5535audio/cs5535audio.c line 152
Quoted 1 line
sound/pci/cs5535audio/cs5535audio.c +1
-0+if ((err =snd_ac97_bus (card,0 , &ops,NULL , &pbus)) <0 )
And so do you ;)
sound/pci/cs5535audio/cs5535audio.c line 196
Quoted 4 lines
sound/pci/cs5535audio/cs5535audio.c +4
-0+if (bm_stat &EOP ) { +cs5535audio_dma_t *dma; + dma = (cs5535audio_dma_t *) + cs5535au->capture_substream->runtime->private_data;
Unneeded typecast.
Review
Quoted 1 line
(quoted diff) +1
-0+ dma->index = (++(dma->index)) % dma->periods;
Is no locking needed for dma->index?
sound/pci/cs5535audio/cs5535audio.c line 225
Quoted 7 lines
sound/pci/cs5535audio/cs5535audio.c +7
-0+switch (count) { +case IRQ_STS : +cs_readl (ACC_GPIO_STATUS ); +break ; +case WU_IRQ_STS : +cs_readl (ACC_GPIO_STATUS ); +break ;
We normally do
switch (count) {
case IRQ_STS:
cs_readl(ACC_GPIO_STATUS);
break;
case WU_IRQ_STS:
cs_readl(ACC_GPIO_STATUS);
break;Just to save a tabstop.
sound/pci/cs5535audio/cs5535audio.c line 258
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+break ; +default : +snd_printk (KERN_ERR +"Unexpected irq src\n" ); +break ; + } + } + } +return IRQ_HANDLED ; +}
Perhaps the default case should return IRQ_NONE.
sound/pci/cs5535audio/cs5535audio.c line 266
Quoted 7 lines
sound/pci/cs5535audio/cs5535audio.c +7
-0+static int snd_cs5535audio_free (cs5535audio_t *cs5535au) +{ +synchronize_irq (cs5535au->irq); +pci_set_power_state (cs5535au->pci,3 ); + +if (cs5535au->irq >=0 ) +free_irq (cs5535au->irq, (void *)cs5535au);
Unneeded typecast.
Quoted 6 lines
sound/pci/cs5535audio/cs5535audio.c +6
-0+static int __devinit snd_cs5535audio_create (snd_card_t *card, +struct pci_dev *pci, +cs5535audio_t **rcs5535au) +{ +cs5535audio_t *cs5535au; +
sound/pci/cs5535audio/cs5535audio.c line 295
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+int err; +static snd_device_ops_t ops = { + .dev_free = snd_cs5535audio_dev_free, + }; + + *rcs5535au =NULL ; +if ((err =pci_enable_device (pci)) <0 ) +return err; + + cs5535au =kcalloc (1 ,sizeof (*cs5535au),GFP_KERNEL );
We have kzalloc() now.
Quoted 6 lines
sound/pci/cs5535audio/cs5535audio.c +6
-0+if (cs5535au ==NULL ) { +pci_disable_device (pci); +return -ENOMEM ; + } +spin_lock_init (&cs5535au->reg_lock); + cs5535au->card = card;
sound/pci/cs5535audio/cs5535audio.c line 311
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+ cs5535au->pci = pci; + cs5535au->irq =-1 ; +if ((err =pci_request_regions (pci,"CS5535 Audio" )) <0 ) { +kfree (cs5535au); +pci_disable_device (pci); +return err; + } + cs5535au->port =pci_resource_start (pci,0 ); +if (request_irq (pci->irq, snd_cs5535audio_interrupt, +SA_INTERRUPT |SA_SHIRQ ,"CS5535 Audio" , (void *) cs5535au)) {
Unneeded typecast.
Quoted 9 lines
sound/pci/cs5535audio/cs5535audio.c +9
-0+snd_printk ("unable to grab IRQ %d\n" , pci->irq); +snd_cs5535audio_free (cs5535au); +return -EBUSY ; + } + + cs5535au->irq = pci->irq; +pci_set_master (pci); + + if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
sound/pci/cs5535audio/cs5535audio.c line 330
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+ cs5535au, &ops)) < 0) { +snd_cs5535audio_free (cs5535au); +return err; + } + +snd_card_set_dev (card, &pci->dev); + + *rcs5535au = cs5535au; +return 0 ; +}
Please consider reworking functions such as the above so as to have a single reutrn' statement. Or one return' for success and just one for the error paths. Reason: a) it's easier to chack that all resources are being freed on the error paths and b) It's easier to add new stuff which allocates new resources which need to be released on error paths. Involves goto spaghetti.
Quoted 40 lines
sound/pci/cs5535audio/cs5535audio.c +40
-0+static int __devinit snd_cs5535audio_probe (struct pci_dev *pci, +const struct pci_device_id *pci_id) +{ +static int dev; +snd_card_t *card; +cs5535audio_t *cs5535au; +int err; + +if (dev >=SNDRV_CARDS ) +return -ENODEV ; +if (!enable[dev]) { + dev++; +return -ENOENT ; + } + + card =snd_card_new (index[dev], id[dev],THIS_MODULE ,0 ); +if (card ==NULL ) +return -ENOMEM ; + +if ((err =snd_cs5535audio_create (card, pci, &cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +if ((err =snd_cs5535audio_mixer (cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +if ((err =snd_cs5535audio_pcm (cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +strcpy (card->driver,DRIVER_NAME ); + +strcpy (card->shortname,"CS5535 Audio" ); +sprintf (card->longname,"%s %s at 0x%lx, irq %i" , + card->shortname, card->driver, + cs5535au->port, cs5535au->irq);
sound/pci/cs5535audio/cs5535audio.c line 381
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+ +if ((err =snd_card_register (card)) <0 ) { +snd_card_free (card); +return err; + } + +pci_set_drvdata (pci, card); + dev++; +return 0 ; +}
Ditto.
The handling of `dev' is racy ;)
sound/pci/cs5535audio/cs5535audio_pcm.c line 145
Quoted 1 line
sound/pci/cs5535audio/cs5535audio_pcm.c +1
-0+ addr = (u32 )substream->runtime->dma_addr;
Nope, _snd_pcm_runtime.addr has type dma_addr_t, which is an opaque type, 64-bit on some platforms. I expect this driver will blow up on those platforms.
sound/pci/cs5535audio/cs5535audio_pcm.c line 146
Quoted 1 line
sound/pci/cs5535audio/cs5535audio_pcm.c +1
-0+ desc_addr = (u32 )dma->desc_buf.addr;
Ditto.
sound/pci/cs5535audio/cs5535audio_pcm.c line 238
Quoted 3 lines
sound/pci/cs5535audio/cs5535audio_pcm.c +3
-0+cs5535audio_t *cs5535au =snd_pcm_substream_chip (substream); +cs5535audio_dma_t *dma = (cs5535audio_dma_t *) + substream->runtime->private_data;
Unneeded cast.
+#define cs_writel(reg, val) outl(val, (int) cs5535au->port + reg)
+#define cs_writeb(reg, val) outb(val, (int) cs5535au->port + reg)
+#define cs_readl(reg) inl((unsigned short) (cs5535au->port + reg))
+#define cs_readw(reg) inw((unsigned short) (cs5535au->port + reg))
+#define cs_readb(reg) inb((unsigned short) (cs5535au->port + reg))
erk. subsystem-wide helper macros which reference local variables?
Oh well - it _is_ a sound driver ;)
jayakumar alsa
maintainerQuoted 1 line · Andrew Morton
By convention we put the asm/ includes after the linux/ includes.
Will do. Thanks for the detailed review!
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +static unsigned short snd_cs5535audio_codec_read(cs5535audio_t *cs5535au,typedefs are unpopular in-kernel. We generally don't get too fussed if a driver maintainer really wants them there. The main objection is that we now have two names for the same thing. Plus they cannot be used when forward-declaring a structure.
I just used those typedefs in order to match the style in all the other alsa drivers. For example:
% egrep typedef $lintree/sound/pci/*.c als4000.c:typedef struct {
atiixp.c:typedef struct snd_atiixp atiixp_t; atiixp.c:typedef struct snd_atiixp_dma atiixp_dma_t; atiixp.c:typedef struct snd_atiixp_dma_ops atiixp_dma_ops_t; atiixp.c:typedef struct atiixp_dma_desc { azt3328.c:typedef struct _snd_azf3328 azf3328_t; azt3328.c:typedef struct azf3328_mixer_reg { bt87x.c:typedef struct snd_bt87x bt87x_t; cmipci.c:typedef struct snd_stru_cmipci cmipci_t; <snip>
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
Quoted 2 lines
This isn't right. `timeout' will have a value of -1 if we timed out. (several instances).
How embarrassing. Will fix. :-)
Quoted 1 line
Ditto. Plus we prefer to not put braces around a single statement like this.
Will fix.
Quoted 2 lines
Again. Perhaps a helper function so this code (and its bug) don't get duplicated so much?
Will do.
Quoted 4 lines
We normally put a space after commas.
Unneeded typecast.
Will fix.
Quoted 4 lines
Quoted 2 lines
+
dma->index= (++(dma->index)) %dma->periods;Is no locking needed for
dma->index?
Looks like I stopped using index after I found I don't need to track the individual descriptors. I'll take it out.
Quoted 2 lines
Just to save a tabstop.
Will do.
Quoted 5 lines
sound/pci/cs5535audio/cs5535audio.c line 258
Quoted 3 lines
sound/pci/cs5535audio/cs5535audio.c +3
-0+ } +return IRQ_HANDLED ; +}Perhaps the default case should return
IRQ_NONE.
In this case I think the code is correct as it is. I check that in the top level irq status register if the irq was from the cs5535 audio. I return NONE if it wasn't there. Then I proceed to read a bunch of other registers, all with the assumption that the irq was from us. I return HANDLED at that point since reading those other registers has cleared any irq from us even if it was from an unexpected src.
Quoted 3 lines
Quoted 1 line
+ cs5535au =
kcalloc(1, sizeof(*cs5535au), GFP_KERNEL);We have
kzalloc()now.
Yes, Takashi pointed that out too. But I did the patch against 13.1 which doesn't have kzalloc. I guess I'll redo the patch against -mm and switch to kzalloc.
Quoted 6 lines
Please consider reworking functions such as the above so as to have a single
reutrn' statement. Or onereturn' for success and just one for the error paths. Reason: a) it's easier to chack that all resources are being freed on the error paths and b) It's easier to add new stuff which allocates new resources which need to be released on error paths. Involves goto spaghetti.
Will do.
Quoted 2 lines
The handling of `dev' is racy ;)
Ok. Will look at that.
Quoted 6 lines
Quoted 2 lines
+ addr = (u32)substream->runtime->dma_addr;
Nope, _snd_pcm_runtime.addr has type
dma_addr_t, which is an opaque type, 64-bit on some platforms. I expect this driver will blow up on those platforms.
The 5535 hw's dma descriptor is only 32 bit capable. I guess I should look into informing the dma alloc that the buffers need to be in the lower 32. Would it be okay to drop the upper then?
Quoted 1 line
From a practical standpoint, the 5535 is only used in x86-32
embeddeded systems so high mem probably won't occur. But you are right, I'll go and try make it right.
Quoted 7 lines
sound/pci/cs5535audio/cs5535audio.h +5
-0+#define cs_writel (reg, val) outl(val, (int) cs5535au->port + reg) +#define cs_writeb (reg, val) outb(val, (int) cs5535au->port + reg) +#define cs_readl (reg) inl((unsigned short) (cs5535au->port + reg)) +#define cs_readw (reg) inw((unsigned short) (cs5535au->port + reg)) +#define cs_readb (reg) inb((unsigned short) (cs5535au->port + reg))erk. subsystem-wide helper macros which reference local variables?
Ok. I'll change that.
Thanks,
jk
-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
Quoted 24 lines · jayakumar alsa
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +static unsigned short snd_cs5535audio_codec_read(cs5535audio_t *cs5535au,typedefs are unpopular in-kernel. We generally don't get too fussed if a driver maintainer really wants them there. The main objection is that we now have two names for the same thing. Plus they cannot be used when forward-declaring a structure.
I just used those typedefs in order to match the style in all the other alsa drivers. For example:
% egrep typedef $lintree/sound/pci/*.c als4000.c:typedef struct {
atiixp.c:typedef structsnd_atiixpatiixp_t; atiixp.c:typedef structsnd_atiixp_dmaatiixp_dma_t; atiixp.c:typedef structsnd_atiixp_dma_opsatiixp_dma_ops_t; atiixp.c:typedef structatiixp_dma_desc{ azt3328.c:typedef struct_snd_azf3328azf3328_t; azt3328.c:typedef structazf3328_mixer_reg{ bt87x.c:typedef structsnd_bt87xbt87x_t; cmipci.c:typedef structsnd_stru_cmipcicmipci_t; <snip>
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
Quoted 5 lines
Quoted 1 line
We have
kzalloc()now.Yes, Takashi pointed that out too. But I did the patch against 13.1 which doesn't have kzalloc. I guess I'll redo the patch against -mm and switch to kzalloc.
2.6.14-rc2 has kzalloc(). Generally, patches should only be based on -mm if there's other stuff in -mm which they depend upon.
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines
+ addr = (u32)substream->runtime->dma_addr;
Nope, _snd_pcm_runtime.addr has type
dma_addr_t, which is an opaque type, 64-bit on some platforms. I expect this driver will blow up on those platforms.The 5535 hw's dma descriptor is only 32 bit capable. I guess I should look into informing the dma alloc that the buffers need to be in the lower 32. Would it be okay to drop the upper then?
An IOMMU permits 64-bit platforms to use 32-bit PCI devices.
Quoted 3 lines
Quoted 1 line
From a practical standpoint, the 5535 is only used in x86-32
embeddeded systems so high mem probably won't occur. But you are right, I'll go and try make it right.
Yup, try to honour the provided type system as much as poss.
Quoted 9 lines
Quoted 7 lines
sound/pci/cs5535audio/cs5535audio.h +5
-0+#define cs_writel (reg, val) outl(val, (int) cs5535au->port + reg) +#define cs_writeb (reg, val) outb(val, (int) cs5535au->port + reg) +#define cs_readl (reg) inl((unsigned short) (cs5535au->port + reg)) +#define cs_readw (reg) inw((unsigned short) (cs5535au->port + reg)) +#define cs_readb (reg) inb((unsigned short) (cs5535au->port + reg))erk. subsystem-wide helper macros which reference local variables?
Ok. I'll change that.
well, again, if that's alsa-style then you might choose to retain it. But it'd be an unpopular approach in most other kernel code.
Quoted 5 lines · Andrew Morton
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
I'd _really_ prefer to fix all of alsa. Alsa is so full of wrappers and multiple names for the same thing that's it's almost impossibly for a normal kernel developer to fix anythign in there.
-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download it for free - -and be entered to win a 42" plasma tv or your very own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
At Wed, 21 Sep 2005 09:31:09 +0100,
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Andrew Morton
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
I'd _really_ prefer to fix all of alsa. Alsa is so full of wrappers and multiple names for the same thing that's it's almost impossibly for a normal kernel developer to fix anythign in there.
Oh I guess you're referring to the messy memory stuff in ALSA code?
(Or you mean about the foo_t style? Then it would be easy to fix, and
I'd appreciate if someone takes this job :)
Basically I agree with all your suggestions - hacks have been there simply as workarounds. They should be removed.
The following are in WIP on my local tree:
- Merge of dma_alloc_coherent() hacks for pages <32bit to kernel core (for i386 and ppc)
- PageReserve and mmaps:
dma_mmap_coherent() will be used in all architectures instead of vma nopage callback. Of course, dma_mmap_coherent() should be ported to all archs.
- Removal of the common allocator in sound/core/memalloc.c Instead, each driver has alloc_buffer() and free_buffer() callbacks.
(The reason to have callbacks is that the buffers can be allocated in
3 places: by the pre-allocator, by proc control and at each open / hw_params setup.)
What I'm still considering are:
- The "right" way to allocate/mmap coherent SG-buffers.
- A common API for mmap vmalloc buffers.
- Subsystem or module-wide check of memory leaks in slab (the notorious kmalloc wrapper)
Please let me know if you have any good solutions, comments or suggestions for the above.
thanks,
Takashi
Quoted 19 lines · Takashi Iwai
At Wed, 21 Sep 2005 09:31:09 +0100,
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Andrew Morton
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
I'd _really_ prefer to fix all of alsa. Alsa is so full of wrappers and multiple names for the same thing that's it's almost impossibly for a normal kernel developer to fix anythign in there.
Oh I guess you're referring to the messy memory stuff in ALSA code?
(Or you mean about the
foo_tstyle? Then it would be easy to fix, and
I'd appreciate if someone takes this job :)
Quoted 7 lines
Basically I agree with all your suggestions - hacks have been there simply as workarounds. They should be removed.
The following are in WIP on my local tree:
- Merge of
dma_alloc_coherent()hacks for pages <32bit to kernel core (for i386 and ppc)
Did this get out?
Quoted 5 lines
- PageReserve and mmaps:
dma_mmap_coherent()will be used in all architectures instead of vma nopage callback. Of course,dma_mmap_coherent()should be ported to all archs.
What about just making the i386 one Russell posted generic? It shouldn't be any less broken than what we do now and every architecture that needs something more specific can implements it.
Quoted 3 lines
- Removal of the common allocator in
sound/core/memalloc.cInstead, each driver hasalloc_buffer()andfree_buffer()callbacks.
this isn't in mainline yet, is it?
Quoted 1 line
- The "right" way to allocate/mmap coherent SG-buffers.
How do you get coherent SG-buffers? The only way to get coherent dma allocations is dma_alloc_coheren (and the pci variant), but that's not handing back an SG list. But even if you build one yourself it should be handled by dma_mmap_coherent.
Quoted 1 line
- A common API for mmap vmalloc buffers.
What API do you think about? If you do alloc_page yourself and use vmap everything gets much much easier. And with the new vm_install_page this becomes pretty easy:
init_routine()
{
...
for (i = 0; i < NR_PAGES; i++) {
pages[i] = alloc_page(GFP_KERNEL);
...
}
dma_buffer = vmap(pages, ...);
}dma_map()
{
for (i = 0; i < NR_PAGES; i++) {
dma_map_page(pages[i, ....);
...
}
}mmap()
{
for (i = 0; i < NR_PAGES; i++) {
vm_insert_page(..., pages[i], ..);
...
}
}maybe some tiny helpers that encapsulate the loop over the pages would be nice, but we shouldn't need much of a new API here.
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
At Tue, 6 Dec 2005 17:42:03 +0000,
Quoted 24 lines · Christoph Hellwig
Quoted 22 lines · Takashi Iwai
At Wed, 21 Sep 2005 09:31:09 +0100,
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Andrew Morton
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
I'd _really_ prefer to fix all of alsa. Alsa is so full of wrappers and multiple names for the same thing that's it's almost impossibly for a normal kernel developer to fix anythign in there.
Oh I guess you're referring to the messy memory stuff in ALSA code?
(Or you mean about the
foo_tstyle? Then it would be easy to fix, and
I'd appreciate if someone takes this job :)Basically I agree with all your suggestions - hacks have been there simply as workarounds. They should be removed.
Quoted 6 lines
Quoted 4 lines
The following are in WIP on my local tree:
- Merge of
dma_alloc_coherent()hacks for pages <32bit to kernel core (for i386 and ppc)Did this get out?
Not yet. It's pending because I had other big rewrites beforehand. I'll prepare a patch for this soon later.
Quoted 8 lines
Quoted 4 lines
- PageReserve and mmaps:
dma_mmap_coherent()will be used in all architectures instead of vma nopage callback. Of course,dma_mmap_coherent()should be ported to all archs.What about just making the i386 one Russell posted generic? It shouldn't be any less broken than what we do now and every architecture that needs something more specific can implements it.
Yes, it's a good starting point, indeed.
Quoted 5 lines
Quoted 3 lines
- Removal of the common allocator in
sound/core/memalloc.cInstead, each driver hasalloc_buffer()andfree_buffer()callbacks.this isn't in mainline yet, is it?
Not yet, too. I'm waiting for stabilization of the recent changes on mm before this clean-up action. Also, still considering the best solution...
Quoted 6 lines
Quoted 1 line
- The "right" way to allocate/mmap coherent SG-buffers.
How do you get coherent SG-buffers? The only way to get coherent dma allocations is
dma_alloc_coheren(and the pci variant), but that's not handing back an SG list. But even if you build one yourself it should be handled bydma_mmap_coherent.
I thought of creating an API for coherent SG-buffers, but it looks ineffective on some architectures to have large number of non-contiguous coherent pages. Each call of dma_alloc_coherent() involves kmalloc of another record for a consistent VM region.
So, currently I gave up it and rewrote the stuff in an easier style, using dma_map_sg() + dma_sync_sg_*() for pages allocated via __vmalloc(). The question is whether mmap of non-coherent buffer may influence on the behavior of user-space apps on architectures like ARM or SPARC. If it really matters, we can disable mmap for such architectures, as a safe solution :)
Quoted 34 lines
Quoted 1 line
- A common API for mmap vmalloc buffers.
What API do you think about? If you do
alloc_pageyourself and use vmap everything gets much much easier. And with the newvm_install_pagethis becomes pretty easy:
init_routine()
{... for (i = 0; i < NR_PAGES; i++) { pages[i] = alloc_page(GFP_KERNEL); ... } dma_buffer = vmap(pages, ...); }
dma_map(){ for (i = 0; i < NR_PAGES; i++) { dma_map_page(pages[i, ....); ... } }
mmap(){ for (i = 0; i < NR_PAGES; i++) { vm_insert_page(..., pages[i], ..); ... } }
Quoted 2 lines
maybe some tiny helpers that encapsulate the loop over the pages would be nice, but we shouldn't need much of a new API here.
My current version has the helper functions as below. This is intended for the generic SG-buffers, and uses struct scatterlist. It looks a bit more complicated than your example above as consequence.
Any comments are appreciated.
Thanks,
Takashi
================
struct snd_sg_buf {
struct scatterlist *sglist;
int nelems;
int nmaps;
int direction;
};static void *do_alloc_sgbuf(struct device *dev, struct snd_sg_buf *sg,
gfp_t gfp)
{
void *vptr;
struct scatterlist *sgl;
int i;
vptr = __vmalloc(sg->nelems << PAGE_SHIFT, gfp, PAGE_KERNEL);
if (! vptr)
return NULL;
/* set up sg list */
sgl = sg->sglist;
memset(sgl, 0, sg->nelems * sizeof(*sgl));
for (i = 0; i < sg->nelems; i++, sgl++) {
sgl->page = vmalloc_to_page(vptr + (i << PAGE_SHIFT));
sgl->offset = 0;
sgl->length = PAGE_SIZE;
}
if (! dev)
return vptr;
sg->nmaps = dma_map_sg(dev, sg->sglist, sg->nelems, sg->direction);
if (! sg->nmaps) {
vfree(vptr);
return NULL;
}
if (dev->dma_mask && *dev->dma_mask &&
*dev->dma_mask < DMA_32BIT_MASK) {
/* check whether all pages are in the given dma mask */
dma_addr_t rmask = ~(*dev->dma_mask);
sgl = sg->sglist;
for (i = 0; i < sg->nmaps; i++, sgl++) {
if (sg_dma_address(sgl) & rmask) {
dma_unmap_sg(dev, sg->sglist, sg->nelems,
sg->direction);
vfree(vptr);
return NULL;
}
}
}
return vptr;
}/*
* allocate an sg buffer and map it
*/
void *snd_dma_alloc_sgbuf(struct device *dev, size_t size, int direction,
struct snd_sg_buf **sg_ret)
{
struct snd_sg_buf *sg;
void *vptr;
sg = kzalloc(sizeof(*sg), GFP_KERNEL);
if (! sg)
return NULL;
size = PAGE_ALIGN(size);
sg->nelems = size >> PAGE_SHIFT;
sg->direction = direction;
sg->sglist = kmalloc(sg->nelems * sizeof(struct scatterlist), GFP_KERNEL);
if (! sg->sglist) {
kfree(sg);
return NULL;
}
if ((vptr = do_alloc_sgbuf(dev, sg, GFP_KERNEL)) == NULL &&
(vptr = do_alloc_sgbuf(dev, sg, GFP_DMA)) == NULL) {
kfree(sg->sglist);
kfree(sg);
return NULL;
}
*sg_ret = sg;
return vptr;
}/*
* unmap and release the sg buffer
*/
void snd_dma_free_sgbuf(struct device *dev, void *vptr, struct snd_sg_buf *sg)
{
if (sg->nmaps)
dma_unmap_sg(dev, sg->sglist, sg->nelems, sg->direction);
vfree(vptr);
kfree(sg->sglist);
kfree(sg);
}/*
* return the bus address at the corresponding offset
*/dma_addr_t snd_sgbuf_get_addr(struct snd_sg_buf *sg, size_t offset)
{
struct scatterlist *sgl;
int i;
if (offset >= (sg->nelems << PAGE_SHIFT))
return 0;
/* short path - no compounds */
if (sg->nelems == sg->nmaps)
return sg_dma_address(&sg->sglist[offset >> PAGE_SHIFT])
+ offset % PAGE_SIZE;
/* look for the corresponding entry... should be optimized */
sgl = sg->sglist;
for (i = 0; i < sg->nmaps; i++, sgl++) {
if (sg_dma_len(sgl) > offset)
return sg_dma_address(sgl) + offset;
offset -= sg_dma_len(sgl);
}
return 0;
}At Tue, 20 Sep 2005 17:23:09 -0700,
Quoted 26 lines · Andrew Morton
Quoted 24 lines · jayakumar alsa
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ +static unsigned short snd_cs5535audio_codec_read(cs5535audio_t *cs5535au,typedefs are unpopular in-kernel. We generally don't get too fussed if a driver maintainer really wants them there. The main objection is that we now have two names for the same thing. Plus they cannot be used when forward-declaring a structure.
I just used those typedefs in order to match the style in all the other alsa drivers. For example:
% egrep typedef $lintree/sound/pci/*.c als4000.c:typedef struct {
atiixp.c:typedef structsnd_atiixpatiixp_t; atiixp.c:typedef structsnd_atiixp_dmaatiixp_dma_t; atiixp.c:typedef structsnd_atiixp_dma_opsatiixp_dma_ops_t; atiixp.c:typedef structatiixp_dma_desc{ azt3328.c:typedef struct_snd_azf3328azf3328_t; azt3328.c:typedef structazf3328_mixer_reg{ bt87x.c:typedef structsnd_bt87xbt87x_t; cmipci.c:typedef structsnd_stru_cmipcicmipci_t; <snip>
Quoted 5 lines
Quoted 2 lines
I'm not sure what to do. I'd be happy to take them out. But I woudn't mind leaving them in if that's what alsa convention is.
I'd be inclined to stick with the alsa style. That's just an fyi if you plan on working in other places.
It's no longer preferred style in ALSA.xxx_t had been used for the magic type-cast checks. But this check was removed, and there is no reason to stick with its style. I'm using struct foo in the recent drivers like hd-audio, too. Tons of *_t are still there just because of laziness :)
Seriously, if you guys want to clean up them, I wouldn't reject at all. But this clean up is at the very tail of my TODO list ;)
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
+ addr = (u32)substream->runtime->dma_addr;
Nope, _snd_pcm_runtime.addr has type
dma_addr_t, which is an opaque type, 64-bit on some platforms. I expect this driver will blow up on those platforms.
Quoted 5 lines
Quoted 3 lines
The 5535 hw's dma descriptor is only 32 bit capable. I guess I should look into informing the dma alloc that the buffers need to be in the lower 32. Would it be okay to drop the upper then?
An IOMMU permits 64-bit platforms to use 32-bit PCI devices.
But still you have to set a 32bit (bus) address to the buffer descriptor of this chip. If you get higher than that, it won't work anyway.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
sound/pci/cs5535audio/cs5535audio.h +5
-0+#define cs_writel (reg, val) outl(val, (int) cs5535au->port + reg) +#define cs_writeb (reg, val) outb(val, (int) cs5535au->port + reg) +#define cs_readl (reg) inl((unsigned short) (cs5535au->port + reg)) +#define cs_readw (reg) inw((unsigned short) (cs5535au->port + reg)) +#define cs_readb (reg) inb((unsigned short) (cs5535au->port + reg))
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
erk. subsystem-wide helper macros which reference local variables?
Ok. I'll change that.
well, again, if that's alsa-style then you might choose to retain it. But it'd be an unpopular approach in most other kernel code.
Well, it's not ALSA style, too. We prefer often like
foo_write(chip, reg, val)
expanded to
outl(val, (chip)->port + (reg))
but don't assume local variables.
Takashi
jayakumar alsa
maintainerQuoted 53 lines
Quoted 40 lines
sound/pci/cs5535audio/cs5535audio.c +40
-0+static int __devinit snd_cs5535audio_probe (struct pci_dev *pci, +const struct pci_device_id *pci_id) +{ +static int dev; +snd_card_t *card; +cs5535audio_t *cs5535au; +int err; + +if (dev >=SNDRV_CARDS ) +return -ENODEV ; +if (!enable[dev]) { + dev++; +return -ENOENT ; + } + + card =snd_card_new (index[dev], id[dev],THIS_MODULE ,0 ); +if (card ==NULL ) +return -ENOMEM ; + +if ((err =snd_cs5535audio_create (card, pci, &cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +if ((err =snd_cs5535audio_mixer (cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +if ((err =snd_cs5535audio_pcm (cs5535au)) <0 ) { +snd_card_free (card); +return err; + } + +strcpy (card->driver,DRIVER_NAME ); + +strcpy (card->shortname,"CS5535 Audio" ); +sprintf (card->longname,"%s %s at 0x%lx, irq %i" , + card->shortname, card->driver, + cs5535au->port, cs5535au->irq);sound/pci/cs5535audio/cs5535audio.c line 381
Quoted 10 lines
sound/pci/cs5535audio/cs5535audio.c +10
-0+ +if ((err =snd_card_register (card)) <0 ) { +snd_card_free (card); +return err; + } + +pci_set_drvdata (pci, card); + dev++; +return 0 ; +}Ditto.
Quoted 1 line
The handling of `dev' is racy ;)
I took a quick look at the dev handling.
% egrep -B20 "dev\+\+" $lintree/sound/pci/*.c
<snip>
--
../azt3328.c- return -ENODEV;
../azt3328.c- if (!enable[dev]) {../azt3328.c: dev++;
--
../azt3328.c-
../azt3328.c- pci_set_drvdata(pci, card);../azt3328.c: dev++;
--
../cmipci.c- return -ENODEV;
../cmipci.c- if (! enable[dev]) {../cmipci.c: dev++;
--
../cmipci.c- } ../cmipci.c- pci_set_drvdata(pci, card); ../cmipci.c: dev++;
--
<snip>
So I think everyone is doing the same thing. I think probe is called from driver_probe_device through device_add and in this case that's from pci_bus_add_device/s from pci_do_scan_bus. I think the device probe functions are therefore called serially and there is no risk of a race condition. I hope that's right.
Thanks,
jk