emu10k1x.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. /*
  2. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  3. * Driver EMU10K1X chips
  4. *
  5. * Parts of this code were adapted from audigyls.c driver which is
  6. * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
  7. *
  8. * BUGS:
  9. * --
  10. *
  11. * TODO:
  12. *
  13. * Chips (SB0200 model):
  14. * - EMU10K1X-DBQ
  15. * - STAC 9708T
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. */
  32. #include <sound/driver.h>
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/pci.h>
  36. #include <linux/slab.h>
  37. #include <linux/moduleparam.h>
  38. #include <sound/core.h>
  39. #include <sound/initval.h>
  40. #include <sound/pcm.h>
  41. #include <sound/ac97_codec.h>
  42. #include <sound/info.h>
  43. #include <sound/rawmidi.h>
  44. MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
  45. MODULE_DESCRIPTION("EMU10K1X");
  46. MODULE_LICENSE("GPL");
  47. MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
  48. // module parameters (see "Module Parameters")
  49. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  50. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  51. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  52. module_param_array(index, int, NULL, 0444);
  53. MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
  54. module_param_array(id, charp, NULL, 0444);
  55. MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
  56. module_param_array(enable, bool, NULL, 0444);
  57. MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
  58. // some definitions were borrowed from emu10k1 driver as they seem to be the same
  59. /************************************************************************************************/
  60. /* PCI function 0 registers, address = <val> + PCIBASE0 */
  61. /************************************************************************************************/
  62. #define PTR 0x00 /* Indexed register set pointer register */
  63. /* NOTE: The CHANNELNUM and ADDRESS words can */
  64. /* be modified independently of each other. */
  65. #define DATA 0x04 /* Indexed register set data register */
  66. #define IPR 0x08 /* Global interrupt pending register */
  67. /* Clear pending interrupts by writing a 1 to */
  68. /* the relevant bits and zero to the other bits */
  69. #define IPR_MIDITRANSBUFEMPTY 0x00000001 /* MIDI UART transmit buffer empty */
  70. #define IPR_MIDIRECVBUFEMPTY 0x00000002 /* MIDI UART receive buffer empty */
  71. #define IPR_CH_0_LOOP 0x00000800 /* Channel 0 loop */
  72. #define IPR_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
  73. #define IPR_CAP_0_LOOP 0x00080000 /* Channel capture loop */
  74. #define IPR_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
  75. #define INTE 0x0c /* Interrupt enable register */
  76. #define INTE_MIDITXENABLE 0x00000001 /* Enable MIDI transmit-buffer-empty interrupts */
  77. #define INTE_MIDIRXENABLE 0x00000002 /* Enable MIDI receive-buffer-empty interrupts */
  78. #define INTE_CH_0_LOOP 0x00000800 /* Channel 0 loop */
  79. #define INTE_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
  80. #define INTE_CAP_0_LOOP 0x00080000 /* Channel capture loop */
  81. #define INTE_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
  82. #define HCFG 0x14 /* Hardware config register */
  83. #define HCFG_LOCKSOUNDCACHE 0x00000008 /* 1 = Cancel bustmaster accesses to soundcache */
  84. /* NOTE: This should generally never be used. */
  85. #define HCFG_AUDIOENABLE 0x00000001 /* 0 = CODECs transmit zero-valued samples */
  86. /* Should be set to 1 when the EMU10K1 is */
  87. /* completely initialized. */
  88. #define GPIO 0x18 /* Defaults: 00001080-Analog, 00001000-SPDIF. */
  89. #define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
  90. #define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
  91. /********************************************************************************************************/
  92. /* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers */
  93. /********************************************************************************************************/
  94. #define PLAYBACK_LIST_ADDR 0x00 /* Base DMA address of a list of pointers to each period/size */
  95. /* One list entry: 4 bytes for DMA address,
  96. * 4 bytes for period_size << 16.
  97. * One list entry is 8 bytes long.
  98. * One list entry for each period in the buffer.
  99. */
  100. #define PLAYBACK_LIST_SIZE 0x01 /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000 */
  101. #define PLAYBACK_LIST_PTR 0x02 /* Pointer to the current period being played */
  102. #define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA addresss */
  103. #define PLAYBACK_PERIOD_SIZE 0x05 /* Playback period size */
  104. #define PLAYBACK_POINTER 0x06 /* Playback period pointer. Sample currently in DAC */
  105. #define PLAYBACK_UNKNOWN1 0x07
  106. #define PLAYBACK_UNKNOWN2 0x08
  107. /* Only one capture channel supported */
  108. #define CAPTURE_DMA_ADDR 0x10 /* Capture DMA address */
  109. #define CAPTURE_BUFFER_SIZE 0x11 /* Capture buffer size */
  110. #define CAPTURE_POINTER 0x12 /* Capture buffer pointer. Sample currently in ADC */
  111. #define CAPTURE_UNKNOWN 0x13
  112. /* From 0x20 - 0x3f, last samples played on each channel */
  113. #define TRIGGER_CHANNEL 0x40 /* Trigger channel playback */
  114. #define TRIGGER_CHANNEL_0 0x00000001 /* Trigger channel 0 */
  115. #define TRIGGER_CHANNEL_1 0x00000002 /* Trigger channel 1 */
  116. #define TRIGGER_CHANNEL_2 0x00000004 /* Trigger channel 2 */
  117. #define TRIGGER_CAPTURE 0x00000100 /* Trigger capture channel */
  118. #define ROUTING 0x41 /* Setup sound routing ? */
  119. #define ROUTING_FRONT_LEFT 0x00000001
  120. #define ROUTING_FRONT_RIGHT 0x00000002
  121. #define ROUTING_REAR_LEFT 0x00000004
  122. #define ROUTING_REAR_RIGHT 0x00000008
  123. #define ROUTING_CENTER_LFE 0x00010000
  124. #define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
  125. #define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
  126. #define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
  127. #define SPCS_CLKACCYMASK 0x30000000 /* Clock accuracy */
  128. #define SPCS_CLKACCY_1000PPM 0x00000000 /* 1000 parts per million */
  129. #define SPCS_CLKACCY_50PPM 0x10000000 /* 50 parts per million */
  130. #define SPCS_CLKACCY_VARIABLE 0x20000000 /* Variable accuracy */
  131. #define SPCS_SAMPLERATEMASK 0x0f000000 /* Sample rate */
  132. #define SPCS_SAMPLERATE_44 0x00000000 /* 44.1kHz sample rate */
  133. #define SPCS_SAMPLERATE_48 0x02000000 /* 48kHz sample rate */
  134. #define SPCS_SAMPLERATE_32 0x03000000 /* 32kHz sample rate */
  135. #define SPCS_CHANNELNUMMASK 0x00f00000 /* Channel number */
  136. #define SPCS_CHANNELNUM_UNSPEC 0x00000000 /* Unspecified channel number */
  137. #define SPCS_CHANNELNUM_LEFT 0x00100000 /* Left channel */
  138. #define SPCS_CHANNELNUM_RIGHT 0x00200000 /* Right channel */
  139. #define SPCS_SOURCENUMMASK 0x000f0000 /* Source number */
  140. #define SPCS_SOURCENUM_UNSPEC 0x00000000 /* Unspecified source number */
  141. #define SPCS_GENERATIONSTATUS 0x00008000 /* Originality flag (see IEC-958 spec) */
  142. #define SPCS_CATEGORYCODEMASK 0x00007f00 /* Category code (see IEC-958 spec) */
  143. #define SPCS_MODEMASK 0x000000c0 /* Mode (see IEC-958 spec) */
  144. #define SPCS_EMPHASISMASK 0x00000038 /* Emphasis */
  145. #define SPCS_EMPHASIS_NONE 0x00000000 /* No emphasis */
  146. #define SPCS_EMPHASIS_50_15 0x00000008 /* 50/15 usec 2 channel */
  147. #define SPCS_COPYRIGHT 0x00000004 /* Copyright asserted flag -- do not modify */
  148. #define SPCS_NOTAUDIODATA 0x00000002 /* 0 = Digital audio, 1 = not audio */
  149. #define SPCS_PROFESSIONAL 0x00000001 /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992) */
  150. #define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
  151. /* This is the MPU port on the card */
  152. #define MUDATA 0x47
  153. #define MUCMD 0x48
  154. #define MUSTAT MUCMD
  155. /* From 0x50 - 0x5f, last samples captured */
  156. /**
  157. * The hardware has 3 channels for playback and 1 for capture.
  158. * - channel 0 is the front channel
  159. * - channel 1 is the rear channel
  160. * - channel 2 is the center/lfe chanel
  161. * Volume is controlled by the AC97 for the front and rear channels by
  162. * the PCM Playback Volume, Sigmatel Surround Playback Volume and
  163. * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
  164. * the front/rear channel mixing in the REAR OUT jack. When using the
  165. * 4-Speaker Stereo, both front and rear channels will be mixed in the
  166. * REAR OUT.
  167. * The center/lfe channel has no volume control and cannot be muted during
  168. * playback.
  169. */
  170. struct emu10k1x_voice {
  171. struct emu10k1x *emu;
  172. int number;
  173. int use;
  174. struct emu10k1x_pcm *epcm;
  175. };
  176. struct emu10k1x_pcm {
  177. struct emu10k1x *emu;
  178. struct snd_pcm_substream *substream;
  179. struct emu10k1x_voice *voice;
  180. unsigned short running;
  181. };
  182. struct emu10k1x_midi {
  183. struct emu10k1x *emu;
  184. struct snd_rawmidi *rmidi;
  185. struct snd_rawmidi_substream *substream_input;
  186. struct snd_rawmidi_substream *substream_output;
  187. unsigned int midi_mode;
  188. spinlock_t input_lock;
  189. spinlock_t output_lock;
  190. spinlock_t open_lock;
  191. int tx_enable, rx_enable;
  192. int port;
  193. int ipr_tx, ipr_rx;
  194. void (*interrupt)(struct emu10k1x *emu, unsigned int status);
  195. };
  196. // definition of the chip-specific record
  197. struct emu10k1x {
  198. struct snd_card *card;
  199. struct pci_dev *pci;
  200. unsigned long port;
  201. struct resource *res_port;
  202. int irq;
  203. unsigned int revision; /* chip revision */
  204. unsigned int serial; /* serial number */
  205. unsigned short model; /* subsystem id */
  206. spinlock_t emu_lock;
  207. spinlock_t voice_lock;
  208. struct snd_ac97 *ac97;
  209. struct snd_pcm *pcm;
  210. struct emu10k1x_voice voices[3];
  211. struct emu10k1x_voice capture_voice;
  212. u32 spdif_bits[3]; // SPDIF out setup
  213. struct snd_dma_buffer dma_buffer;
  214. struct emu10k1x_midi midi;
  215. };
  216. /* hardware definition */
  217. static struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
  218. .info = (SNDRV_PCM_INFO_MMAP |
  219. SNDRV_PCM_INFO_INTERLEAVED |
  220. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  221. SNDRV_PCM_INFO_MMAP_VALID),
  222. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  223. .rates = SNDRV_PCM_RATE_48000,
  224. .rate_min = 48000,
  225. .rate_max = 48000,
  226. .channels_min = 2,
  227. .channels_max = 2,
  228. .buffer_bytes_max = (32*1024),
  229. .period_bytes_min = 64,
  230. .period_bytes_max = (16*1024),
  231. .periods_min = 2,
  232. .periods_max = 8,
  233. .fifo_size = 0,
  234. };
  235. static struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
  236. .info = (SNDRV_PCM_INFO_MMAP |
  237. SNDRV_PCM_INFO_INTERLEAVED |
  238. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  239. SNDRV_PCM_INFO_MMAP_VALID),
  240. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  241. .rates = SNDRV_PCM_RATE_48000,
  242. .rate_min = 48000,
  243. .rate_max = 48000,
  244. .channels_min = 2,
  245. .channels_max = 2,
  246. .buffer_bytes_max = (32*1024),
  247. .period_bytes_min = 64,
  248. .period_bytes_max = (16*1024),
  249. .periods_min = 2,
  250. .periods_max = 2,
  251. .fifo_size = 0,
  252. };
  253. static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
  254. unsigned int reg,
  255. unsigned int chn)
  256. {
  257. unsigned long flags;
  258. unsigned int regptr, val;
  259. regptr = (reg << 16) | chn;
  260. spin_lock_irqsave(&emu->emu_lock, flags);
  261. outl(regptr, emu->port + PTR);
  262. val = inl(emu->port + DATA);
  263. spin_unlock_irqrestore(&emu->emu_lock, flags);
  264. return val;
  265. }
  266. static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
  267. unsigned int reg,
  268. unsigned int chn,
  269. unsigned int data)
  270. {
  271. unsigned int regptr;
  272. unsigned long flags;
  273. regptr = (reg << 16) | chn;
  274. spin_lock_irqsave(&emu->emu_lock, flags);
  275. outl(regptr, emu->port + PTR);
  276. outl(data, emu->port + DATA);
  277. spin_unlock_irqrestore(&emu->emu_lock, flags);
  278. }
  279. static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
  280. {
  281. unsigned long flags;
  282. unsigned int enable;
  283. spin_lock_irqsave(&emu->emu_lock, flags);
  284. enable = inl(emu->port + INTE) | intrenb;
  285. outl(enable, emu->port + INTE);
  286. spin_unlock_irqrestore(&emu->emu_lock, flags);
  287. }
  288. static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
  289. {
  290. unsigned long flags;
  291. unsigned int enable;
  292. spin_lock_irqsave(&emu->emu_lock, flags);
  293. enable = inl(emu->port + INTE) & ~intrenb;
  294. outl(enable, emu->port + INTE);
  295. spin_unlock_irqrestore(&emu->emu_lock, flags);
  296. }
  297. static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
  298. {
  299. unsigned long flags;
  300. spin_lock_irqsave(&emu->emu_lock, flags);
  301. outl(value, emu->port + GPIO);
  302. spin_unlock_irqrestore(&emu->emu_lock, flags);
  303. }
  304. static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
  305. {
  306. kfree(runtime->private_data);
  307. }
  308. static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
  309. {
  310. struct emu10k1x_pcm *epcm;
  311. if ((epcm = voice->epcm) == NULL)
  312. return;
  313. if (epcm->substream == NULL)
  314. return;
  315. #if 0
  316. snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
  317. epcm->substream->ops->pointer(epcm->substream),
  318. snd_pcm_lib_period_bytes(epcm->substream),
  319. snd_pcm_lib_buffer_bytes(epcm->substream));
  320. #endif
  321. snd_pcm_period_elapsed(epcm->substream);
  322. }
  323. /* open callback */
  324. static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
  325. {
  326. struct emu10k1x *chip = snd_pcm_substream_chip(substream);
  327. struct emu10k1x_pcm *epcm;
  328. struct snd_pcm_runtime *runtime = substream->runtime;
  329. int err;
  330. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
  331. return err;
  332. }
  333. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  334. return err;
  335. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  336. if (epcm == NULL)
  337. return -ENOMEM;
  338. epcm->emu = chip;
  339. epcm->substream = substream;
  340. runtime->private_data = epcm;
  341. runtime->private_free = snd_emu10k1x_pcm_free_substream;
  342. runtime->hw = snd_emu10k1x_playback_hw;
  343. return 0;
  344. }
  345. /* close callback */
  346. static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
  347. {
  348. return 0;
  349. }
  350. /* hw_params callback */
  351. static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
  352. struct snd_pcm_hw_params *hw_params)
  353. {
  354. struct snd_pcm_runtime *runtime = substream->runtime;
  355. struct emu10k1x_pcm *epcm = runtime->private_data;
  356. if (! epcm->voice) {
  357. epcm->voice = &epcm->emu->voices[substream->pcm->device];
  358. epcm->voice->use = 1;
  359. epcm->voice->epcm = epcm;
  360. }
  361. return snd_pcm_lib_malloc_pages(substream,
  362. params_buffer_bytes(hw_params));
  363. }
  364. /* hw_free callback */
  365. static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
  366. {
  367. struct snd_pcm_runtime *runtime = substream->runtime;
  368. struct emu10k1x_pcm *epcm;
  369. if (runtime->private_data == NULL)
  370. return 0;
  371. epcm = runtime->private_data;
  372. if (epcm->voice) {
  373. epcm->voice->use = 0;
  374. epcm->voice->epcm = NULL;
  375. epcm->voice = NULL;
  376. }
  377. return snd_pcm_lib_free_pages(substream);
  378. }
  379. /* prepare callback */
  380. static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
  381. {
  382. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  383. struct snd_pcm_runtime *runtime = substream->runtime;
  384. struct emu10k1x_pcm *epcm = runtime->private_data;
  385. int voice = epcm->voice->number;
  386. u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
  387. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  388. int i;
  389. for(i=0; i < runtime->periods; i++) {
  390. *table_base++=runtime->dma_addr+(i*period_size_bytes);
  391. *table_base++=period_size_bytes<<16;
  392. }
  393. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
  394. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
  395. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
  396. snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
  397. snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
  398. snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
  399. snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
  400. snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
  401. return 0;
  402. }
  403. /* trigger callback */
  404. static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
  405. int cmd)
  406. {
  407. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  408. struct snd_pcm_runtime *runtime = substream->runtime;
  409. struct emu10k1x_pcm *epcm = runtime->private_data;
  410. int channel = epcm->voice->number;
  411. int result = 0;
  412. // snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
  413. switch (cmd) {
  414. case SNDRV_PCM_TRIGGER_START:
  415. if(runtime->periods == 2)
  416. snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
  417. else
  418. snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
  419. epcm->running = 1;
  420. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
  421. break;
  422. case SNDRV_PCM_TRIGGER_STOP:
  423. epcm->running = 0;
  424. snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
  425. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
  426. break;
  427. default:
  428. result = -EINVAL;
  429. break;
  430. }
  431. return result;
  432. }
  433. /* pointer callback */
  434. static snd_pcm_uframes_t
  435. snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
  436. {
  437. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  438. struct snd_pcm_runtime *runtime = substream->runtime;
  439. struct emu10k1x_pcm *epcm = runtime->private_data;
  440. int channel = epcm->voice->number;
  441. snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
  442. if (!epcm->running)
  443. return 0;
  444. ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  445. ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
  446. ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  447. if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
  448. return 0;
  449. if (ptr3 != ptr4)
  450. ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
  451. ptr2 = bytes_to_frames(runtime, ptr1);
  452. ptr2 += (ptr4 >> 3) * runtime->period_size;
  453. ptr = ptr2;
  454. if (ptr >= runtime->buffer_size)
  455. ptr -= runtime->buffer_size;
  456. return ptr;
  457. }
  458. /* operators */
  459. static struct snd_pcm_ops snd_emu10k1x_playback_ops = {
  460. .open = snd_emu10k1x_playback_open,
  461. .close = snd_emu10k1x_playback_close,
  462. .ioctl = snd_pcm_lib_ioctl,
  463. .hw_params = snd_emu10k1x_pcm_hw_params,
  464. .hw_free = snd_emu10k1x_pcm_hw_free,
  465. .prepare = snd_emu10k1x_pcm_prepare,
  466. .trigger = snd_emu10k1x_pcm_trigger,
  467. .pointer = snd_emu10k1x_pcm_pointer,
  468. };
  469. /* open_capture callback */
  470. static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
  471. {
  472. struct emu10k1x *chip = snd_pcm_substream_chip(substream);
  473. struct emu10k1x_pcm *epcm;
  474. struct snd_pcm_runtime *runtime = substream->runtime;
  475. int err;
  476. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  477. return err;
  478. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  479. return err;
  480. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  481. if (epcm == NULL)
  482. return -ENOMEM;
  483. epcm->emu = chip;
  484. epcm->substream = substream;
  485. runtime->private_data = epcm;
  486. runtime->private_free = snd_emu10k1x_pcm_free_substream;
  487. runtime->hw = snd_emu10k1x_capture_hw;
  488. return 0;
  489. }
  490. /* close callback */
  491. static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
  492. {
  493. return 0;
  494. }
  495. /* hw_params callback */
  496. static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  497. struct snd_pcm_hw_params *hw_params)
  498. {
  499. struct snd_pcm_runtime *runtime = substream->runtime;
  500. struct emu10k1x_pcm *epcm = runtime->private_data;
  501. if (! epcm->voice) {
  502. if (epcm->emu->capture_voice.use)
  503. return -EBUSY;
  504. epcm->voice = &epcm->emu->capture_voice;
  505. epcm->voice->epcm = epcm;
  506. epcm->voice->use = 1;
  507. }
  508. return snd_pcm_lib_malloc_pages(substream,
  509. params_buffer_bytes(hw_params));
  510. }
  511. /* hw_free callback */
  512. static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  513. {
  514. struct snd_pcm_runtime *runtime = substream->runtime;
  515. struct emu10k1x_pcm *epcm;
  516. if (runtime->private_data == NULL)
  517. return 0;
  518. epcm = runtime->private_data;
  519. if (epcm->voice) {
  520. epcm->voice->use = 0;
  521. epcm->voice->epcm = NULL;
  522. epcm->voice = NULL;
  523. }
  524. return snd_pcm_lib_free_pages(substream);
  525. }
  526. /* prepare capture callback */
  527. static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
  528. {
  529. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  530. struct snd_pcm_runtime *runtime = substream->runtime;
  531. snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
  532. snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  533. snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
  534. snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
  535. return 0;
  536. }
  537. /* trigger_capture callback */
  538. static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
  539. int cmd)
  540. {
  541. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  542. struct snd_pcm_runtime *runtime = substream->runtime;
  543. struct emu10k1x_pcm *epcm = runtime->private_data;
  544. int result = 0;
  545. switch (cmd) {
  546. case SNDRV_PCM_TRIGGER_START:
  547. snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
  548. INTE_CAP_0_HALF_LOOP);
  549. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
  550. epcm->running = 1;
  551. break;
  552. case SNDRV_PCM_TRIGGER_STOP:
  553. epcm->running = 0;
  554. snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
  555. INTE_CAP_0_HALF_LOOP);
  556. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
  557. break;
  558. default:
  559. result = -EINVAL;
  560. break;
  561. }
  562. return result;
  563. }
  564. /* pointer_capture callback */
  565. static snd_pcm_uframes_t
  566. snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
  567. {
  568. struct emu10k1x *emu = snd_pcm_substream_chip(substream);
  569. struct snd_pcm_runtime *runtime = substream->runtime;
  570. struct emu10k1x_pcm *epcm = runtime->private_data;
  571. snd_pcm_uframes_t ptr;
  572. if (!epcm->running)
  573. return 0;
  574. ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
  575. if (ptr >= runtime->buffer_size)
  576. ptr -= runtime->buffer_size;
  577. return ptr;
  578. }
  579. static struct snd_pcm_ops snd_emu10k1x_capture_ops = {
  580. .open = snd_emu10k1x_pcm_open_capture,
  581. .close = snd_emu10k1x_pcm_close_capture,
  582. .ioctl = snd_pcm_lib_ioctl,
  583. .hw_params = snd_emu10k1x_pcm_hw_params_capture,
  584. .hw_free = snd_emu10k1x_pcm_hw_free_capture,
  585. .prepare = snd_emu10k1x_pcm_prepare_capture,
  586. .trigger = snd_emu10k1x_pcm_trigger_capture,
  587. .pointer = snd_emu10k1x_pcm_pointer_capture,
  588. };
  589. static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
  590. unsigned short reg)
  591. {
  592. struct emu10k1x *emu = ac97->private_data;
  593. unsigned long flags;
  594. unsigned short val;
  595. spin_lock_irqsave(&emu->emu_lock, flags);
  596. outb(reg, emu->port + AC97ADDRESS);
  597. val = inw(emu->port + AC97DATA);
  598. spin_unlock_irqrestore(&emu->emu_lock, flags);
  599. return val;
  600. }
  601. static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
  602. unsigned short reg, unsigned short val)
  603. {
  604. struct emu10k1x *emu = ac97->private_data;
  605. unsigned long flags;
  606. spin_lock_irqsave(&emu->emu_lock, flags);
  607. outb(reg, emu->port + AC97ADDRESS);
  608. outw(val, emu->port + AC97DATA);
  609. spin_unlock_irqrestore(&emu->emu_lock, flags);
  610. }
  611. static int snd_emu10k1x_ac97(struct emu10k1x *chip)
  612. {
  613. struct snd_ac97_bus *pbus;
  614. struct snd_ac97_template ac97;
  615. int err;
  616. static struct snd_ac97_bus_ops ops = {
  617. .write = snd_emu10k1x_ac97_write,
  618. .read = snd_emu10k1x_ac97_read,
  619. };
  620. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  621. return err;
  622. pbus->no_vra = 1; /* we don't need VRA */
  623. memset(&ac97, 0, sizeof(ac97));
  624. ac97.private_data = chip;
  625. ac97.scaps = AC97_SCAP_NO_SPDIF;
  626. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  627. }
  628. static int snd_emu10k1x_free(struct emu10k1x *chip)
  629. {
  630. snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
  631. // disable interrupts
  632. outl(0, chip->port + INTE);
  633. // disable audio
  634. outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
  635. // release the i/o port
  636. release_and_free_resource(chip->res_port);
  637. // release the irq
  638. if (chip->irq >= 0)
  639. free_irq(chip->irq, (void *)chip);
  640. // release the DMA
  641. if (chip->dma_buffer.area) {
  642. snd_dma_free_pages(&chip->dma_buffer);
  643. }
  644. pci_disable_device(chip->pci);
  645. // release the data
  646. kfree(chip);
  647. return 0;
  648. }
  649. static int snd_emu10k1x_dev_free(struct snd_device *device)
  650. {
  651. struct emu10k1x *chip = device->device_data;
  652. return snd_emu10k1x_free(chip);
  653. }
  654. static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
  655. struct pt_regs *regs)
  656. {
  657. unsigned int status;
  658. struct emu10k1x *chip = dev_id;
  659. struct emu10k1x_voice *pvoice = chip->voices;
  660. int i;
  661. int mask;
  662. status = inl(chip->port + IPR);
  663. if (! status)
  664. return IRQ_NONE;
  665. // capture interrupt
  666. if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
  667. struct emu10k1x_voice *pvoice = &chip->capture_voice;
  668. if (pvoice->use)
  669. snd_emu10k1x_pcm_interrupt(chip, pvoice);
  670. else
  671. snd_emu10k1x_intr_disable(chip,
  672. INTE_CAP_0_LOOP |
  673. INTE_CAP_0_HALF_LOOP);
  674. }
  675. mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
  676. for (i = 0; i < 3; i++) {
  677. if (status & mask) {
  678. if (pvoice->use)
  679. snd_emu10k1x_pcm_interrupt(chip, pvoice);
  680. else
  681. snd_emu10k1x_intr_disable(chip, mask);
  682. }
  683. pvoice++;
  684. mask <<= 1;
  685. }
  686. if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
  687. if (chip->midi.interrupt)
  688. chip->midi.interrupt(chip, status);
  689. else
  690. snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
  691. }
  692. // acknowledge the interrupt if necessary
  693. outl(status, chip->port + IPR);
  694. // snd_printk(KERN_INFO "interrupt %08x\n", status);
  695. return IRQ_HANDLED;
  696. }
  697. static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
  698. {
  699. struct snd_pcm *pcm;
  700. int err;
  701. int capture = 0;
  702. if (rpcm)
  703. *rpcm = NULL;
  704. if (device == 0)
  705. capture = 1;
  706. if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
  707. return err;
  708. pcm->private_data = emu;
  709. switch(device) {
  710. case 0:
  711. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  712. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
  713. break;
  714. case 1:
  715. case 2:
  716. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  717. break;
  718. }
  719. pcm->info_flags = 0;
  720. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  721. switch(device) {
  722. case 0:
  723. strcpy(pcm->name, "EMU10K1X Front");
  724. break;
  725. case 1:
  726. strcpy(pcm->name, "EMU10K1X Rear");
  727. break;
  728. case 2:
  729. strcpy(pcm->name, "EMU10K1X Center/LFE");
  730. break;
  731. }
  732. emu->pcm = pcm;
  733. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  734. snd_dma_pci_data(emu->pci),
  735. 32*1024, 32*1024);
  736. if (rpcm)
  737. *rpcm = pcm;
  738. return 0;
  739. }
  740. static int __devinit snd_emu10k1x_create(struct snd_card *card,
  741. struct pci_dev *pci,
  742. struct emu10k1x **rchip)
  743. {
  744. struct emu10k1x *chip;
  745. int err;
  746. int ch;
  747. static struct snd_device_ops ops = {
  748. .dev_free = snd_emu10k1x_dev_free,
  749. };
  750. *rchip = NULL;
  751. if ((err = pci_enable_device(pci)) < 0)
  752. return err;
  753. if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
  754. pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
  755. snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
  756. pci_disable_device(pci);
  757. return -ENXIO;
  758. }
  759. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  760. if (chip == NULL) {
  761. pci_disable_device(pci);
  762. return -ENOMEM;
  763. }
  764. chip->card = card;
  765. chip->pci = pci;
  766. chip->irq = -1;
  767. spin_lock_init(&chip->emu_lock);
  768. spin_lock_init(&chip->voice_lock);
  769. chip->port = pci_resource_start(pci, 0);
  770. if ((chip->res_port = request_region(chip->port, 8,
  771. "EMU10K1X")) == NULL) {
  772. snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
  773. snd_emu10k1x_free(chip);
  774. return -EBUSY;
  775. }
  776. if (request_irq(pci->irq, snd_emu10k1x_interrupt,
  777. SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
  778. (void *)chip)) {
  779. snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
  780. snd_emu10k1x_free(chip);
  781. return -EBUSY;
  782. }
  783. chip->irq = pci->irq;
  784. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  785. 4 * 1024, &chip->dma_buffer) < 0) {
  786. snd_emu10k1x_free(chip);
  787. return -ENOMEM;
  788. }
  789. pci_set_master(pci);
  790. /* read revision & serial */
  791. pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
  792. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  793. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  794. snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  795. chip->revision, chip->serial);
  796. outl(0, chip->port + INTE);
  797. for(ch = 0; ch < 3; ch++) {
  798. chip->voices[ch].emu = chip;
  799. chip->voices[ch].number = ch;
  800. }
  801. /*
  802. * Init to 0x02109204 :
  803. * Clock accuracy = 0 (1000ppm)
  804. * Sample Rate = 2 (48kHz)
  805. * Audio Channel = 1 (Left of 2)
  806. * Source Number = 0 (Unspecified)
  807. * Generation Status = 1 (Original for Cat Code 12)
  808. * Cat Code = 12 (Digital Signal Mixer)
  809. * Mode = 0 (Mode 0)
  810. * Emphasis = 0 (None)
  811. * CP = 1 (Copyright unasserted)
  812. * AN = 0 (Audio data)
  813. * P = 0 (Consumer)
  814. */
  815. snd_emu10k1x_ptr_write(chip, SPCS0, 0,
  816. chip->spdif_bits[0] =
  817. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  818. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  819. SPCS_GENERATIONSTATUS | 0x00001200 |
  820. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  821. snd_emu10k1x_ptr_write(chip, SPCS1, 0,
  822. chip->spdif_bits[1] =
  823. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  824. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  825. SPCS_GENERATIONSTATUS | 0x00001200 |
  826. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  827. snd_emu10k1x_ptr_write(chip, SPCS2, 0,
  828. chip->spdif_bits[2] =
  829. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  830. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  831. SPCS_GENERATIONSTATUS | 0x00001200 |
  832. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  833. snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
  834. snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
  835. snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
  836. outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  837. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  838. chip, &ops)) < 0) {
  839. snd_emu10k1x_free(chip);
  840. return err;
  841. }
  842. *rchip = chip;
  843. return 0;
  844. }
  845. static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
  846. struct snd_info_buffer *buffer)
  847. {
  848. struct emu10k1x *emu = entry->private_data;
  849. unsigned long value,value1,value2;
  850. unsigned long flags;
  851. int i;
  852. snd_iprintf(buffer, "Registers:\n\n");
  853. for(i = 0; i < 0x20; i+=4) {
  854. spin_lock_irqsave(&emu->emu_lock, flags);
  855. value = inl(emu->port + i);
  856. spin_unlock_irqrestore(&emu->emu_lock, flags);
  857. snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
  858. }
  859. snd_iprintf(buffer, "\nRegisters\n\n");
  860. for(i = 0; i <= 0x48; i++) {
  861. value = snd_emu10k1x_ptr_read(emu, i, 0);
  862. if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
  863. value1 = snd_emu10k1x_ptr_read(emu, i, 1);
  864. value2 = snd_emu10k1x_ptr_read(emu, i, 2);
  865. snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
  866. } else {
  867. snd_iprintf(buffer, "%02X: %08lX\n", i, value);
  868. }
  869. }
  870. }
  871. static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
  872. struct snd_info_buffer *buffer)
  873. {
  874. struct emu10k1x *emu = entry->private_data;
  875. char line[64];
  876. unsigned int reg, channel_id , val;
  877. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  878. if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
  879. continue;
  880. if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
  881. && (channel_id >=0) && (channel_id <= 2) )
  882. snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
  883. }
  884. }
  885. static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
  886. {
  887. struct snd_info_entry *entry;
  888. if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
  889. snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
  890. entry->c.text.write_size = 64;
  891. entry->c.text.write = snd_emu10k1x_proc_reg_write;
  892. entry->mode |= S_IWUSR;
  893. entry->private_data = emu;
  894. }
  895. return 0;
  896. }
  897. static int snd_emu10k1x_shared_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  898. {
  899. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  900. uinfo->count = 1;
  901. uinfo->value.integer.min = 0;
  902. uinfo->value.integer.max = 1;
  903. return 0;
  904. }
  905. static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
  906. struct snd_ctl_elem_value *ucontrol)
  907. {
  908. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  909. ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
  910. return 0;
  911. }
  912. static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
  913. struct snd_ctl_elem_value *ucontrol)
  914. {
  915. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  916. unsigned int val;
  917. int change = 0;
  918. val = ucontrol->value.integer.value[0] ;
  919. if (val) {
  920. // enable spdif output
  921. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
  922. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
  923. snd_emu10k1x_gpio_write(emu, 0x1000);
  924. } else {
  925. // disable spdif output
  926. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
  927. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
  928. snd_emu10k1x_gpio_write(emu, 0x1080);
  929. }
  930. return change;
  931. }
  932. static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
  933. {
  934. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  935. .name = "Analog/Digital Output Jack",
  936. .info = snd_emu10k1x_shared_spdif_info,
  937. .get = snd_emu10k1x_shared_spdif_get,
  938. .put = snd_emu10k1x_shared_spdif_put
  939. };
  940. static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  941. {
  942. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  943. uinfo->count = 1;
  944. return 0;
  945. }
  946. static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
  947. struct snd_ctl_elem_value *ucontrol)
  948. {
  949. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  950. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  951. ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
  952. ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
  953. ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
  954. ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
  955. return 0;
  956. }
  957. static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
  958. struct snd_ctl_elem_value *ucontrol)
  959. {
  960. ucontrol->value.iec958.status[0] = 0xff;
  961. ucontrol->value.iec958.status[1] = 0xff;
  962. ucontrol->value.iec958.status[2] = 0xff;
  963. ucontrol->value.iec958.status[3] = 0xff;
  964. return 0;
  965. }
  966. static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
  967. struct snd_ctl_elem_value *ucontrol)
  968. {
  969. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  970. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  971. int change;
  972. unsigned int val;
  973. val = (ucontrol->value.iec958.status[0] << 0) |
  974. (ucontrol->value.iec958.status[1] << 8) |
  975. (ucontrol->value.iec958.status[2] << 16) |
  976. (ucontrol->value.iec958.status[3] << 24);
  977. change = val != emu->spdif_bits[idx];
  978. if (change) {
  979. snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
  980. emu->spdif_bits[idx] = val;
  981. }
  982. return change;
  983. }
  984. static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
  985. {
  986. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  987. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  988. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  989. .count = 3,
  990. .info = snd_emu10k1x_spdif_info,
  991. .get = snd_emu10k1x_spdif_get_mask
  992. };
  993. static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
  994. {
  995. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  996. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  997. .count = 3,
  998. .info = snd_emu10k1x_spdif_info,
  999. .get = snd_emu10k1x_spdif_get,
  1000. .put = snd_emu10k1x_spdif_put
  1001. };
  1002. static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
  1003. {
  1004. int err;
  1005. struct snd_kcontrol *kctl;
  1006. struct snd_card *card = emu->card;
  1007. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
  1008. return -ENOMEM;
  1009. if ((err = snd_ctl_add(card, kctl)))
  1010. return err;
  1011. if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
  1012. return -ENOMEM;
  1013. if ((err = snd_ctl_add(card, kctl)))
  1014. return err;
  1015. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
  1016. return -ENOMEM;
  1017. if ((err = snd_ctl_add(card, kctl)))
  1018. return err;
  1019. return 0;
  1020. }
  1021. #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
  1022. #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
  1023. static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
  1024. {
  1025. return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
  1026. }
  1027. static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
  1028. {
  1029. snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
  1030. }
  1031. #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
  1032. #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
  1033. #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
  1034. #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
  1035. #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
  1036. #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
  1037. #define MPU401_RESET 0xff
  1038. #define MPU401_ENTER_UART 0x3f
  1039. #define MPU401_ACK 0xfe
  1040. static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
  1041. {
  1042. int timeout = 100000;
  1043. for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
  1044. mpu401_read_data(emu, mpu);
  1045. #ifdef CONFIG_SND_DEBUG
  1046. if (timeout <= 0)
  1047. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
  1048. #endif
  1049. }
  1050. /*
  1051. */
  1052. static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
  1053. struct emu10k1x_midi *midi, unsigned int status)
  1054. {
  1055. unsigned char byte;
  1056. if (midi->rmidi == NULL) {
  1057. snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
  1058. return;
  1059. }
  1060. spin_lock(&midi->input_lock);
  1061. if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
  1062. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1063. mpu401_clear_rx(emu, midi);
  1064. } else {
  1065. byte = mpu401_read_data(emu, midi);
  1066. if (midi->substream_input)
  1067. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  1068. }
  1069. }
  1070. spin_unlock(&midi->input_lock);
  1071. spin_lock(&midi->output_lock);
  1072. if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
  1073. if (midi->substream_output &&
  1074. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  1075. mpu401_write_data(emu, midi, byte);
  1076. } else {
  1077. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1078. }
  1079. }
  1080. spin_unlock(&midi->output_lock);
  1081. }
  1082. static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
  1083. {
  1084. do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
  1085. }
  1086. static void snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
  1087. struct emu10k1x_midi *midi, unsigned char cmd, int ack)
  1088. {
  1089. unsigned long flags;
  1090. int timeout, ok;
  1091. spin_lock_irqsave(&midi->input_lock, flags);
  1092. mpu401_write_data(emu, midi, 0x00);
  1093. /* mpu401_clear_rx(emu, midi); */
  1094. mpu401_write_cmd(emu, midi, cmd);
  1095. if (ack) {
  1096. ok = 0;
  1097. timeout = 10000;
  1098. while (!ok && timeout-- > 0) {
  1099. if (mpu401_input_avail(emu, midi)) {
  1100. if (mpu401_read_data(emu, midi) == MPU401_ACK)
  1101. ok = 1;
  1102. }
  1103. }
  1104. if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
  1105. ok = 1;
  1106. } else {
  1107. ok = 1;
  1108. }
  1109. spin_unlock_irqrestore(&midi->input_lock, flags);
  1110. if (!ok)
  1111. snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
  1112. cmd, emu->port,
  1113. mpu401_read_stat(emu, midi),
  1114. mpu401_read_data(emu, midi));
  1115. }
  1116. static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
  1117. {
  1118. struct emu10k1x *emu;
  1119. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1120. unsigned long flags;
  1121. emu = midi->emu;
  1122. snd_assert(emu, return -ENXIO);
  1123. spin_lock_irqsave(&midi->open_lock, flags);
  1124. midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
  1125. midi->substream_input = substream;
  1126. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1127. spin_unlock_irqrestore(&midi->open_lock, flags);
  1128. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
  1129. snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  1130. } else {
  1131. spin_unlock_irqrestore(&midi->open_lock, flags);
  1132. }
  1133. return 0;
  1134. }
  1135. static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
  1136. {
  1137. struct emu10k1x *emu;
  1138. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1139. unsigned long flags;
  1140. emu = midi->emu;
  1141. snd_assert(emu, return -ENXIO);
  1142. spin_lock_irqsave(&midi->open_lock, flags);
  1143. midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
  1144. midi->substream_output = substream;
  1145. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1146. spin_unlock_irqrestore(&midi->open_lock, flags);
  1147. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
  1148. snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  1149. } else {
  1150. spin_unlock_irqrestore(&midi->open_lock, flags);
  1151. }
  1152. return 0;
  1153. }
  1154. static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
  1155. {
  1156. struct emu10k1x *emu;
  1157. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1158. unsigned long flags;
  1159. emu = midi->emu;
  1160. snd_assert(emu, return -ENXIO);
  1161. spin_lock_irqsave(&midi->open_lock, flags);
  1162. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1163. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
  1164. midi->substream_input = NULL;
  1165. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1166. spin_unlock_irqrestore(&midi->open_lock, flags);
  1167. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1168. } else {
  1169. spin_unlock_irqrestore(&midi->open_lock, flags);
  1170. }
  1171. return 0;
  1172. }
  1173. static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
  1174. {
  1175. struct emu10k1x *emu;
  1176. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1177. unsigned long flags;
  1178. emu = midi->emu;
  1179. snd_assert(emu, return -ENXIO);
  1180. spin_lock_irqsave(&midi->open_lock, flags);
  1181. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1182. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
  1183. midi->substream_output = NULL;
  1184. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1185. spin_unlock_irqrestore(&midi->open_lock, flags);
  1186. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1187. } else {
  1188. spin_unlock_irqrestore(&midi->open_lock, flags);
  1189. }
  1190. return 0;
  1191. }
  1192. static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1193. {
  1194. struct emu10k1x *emu;
  1195. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1196. emu = midi->emu;
  1197. snd_assert(emu, return);
  1198. if (up)
  1199. snd_emu10k1x_intr_enable(emu, midi->rx_enable);
  1200. else
  1201. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1202. }
  1203. static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  1204. {
  1205. struct emu10k1x *emu;
  1206. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1207. unsigned long flags;
  1208. emu = midi->emu;
  1209. snd_assert(emu, return);
  1210. if (up) {
  1211. int max = 4;
  1212. unsigned char byte;
  1213. /* try to send some amount of bytes here before interrupts */
  1214. spin_lock_irqsave(&midi->output_lock, flags);
  1215. while (max > 0) {
  1216. if (mpu401_output_ready(emu, midi)) {
  1217. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
  1218. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  1219. /* no more data */
  1220. spin_unlock_irqrestore(&midi->output_lock, flags);
  1221. return;
  1222. }
  1223. mpu401_write_data(emu, midi, byte);
  1224. max--;
  1225. } else {
  1226. break;
  1227. }
  1228. }
  1229. spin_unlock_irqrestore(&midi->output_lock, flags);
  1230. snd_emu10k1x_intr_enable(emu, midi->tx_enable);
  1231. } else {
  1232. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1233. }
  1234. }
  1235. /*
  1236. */
  1237. static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
  1238. {
  1239. .open = snd_emu10k1x_midi_output_open,
  1240. .close = snd_emu10k1x_midi_output_close,
  1241. .trigger = snd_emu10k1x_midi_output_trigger,
  1242. };
  1243. static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
  1244. {
  1245. .open = snd_emu10k1x_midi_input_open,
  1246. .close = snd_emu10k1x_midi_input_close,
  1247. .trigger = snd_emu10k1x_midi_input_trigger,
  1248. };
  1249. static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
  1250. {
  1251. struct emu10k1x_midi *midi = rmidi->private_data;
  1252. midi->interrupt = NULL;
  1253. midi->rmidi = NULL;
  1254. }
  1255. static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
  1256. struct emu10k1x_midi *midi, int device, char *name)
  1257. {
  1258. struct snd_rawmidi *rmidi;
  1259. int err;
  1260. if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
  1261. return err;
  1262. midi->emu = emu;
  1263. spin_lock_init(&midi->open_lock);
  1264. spin_lock_init(&midi->input_lock);
  1265. spin_lock_init(&midi->output_lock);
  1266. strcpy(rmidi->name, name);
  1267. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
  1268. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
  1269. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  1270. SNDRV_RAWMIDI_INFO_INPUT |
  1271. SNDRV_RAWMIDI_INFO_DUPLEX;
  1272. rmidi->private_data = midi;
  1273. rmidi->private_free = snd_emu10k1x_midi_free;
  1274. midi->rmidi = rmidi;
  1275. return 0;
  1276. }
  1277. static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
  1278. {
  1279. struct emu10k1x_midi *midi = &emu->midi;
  1280. int err;
  1281. if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
  1282. return err;
  1283. midi->tx_enable = INTE_MIDITXENABLE;
  1284. midi->rx_enable = INTE_MIDIRXENABLE;
  1285. midi->port = MUDATA;
  1286. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  1287. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  1288. midi->interrupt = snd_emu10k1x_midi_interrupt;
  1289. return 0;
  1290. }
  1291. static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
  1292. const struct pci_device_id *pci_id)
  1293. {
  1294. static int dev;
  1295. struct snd_card *card;
  1296. struct emu10k1x *chip;
  1297. int err;
  1298. if (dev >= SNDRV_CARDS)
  1299. return -ENODEV;
  1300. if (!enable[dev]) {
  1301. dev++;
  1302. return -ENOENT;
  1303. }
  1304. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1305. if (card == NULL)
  1306. return -ENOMEM;
  1307. if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
  1308. snd_card_free(card);
  1309. return err;
  1310. }
  1311. if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
  1312. snd_card_free(card);
  1313. return err;
  1314. }
  1315. if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
  1316. snd_card_free(card);
  1317. return err;
  1318. }
  1319. if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
  1320. snd_card_free(card);
  1321. return err;
  1322. }
  1323. if ((err = snd_emu10k1x_ac97(chip)) < 0) {
  1324. snd_card_free(card);
  1325. return err;
  1326. }
  1327. if ((err = snd_emu10k1x_mixer(chip)) < 0) {
  1328. snd_card_free(card);
  1329. return err;
  1330. }
  1331. if ((err = snd_emu10k1x_midi(chip)) < 0) {
  1332. snd_card_free(card);
  1333. return err;
  1334. }
  1335. snd_emu10k1x_proc_init(chip);
  1336. strcpy(card->driver, "EMU10K1X");
  1337. strcpy(card->shortname, "Dell Sound Blaster Live!");
  1338. sprintf(card->longname, "%s at 0x%lx irq %i",
  1339. card->shortname, chip->port, chip->irq);
  1340. if ((err = snd_card_register(card)) < 0) {
  1341. snd_card_free(card);
  1342. return err;
  1343. }
  1344. pci_set_drvdata(pci, card);
  1345. dev++;
  1346. return 0;
  1347. }
  1348. static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
  1349. {
  1350. snd_card_free(pci_get_drvdata(pci));
  1351. pci_set_drvdata(pci, NULL);
  1352. }
  1353. // PCI IDs
  1354. static struct pci_device_id snd_emu10k1x_ids[] = {
  1355. { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
  1356. { 0, }
  1357. };
  1358. MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
  1359. // pci_driver definition
  1360. static struct pci_driver driver = {
  1361. .name = "EMU10K1X",
  1362. .id_table = snd_emu10k1x_ids,
  1363. .probe = snd_emu10k1x_probe,
  1364. .remove = __devexit_p(snd_emu10k1x_remove),
  1365. };
  1366. // initialization of the module
  1367. static int __init alsa_card_emu10k1x_init(void)
  1368. {
  1369. int err;
  1370. if ((err = pci_register_driver(&driver)) > 0)
  1371. return err;
  1372. return 0;
  1373. }
  1374. // clean up the module
  1375. static void __exit alsa_card_emu10k1x_exit(void)
  1376. {
  1377. pci_unregister_driver(&driver);
  1378. }
  1379. module_init(alsa_card_emu10k1x_init)
  1380. module_exit(alsa_card_emu10k1x_exit)