1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Constructor</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="basic-flow.html" title="Chapter 2. Basic Flow for PCI Drivers"><link rel="prev" href="basic-flow-example.html" title="Full Code Example"><link rel="next" href="basic-flow-destructor.html" title="Destructor"></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">Constructor</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="basic-flow-example.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Basic Flow for PCI Drivers</th><td width="20%" align="right"> <a accesskey="n" href="basic-flow-destructor.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="basic-flow-constructor"></a>Constructor</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-device-index">1) Check and increment the device index.</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-create-card">2) Create a card instance</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-create-main">3) Create a main component</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-main-component">4) Set the driver ID and name strings.</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-create-other">5) Create other components, such as mixer, MIDI, etc.</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-register-card">6) Register the card instance.</a></span></dt><dt><span class="section"><a href="basic-flow-constructor.html#basic-flow-constructor-set-pci">7) Set the PCI driver data and return zero.</a></span></dt></dl></div><p> 2 The real constructor of PCI drivers is the <code class="function">probe</code> callback. 3 The <code class="function">probe</code> callback and other component-constructors which are called 4 from the <code class="function">probe</code> callback cannot be used with 5 the <em class="parameter"><code>__init</code></em> prefix 6 because any PCI device could be a hotplug device. 7 </p><p> 8 In the <code class="function">probe</code> callback, the following scheme is often used. 9 </p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-device-index"></a>1) Check and increment the device index.</h3></div></div></div><p> 10 </p><div class="informalexample"><pre class="programlisting"> 11 12 static int dev; 13 .... 14 if (dev >= SNDRV_CARDS) 15 return -ENODEV; 16 if (!enable[dev]) { 17 dev++; 18 return -ENOENT; 19 } 20 21 </pre></div><p> 22 23 where enable[dev] is the module option. 24 </p><p> 25 Each time the <code class="function">probe</code> callback is called, check the 26 availability of the device. If not available, simply increment 27 the device index and returns. dev will be incremented also 28 later (<a class="link" href="basic-flow-constructor.html#basic-flow-constructor-set-pci" title="7) Set the PCI driver data and return zero."><em class="citetitle">step 29 7</em></a>). 30 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-create-card"></a>2) Create a card instance</h3></div></div></div><p> 31 </p><div class="informalexample"><pre class="programlisting"> 32 33 struct snd_card *card; 34 int err; 35 .... 36 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 37 0, &card); 38 39 </pre></div><p> 40 </p><p> 41 The details will be explained in the section 42 <a class="link" href="card-management.html#card-management-card-instance" title="Card Instance"><em class="citetitle"> 43 Management of Cards and Components</em></a>. 44 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-create-main"></a>3) Create a main component</h3></div></div></div><p> 45 In this part, the PCI resources are allocated. 46 47 </p><div class="informalexample"><pre class="programlisting"> 48 49 struct mychip *chip; 50 .... 51 err = snd_mychip_create(card, pci, &chip); 52 if (err < 0) { 53 snd_card_free(card); 54 return err; 55 } 56 57 </pre></div><p> 58 59 The details will be explained in the section <a class="link" href="pci-resource.html" title="Chapter 4. PCI Resource Management"><em class="citetitle">PCI Resource 60 Management</em></a>. 61 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-main-component"></a>4) Set the driver ID and name strings.</h3></div></div></div><p> 62 </p><div class="informalexample"><pre class="programlisting"> 63 64 strcpy(card->driver, "My Chip"); 65 strcpy(card->shortname, "My Own Chip 123"); 66 sprintf(card->longname, "%s at 0x%lx irq %i", 67 card->shortname, chip->ioport, chip->irq); 68 69 </pre></div><p> 70 71 The driver field holds the minimal ID string of the 72 chip. This is used by alsa-lib's configurator, so keep it 73 simple but unique. 74 Even the same driver can have different driver IDs to 75 distinguish the functionality of each chip type. 76 </p><p> 77 The shortname field is a string shown as more verbose 78 name. The longname field contains the information 79 shown in <code class="filename">/proc/asound/cards</code>. 80 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-create-other"></a>5) Create other components, such as mixer, MIDI, etc.</h3></div></div></div><p> 81 Here you define the basic components such as 82 <a class="link" href="pcm-interface.html" title="Chapter 5. PCM Interface"><em class="citetitle">PCM</em></a>, 83 mixer (e.g. <a class="link" href="api-ac97.html" title="Chapter 7. API for AC97 Codec"><em class="citetitle">AC97</em></a>), 84 MIDI (e.g. <a class="link" href="midi-interface.html" title="Chapter 8. MIDI (MPU401-UART) Interface"><em class="citetitle">MPU-401</em></a>), 85 and other interfaces. 86 Also, if you want a <a class="link" href="proc-interface.html" title="Chapter 12. Proc Interface"><em class="citetitle">proc 87 file</em></a>, define it here, too. 88 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-register-card"></a>6) Register the card instance.</h3></div></div></div><p> 89 </p><div class="informalexample"><pre class="programlisting"> 90 91 err = snd_card_register(card); 92 if (err < 0) { 93 snd_card_free(card); 94 return err; 95 } 96 97 </pre></div><p> 98 </p><p> 99 Will be explained in the section <a class="link" href="card-management-registration.html" title="Registration and Release"><em class="citetitle">Management 100 of Cards and Components</em></a>, too. 101 </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="basic-flow-constructor-set-pci"></a>7) Set the PCI driver data and return zero.</h3></div></div></div><p> 102 </p><div class="informalexample"><pre class="programlisting"> 103 104 pci_set_drvdata(pci, card); 105 dev++; 106 return 0; 107 108 </pre></div><p> 109 110 In the above, the card record is stored. This pointer is 111 used in the remove callback and power-management 112 callbacks, too. 113 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="basic-flow-example.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="basic-flow.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="basic-flow-destructor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Full Code Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Destructor</td></tr></table></div></body></html> 114