1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;4.&#160;PCI Resource Management</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Writing an ALSA Driver"><link rel="up" href="index.html" title="Writing an ALSA Driver"><link rel="prev" href="card-management-registration.html" title="Registration and Release"><link rel="next" href="pci-resource-some-haftas.html" title="Some Hafta's"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;4.&#160;PCI Resource Management</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="card-management-registration.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="pci-resource-some-haftas.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="pci-resource"></a>Chapter&#160;4.&#160;PCI Resource Management</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="pci-resource.html#pci-resource-example">Full Code Example</a></span></dt><dt><span class="section"><a href="pci-resource-some-haftas.html">Some Hafta's</a></span></dt><dt><span class="section"><a href="pci-resource-resource-allocation.html">Resource Allocation</a></span></dt><dt><span class="section"><a href="pci-resource-entries.html">PCI Entries</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="pci-resource-example"></a>Full Code Example</h2></div></div></div><p>
2        In this section, we'll complete the chip-specific constructor,
3      destructor and PCI entries. Example code is shown first,
4      below. 
5
6        </p><div class="example"><a name="idp1093954540"></a><p class="title"><b>Example&#160;4.1.&#160;PCI Resource Management Example</b></p><div class="example-contents"><pre class="programlisting">
7
8  struct mychip {
9          struct snd_card *card;
10          struct pci_dev *pci;
11
12          unsigned long port;
13          int irq;
14  };
15
16  static int snd_mychip_free(struct mychip *chip)
17  {
18          /* disable hardware here if any */
19          .... /* (not implemented in this document) */
20
21          /* release the irq */
22          if (chip-&gt;irq &gt;= 0)
23                  free_irq(chip-&gt;irq, chip);
24          /* release the I/O ports &amp; memory */
25          pci_release_regions(chip-&gt;pci);
26          /* disable the PCI entry */
27          pci_disable_device(chip-&gt;pci);
28          /* release the data */
29          kfree(chip);
30          return 0;
31  }
32
33  /* chip-specific constructor */
34  static int snd_mychip_create(struct snd_card *card,
35                               struct pci_dev *pci,
36                               struct mychip **rchip)
37  {
38          struct mychip *chip;
39          int err;
40          static struct snd_device_ops ops = {
41                 .dev_free = snd_mychip_dev_free,
42          };
43
44          *rchip = NULL;
45
46          /* initialize the PCI entry */
47          err = pci_enable_device(pci);
48          if (err &lt; 0)
49                  return err;
50          /* check PCI availability (28bit DMA) */
51          if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) &lt; 0 ||
52              pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) &lt; 0) {
53                  printk(KERN_ERR "error to set 28bit mask DMA\n");
54                  pci_disable_device(pci);
55                  return -ENXIO;
56          }
57
58          chip = kzalloc(sizeof(*chip), GFP_KERNEL);
59          if (chip == NULL) {
60                  pci_disable_device(pci);
61                  return -ENOMEM;
62          }
63
64          /* initialize the stuff */
65          chip-&gt;card = card;
66          chip-&gt;pci = pci;
67          chip-&gt;irq = -1;
68
69          /* (1) PCI resource allocation */
70          err = pci_request_regions(pci, "My Chip");
71          if (err &lt; 0) {
72                  kfree(chip);
73                  pci_disable_device(pci);
74                  return err;
75          }
76          chip-&gt;port = pci_resource_start(pci, 0);
77          if (request_irq(pci-&gt;irq, snd_mychip_interrupt,
78                          IRQF_SHARED, KBUILD_MODNAME, chip)) {
79                  printk(KERN_ERR "cannot grab irq %d\n", pci-&gt;irq);
80                  snd_mychip_free(chip);
81                  return -EBUSY;
82          }
83          chip-&gt;irq = pci-&gt;irq;
84
85          /* (2) initialization of the chip hardware */
86          .... /*   (not implemented in this document) */
87
88          err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &amp;ops);
89          if (err &lt; 0) {
90                  snd_mychip_free(chip);
91                  return err;
92          }
93
94          *rchip = chip;
95          return 0;
96  }        
97
98  /* PCI IDs */
99  static struct pci_device_id snd_mychip_ids[] = {
100          { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
101            PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
102          ....
103          { 0, }
104  };
105  MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
106
107  /* pci_driver definition */
108  static struct pci_driver driver = {
109          .name = KBUILD_MODNAME,
110          .id_table = snd_mychip_ids,
111          .probe = snd_mychip_probe,
112          .remove = snd_mychip_remove,
113  };
114
115  /* module initialization */
116  static int __init alsa_card_mychip_init(void)
117  {
118          return pci_register_driver(&amp;driver);
119  }
120
121  /* module clean up */
122  static void __exit alsa_card_mychip_exit(void)
123  {
124          pci_unregister_driver(&amp;driver);
125  }
126
127  module_init(alsa_card_mychip_init)
128  module_exit(alsa_card_mychip_exit)
129
130  EXPORT_NO_SYMBOLS; /* for old kernels only */
131
132          </pre></div></div><p><br class="example-break">
133      </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="card-management-registration.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="pci-resource-some-haftas.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Registration and Release&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Some Hafta's</td></tr></table></div></body></html>
134