emu10k1x.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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. typedef struct snd_emu10k1x_voice emu10k1x_voice_t;
  171. typedef struct snd_emu10k1x emu10k1x_t;
  172. typedef struct snd_emu10k1x_pcm emu10k1x_pcm_t;
  173. struct snd_emu10k1x_voice {
  174. emu10k1x_t *emu;
  175. int number;
  176. int use;
  177. emu10k1x_pcm_t *epcm;
  178. };
  179. struct snd_emu10k1x_pcm {
  180. emu10k1x_t *emu;
  181. snd_pcm_substream_t *substream;
  182. emu10k1x_voice_t *voice;
  183. unsigned short running;
  184. };
  185. typedef struct {
  186. struct snd_emu10k1x *emu;
  187. snd_rawmidi_t *rmidi;
  188. snd_rawmidi_substream_t *substream_input;
  189. snd_rawmidi_substream_t *substream_output;
  190. unsigned int midi_mode;
  191. spinlock_t input_lock;
  192. spinlock_t output_lock;
  193. spinlock_t open_lock;
  194. int tx_enable, rx_enable;
  195. int port;
  196. int ipr_tx, ipr_rx;
  197. void (*interrupt)(emu10k1x_t *emu, unsigned int status);
  198. } emu10k1x_midi_t;
  199. // definition of the chip-specific record
  200. struct snd_emu10k1x {
  201. snd_card_t *card;
  202. struct pci_dev *pci;
  203. unsigned long port;
  204. struct resource *res_port;
  205. int irq;
  206. unsigned int revision; /* chip revision */
  207. unsigned int serial; /* serial number */
  208. unsigned short model; /* subsystem id */
  209. spinlock_t emu_lock;
  210. spinlock_t voice_lock;
  211. ac97_t *ac97;
  212. snd_pcm_t *pcm;
  213. emu10k1x_voice_t voices[3];
  214. emu10k1x_voice_t capture_voice;
  215. u32 spdif_bits[3]; // SPDIF out setup
  216. struct snd_dma_buffer dma_buffer;
  217. emu10k1x_midi_t midi;
  218. };
  219. /* hardware definition */
  220. static snd_pcm_hardware_t snd_emu10k1x_playback_hw = {
  221. .info = (SNDRV_PCM_INFO_MMAP |
  222. SNDRV_PCM_INFO_INTERLEAVED |
  223. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  224. SNDRV_PCM_INFO_MMAP_VALID),
  225. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  226. .rates = SNDRV_PCM_RATE_48000,
  227. .rate_min = 48000,
  228. .rate_max = 48000,
  229. .channels_min = 2,
  230. .channels_max = 2,
  231. .buffer_bytes_max = (32*1024),
  232. .period_bytes_min = 64,
  233. .period_bytes_max = (16*1024),
  234. .periods_min = 2,
  235. .periods_max = 8,
  236. .fifo_size = 0,
  237. };
  238. static snd_pcm_hardware_t snd_emu10k1x_capture_hw = {
  239. .info = (SNDRV_PCM_INFO_MMAP |
  240. SNDRV_PCM_INFO_INTERLEAVED |
  241. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  242. SNDRV_PCM_INFO_MMAP_VALID),
  243. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  244. .rates = SNDRV_PCM_RATE_48000,
  245. .rate_min = 48000,
  246. .rate_max = 48000,
  247. .channels_min = 2,
  248. .channels_max = 2,
  249. .buffer_bytes_max = (32*1024),
  250. .period_bytes_min = 64,
  251. .period_bytes_max = (16*1024),
  252. .periods_min = 2,
  253. .periods_max = 2,
  254. .fifo_size = 0,
  255. };
  256. static unsigned int snd_emu10k1x_ptr_read(emu10k1x_t * emu,
  257. unsigned int reg,
  258. unsigned int chn)
  259. {
  260. unsigned long flags;
  261. unsigned int regptr, val;
  262. regptr = (reg << 16) | chn;
  263. spin_lock_irqsave(&emu->emu_lock, flags);
  264. outl(regptr, emu->port + PTR);
  265. val = inl(emu->port + DATA);
  266. spin_unlock_irqrestore(&emu->emu_lock, flags);
  267. return val;
  268. }
  269. static void snd_emu10k1x_ptr_write(emu10k1x_t *emu,
  270. unsigned int reg,
  271. unsigned int chn,
  272. unsigned int data)
  273. {
  274. unsigned int regptr;
  275. unsigned long flags;
  276. regptr = (reg << 16) | chn;
  277. spin_lock_irqsave(&emu->emu_lock, flags);
  278. outl(regptr, emu->port + PTR);
  279. outl(data, emu->port + DATA);
  280. spin_unlock_irqrestore(&emu->emu_lock, flags);
  281. }
  282. static void snd_emu10k1x_intr_enable(emu10k1x_t *emu, unsigned int intrenb)
  283. {
  284. unsigned long flags;
  285. unsigned int enable;
  286. spin_lock_irqsave(&emu->emu_lock, flags);
  287. enable = inl(emu->port + INTE) | intrenb;
  288. outl(enable, emu->port + INTE);
  289. spin_unlock_irqrestore(&emu->emu_lock, flags);
  290. }
  291. static void snd_emu10k1x_intr_disable(emu10k1x_t *emu, unsigned int intrenb)
  292. {
  293. unsigned long flags;
  294. unsigned int enable;
  295. spin_lock_irqsave(&emu->emu_lock, flags);
  296. enable = inl(emu->port + INTE) & ~intrenb;
  297. outl(enable, emu->port + INTE);
  298. spin_unlock_irqrestore(&emu->emu_lock, flags);
  299. }
  300. static void snd_emu10k1x_gpio_write(emu10k1x_t *emu, unsigned int value)
  301. {
  302. unsigned long flags;
  303. spin_lock_irqsave(&emu->emu_lock, flags);
  304. outl(value, emu->port + GPIO);
  305. spin_unlock_irqrestore(&emu->emu_lock, flags);
  306. }
  307. static void snd_emu10k1x_pcm_free_substream(snd_pcm_runtime_t *runtime)
  308. {
  309. kfree(runtime->private_data);
  310. }
  311. static void snd_emu10k1x_pcm_interrupt(emu10k1x_t *emu, emu10k1x_voice_t *voice)
  312. {
  313. emu10k1x_pcm_t *epcm;
  314. if ((epcm = voice->epcm) == NULL)
  315. return;
  316. if (epcm->substream == NULL)
  317. return;
  318. #if 0
  319. snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
  320. epcm->substream->ops->pointer(epcm->substream),
  321. snd_pcm_lib_period_bytes(epcm->substream),
  322. snd_pcm_lib_buffer_bytes(epcm->substream));
  323. #endif
  324. snd_pcm_period_elapsed(epcm->substream);
  325. }
  326. /* open callback */
  327. static int snd_emu10k1x_playback_open(snd_pcm_substream_t *substream)
  328. {
  329. emu10k1x_t *chip = snd_pcm_substream_chip(substream);
  330. emu10k1x_pcm_t *epcm;
  331. snd_pcm_runtime_t *runtime = substream->runtime;
  332. int err;
  333. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
  334. return err;
  335. }
  336. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  337. return err;
  338. epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
  339. if (epcm == NULL)
  340. return -ENOMEM;
  341. epcm->emu = chip;
  342. epcm->substream = substream;
  343. runtime->private_data = epcm;
  344. runtime->private_free = snd_emu10k1x_pcm_free_substream;
  345. runtime->hw = snd_emu10k1x_playback_hw;
  346. return 0;
  347. }
  348. /* close callback */
  349. static int snd_emu10k1x_playback_close(snd_pcm_substream_t *substream)
  350. {
  351. return 0;
  352. }
  353. /* hw_params callback */
  354. static int snd_emu10k1x_pcm_hw_params(snd_pcm_substream_t *substream,
  355. snd_pcm_hw_params_t * hw_params)
  356. {
  357. snd_pcm_runtime_t *runtime = substream->runtime;
  358. emu10k1x_pcm_t *epcm = runtime->private_data;
  359. if (! epcm->voice) {
  360. epcm->voice = &epcm->emu->voices[substream->pcm->device];
  361. epcm->voice->use = 1;
  362. epcm->voice->epcm = epcm;
  363. }
  364. return snd_pcm_lib_malloc_pages(substream,
  365. params_buffer_bytes(hw_params));
  366. }
  367. /* hw_free callback */
  368. static int snd_emu10k1x_pcm_hw_free(snd_pcm_substream_t *substream)
  369. {
  370. snd_pcm_runtime_t *runtime = substream->runtime;
  371. emu10k1x_pcm_t *epcm;
  372. if (runtime->private_data == NULL)
  373. return 0;
  374. epcm = runtime->private_data;
  375. if (epcm->voice) {
  376. epcm->voice->use = 0;
  377. epcm->voice->epcm = NULL;
  378. epcm->voice = NULL;
  379. }
  380. return snd_pcm_lib_free_pages(substream);
  381. }
  382. /* prepare callback */
  383. static int snd_emu10k1x_pcm_prepare(snd_pcm_substream_t *substream)
  384. {
  385. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  386. snd_pcm_runtime_t *runtime = substream->runtime;
  387. emu10k1x_pcm_t *epcm = runtime->private_data;
  388. int voice = epcm->voice->number;
  389. u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
  390. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  391. int i;
  392. for(i=0; i < runtime->periods; i++) {
  393. *table_base++=runtime->dma_addr+(i*period_size_bytes);
  394. *table_base++=period_size_bytes<<16;
  395. }
  396. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
  397. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
  398. snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
  399. snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
  400. snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
  401. snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
  402. snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
  403. snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
  404. return 0;
  405. }
  406. /* trigger callback */
  407. static int snd_emu10k1x_pcm_trigger(snd_pcm_substream_t *substream,
  408. int cmd)
  409. {
  410. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  411. snd_pcm_runtime_t *runtime = substream->runtime;
  412. emu10k1x_pcm_t *epcm = runtime->private_data;
  413. int channel = epcm->voice->number;
  414. int result = 0;
  415. // snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
  416. switch (cmd) {
  417. case SNDRV_PCM_TRIGGER_START:
  418. if(runtime->periods == 2)
  419. snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
  420. else
  421. snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
  422. epcm->running = 1;
  423. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
  424. break;
  425. case SNDRV_PCM_TRIGGER_STOP:
  426. epcm->running = 0;
  427. snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
  428. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
  429. break;
  430. default:
  431. result = -EINVAL;
  432. break;
  433. }
  434. return result;
  435. }
  436. /* pointer callback */
  437. static snd_pcm_uframes_t
  438. snd_emu10k1x_pcm_pointer(snd_pcm_substream_t *substream)
  439. {
  440. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  441. snd_pcm_runtime_t *runtime = substream->runtime;
  442. emu10k1x_pcm_t *epcm = runtime->private_data;
  443. int channel = epcm->voice->number;
  444. snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
  445. if (!epcm->running)
  446. return 0;
  447. ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  448. ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
  449. ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  450. if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
  451. return 0;
  452. if (ptr3 != ptr4)
  453. ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
  454. ptr2 = bytes_to_frames(runtime, ptr1);
  455. ptr2 += (ptr4 >> 3) * runtime->period_size;
  456. ptr = ptr2;
  457. if (ptr >= runtime->buffer_size)
  458. ptr -= runtime->buffer_size;
  459. return ptr;
  460. }
  461. /* operators */
  462. static snd_pcm_ops_t snd_emu10k1x_playback_ops = {
  463. .open = snd_emu10k1x_playback_open,
  464. .close = snd_emu10k1x_playback_close,
  465. .ioctl = snd_pcm_lib_ioctl,
  466. .hw_params = snd_emu10k1x_pcm_hw_params,
  467. .hw_free = snd_emu10k1x_pcm_hw_free,
  468. .prepare = snd_emu10k1x_pcm_prepare,
  469. .trigger = snd_emu10k1x_pcm_trigger,
  470. .pointer = snd_emu10k1x_pcm_pointer,
  471. };
  472. /* open_capture callback */
  473. static int snd_emu10k1x_pcm_open_capture(snd_pcm_substream_t *substream)
  474. {
  475. emu10k1x_t *chip = snd_pcm_substream_chip(substream);
  476. emu10k1x_pcm_t *epcm;
  477. snd_pcm_runtime_t *runtime = substream->runtime;
  478. int err;
  479. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  480. return err;
  481. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  482. return err;
  483. epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
  484. if (epcm == NULL)
  485. return -ENOMEM;
  486. epcm->emu = chip;
  487. epcm->substream = substream;
  488. runtime->private_data = epcm;
  489. runtime->private_free = snd_emu10k1x_pcm_free_substream;
  490. runtime->hw = snd_emu10k1x_capture_hw;
  491. return 0;
  492. }
  493. /* close callback */
  494. static int snd_emu10k1x_pcm_close_capture(snd_pcm_substream_t *substream)
  495. {
  496. return 0;
  497. }
  498. /* hw_params callback */
  499. static int snd_emu10k1x_pcm_hw_params_capture(snd_pcm_substream_t *substream,
  500. snd_pcm_hw_params_t * hw_params)
  501. {
  502. snd_pcm_runtime_t *runtime = substream->runtime;
  503. emu10k1x_pcm_t *epcm = runtime->private_data;
  504. if (! epcm->voice) {
  505. if (epcm->emu->capture_voice.use)
  506. return -EBUSY;
  507. epcm->voice = &epcm->emu->capture_voice;
  508. epcm->voice->epcm = epcm;
  509. epcm->voice->use = 1;
  510. }
  511. return snd_pcm_lib_malloc_pages(substream,
  512. params_buffer_bytes(hw_params));
  513. }
  514. /* hw_free callback */
  515. static int snd_emu10k1x_pcm_hw_free_capture(snd_pcm_substream_t *substream)
  516. {
  517. snd_pcm_runtime_t *runtime = substream->runtime;
  518. emu10k1x_pcm_t *epcm;
  519. if (runtime->private_data == NULL)
  520. return 0;
  521. epcm = runtime->private_data;
  522. if (epcm->voice) {
  523. epcm->voice->use = 0;
  524. epcm->voice->epcm = NULL;
  525. epcm->voice = NULL;
  526. }
  527. return snd_pcm_lib_free_pages(substream);
  528. }
  529. /* prepare capture callback */
  530. static int snd_emu10k1x_pcm_prepare_capture(snd_pcm_substream_t *substream)
  531. {
  532. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  533. snd_pcm_runtime_t *runtime = substream->runtime;
  534. snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
  535. snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  536. snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
  537. snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
  538. return 0;
  539. }
  540. /* trigger_capture callback */
  541. static int snd_emu10k1x_pcm_trigger_capture(snd_pcm_substream_t *substream,
  542. int cmd)
  543. {
  544. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  545. snd_pcm_runtime_t *runtime = substream->runtime;
  546. emu10k1x_pcm_t *epcm = runtime->private_data;
  547. int result = 0;
  548. switch (cmd) {
  549. case SNDRV_PCM_TRIGGER_START:
  550. snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
  551. INTE_CAP_0_HALF_LOOP);
  552. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
  553. epcm->running = 1;
  554. break;
  555. case SNDRV_PCM_TRIGGER_STOP:
  556. epcm->running = 0;
  557. snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
  558. INTE_CAP_0_HALF_LOOP);
  559. snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
  560. break;
  561. default:
  562. result = -EINVAL;
  563. break;
  564. }
  565. return result;
  566. }
  567. /* pointer_capture callback */
  568. static snd_pcm_uframes_t
  569. snd_emu10k1x_pcm_pointer_capture(snd_pcm_substream_t *substream)
  570. {
  571. emu10k1x_t *emu = snd_pcm_substream_chip(substream);
  572. snd_pcm_runtime_t *runtime = substream->runtime;
  573. emu10k1x_pcm_t *epcm = runtime->private_data;
  574. snd_pcm_uframes_t ptr;
  575. if (!epcm->running)
  576. return 0;
  577. ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
  578. if (ptr >= runtime->buffer_size)
  579. ptr -= runtime->buffer_size;
  580. return ptr;
  581. }
  582. static snd_pcm_ops_t snd_emu10k1x_capture_ops = {
  583. .open = snd_emu10k1x_pcm_open_capture,
  584. .close = snd_emu10k1x_pcm_close_capture,
  585. .ioctl = snd_pcm_lib_ioctl,
  586. .hw_params = snd_emu10k1x_pcm_hw_params_capture,
  587. .hw_free = snd_emu10k1x_pcm_hw_free_capture,
  588. .prepare = snd_emu10k1x_pcm_prepare_capture,
  589. .trigger = snd_emu10k1x_pcm_trigger_capture,
  590. .pointer = snd_emu10k1x_pcm_pointer_capture,
  591. };
  592. static unsigned short snd_emu10k1x_ac97_read(ac97_t *ac97,
  593. unsigned short reg)
  594. {
  595. emu10k1x_t *emu = ac97->private_data;
  596. unsigned long flags;
  597. unsigned short val;
  598. spin_lock_irqsave(&emu->emu_lock, flags);
  599. outb(reg, emu->port + AC97ADDRESS);
  600. val = inw(emu->port + AC97DATA);
  601. spin_unlock_irqrestore(&emu->emu_lock, flags);
  602. return val;
  603. }
  604. static void snd_emu10k1x_ac97_write(ac97_t *ac97,
  605. unsigned short reg, unsigned short val)
  606. {
  607. emu10k1x_t *emu = ac97->private_data;
  608. unsigned long flags;
  609. spin_lock_irqsave(&emu->emu_lock, flags);
  610. outb(reg, emu->port + AC97ADDRESS);
  611. outw(val, emu->port + AC97DATA);
  612. spin_unlock_irqrestore(&emu->emu_lock, flags);
  613. }
  614. static int snd_emu10k1x_ac97(emu10k1x_t *chip)
  615. {
  616. ac97_bus_t *pbus;
  617. ac97_template_t ac97;
  618. int err;
  619. static ac97_bus_ops_t ops = {
  620. .write = snd_emu10k1x_ac97_write,
  621. .read = snd_emu10k1x_ac97_read,
  622. };
  623. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  624. return err;
  625. pbus->no_vra = 1; /* we don't need VRA */
  626. memset(&ac97, 0, sizeof(ac97));
  627. ac97.private_data = chip;
  628. ac97.scaps = AC97_SCAP_NO_SPDIF;
  629. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  630. }
  631. static int snd_emu10k1x_free(emu10k1x_t *chip)
  632. {
  633. snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
  634. // disable interrupts
  635. outl(0, chip->port + INTE);
  636. // disable audio
  637. outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
  638. // release the i/o port
  639. if (chip->res_port) {
  640. release_resource(chip->res_port);
  641. kfree_nocheck(chip->res_port);
  642. }
  643. // release the irq
  644. if (chip->irq >= 0)
  645. free_irq(chip->irq, (void *)chip);
  646. // release the DMA
  647. if (chip->dma_buffer.area) {
  648. snd_dma_free_pages(&chip->dma_buffer);
  649. }
  650. pci_disable_device(chip->pci);
  651. // release the data
  652. kfree(chip);
  653. return 0;
  654. }
  655. static int snd_emu10k1x_dev_free(snd_device_t *device)
  656. {
  657. emu10k1x_t *chip = device->device_data;
  658. return snd_emu10k1x_free(chip);
  659. }
  660. static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
  661. struct pt_regs *regs)
  662. {
  663. unsigned int status;
  664. emu10k1x_t *chip = dev_id;
  665. emu10k1x_voice_t *pvoice = chip->voices;
  666. int i;
  667. int mask;
  668. status = inl(chip->port + IPR);
  669. if(status) {
  670. // capture interrupt
  671. if(status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
  672. emu10k1x_voice_t *pvoice = &chip->capture_voice;
  673. if(pvoice->use)
  674. snd_emu10k1x_pcm_interrupt(chip, pvoice);
  675. else
  676. snd_emu10k1x_intr_disable(chip,
  677. INTE_CAP_0_LOOP |
  678. INTE_CAP_0_HALF_LOOP);
  679. }
  680. mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
  681. for(i = 0; i < 3; i++) {
  682. if(status & mask) {
  683. if(pvoice->use)
  684. snd_emu10k1x_pcm_interrupt(chip, pvoice);
  685. else
  686. snd_emu10k1x_intr_disable(chip, mask);
  687. }
  688. pvoice++;
  689. mask <<= 1;
  690. }
  691. if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
  692. if (chip->midi.interrupt)
  693. chip->midi.interrupt(chip, status);
  694. else
  695. snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
  696. }
  697. // acknowledge the interrupt if necessary
  698. if(status)
  699. outl(status, chip->port+IPR);
  700. // snd_printk(KERN_INFO "interrupt %08x\n", status);
  701. }
  702. return IRQ_HANDLED;
  703. }
  704. static void snd_emu10k1x_pcm_free(snd_pcm_t *pcm)
  705. {
  706. emu10k1x_t *emu = pcm->private_data;
  707. emu->pcm = NULL;
  708. snd_pcm_lib_preallocate_free_for_all(pcm);
  709. }
  710. static int __devinit snd_emu10k1x_pcm(emu10k1x_t *emu, int device, snd_pcm_t **rpcm)
  711. {
  712. snd_pcm_t *pcm;
  713. int err;
  714. int capture = 0;
  715. if (rpcm)
  716. *rpcm = NULL;
  717. if (device == 0)
  718. capture = 1;
  719. if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
  720. return err;
  721. pcm->private_data = emu;
  722. pcm->private_free = snd_emu10k1x_pcm_free;
  723. switch(device) {
  724. case 0:
  725. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  726. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
  727. break;
  728. case 1:
  729. case 2:
  730. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  731. break;
  732. }
  733. pcm->info_flags = 0;
  734. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  735. switch(device) {
  736. case 0:
  737. strcpy(pcm->name, "EMU10K1X Front");
  738. break;
  739. case 1:
  740. strcpy(pcm->name, "EMU10K1X Rear");
  741. break;
  742. case 2:
  743. strcpy(pcm->name, "EMU10K1X Center/LFE");
  744. break;
  745. }
  746. emu->pcm = pcm;
  747. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  748. snd_dma_pci_data(emu->pci),
  749. 32*1024, 32*1024);
  750. if (rpcm)
  751. *rpcm = pcm;
  752. return 0;
  753. }
  754. static int __devinit snd_emu10k1x_create(snd_card_t *card,
  755. struct pci_dev *pci,
  756. emu10k1x_t **rchip)
  757. {
  758. emu10k1x_t *chip;
  759. int err;
  760. int ch;
  761. static snd_device_ops_t ops = {
  762. .dev_free = snd_emu10k1x_dev_free,
  763. };
  764. *rchip = NULL;
  765. if ((err = pci_enable_device(pci)) < 0)
  766. return err;
  767. if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
  768. pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
  769. snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
  770. pci_disable_device(pci);
  771. return -ENXIO;
  772. }
  773. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  774. if (chip == NULL) {
  775. pci_disable_device(pci);
  776. return -ENOMEM;
  777. }
  778. chip->card = card;
  779. chip->pci = pci;
  780. chip->irq = -1;
  781. spin_lock_init(&chip->emu_lock);
  782. spin_lock_init(&chip->voice_lock);
  783. chip->port = pci_resource_start(pci, 0);
  784. if ((chip->res_port = request_region(chip->port, 8,
  785. "EMU10K1X")) == NULL) {
  786. snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
  787. snd_emu10k1x_free(chip);
  788. return -EBUSY;
  789. }
  790. if (request_irq(pci->irq, snd_emu10k1x_interrupt,
  791. SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
  792. (void *)chip)) {
  793. snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
  794. snd_emu10k1x_free(chip);
  795. return -EBUSY;
  796. }
  797. chip->irq = pci->irq;
  798. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  799. 4 * 1024, &chip->dma_buffer) < 0) {
  800. snd_emu10k1x_free(chip);
  801. return -ENOMEM;
  802. }
  803. pci_set_master(pci);
  804. /* read revision & serial */
  805. pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
  806. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  807. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  808. snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  809. chip->revision, chip->serial);
  810. outl(0, chip->port + INTE);
  811. for(ch = 0; ch < 3; ch++) {
  812. chip->voices[ch].emu = chip;
  813. chip->voices[ch].number = ch;
  814. }
  815. /*
  816. * Init to 0x02109204 :
  817. * Clock accuracy = 0 (1000ppm)
  818. * Sample Rate = 2 (48kHz)
  819. * Audio Channel = 1 (Left of 2)
  820. * Source Number = 0 (Unspecified)
  821. * Generation Status = 1 (Original for Cat Code 12)
  822. * Cat Code = 12 (Digital Signal Mixer)
  823. * Mode = 0 (Mode 0)
  824. * Emphasis = 0 (None)
  825. * CP = 1 (Copyright unasserted)
  826. * AN = 0 (Audio data)
  827. * P = 0 (Consumer)
  828. */
  829. snd_emu10k1x_ptr_write(chip, SPCS0, 0,
  830. chip->spdif_bits[0] =
  831. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  832. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  833. SPCS_GENERATIONSTATUS | 0x00001200 |
  834. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  835. snd_emu10k1x_ptr_write(chip, SPCS1, 0,
  836. chip->spdif_bits[1] =
  837. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  838. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  839. SPCS_GENERATIONSTATUS | 0x00001200 |
  840. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  841. snd_emu10k1x_ptr_write(chip, SPCS2, 0,
  842. chip->spdif_bits[2] =
  843. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  844. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  845. SPCS_GENERATIONSTATUS | 0x00001200 |
  846. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  847. snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
  848. snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
  849. snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
  850. outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  851. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  852. chip, &ops)) < 0) {
  853. snd_emu10k1x_free(chip);
  854. return err;
  855. }
  856. *rchip = chip;
  857. return 0;
  858. }
  859. static void snd_emu10k1x_proc_reg_read(snd_info_entry_t *entry,
  860. snd_info_buffer_t * buffer)
  861. {
  862. emu10k1x_t *emu = entry->private_data;
  863. unsigned long value,value1,value2;
  864. unsigned long flags;
  865. int i;
  866. snd_iprintf(buffer, "Registers:\n\n");
  867. for(i = 0; i < 0x20; i+=4) {
  868. spin_lock_irqsave(&emu->emu_lock, flags);
  869. value = inl(emu->port + i);
  870. spin_unlock_irqrestore(&emu->emu_lock, flags);
  871. snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
  872. }
  873. snd_iprintf(buffer, "\nRegisters\n\n");
  874. for(i = 0; i <= 0x48; i++) {
  875. value = snd_emu10k1x_ptr_read(emu, i, 0);
  876. if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
  877. value1 = snd_emu10k1x_ptr_read(emu, i, 1);
  878. value2 = snd_emu10k1x_ptr_read(emu, i, 2);
  879. snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
  880. } else {
  881. snd_iprintf(buffer, "%02X: %08lX\n", i, value);
  882. }
  883. }
  884. }
  885. static void snd_emu10k1x_proc_reg_write(snd_info_entry_t *entry,
  886. snd_info_buffer_t *buffer)
  887. {
  888. emu10k1x_t *emu = entry->private_data;
  889. char line[64];
  890. unsigned int reg, channel_id , val;
  891. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  892. if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
  893. continue;
  894. if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
  895. && (channel_id >=0) && (channel_id <= 2) )
  896. snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
  897. }
  898. }
  899. static int __devinit snd_emu10k1x_proc_init(emu10k1x_t * emu)
  900. {
  901. snd_info_entry_t *entry;
  902. if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
  903. snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
  904. entry->c.text.write_size = 64;
  905. entry->c.text.write = snd_emu10k1x_proc_reg_write;
  906. entry->mode |= S_IWUSR;
  907. entry->private_data = emu;
  908. }
  909. return 0;
  910. }
  911. static int snd_emu10k1x_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  912. {
  913. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  914. uinfo->count = 1;
  915. uinfo->value.integer.min = 0;
  916. uinfo->value.integer.max = 1;
  917. return 0;
  918. }
  919. static int snd_emu10k1x_shared_spdif_get(snd_kcontrol_t * kcontrol,
  920. snd_ctl_elem_value_t * ucontrol)
  921. {
  922. emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
  923. ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
  924. return 0;
  925. }
  926. static int snd_emu10k1x_shared_spdif_put(snd_kcontrol_t * kcontrol,
  927. snd_ctl_elem_value_t * ucontrol)
  928. {
  929. emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
  930. unsigned int val;
  931. int change = 0;
  932. val = ucontrol->value.integer.value[0] ;
  933. if (val) {
  934. // enable spdif output
  935. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
  936. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
  937. snd_emu10k1x_gpio_write(emu, 0x1000);
  938. } else {
  939. // disable spdif output
  940. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
  941. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
  942. snd_emu10k1x_gpio_write(emu, 0x1080);
  943. }
  944. return change;
  945. }
  946. static snd_kcontrol_new_t snd_emu10k1x_shared_spdif __devinitdata =
  947. {
  948. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  949. .name = "Analog/Digital Output Jack",
  950. .info = snd_emu10k1x_shared_spdif_info,
  951. .get = snd_emu10k1x_shared_spdif_get,
  952. .put = snd_emu10k1x_shared_spdif_put
  953. };
  954. static int snd_emu10k1x_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  955. {
  956. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  957. uinfo->count = 1;
  958. return 0;
  959. }
  960. static int snd_emu10k1x_spdif_get(snd_kcontrol_t * kcontrol,
  961. snd_ctl_elem_value_t * ucontrol)
  962. {
  963. emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
  964. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  965. ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
  966. ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
  967. ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
  968. ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
  969. return 0;
  970. }
  971. static int snd_emu10k1x_spdif_get_mask(snd_kcontrol_t * kcontrol,
  972. snd_ctl_elem_value_t * ucontrol)
  973. {
  974. ucontrol->value.iec958.status[0] = 0xff;
  975. ucontrol->value.iec958.status[1] = 0xff;
  976. ucontrol->value.iec958.status[2] = 0xff;
  977. ucontrol->value.iec958.status[3] = 0xff;
  978. return 0;
  979. }
  980. static int snd_emu10k1x_spdif_put(snd_kcontrol_t * kcontrol,
  981. snd_ctl_elem_value_t * ucontrol)
  982. {
  983. emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
  984. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  985. int change;
  986. unsigned int val;
  987. val = (ucontrol->value.iec958.status[0] << 0) |
  988. (ucontrol->value.iec958.status[1] << 8) |
  989. (ucontrol->value.iec958.status[2] << 16) |
  990. (ucontrol->value.iec958.status[3] << 24);
  991. change = val != emu->spdif_bits[idx];
  992. if (change) {
  993. snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
  994. emu->spdif_bits[idx] = val;
  995. }
  996. return change;
  997. }
  998. static snd_kcontrol_new_t snd_emu10k1x_spdif_mask_control =
  999. {
  1000. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1001. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1002. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1003. .count = 3,
  1004. .info = snd_emu10k1x_spdif_info,
  1005. .get = snd_emu10k1x_spdif_get_mask
  1006. };
  1007. static snd_kcontrol_new_t snd_emu10k1x_spdif_control =
  1008. {
  1009. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1010. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1011. .count = 3,
  1012. .info = snd_emu10k1x_spdif_info,
  1013. .get = snd_emu10k1x_spdif_get,
  1014. .put = snd_emu10k1x_spdif_put
  1015. };
  1016. static int __devinit snd_emu10k1x_mixer(emu10k1x_t *emu)
  1017. {
  1018. int err;
  1019. snd_kcontrol_t *kctl;
  1020. snd_card_t *card = emu->card;
  1021. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
  1022. return -ENOMEM;
  1023. if ((err = snd_ctl_add(card, kctl)))
  1024. return err;
  1025. if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
  1026. return -ENOMEM;
  1027. if ((err = snd_ctl_add(card, kctl)))
  1028. return err;
  1029. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
  1030. return -ENOMEM;
  1031. if ((err = snd_ctl_add(card, kctl)))
  1032. return err;
  1033. return 0;
  1034. }
  1035. #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
  1036. #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
  1037. static inline unsigned char mpu401_read(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int idx)
  1038. {
  1039. return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
  1040. }
  1041. static inline void mpu401_write(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int data, int idx)
  1042. {
  1043. snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
  1044. }
  1045. #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
  1046. #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
  1047. #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
  1048. #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
  1049. #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
  1050. #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
  1051. #define MPU401_RESET 0xff
  1052. #define MPU401_ENTER_UART 0x3f
  1053. #define MPU401_ACK 0xfe
  1054. static void mpu401_clear_rx(emu10k1x_t *emu, emu10k1x_midi_t *mpu)
  1055. {
  1056. int timeout = 100000;
  1057. for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
  1058. mpu401_read_data(emu, mpu);
  1059. #ifdef CONFIG_SND_DEBUG
  1060. if (timeout <= 0)
  1061. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
  1062. #endif
  1063. }
  1064. /*
  1065. */
  1066. static void do_emu10k1x_midi_interrupt(emu10k1x_t *emu, emu10k1x_midi_t *midi, unsigned int status)
  1067. {
  1068. unsigned char byte;
  1069. if (midi->rmidi == NULL) {
  1070. snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
  1071. return;
  1072. }
  1073. spin_lock(&midi->input_lock);
  1074. if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
  1075. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1076. mpu401_clear_rx(emu, midi);
  1077. } else {
  1078. byte = mpu401_read_data(emu, midi);
  1079. if (midi->substream_input)
  1080. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  1081. }
  1082. }
  1083. spin_unlock(&midi->input_lock);
  1084. spin_lock(&midi->output_lock);
  1085. if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
  1086. if (midi->substream_output &&
  1087. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  1088. mpu401_write_data(emu, midi, byte);
  1089. } else {
  1090. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1091. }
  1092. }
  1093. spin_unlock(&midi->output_lock);
  1094. }
  1095. static void snd_emu10k1x_midi_interrupt(emu10k1x_t *emu, unsigned int status)
  1096. {
  1097. do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
  1098. }
  1099. static void snd_emu10k1x_midi_cmd(emu10k1x_t * emu, emu10k1x_midi_t *midi, unsigned char cmd, int ack)
  1100. {
  1101. unsigned long flags;
  1102. int timeout, ok;
  1103. spin_lock_irqsave(&midi->input_lock, flags);
  1104. mpu401_write_data(emu, midi, 0x00);
  1105. /* mpu401_clear_rx(emu, midi); */
  1106. mpu401_write_cmd(emu, midi, cmd);
  1107. if (ack) {
  1108. ok = 0;
  1109. timeout = 10000;
  1110. while (!ok && timeout-- > 0) {
  1111. if (mpu401_input_avail(emu, midi)) {
  1112. if (mpu401_read_data(emu, midi) == MPU401_ACK)
  1113. ok = 1;
  1114. }
  1115. }
  1116. if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
  1117. ok = 1;
  1118. } else {
  1119. ok = 1;
  1120. }
  1121. spin_unlock_irqrestore(&midi->input_lock, flags);
  1122. if (!ok)
  1123. snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
  1124. cmd, emu->port,
  1125. mpu401_read_stat(emu, midi),
  1126. mpu401_read_data(emu, midi));
  1127. }
  1128. static int snd_emu10k1x_midi_input_open(snd_rawmidi_substream_t * substream)
  1129. {
  1130. emu10k1x_t *emu;
  1131. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1132. unsigned long flags;
  1133. emu = midi->emu;
  1134. snd_assert(emu, return -ENXIO);
  1135. spin_lock_irqsave(&midi->open_lock, flags);
  1136. midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
  1137. midi->substream_input = substream;
  1138. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1139. spin_unlock_irqrestore(&midi->open_lock, flags);
  1140. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
  1141. snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  1142. } else {
  1143. spin_unlock_irqrestore(&midi->open_lock, flags);
  1144. }
  1145. return 0;
  1146. }
  1147. static int snd_emu10k1x_midi_output_open(snd_rawmidi_substream_t * substream)
  1148. {
  1149. emu10k1x_t *emu;
  1150. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1151. unsigned long flags;
  1152. emu = midi->emu;
  1153. snd_assert(emu, return -ENXIO);
  1154. spin_lock_irqsave(&midi->open_lock, flags);
  1155. midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
  1156. midi->substream_output = substream;
  1157. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1158. spin_unlock_irqrestore(&midi->open_lock, flags);
  1159. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
  1160. snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
  1161. } else {
  1162. spin_unlock_irqrestore(&midi->open_lock, flags);
  1163. }
  1164. return 0;
  1165. }
  1166. static int snd_emu10k1x_midi_input_close(snd_rawmidi_substream_t * substream)
  1167. {
  1168. emu10k1x_t *emu;
  1169. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1170. unsigned long flags;
  1171. emu = midi->emu;
  1172. snd_assert(emu, return -ENXIO);
  1173. spin_lock_irqsave(&midi->open_lock, flags);
  1174. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1175. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
  1176. midi->substream_input = NULL;
  1177. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1178. spin_unlock_irqrestore(&midi->open_lock, flags);
  1179. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1180. } else {
  1181. spin_unlock_irqrestore(&midi->open_lock, flags);
  1182. }
  1183. return 0;
  1184. }
  1185. static int snd_emu10k1x_midi_output_close(snd_rawmidi_substream_t * substream)
  1186. {
  1187. emu10k1x_t *emu;
  1188. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1189. unsigned long flags;
  1190. emu = midi->emu;
  1191. snd_assert(emu, return -ENXIO);
  1192. spin_lock_irqsave(&midi->open_lock, flags);
  1193. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1194. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
  1195. midi->substream_output = NULL;
  1196. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1197. spin_unlock_irqrestore(&midi->open_lock, flags);
  1198. snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1199. } else {
  1200. spin_unlock_irqrestore(&midi->open_lock, flags);
  1201. }
  1202. return 0;
  1203. }
  1204. static void snd_emu10k1x_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  1205. {
  1206. emu10k1x_t *emu;
  1207. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1208. emu = midi->emu;
  1209. snd_assert(emu, return);
  1210. if (up)
  1211. snd_emu10k1x_intr_enable(emu, midi->rx_enable);
  1212. else
  1213. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1214. }
  1215. static void snd_emu10k1x_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  1216. {
  1217. emu10k1x_t *emu;
  1218. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
  1219. unsigned long flags;
  1220. emu = midi->emu;
  1221. snd_assert(emu, return);
  1222. if (up) {
  1223. int max = 4;
  1224. unsigned char byte;
  1225. /* try to send some amount of bytes here before interrupts */
  1226. spin_lock_irqsave(&midi->output_lock, flags);
  1227. while (max > 0) {
  1228. if (mpu401_output_ready(emu, midi)) {
  1229. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
  1230. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  1231. /* no more data */
  1232. spin_unlock_irqrestore(&midi->output_lock, flags);
  1233. return;
  1234. }
  1235. mpu401_write_data(emu, midi, byte);
  1236. max--;
  1237. } else {
  1238. break;
  1239. }
  1240. }
  1241. spin_unlock_irqrestore(&midi->output_lock, flags);
  1242. snd_emu10k1x_intr_enable(emu, midi->tx_enable);
  1243. } else {
  1244. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1245. }
  1246. }
  1247. /*
  1248. */
  1249. static snd_rawmidi_ops_t snd_emu10k1x_midi_output =
  1250. {
  1251. .open = snd_emu10k1x_midi_output_open,
  1252. .close = snd_emu10k1x_midi_output_close,
  1253. .trigger = snd_emu10k1x_midi_output_trigger,
  1254. };
  1255. static snd_rawmidi_ops_t snd_emu10k1x_midi_input =
  1256. {
  1257. .open = snd_emu10k1x_midi_input_open,
  1258. .close = snd_emu10k1x_midi_input_close,
  1259. .trigger = snd_emu10k1x_midi_input_trigger,
  1260. };
  1261. static void snd_emu10k1x_midi_free(snd_rawmidi_t *rmidi)
  1262. {
  1263. emu10k1x_midi_t *midi = (emu10k1x_midi_t *)rmidi->private_data;
  1264. midi->interrupt = NULL;
  1265. midi->rmidi = NULL;
  1266. }
  1267. static int __devinit emu10k1x_midi_init(emu10k1x_t *emu, emu10k1x_midi_t *midi, int device, char *name)
  1268. {
  1269. snd_rawmidi_t *rmidi;
  1270. int err;
  1271. if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
  1272. return err;
  1273. midi->emu = emu;
  1274. spin_lock_init(&midi->open_lock);
  1275. spin_lock_init(&midi->input_lock);
  1276. spin_lock_init(&midi->output_lock);
  1277. strcpy(rmidi->name, name);
  1278. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
  1279. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
  1280. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  1281. SNDRV_RAWMIDI_INFO_INPUT |
  1282. SNDRV_RAWMIDI_INFO_DUPLEX;
  1283. rmidi->private_data = midi;
  1284. rmidi->private_free = snd_emu10k1x_midi_free;
  1285. midi->rmidi = rmidi;
  1286. return 0;
  1287. }
  1288. static int __devinit snd_emu10k1x_midi(emu10k1x_t *emu)
  1289. {
  1290. emu10k1x_midi_t *midi = &emu->midi;
  1291. int err;
  1292. if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
  1293. return err;
  1294. midi->tx_enable = INTE_MIDITXENABLE;
  1295. midi->rx_enable = INTE_MIDIRXENABLE;
  1296. midi->port = MUDATA;
  1297. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  1298. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  1299. midi->interrupt = snd_emu10k1x_midi_interrupt;
  1300. return 0;
  1301. }
  1302. static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
  1303. const struct pci_device_id *pci_id)
  1304. {
  1305. static int dev;
  1306. snd_card_t *card;
  1307. emu10k1x_t *chip;
  1308. int err;
  1309. if (dev >= SNDRV_CARDS)
  1310. return -ENODEV;
  1311. if (!enable[dev]) {
  1312. dev++;
  1313. return -ENOENT;
  1314. }
  1315. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1316. if (card == NULL)
  1317. return -ENOMEM;
  1318. if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
  1319. snd_card_free(card);
  1320. return err;
  1321. }
  1322. if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
  1323. snd_card_free(card);
  1324. return err;
  1325. }
  1326. if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
  1327. snd_card_free(card);
  1328. return err;
  1329. }
  1330. if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
  1331. snd_card_free(card);
  1332. return err;
  1333. }
  1334. if ((err = snd_emu10k1x_ac97(chip)) < 0) {
  1335. snd_card_free(card);
  1336. return err;
  1337. }
  1338. if ((err = snd_emu10k1x_mixer(chip)) < 0) {
  1339. snd_card_free(card);
  1340. return err;
  1341. }
  1342. if ((err = snd_emu10k1x_midi(chip)) < 0) {
  1343. snd_card_free(card);
  1344. return err;
  1345. }
  1346. snd_emu10k1x_proc_init(chip);
  1347. strcpy(card->driver, "EMU10K1X");
  1348. strcpy(card->shortname, "Dell Sound Blaster Live!");
  1349. sprintf(card->longname, "%s at 0x%lx irq %i",
  1350. card->shortname, chip->port, chip->irq);
  1351. if ((err = snd_card_register(card)) < 0) {
  1352. snd_card_free(card);
  1353. return err;
  1354. }
  1355. pci_set_drvdata(pci, card);
  1356. dev++;
  1357. return 0;
  1358. }
  1359. static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
  1360. {
  1361. snd_card_free(pci_get_drvdata(pci));
  1362. pci_set_drvdata(pci, NULL);
  1363. }
  1364. // PCI IDs
  1365. static struct pci_device_id snd_emu10k1x_ids[] = {
  1366. { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
  1367. { 0, }
  1368. };
  1369. MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
  1370. // pci_driver definition
  1371. static struct pci_driver driver = {
  1372. .name = "EMU10K1X",
  1373. .id_table = snd_emu10k1x_ids,
  1374. .probe = snd_emu10k1x_probe,
  1375. .remove = __devexit_p(snd_emu10k1x_remove),
  1376. };
  1377. // initialization of the module
  1378. static int __init alsa_card_emu10k1x_init(void)
  1379. {
  1380. int err;
  1381. if ((err = pci_register_driver(&driver)) > 0)
  1382. return err;
  1383. return 0;
  1384. }
  1385. // clean up the module
  1386. static void __exit alsa_card_emu10k1x_exit(void)
  1387. {
  1388. pci_unregister_driver(&driver);
  1389. }
  1390. module_init(alsa_card_emu10k1x_init)
  1391. module_exit(alsa_card_emu10k1x_exit)