The allocation of I/O ports and irqs are done via standard kernel functions. Unlike ALSA ver.0.5.x., there are no helpers for that. And these resources must be released in the destructor function (see below). Also, on ALSA 0.9.x, you don't need to allocate (pseudo-)DMA for PCI like ALSA 0.5.x.
Now assume that this PCI device has an I/O port with 8 bytes
and an interrupt. Then struct mychip will have the
following fields:
For an i/o port (and also a memory region), you need to have
the resource pointer for the standard resource management. For
an irq, you have to keep only the irq number (integer). But you
need to initialize this number as -1 before actual allocation,
since irq 0 is valid. The port address and its resource pointer
can be initialized as null by
kzalloc() automatically, so you
don't have to take care of resetting them.
The allocation of an i/o port is done like this:
if ((err = pci_request_regions(pci, "My Chip")) < 0) {
kfree(chip);
pci_disable_device(pci);
return err;
}
chip->port = pci_resource_start(pci, 0);
It will reserve the i/o port region of 8 bytes of the given
PCI device. The returned value, chip->res_port, is allocated
via kmalloc() by
request_region(). The pointer must be
released via kfree(), but there is some
problem regarding this. This issue will be explained more below.
The allocation of an interrupt source is done like this:
if (request_irq(pci->irq, snd_mychip_interrupt,
IRQF_DISABLED|IRQF_SHARED, "My Chip", chip)) {
printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
snd_mychip_free(chip);
return -EBUSY;
}
chip->irq = pci->irq;
snd_mychip_interrupt() is the
interrupt handler defined later.
Note that chip->irq should be defined
only when request_irq() succeeded.
On the PCI bus, the interrupts can be shared. Thus,
IRQF_SHARED is given as the interrupt flag of
request_irq().
The last argument of request_irq() is the
data pointer passed to the interrupt handler. Usually, the
chip-specific record is used for that, but you can use what you
like, too.
I won't define the detail of the interrupt handler at this point, but at least its appearance can be explained now. The interrupt handler looks usually like the following:
static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
{
struct mychip *chip = dev_id;
....
return IRQ_HANDLED;
}
Now let's write the corresponding destructor for the resources above. The role of destructor is simple: disable the hardware (if already activated) and release the resources. So far, we have no hardware part, so the disabling is not written here.
For releasing the resources, "check-and-release" method is a safer way. For the interrupt, do like this:
Since the irq number can start from 0, you should initialize chip->irq with a negative value (e.g. -1), so that you can check the validity of the irq number as above. When you requested I/O ports or memory regions via
pci_request_region() or
pci_request_regions() like this example,
release the resource(s) using the corresponding function,
pci_release_region() or
pci_release_regions().
When you requested manually via request_region()
or request_mem_region, you can release it via
release_resource(). Suppose that you keep
the resource pointer returned from request_region()
in chip->res_port, the release procedure looks like below:
Don't forget to call pci_disable_device()
before all finished.
And finally, release the chip-specific record.
Again, remember that you cannot
set __devexit prefix for this destructor.
We didn't implement the hardware-disabling part in the above. If you need to do this, please note that the destructor may be called even before the initialization of the chip is completed. It would be better to have a flag to skip the hardware-disabling if the hardware was not initialized yet.
When the chip-data is assigned to the card using
snd_device_new() with
SNDRV_DEV_LOWLELVEL , its destructor is
called at the last. That is, it is assured that all other
components like PCMs and controls have been already released.
You don't have to call stopping PCMs, etc. explicitly, but just
stop the hardware in the low-level.
The management of a memory-mapped region is almost as same as the management of an i/o port. You'll need three fields like the following:
and the allocation would be like below: and the corresponding destructor would be: