skeleton.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * PCI sound skeleton example
  3. *
  4. * (c) 1998 Red Hat Software
  5. *
  6. * This software may be used and distributed according to the
  7. * terms of the GNU General Public License, incorporated herein by
  8. * reference.
  9. *
  10. * This example is designed to be built in the linux/drivers/sound
  11. * directory as part of a kernel build. The example is modular only
  12. * drop me a note once you have a working modular driver and want
  13. * to integrate it with the main code.
  14. * -- Alan <alan@redhat.com>
  15. *
  16. * This is a first draft. Please report any errors, corrections or
  17. * improvements to me.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <asm/io.h>
  26. #include "sound_config.h"
  27. /*
  28. * Define our PCI vendor ID here
  29. */
  30. #ifndef PCI_VENDOR_MYIDENT
  31. #define PCI_VENDOR_MYIDENT 0x125D
  32. /*
  33. * PCI identity for the card.
  34. */
  35. #define PCI_DEVICE_ID_MYIDENT_MYCARD1 0x1969
  36. #endif
  37. #define CARD_NAME "ExampleWave 3D Pro Ultra ThingyWotsit"
  38. #define MAX_CARDS 8
  39. /*
  40. * Each address_info object holds the information about one of
  41. * our card resources. In this case the MSS emulation of our
  42. * ficticious card. Its used to manage and attach things.
  43. */
  44. static struct address_info mss_data[MAX_CARDS];
  45. static int cards;
  46. /*
  47. * Install the actual card. This is an example
  48. */
  49. static int mycard_install(struct pci_dev *pcidev)
  50. {
  51. int iobase;
  52. int mssbase;
  53. int mpubase;
  54. u8 x;
  55. u16 w;
  56. u32 v;
  57. int i;
  58. int dma;
  59. /*
  60. * Our imaginary code has its I/O on PCI address 0, a
  61. * MSS on PCI address 1 and an MPU on address 2
  62. *
  63. * For the example we will only initialise the MSS
  64. */
  65. iobase = pci_resource_start(pcidev, 0);
  66. mssbase = pci_resource_start(pcidev, 1);
  67. mpubase = pci_resource_start(pcidev, 2);
  68. /*
  69. * Reset the board
  70. */
  71. /*
  72. * Wait for completion. udelay() waits in microseconds
  73. */
  74. udelay(100);
  75. /*
  76. * Ok card ready. Begin setup proper. You might for example
  77. * load the firmware here
  78. */
  79. dma = card_specific_magic(ioaddr);
  80. /*
  81. * Turn on legacy mode (example), There are also byte and
  82. * dword (32bit) PCI configuration function calls
  83. */
  84. pci_read_config_word(pcidev, 0x40, &w);
  85. w&=~(1<<15); /* legacy decode on */
  86. w|=(1<<14); /* Reserved write as 1 in this case */
  87. w|=(1<<3)|(1<<1)|(1<<0); /* SB on , FM on, MPU on */
  88. pci_write_config_word(pcidev, 0x40, w);
  89. /*
  90. * Let the user know we found his toy.
  91. */
  92. printk(KERN_INFO "Programmed "CARD_NAME" at 0x%X to legacy mode.\n",
  93. iobase);
  94. /*
  95. * Now set it up the description of the card
  96. */
  97. mss_data[cards].io_base = mssbase;
  98. mss_data[cards].irq = pcidev->irq;
  99. mss_data[cards].dma = dma;
  100. /*
  101. * Check there is an MSS present
  102. */
  103. if(ad1848_detect(mssbase, NULL, mss_data[cards].osp)==0)
  104. return 0;
  105. /*
  106. * Initialize it
  107. */
  108. mss_data[cards].slots[3] = ad1848_init("MyCard MSS 16bit",
  109. mssbase,
  110. mss_data[cards].irq,
  111. mss_data[cards].dma,
  112. mss_data[cards].dma,
  113. 0,
  114. 0,
  115. THIS_MODULE);
  116. cards++;
  117. return 1;
  118. }
  119. /*
  120. * This loop walks the PCI configuration database and finds where
  121. * the sound cards are.
  122. */
  123. int init_mycard(void)
  124. {
  125. struct pci_dev *pcidev=NULL;
  126. int count=0;
  127. while((pcidev = pci_find_device(PCI_VENDOR_MYIDENT, PCI_DEVICE_ID_MYIDENT_MYCARD1, pcidev))!=NULL)
  128. {
  129. if (pci_enable_device(pcidev))
  130. continue;
  131. count+=mycard_install(pcidev);
  132. if(count)
  133. return 0;
  134. if(count==MAX_CARDS)
  135. break;
  136. }
  137. if(count==0)
  138. return -ENODEV;
  139. return 0;
  140. }
  141. /*
  142. * This function is called when the user or kernel loads the
  143. * module into memory.
  144. */
  145. int init_module(void)
  146. {
  147. if(init_mycard()<0)
  148. {
  149. printk(KERN_ERR "No "CARD_NAME" cards found.\n");
  150. return -ENODEV;
  151. }
  152. return 0;
  153. }
  154. /*
  155. * This is called when it is removed. It will only be removed
  156. * when its use count is 0.
  157. */
  158. void cleanup_module(void)
  159. {
  160. for(i=0;i< cards; i++)
  161. {
  162. /*
  163. * Free attached resources
  164. */
  165. ad1848_unload(mss_data[i].io_base,
  166. mss_data[i].irq,
  167. mss_data[i].dma,
  168. mss_data[i].dma,
  169. 0);
  170. /*
  171. * And disconnect the device from the kernel
  172. */
  173. sound_unload_audiodevice(mss_data[i].slots[3]);
  174. }
  175. }