emu10k1x.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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 <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pci.h>
  35. #include <linux/dma-mapping.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 char 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 intr_enable;
  283. spin_lock_irqsave(&emu->emu_lock, flags);
  284. intr_enable = inl(emu->port + INTE) | intrenb;
  285. outl(intr_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 intr_enable;
  292. spin_lock_irqsave(&emu->emu_lock, flags);
  293. intr_enable = inl(emu->port + INTE) & ~intrenb;
  294. outl(intr_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 irq */
  636. if (chip->irq >= 0)
  637. free_irq(chip->irq, chip);
  638. // release the i/o port
  639. release_and_free_resource(chip->res_port);
  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. {
  656. unsigned int status;
  657. struct emu10k1x *chip = dev_id;
  658. struct emu10k1x_voice *pvoice = chip->voices;
  659. int i;
  660. int mask;
  661. status = inl(chip->port + IPR);
  662. if (! status)
  663. return IRQ_NONE;
  664. // capture interrupt
  665. if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
  666. struct emu10k1x_voice *cap_voice = &chip->capture_voice;
  667. if (cap_voice->use)
  668. snd_emu10k1x_pcm_interrupt(chip, cap_voice);
  669. else
  670. snd_emu10k1x_intr_disable(chip,
  671. INTE_CAP_0_LOOP |
  672. INTE_CAP_0_HALF_LOOP);
  673. }
  674. mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
  675. for (i = 0; i < 3; i++) {
  676. if (status & mask) {
  677. if (pvoice->use)
  678. snd_emu10k1x_pcm_interrupt(chip, pvoice);
  679. else
  680. snd_emu10k1x_intr_disable(chip, mask);
  681. }
  682. pvoice++;
  683. mask <<= 1;
  684. }
  685. if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
  686. if (chip->midi.interrupt)
  687. chip->midi.interrupt(chip, status);
  688. else
  689. snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
  690. }
  691. // acknowledge the interrupt if necessary
  692. outl(status, chip->port + IPR);
  693. // snd_printk(KERN_INFO "interrupt %08x\n", status);
  694. return IRQ_HANDLED;
  695. }
  696. static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
  697. {
  698. struct snd_pcm *pcm;
  699. int err;
  700. int capture = 0;
  701. if (rpcm)
  702. *rpcm = NULL;
  703. if (device == 0)
  704. capture = 1;
  705. if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
  706. return err;
  707. pcm->private_data = emu;
  708. switch(device) {
  709. case 0:
  710. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  711. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
  712. break;
  713. case 1:
  714. case 2:
  715. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
  716. break;
  717. }
  718. pcm->info_flags = 0;
  719. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  720. switch(device) {
  721. case 0:
  722. strcpy(pcm->name, "EMU10K1X Front");
  723. break;
  724. case 1:
  725. strcpy(pcm->name, "EMU10K1X Rear");
  726. break;
  727. case 2:
  728. strcpy(pcm->name, "EMU10K1X Center/LFE");
  729. break;
  730. }
  731. emu->pcm = pcm;
  732. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  733. snd_dma_pci_data(emu->pci),
  734. 32*1024, 32*1024);
  735. if (rpcm)
  736. *rpcm = pcm;
  737. return 0;
  738. }
  739. static int __devinit snd_emu10k1x_create(struct snd_card *card,
  740. struct pci_dev *pci,
  741. struct emu10k1x **rchip)
  742. {
  743. struct emu10k1x *chip;
  744. int err;
  745. int ch;
  746. static struct snd_device_ops ops = {
  747. .dev_free = snd_emu10k1x_dev_free,
  748. };
  749. *rchip = NULL;
  750. if ((err = pci_enable_device(pci)) < 0)
  751. return err;
  752. if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
  753. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
  754. snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
  755. pci_disable_device(pci);
  756. return -ENXIO;
  757. }
  758. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  759. if (chip == NULL) {
  760. pci_disable_device(pci);
  761. return -ENOMEM;
  762. }
  763. chip->card = card;
  764. chip->pci = pci;
  765. chip->irq = -1;
  766. spin_lock_init(&chip->emu_lock);
  767. spin_lock_init(&chip->voice_lock);
  768. chip->port = pci_resource_start(pci, 0);
  769. if ((chip->res_port = request_region(chip->port, 8,
  770. "EMU10K1X")) == NULL) {
  771. snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
  772. snd_emu10k1x_free(chip);
  773. return -EBUSY;
  774. }
  775. if (request_irq(pci->irq, snd_emu10k1x_interrupt,
  776. IRQF_SHARED, "EMU10K1X", chip)) {
  777. snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
  778. snd_emu10k1x_free(chip);
  779. return -EBUSY;
  780. }
  781. chip->irq = pci->irq;
  782. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  783. 4 * 1024, &chip->dma_buffer) < 0) {
  784. snd_emu10k1x_free(chip);
  785. return -ENOMEM;
  786. }
  787. pci_set_master(pci);
  788. /* read revision & serial */
  789. chip->revision = pci->revision;
  790. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  791. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  792. snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  793. chip->revision, chip->serial);
  794. outl(0, chip->port + INTE);
  795. for(ch = 0; ch < 3; ch++) {
  796. chip->voices[ch].emu = chip;
  797. chip->voices[ch].number = ch;
  798. }
  799. /*
  800. * Init to 0x02109204 :
  801. * Clock accuracy = 0 (1000ppm)
  802. * Sample Rate = 2 (48kHz)
  803. * Audio Channel = 1 (Left of 2)
  804. * Source Number = 0 (Unspecified)
  805. * Generation Status = 1 (Original for Cat Code 12)
  806. * Cat Code = 12 (Digital Signal Mixer)
  807. * Mode = 0 (Mode 0)
  808. * Emphasis = 0 (None)
  809. * CP = 1 (Copyright unasserted)
  810. * AN = 0 (Audio data)
  811. * P = 0 (Consumer)
  812. */
  813. snd_emu10k1x_ptr_write(chip, SPCS0, 0,
  814. chip->spdif_bits[0] =
  815. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  816. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  817. SPCS_GENERATIONSTATUS | 0x00001200 |
  818. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  819. snd_emu10k1x_ptr_write(chip, SPCS1, 0,
  820. chip->spdif_bits[1] =
  821. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  822. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  823. SPCS_GENERATIONSTATUS | 0x00001200 |
  824. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  825. snd_emu10k1x_ptr_write(chip, SPCS2, 0,
  826. chip->spdif_bits[2] =
  827. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  828. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  829. SPCS_GENERATIONSTATUS | 0x00001200 |
  830. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  831. snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
  832. snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
  833. snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
  834. outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  835. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  836. chip, &ops)) < 0) {
  837. snd_emu10k1x_free(chip);
  838. return err;
  839. }
  840. *rchip = chip;
  841. return 0;
  842. }
  843. static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
  844. struct snd_info_buffer *buffer)
  845. {
  846. struct emu10k1x *emu = entry->private_data;
  847. unsigned long value,value1,value2;
  848. unsigned long flags;
  849. int i;
  850. snd_iprintf(buffer, "Registers:\n\n");
  851. for(i = 0; i < 0x20; i+=4) {
  852. spin_lock_irqsave(&emu->emu_lock, flags);
  853. value = inl(emu->port + i);
  854. spin_unlock_irqrestore(&emu->emu_lock, flags);
  855. snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
  856. }
  857. snd_iprintf(buffer, "\nRegisters\n\n");
  858. for(i = 0; i <= 0x48; i++) {
  859. value = snd_emu10k1x_ptr_read(emu, i, 0);
  860. if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
  861. value1 = snd_emu10k1x_ptr_read(emu, i, 1);
  862. value2 = snd_emu10k1x_ptr_read(emu, i, 2);
  863. snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
  864. } else {
  865. snd_iprintf(buffer, "%02X: %08lX\n", i, value);
  866. }
  867. }
  868. }
  869. static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
  870. struct snd_info_buffer *buffer)
  871. {
  872. struct emu10k1x *emu = entry->private_data;
  873. char line[64];
  874. unsigned int reg, channel_id , val;
  875. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  876. if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
  877. continue;
  878. if ((reg < 0x49) && (reg >= 0) && (val <= 0xffffffff)
  879. && (channel_id >= 0) && (channel_id <= 2) )
  880. snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
  881. }
  882. }
  883. static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
  884. {
  885. struct snd_info_entry *entry;
  886. if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
  887. snd_info_set_text_ops(entry, emu, snd_emu10k1x_proc_reg_read);
  888. entry->c.text.write = snd_emu10k1x_proc_reg_write;
  889. entry->mode |= S_IWUSR;
  890. entry->private_data = emu;
  891. }
  892. return 0;
  893. }
  894. #define snd_emu10k1x_shared_spdif_info snd_ctl_boolean_mono_info
  895. static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
  896. struct snd_ctl_elem_value *ucontrol)
  897. {
  898. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  899. ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
  900. return 0;
  901. }
  902. static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
  903. struct snd_ctl_elem_value *ucontrol)
  904. {
  905. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  906. unsigned int val;
  907. int change = 0;
  908. val = ucontrol->value.integer.value[0] ;
  909. if (val) {
  910. // enable spdif output
  911. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
  912. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
  913. snd_emu10k1x_gpio_write(emu, 0x1000);
  914. } else {
  915. // disable spdif output
  916. snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
  917. snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
  918. snd_emu10k1x_gpio_write(emu, 0x1080);
  919. }
  920. return change;
  921. }
  922. static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
  923. {
  924. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  925. .name = "Analog/Digital Output Jack",
  926. .info = snd_emu10k1x_shared_spdif_info,
  927. .get = snd_emu10k1x_shared_spdif_get,
  928. .put = snd_emu10k1x_shared_spdif_put
  929. };
  930. static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  931. {
  932. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  933. uinfo->count = 1;
  934. return 0;
  935. }
  936. static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
  937. struct snd_ctl_elem_value *ucontrol)
  938. {
  939. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  940. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  941. ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
  942. ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
  943. ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
  944. ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
  945. return 0;
  946. }
  947. static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
  948. struct snd_ctl_elem_value *ucontrol)
  949. {
  950. ucontrol->value.iec958.status[0] = 0xff;
  951. ucontrol->value.iec958.status[1] = 0xff;
  952. ucontrol->value.iec958.status[2] = 0xff;
  953. ucontrol->value.iec958.status[3] = 0xff;
  954. return 0;
  955. }
  956. static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
  957. struct snd_ctl_elem_value *ucontrol)
  958. {
  959. struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
  960. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  961. int change;
  962. unsigned int val;
  963. val = (ucontrol->value.iec958.status[0] << 0) |
  964. (ucontrol->value.iec958.status[1] << 8) |
  965. (ucontrol->value.iec958.status[2] << 16) |
  966. (ucontrol->value.iec958.status[3] << 24);
  967. change = val != emu->spdif_bits[idx];
  968. if (change) {
  969. snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
  970. emu->spdif_bits[idx] = val;
  971. }
  972. return change;
  973. }
  974. static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
  975. {
  976. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  977. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  978. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  979. .count = 3,
  980. .info = snd_emu10k1x_spdif_info,
  981. .get = snd_emu10k1x_spdif_get_mask
  982. };
  983. static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
  984. {
  985. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  986. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  987. .count = 3,
  988. .info = snd_emu10k1x_spdif_info,
  989. .get = snd_emu10k1x_spdif_get,
  990. .put = snd_emu10k1x_spdif_put
  991. };
  992. static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
  993. {
  994. int err;
  995. struct snd_kcontrol *kctl;
  996. struct snd_card *card = emu->card;
  997. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
  998. return -ENOMEM;
  999. if ((err = snd_ctl_add(card, kctl)))
  1000. return err;
  1001. if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
  1002. return -ENOMEM;
  1003. if ((err = snd_ctl_add(card, kctl)))
  1004. return err;
  1005. if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
  1006. return -ENOMEM;
  1007. if ((err = snd_ctl_add(card, kctl)))
  1008. return err;
  1009. return 0;
  1010. }
  1011. #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
  1012. #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
  1013. static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
  1014. {
  1015. return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
  1016. }
  1017. static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
  1018. {
  1019. snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
  1020. }
  1021. #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
  1022. #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
  1023. #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
  1024. #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
  1025. #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
  1026. #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
  1027. #define MPU401_RESET 0xff
  1028. #define MPU401_ENTER_UART 0x3f
  1029. #define MPU401_ACK 0xfe
  1030. static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
  1031. {
  1032. int timeout = 100000;
  1033. for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
  1034. mpu401_read_data(emu, mpu);
  1035. #ifdef CONFIG_SND_DEBUG
  1036. if (timeout <= 0)
  1037. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
  1038. #endif
  1039. }
  1040. /*
  1041. */
  1042. static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
  1043. struct emu10k1x_midi *midi, unsigned int status)
  1044. {
  1045. unsigned char byte;
  1046. if (midi->rmidi == NULL) {
  1047. snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
  1048. return;
  1049. }
  1050. spin_lock(&midi->input_lock);
  1051. if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
  1052. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1053. mpu401_clear_rx(emu, midi);
  1054. } else {
  1055. byte = mpu401_read_data(emu, midi);
  1056. if (midi->substream_input)
  1057. snd_rawmidi_receive(midi->substream_input, &byte, 1);
  1058. }
  1059. }
  1060. spin_unlock(&midi->input_lock);
  1061. spin_lock(&midi->output_lock);
  1062. if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
  1063. if (midi->substream_output &&
  1064. snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
  1065. mpu401_write_data(emu, midi, byte);
  1066. } else {
  1067. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1068. }
  1069. }
  1070. spin_unlock(&midi->output_lock);
  1071. }
  1072. static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
  1073. {
  1074. do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
  1075. }
  1076. static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
  1077. struct emu10k1x_midi *midi, unsigned char cmd, int ack)
  1078. {
  1079. unsigned long flags;
  1080. int timeout, ok;
  1081. spin_lock_irqsave(&midi->input_lock, flags);
  1082. mpu401_write_data(emu, midi, 0x00);
  1083. /* mpu401_clear_rx(emu, midi); */
  1084. mpu401_write_cmd(emu, midi, cmd);
  1085. if (ack) {
  1086. ok = 0;
  1087. timeout = 10000;
  1088. while (!ok && timeout-- > 0) {
  1089. if (mpu401_input_avail(emu, midi)) {
  1090. if (mpu401_read_data(emu, midi) == MPU401_ACK)
  1091. ok = 1;
  1092. }
  1093. }
  1094. if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
  1095. ok = 1;
  1096. } else {
  1097. ok = 1;
  1098. }
  1099. spin_unlock_irqrestore(&midi->input_lock, flags);
  1100. if (!ok) {
  1101. snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
  1102. cmd, emu->port,
  1103. mpu401_read_stat(emu, midi),
  1104. mpu401_read_data(emu, midi));
  1105. return 1;
  1106. }
  1107. return 0;
  1108. }
  1109. static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
  1110. {
  1111. struct emu10k1x *emu;
  1112. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1113. unsigned long flags;
  1114. emu = midi->emu;
  1115. if (snd_BUG_ON(!emu))
  1116. return -ENXIO;
  1117. spin_lock_irqsave(&midi->open_lock, flags);
  1118. midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
  1119. midi->substream_input = substream;
  1120. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1121. spin_unlock_irqrestore(&midi->open_lock, flags);
  1122. if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
  1123. goto error_out;
  1124. if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
  1125. goto error_out;
  1126. } else {
  1127. spin_unlock_irqrestore(&midi->open_lock, flags);
  1128. }
  1129. return 0;
  1130. error_out:
  1131. return -EIO;
  1132. }
  1133. static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
  1134. {
  1135. struct emu10k1x *emu;
  1136. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1137. unsigned long flags;
  1138. emu = midi->emu;
  1139. if (snd_BUG_ON(!emu))
  1140. return -ENXIO;
  1141. spin_lock_irqsave(&midi->open_lock, flags);
  1142. midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
  1143. midi->substream_output = substream;
  1144. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1145. spin_unlock_irqrestore(&midi->open_lock, flags);
  1146. if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
  1147. goto error_out;
  1148. if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
  1149. goto error_out;
  1150. } else {
  1151. spin_unlock_irqrestore(&midi->open_lock, flags);
  1152. }
  1153. return 0;
  1154. error_out:
  1155. return -EIO;
  1156. }
  1157. static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
  1158. {
  1159. struct emu10k1x *emu;
  1160. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1161. unsigned long flags;
  1162. int err = 0;
  1163. emu = midi->emu;
  1164. if (snd_BUG_ON(!emu))
  1165. return -ENXIO;
  1166. spin_lock_irqsave(&midi->open_lock, flags);
  1167. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1168. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
  1169. midi->substream_input = NULL;
  1170. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
  1171. spin_unlock_irqrestore(&midi->open_lock, flags);
  1172. err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1173. } else {
  1174. spin_unlock_irqrestore(&midi->open_lock, flags);
  1175. }
  1176. return err;
  1177. }
  1178. static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
  1179. {
  1180. struct emu10k1x *emu;
  1181. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1182. unsigned long flags;
  1183. int err = 0;
  1184. emu = midi->emu;
  1185. if (snd_BUG_ON(!emu))
  1186. return -ENXIO;
  1187. spin_lock_irqsave(&midi->open_lock, flags);
  1188. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1189. midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
  1190. midi->substream_output = NULL;
  1191. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
  1192. spin_unlock_irqrestore(&midi->open_lock, flags);
  1193. err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
  1194. } else {
  1195. spin_unlock_irqrestore(&midi->open_lock, flags);
  1196. }
  1197. return err;
  1198. }
  1199. static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1200. {
  1201. struct emu10k1x *emu;
  1202. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1203. emu = midi->emu;
  1204. if (snd_BUG_ON(!emu))
  1205. return;
  1206. if (up)
  1207. snd_emu10k1x_intr_enable(emu, midi->rx_enable);
  1208. else
  1209. snd_emu10k1x_intr_disable(emu, midi->rx_enable);
  1210. }
  1211. static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  1212. {
  1213. struct emu10k1x *emu;
  1214. struct emu10k1x_midi *midi = substream->rmidi->private_data;
  1215. unsigned long flags;
  1216. emu = midi->emu;
  1217. if (snd_BUG_ON(!emu))
  1218. return;
  1219. if (up) {
  1220. int max = 4;
  1221. unsigned char byte;
  1222. /* try to send some amount of bytes here before interrupts */
  1223. spin_lock_irqsave(&midi->output_lock, flags);
  1224. while (max > 0) {
  1225. if (mpu401_output_ready(emu, midi)) {
  1226. if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
  1227. snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  1228. /* no more data */
  1229. spin_unlock_irqrestore(&midi->output_lock, flags);
  1230. return;
  1231. }
  1232. mpu401_write_data(emu, midi, byte);
  1233. max--;
  1234. } else {
  1235. break;
  1236. }
  1237. }
  1238. spin_unlock_irqrestore(&midi->output_lock, flags);
  1239. snd_emu10k1x_intr_enable(emu, midi->tx_enable);
  1240. } else {
  1241. snd_emu10k1x_intr_disable(emu, midi->tx_enable);
  1242. }
  1243. }
  1244. /*
  1245. */
  1246. static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
  1247. {
  1248. .open = snd_emu10k1x_midi_output_open,
  1249. .close = snd_emu10k1x_midi_output_close,
  1250. .trigger = snd_emu10k1x_midi_output_trigger,
  1251. };
  1252. static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
  1253. {
  1254. .open = snd_emu10k1x_midi_input_open,
  1255. .close = snd_emu10k1x_midi_input_close,
  1256. .trigger = snd_emu10k1x_midi_input_trigger,
  1257. };
  1258. static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
  1259. {
  1260. struct emu10k1x_midi *midi = rmidi->private_data;
  1261. midi->interrupt = NULL;
  1262. midi->rmidi = NULL;
  1263. }
  1264. static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
  1265. struct emu10k1x_midi *midi, int device, char *name)
  1266. {
  1267. struct snd_rawmidi *rmidi;
  1268. int err;
  1269. if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
  1270. return err;
  1271. midi->emu = emu;
  1272. spin_lock_init(&midi->open_lock);
  1273. spin_lock_init(&midi->input_lock);
  1274. spin_lock_init(&midi->output_lock);
  1275. strcpy(rmidi->name, name);
  1276. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
  1277. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
  1278. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  1279. SNDRV_RAWMIDI_INFO_INPUT |
  1280. SNDRV_RAWMIDI_INFO_DUPLEX;
  1281. rmidi->private_data = midi;
  1282. rmidi->private_free = snd_emu10k1x_midi_free;
  1283. midi->rmidi = rmidi;
  1284. return 0;
  1285. }
  1286. static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
  1287. {
  1288. struct emu10k1x_midi *midi = &emu->midi;
  1289. int err;
  1290. if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
  1291. return err;
  1292. midi->tx_enable = INTE_MIDITXENABLE;
  1293. midi->rx_enable = INTE_MIDIRXENABLE;
  1294. midi->port = MUDATA;
  1295. midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
  1296. midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
  1297. midi->interrupt = snd_emu10k1x_midi_interrupt;
  1298. return 0;
  1299. }
  1300. static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
  1301. const struct pci_device_id *pci_id)
  1302. {
  1303. static int dev;
  1304. struct snd_card *card;
  1305. struct emu10k1x *chip;
  1306. int err;
  1307. if (dev >= SNDRV_CARDS)
  1308. return -ENODEV;
  1309. if (!enable[dev]) {
  1310. dev++;
  1311. return -ENOENT;
  1312. }
  1313. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1314. if (err < 0)
  1315. return err;
  1316. if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
  1317. snd_card_free(card);
  1318. return err;
  1319. }
  1320. if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
  1321. snd_card_free(card);
  1322. return err;
  1323. }
  1324. if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
  1325. snd_card_free(card);
  1326. return err;
  1327. }
  1328. if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
  1329. snd_card_free(card);
  1330. return err;
  1331. }
  1332. if ((err = snd_emu10k1x_ac97(chip)) < 0) {
  1333. snd_card_free(card);
  1334. return err;
  1335. }
  1336. if ((err = snd_emu10k1x_mixer(chip)) < 0) {
  1337. snd_card_free(card);
  1338. return err;
  1339. }
  1340. if ((err = snd_emu10k1x_midi(chip)) < 0) {
  1341. snd_card_free(card);
  1342. return err;
  1343. }
  1344. snd_emu10k1x_proc_init(chip);
  1345. strcpy(card->driver, "EMU10K1X");
  1346. strcpy(card->shortname, "Dell Sound Blaster Live!");
  1347. sprintf(card->longname, "%s at 0x%lx irq %i",
  1348. card->shortname, chip->port, chip->irq);
  1349. snd_card_set_dev(card, &pci->dev);
  1350. if ((err = snd_card_register(card)) < 0) {
  1351. snd_card_free(card);
  1352. return err;
  1353. }
  1354. pci_set_drvdata(pci, card);
  1355. dev++;
  1356. return 0;
  1357. }
  1358. static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
  1359. {
  1360. snd_card_free(pci_get_drvdata(pci));
  1361. pci_set_drvdata(pci, NULL);
  1362. }
  1363. // PCI IDs
  1364. static struct pci_device_id snd_emu10k1x_ids[] = {
  1365. { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
  1366. { 0, }
  1367. };
  1368. MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
  1369. // pci_driver definition
  1370. static struct pci_driver driver = {
  1371. .name = "EMU10K1X",
  1372. .id_table = snd_emu10k1x_ids,
  1373. .probe = snd_emu10k1x_probe,
  1374. .remove = __devexit_p(snd_emu10k1x_remove),
  1375. };
  1376. // initialization of the module
  1377. static int __init alsa_card_emu10k1x_init(void)
  1378. {
  1379. return pci_register_driver(&driver);
  1380. }
  1381. // clean up the module
  1382. static void __exit alsa_card_emu10k1x_exit(void)
  1383. {
  1384. pci_unregister_driver(&driver);
  1385. }
  1386. module_init(alsa_card_emu10k1x_init)
  1387. module_exit(alsa_card_emu10k1x_exit)