ca0106_main.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. /*
  2. * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
  3. * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
  4. * Version: 0.0.23
  5. *
  6. * FEATURES currently supported:
  7. * Front, Rear and Center/LFE.
  8. * Surround40 and Surround51.
  9. * Capture from MIC an LINE IN input.
  10. * SPDIF digital playback of PCM stereo and AC3/DTS works.
  11. * (One can use a standard mono mini-jack to one RCA plugs cable.
  12. * or one can use a standard stereo mini-jack to two RCA plugs cable.
  13. * Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
  14. * ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
  15. * Notes on how to capture sound:
  16. * The AC97 is used in the PLAYBACK direction.
  17. * The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
  18. * So, to record from the MIC, set the MIC Playback volume to max,
  19. * unmute the MIC and turn up the MASTER Playback volume.
  20. * So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
  21. *
  22. * The only playback controls that currently do anything are: -
  23. * Analog Front
  24. * Analog Rear
  25. * Analog Center/LFE
  26. * SPDIF Front
  27. * SPDIF Rear
  28. * SPDIF Center/LFE
  29. *
  30. * For capture from Mic in or Line in.
  31. * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
  32. *
  33. * CAPTURE feedback into PLAYBACK
  34. *
  35. * Changelog:
  36. * Support interrupts per period.
  37. * Removed noise from Center/LFE channel when in Analog mode.
  38. * Rename and remove mixer controls.
  39. * 0.0.6
  40. * Use separate card based DMA buffer for periods table list.
  41. * 0.0.7
  42. * Change remove and rename ctrls into lists.
  43. * 0.0.8
  44. * Try to fix capture sources.
  45. * 0.0.9
  46. * Fix AC3 output.
  47. * Enable S32_LE format support.
  48. * 0.0.10
  49. * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
  50. * 0.0.11
  51. * Add Model name recognition.
  52. * 0.0.12
  53. * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
  54. * Remove redundent "voice" handling.
  55. * 0.0.13
  56. * Single trigger call for multi channels.
  57. * 0.0.14
  58. * Set limits based on what the sound card hardware can do.
  59. * playback periods_min=2, periods_max=8
  60. * capture hw constraints require period_size = n * 64 bytes.
  61. * playback hw constraints require period_size = n * 64 bytes.
  62. * 0.0.15
  63. * Minor updates.
  64. * 0.0.16
  65. * Implement 192000 sample rate.
  66. * 0.0.17
  67. * Add support for SB0410 and SB0413.
  68. * 0.0.18
  69. * Modified Copyright message.
  70. * 0.0.19
  71. * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
  72. * The output codec needs resetting, otherwise all output is muted.
  73. * 0.0.20
  74. * Merge "pci_disable_device(pci);" fixes.
  75. * 0.0.21
  76. * Add 4 capture channels. (SPDIF only comes in on channel 0. )
  77. * Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
  78. * 0.0.22
  79. * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
  80. * 0.0.23
  81. * Implement support for Line-in capture on SB Live 24bit.
  82. *
  83. * BUGS:
  84. * Some stability problems when unloading the snd-ca0106 kernel module.
  85. * --
  86. *
  87. * TODO:
  88. * 4 Capture channels, only one implemented so far.
  89. * Other capture rates apart from 48khz not implemented.
  90. * MIDI
  91. * --
  92. * GENERAL INFO:
  93. * Model: SB0310
  94. * P17 Chip: CA0106-DAT
  95. * AC97 Codec: STAC 9721
  96. * ADC: Philips 1361T (Stereo 24bit)
  97. * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
  98. *
  99. * GENERAL INFO:
  100. * Model: SB0410
  101. * P17 Chip: CA0106-DAT
  102. * AC97 Codec: None
  103. * ADC: WM8775EDS (4 Channel)
  104. * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
  105. * SPDIF Out control switches between Mic in and SPDIF out.
  106. * No sound out or mic input working yet.
  107. *
  108. * GENERAL INFO:
  109. * Model: SB0413
  110. * P17 Chip: CA0106-DAT
  111. * AC97 Codec: None.
  112. * ADC: Unknown
  113. * DAC: Unknown
  114. * Trying to handle it like the SB0410.
  115. *
  116. * This code was initally based on code from ALSA's emu10k1x.c which is:
  117. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  118. *
  119. * This program is free software; you can redistribute it and/or modify
  120. * it under the terms of the GNU General Public License as published by
  121. * the Free Software Foundation; either version 2 of the License, or
  122. * (at your option) any later version.
  123. *
  124. * This program is distributed in the hope that it will be useful,
  125. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  126. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  127. * GNU General Public License for more details.
  128. *
  129. * You should have received a copy of the GNU General Public License
  130. * along with this program; if not, write to the Free Software
  131. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  132. *
  133. */
  134. #include <sound/driver.h>
  135. #include <linux/delay.h>
  136. #include <linux/init.h>
  137. #include <linux/interrupt.h>
  138. #include <linux/pci.h>
  139. #include <linux/slab.h>
  140. #include <linux/moduleparam.h>
  141. #include <linux/dma-mapping.h>
  142. #include <sound/core.h>
  143. #include <sound/initval.h>
  144. #include <sound/pcm.h>
  145. #include <sound/ac97_codec.h>
  146. #include <sound/info.h>
  147. MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
  148. MODULE_DESCRIPTION("CA0106");
  149. MODULE_LICENSE("GPL");
  150. MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
  151. // module parameters (see "Module Parameters")
  152. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  153. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  154. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  155. module_param_array(index, int, NULL, 0444);
  156. MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
  157. module_param_array(id, charp, NULL, 0444);
  158. MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
  159. module_param_array(enable, bool, NULL, 0444);
  160. MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
  161. #include "ca0106.h"
  162. static struct snd_ca0106_details ca0106_chip_details[] = {
  163. /* AudigyLS[SB0310] */
  164. { .serial = 0x10021102,
  165. .name = "AudigyLS [SB0310]",
  166. .ac97 = 1 } ,
  167. /* Unknown AudigyLS that also says SB0310 on it */
  168. { .serial = 0x10051102,
  169. .name = "AudigyLS [SB0310b]",
  170. .ac97 = 1 } ,
  171. /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
  172. { .serial = 0x10061102,
  173. .name = "Live! 7.1 24bit [SB0410]",
  174. .gpio_type = 1,
  175. .i2c_adc = 1 } ,
  176. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  177. { .serial = 0x10071102,
  178. .name = "Live! 7.1 24bit [SB0413]",
  179. .gpio_type = 1,
  180. .i2c_adc = 1 } ,
  181. /* New Audigy SE. Has a different DAC. */
  182. /* SB0570:
  183. * CTRL:CA0106-DAT
  184. * ADC: WM8775EDS
  185. * DAC: WM8768GEDS
  186. */
  187. { .serial = 0x100a1102,
  188. .name = "Audigy SE [SB0570]",
  189. .gpio_type = 1,
  190. .i2c_adc = 1,
  191. .spi_dac = 1 } ,
  192. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  193. /* SB0438
  194. * CTRL:CA0106-DAT
  195. * ADC: WM8775SEDS
  196. * DAC: CS4382-KQZ
  197. */
  198. { .serial = 0x10091462,
  199. .name = "MSI K8N Diamond MB [SB0438]",
  200. .gpio_type = 2,
  201. .i2c_adc = 1 } ,
  202. /* Shuttle XPC SD31P which has an onboard Creative Labs
  203. * Sound Blaster Live! 24-bit EAX
  204. * high-definition 7.1 audio processor".
  205. * Added using info from andrewvegan in alsa bug #1298
  206. */
  207. { .serial = 0x30381297,
  208. .name = "Shuttle XPC SD31P [SD31P]",
  209. .gpio_type = 1,
  210. .i2c_adc = 1 } ,
  211. /* Shuttle XPC SD11G5 which has an onboard Creative Labs
  212. * Sound Blaster Live! 24-bit EAX
  213. * high-definition 7.1 audio processor".
  214. * Fixes ALSA bug#1600
  215. */
  216. { .serial = 0x30411297,
  217. .name = "Shuttle XPC SD11G5 [SD11G5]",
  218. .gpio_type = 1,
  219. .i2c_adc = 1 } ,
  220. { .serial = 0,
  221. .name = "AudigyLS [Unknown]" }
  222. };
  223. /* hardware definition */
  224. static struct snd_pcm_hardware snd_ca0106_playback_hw = {
  225. .info = (SNDRV_PCM_INFO_MMAP |
  226. SNDRV_PCM_INFO_INTERLEAVED |
  227. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  228. SNDRV_PCM_INFO_MMAP_VALID),
  229. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  230. .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  231. SNDRV_PCM_RATE_192000),
  232. .rate_min = 48000,
  233. .rate_max = 192000,
  234. .channels_min = 2, //1,
  235. .channels_max = 2, //6,
  236. .buffer_bytes_max = ((65536 - 64) * 8),
  237. .period_bytes_min = 64,
  238. .period_bytes_max = (65536 - 64),
  239. .periods_min = 2,
  240. .periods_max = 8,
  241. .fifo_size = 0,
  242. };
  243. static struct snd_pcm_hardware snd_ca0106_capture_hw = {
  244. .info = (SNDRV_PCM_INFO_MMAP |
  245. SNDRV_PCM_INFO_INTERLEAVED |
  246. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  247. SNDRV_PCM_INFO_MMAP_VALID),
  248. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  249. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  250. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  251. .rate_min = 44100,
  252. .rate_max = 192000,
  253. .channels_min = 2,
  254. .channels_max = 2,
  255. .buffer_bytes_max = ((65536 - 64) * 8),
  256. .period_bytes_min = 64,
  257. .period_bytes_max = (65536 - 64),
  258. .periods_min = 2,
  259. .periods_max = 2,
  260. .fifo_size = 0,
  261. };
  262. unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
  263. unsigned int reg,
  264. unsigned int chn)
  265. {
  266. unsigned long flags;
  267. unsigned int regptr, val;
  268. regptr = (reg << 16) | chn;
  269. spin_lock_irqsave(&emu->emu_lock, flags);
  270. outl(regptr, emu->port + PTR);
  271. val = inl(emu->port + DATA);
  272. spin_unlock_irqrestore(&emu->emu_lock, flags);
  273. return val;
  274. }
  275. void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
  276. unsigned int reg,
  277. unsigned int chn,
  278. unsigned int data)
  279. {
  280. unsigned int regptr;
  281. unsigned long flags;
  282. regptr = (reg << 16) | chn;
  283. spin_lock_irqsave(&emu->emu_lock, flags);
  284. outl(regptr, emu->port + PTR);
  285. outl(data, emu->port + DATA);
  286. spin_unlock_irqrestore(&emu->emu_lock, flags);
  287. }
  288. int snd_ca0106_spi_write(struct snd_ca0106 * emu,
  289. unsigned int data)
  290. {
  291. unsigned int reset, set;
  292. unsigned int reg, tmp;
  293. int n, result;
  294. reg = SPI;
  295. if (data > 0xffff) /* Only 16bit values allowed */
  296. return 1;
  297. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  298. reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
  299. set = reset | 0x10000; /* Set xxx1xxxx */
  300. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  301. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
  302. snd_ca0106_ptr_write(emu, reg, 0, set | data);
  303. result = 1;
  304. /* Wait for status bit to return to 0 */
  305. for (n = 0; n < 100; n++) {
  306. udelay(10);
  307. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  308. if (!(tmp & 0x10000)) {
  309. result = 0;
  310. break;
  311. }
  312. }
  313. if (result) /* Timed out */
  314. return 1;
  315. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  316. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
  317. return 0;
  318. }
  319. /* The ADC does not support i2c read, so only write is implemented */
  320. int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
  321. u32 reg,
  322. u32 value)
  323. {
  324. u32 tmp;
  325. int timeout = 0;
  326. int status;
  327. int retry;
  328. if ((reg > 0x7f) || (value > 0x1ff)) {
  329. snd_printk(KERN_ERR "i2c_write: invalid values.\n");
  330. return -EINVAL;
  331. }
  332. tmp = reg << 25 | value << 16;
  333. // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
  334. /* Not sure what this I2C channel controls. */
  335. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  336. /* This controls the I2C connected to the WM8775 ADC Codec */
  337. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  338. for (retry = 0; retry < 10; retry++) {
  339. /* Send the data to i2c */
  340. //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  341. //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  342. tmp = 0;
  343. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  344. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  345. /* Wait till the transaction ends */
  346. while (1) {
  347. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  348. //snd_printk("I2C:status=0x%x\n", status);
  349. timeout++;
  350. if ((status & I2C_A_ADC_START) == 0)
  351. break;
  352. if (timeout > 1000)
  353. break;
  354. }
  355. //Read back and see if the transaction is successful
  356. if ((status & I2C_A_ADC_ABORT) == 0)
  357. break;
  358. }
  359. if (retry == 10) {
  360. snd_printk(KERN_ERR "Writing to ADC failed!\n");
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
  366. {
  367. unsigned long flags;
  368. unsigned int enable;
  369. spin_lock_irqsave(&emu->emu_lock, flags);
  370. enable = inl(emu->port + INTE) | intrenb;
  371. outl(enable, emu->port + INTE);
  372. spin_unlock_irqrestore(&emu->emu_lock, flags);
  373. }
  374. static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
  375. {
  376. unsigned long flags;
  377. unsigned int enable;
  378. spin_lock_irqsave(&emu->emu_lock, flags);
  379. enable = inl(emu->port + INTE) & ~intrenb;
  380. outl(enable, emu->port + INTE);
  381. spin_unlock_irqrestore(&emu->emu_lock, flags);
  382. }
  383. static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
  384. {
  385. kfree(runtime->private_data);
  386. }
  387. /* open_playback callback */
  388. static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
  389. int channel_id)
  390. {
  391. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  392. struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
  393. struct snd_ca0106_pcm *epcm;
  394. struct snd_pcm_runtime *runtime = substream->runtime;
  395. int err;
  396. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  397. if (epcm == NULL)
  398. return -ENOMEM;
  399. epcm->emu = chip;
  400. epcm->substream = substream;
  401. epcm->channel_id=channel_id;
  402. runtime->private_data = epcm;
  403. runtime->private_free = snd_ca0106_pcm_free_substream;
  404. runtime->hw = snd_ca0106_playback_hw;
  405. channel->emu = chip;
  406. channel->number = channel_id;
  407. channel->use = 1;
  408. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  409. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  410. channel->epcm = epcm;
  411. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  412. return err;
  413. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  414. return err;
  415. return 0;
  416. }
  417. /* close callback */
  418. static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
  419. {
  420. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  421. struct snd_pcm_runtime *runtime = substream->runtime;
  422. struct snd_ca0106_pcm *epcm = runtime->private_data;
  423. chip->playback_channels[epcm->channel_id].use = 0;
  424. /* FIXME: maybe zero others */
  425. return 0;
  426. }
  427. static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
  428. {
  429. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  430. }
  431. static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
  432. {
  433. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  434. }
  435. static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
  436. {
  437. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  438. }
  439. static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
  440. {
  441. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  442. }
  443. /* open_capture callback */
  444. static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
  445. int channel_id)
  446. {
  447. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  448. struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
  449. struct snd_ca0106_pcm *epcm;
  450. struct snd_pcm_runtime *runtime = substream->runtime;
  451. int err;
  452. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  453. if (epcm == NULL) {
  454. snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
  455. return -ENOMEM;
  456. }
  457. epcm->emu = chip;
  458. epcm->substream = substream;
  459. epcm->channel_id=channel_id;
  460. runtime->private_data = epcm;
  461. runtime->private_free = snd_ca0106_pcm_free_substream;
  462. runtime->hw = snd_ca0106_capture_hw;
  463. channel->emu = chip;
  464. channel->number = channel_id;
  465. channel->use = 1;
  466. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  467. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  468. channel->epcm = epcm;
  469. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  470. return err;
  471. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  472. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  473. return err;
  474. return 0;
  475. }
  476. /* close callback */
  477. static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
  478. {
  479. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  480. struct snd_pcm_runtime *runtime = substream->runtime;
  481. struct snd_ca0106_pcm *epcm = runtime->private_data;
  482. chip->capture_channels[epcm->channel_id].use = 0;
  483. /* FIXME: maybe zero others */
  484. return 0;
  485. }
  486. static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
  487. {
  488. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  489. }
  490. static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
  491. {
  492. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  493. }
  494. static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
  495. {
  496. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  497. }
  498. static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
  499. {
  500. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  501. }
  502. /* hw_params callback */
  503. static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  504. struct snd_pcm_hw_params *hw_params)
  505. {
  506. return snd_pcm_lib_malloc_pages(substream,
  507. params_buffer_bytes(hw_params));
  508. }
  509. /* hw_free callback */
  510. static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  511. {
  512. return snd_pcm_lib_free_pages(substream);
  513. }
  514. /* hw_params callback */
  515. static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  516. struct snd_pcm_hw_params *hw_params)
  517. {
  518. return snd_pcm_lib_malloc_pages(substream,
  519. params_buffer_bytes(hw_params));
  520. }
  521. /* hw_free callback */
  522. static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  523. {
  524. return snd_pcm_lib_free_pages(substream);
  525. }
  526. /* prepare playback callback */
  527. static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
  528. {
  529. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  530. struct snd_pcm_runtime *runtime = substream->runtime;
  531. struct snd_ca0106_pcm *epcm = runtime->private_data;
  532. int channel = epcm->channel_id;
  533. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  534. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  535. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  536. u32 hcfg_set = 0x00000000;
  537. u32 hcfg;
  538. u32 reg40_mask = 0x30000 << (channel<<1);
  539. u32 reg40_set = 0;
  540. u32 reg40;
  541. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  542. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  543. u32 reg71_set = 0;
  544. u32 reg71;
  545. int i;
  546. //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
  547. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  548. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  549. /* Rate can be set per channel. */
  550. /* reg40 control host to fifo */
  551. /* reg71 controls DAC rate. */
  552. switch (runtime->rate) {
  553. case 44100:
  554. reg40_set = 0x10000 << (channel<<1);
  555. reg71_set = 0x01010000;
  556. break;
  557. case 48000:
  558. reg40_set = 0;
  559. reg71_set = 0;
  560. break;
  561. case 96000:
  562. reg40_set = 0x20000 << (channel<<1);
  563. reg71_set = 0x02020000;
  564. break;
  565. case 192000:
  566. reg40_set = 0x30000 << (channel<<1);
  567. reg71_set = 0x03030000;
  568. break;
  569. default:
  570. reg40_set = 0;
  571. reg71_set = 0;
  572. break;
  573. }
  574. /* Format is a global setting */
  575. /* FIXME: Only let the first channel accessed set this. */
  576. switch (runtime->format) {
  577. case SNDRV_PCM_FORMAT_S16_LE:
  578. hcfg_set = 0;
  579. break;
  580. case SNDRV_PCM_FORMAT_S32_LE:
  581. hcfg_set = HCFG_PLAYBACK_S32_LE;
  582. break;
  583. default:
  584. hcfg_set = 0;
  585. break;
  586. }
  587. hcfg = inl(emu->port + HCFG) ;
  588. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  589. outl(hcfg, emu->port + HCFG);
  590. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  591. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  592. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  593. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  594. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  595. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  596. /* FIXME: Check emu->buffer.size before actually writing to it. */
  597. for(i=0; i < runtime->periods; i++) {
  598. table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
  599. table_base[i*2+1] = period_size_bytes << 16;
  600. }
  601. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  602. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  603. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  604. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  605. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  606. /* FIXME test what 0 bytes does. */
  607. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  608. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  609. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  610. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  611. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  612. #if 0
  613. snd_ca0106_ptr_write(emu, SPCS0, 0,
  614. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  615. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  616. SPCS_GENERATIONSTATUS | 0x00001200 |
  617. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  618. }
  619. #endif
  620. return 0;
  621. }
  622. /* prepare capture callback */
  623. static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
  624. {
  625. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  626. struct snd_pcm_runtime *runtime = substream->runtime;
  627. struct snd_ca0106_pcm *epcm = runtime->private_data;
  628. int channel = epcm->channel_id;
  629. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  630. u32 hcfg_set = 0x00000000;
  631. u32 hcfg;
  632. u32 over_sampling=0x2;
  633. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  634. u32 reg71_set = 0;
  635. u32 reg71;
  636. //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
  637. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  638. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  639. /* reg71 controls ADC rate. */
  640. switch (runtime->rate) {
  641. case 44100:
  642. reg71_set = 0x00004000;
  643. break;
  644. case 48000:
  645. reg71_set = 0;
  646. break;
  647. case 96000:
  648. reg71_set = 0x00008000;
  649. over_sampling=0xa;
  650. break;
  651. case 192000:
  652. reg71_set = 0x0000c000;
  653. over_sampling=0xa;
  654. break;
  655. default:
  656. reg71_set = 0;
  657. break;
  658. }
  659. /* Format is a global setting */
  660. /* FIXME: Only let the first channel accessed set this. */
  661. switch (runtime->format) {
  662. case SNDRV_PCM_FORMAT_S16_LE:
  663. hcfg_set = 0;
  664. break;
  665. case SNDRV_PCM_FORMAT_S32_LE:
  666. hcfg_set = HCFG_CAPTURE_S32_LE;
  667. break;
  668. default:
  669. hcfg_set = 0;
  670. break;
  671. }
  672. hcfg = inl(emu->port + HCFG) ;
  673. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  674. outl(hcfg, emu->port + HCFG);
  675. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  676. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  677. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  678. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  679. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  680. }
  681. //printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, frames_to_bytes(runtime, 1));
  682. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  683. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  684. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  685. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  686. return 0;
  687. }
  688. /* trigger_playback callback */
  689. static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
  690. int cmd)
  691. {
  692. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  693. struct snd_pcm_runtime *runtime;
  694. struct snd_ca0106_pcm *epcm;
  695. int channel;
  696. int result = 0;
  697. struct list_head *pos;
  698. struct snd_pcm_substream *s;
  699. u32 basic = 0;
  700. u32 extended = 0;
  701. int running=0;
  702. switch (cmd) {
  703. case SNDRV_PCM_TRIGGER_START:
  704. running=1;
  705. break;
  706. case SNDRV_PCM_TRIGGER_STOP:
  707. default:
  708. running=0;
  709. break;
  710. }
  711. snd_pcm_group_for_each(pos, substream) {
  712. s = snd_pcm_group_substream_entry(pos);
  713. runtime = s->runtime;
  714. epcm = runtime->private_data;
  715. channel = epcm->channel_id;
  716. //snd_printk("channel=%d\n",channel);
  717. epcm->running = running;
  718. basic |= (0x1<<channel);
  719. extended |= (0x10<<channel);
  720. snd_pcm_trigger_done(s, substream);
  721. }
  722. //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
  723. switch (cmd) {
  724. case SNDRV_PCM_TRIGGER_START:
  725. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
  726. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
  727. break;
  728. case SNDRV_PCM_TRIGGER_STOP:
  729. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
  730. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
  731. break;
  732. default:
  733. result = -EINVAL;
  734. break;
  735. }
  736. return result;
  737. }
  738. /* trigger_capture callback */
  739. static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
  740. int cmd)
  741. {
  742. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  743. struct snd_pcm_runtime *runtime = substream->runtime;
  744. struct snd_ca0106_pcm *epcm = runtime->private_data;
  745. int channel = epcm->channel_id;
  746. int result = 0;
  747. switch (cmd) {
  748. case SNDRV_PCM_TRIGGER_START:
  749. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  750. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  751. epcm->running = 1;
  752. break;
  753. case SNDRV_PCM_TRIGGER_STOP:
  754. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  755. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  756. epcm->running = 0;
  757. break;
  758. default:
  759. result = -EINVAL;
  760. break;
  761. }
  762. return result;
  763. }
  764. /* pointer_playback callback */
  765. static snd_pcm_uframes_t
  766. snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
  767. {
  768. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  769. struct snd_pcm_runtime *runtime = substream->runtime;
  770. struct snd_ca0106_pcm *epcm = runtime->private_data;
  771. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  772. int channel = epcm->channel_id;
  773. if (!epcm->running)
  774. return 0;
  775. ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  776. ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  777. ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  778. if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  779. ptr2 = bytes_to_frames(runtime, ptr1);
  780. ptr2+= (ptr4 >> 3) * runtime->period_size;
  781. ptr=ptr2;
  782. if (ptr >= runtime->buffer_size)
  783. ptr -= runtime->buffer_size;
  784. //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
  785. return ptr;
  786. }
  787. /* pointer_capture callback */
  788. static snd_pcm_uframes_t
  789. snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
  790. {
  791. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  792. struct snd_pcm_runtime *runtime = substream->runtime;
  793. struct snd_ca0106_pcm *epcm = runtime->private_data;
  794. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  795. int channel = channel=epcm->channel_id;
  796. if (!epcm->running)
  797. return 0;
  798. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  799. ptr2 = bytes_to_frames(runtime, ptr1);
  800. ptr=ptr2;
  801. if (ptr >= runtime->buffer_size)
  802. ptr -= runtime->buffer_size;
  803. //printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);
  804. return ptr;
  805. }
  806. /* operators */
  807. static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
  808. .open = snd_ca0106_pcm_open_playback_front,
  809. .close = snd_ca0106_pcm_close_playback,
  810. .ioctl = snd_pcm_lib_ioctl,
  811. .hw_params = snd_ca0106_pcm_hw_params_playback,
  812. .hw_free = snd_ca0106_pcm_hw_free_playback,
  813. .prepare = snd_ca0106_pcm_prepare_playback,
  814. .trigger = snd_ca0106_pcm_trigger_playback,
  815. .pointer = snd_ca0106_pcm_pointer_playback,
  816. };
  817. static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
  818. .open = snd_ca0106_pcm_open_0_capture,
  819. .close = snd_ca0106_pcm_close_capture,
  820. .ioctl = snd_pcm_lib_ioctl,
  821. .hw_params = snd_ca0106_pcm_hw_params_capture,
  822. .hw_free = snd_ca0106_pcm_hw_free_capture,
  823. .prepare = snd_ca0106_pcm_prepare_capture,
  824. .trigger = snd_ca0106_pcm_trigger_capture,
  825. .pointer = snd_ca0106_pcm_pointer_capture,
  826. };
  827. static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
  828. .open = snd_ca0106_pcm_open_1_capture,
  829. .close = snd_ca0106_pcm_close_capture,
  830. .ioctl = snd_pcm_lib_ioctl,
  831. .hw_params = snd_ca0106_pcm_hw_params_capture,
  832. .hw_free = snd_ca0106_pcm_hw_free_capture,
  833. .prepare = snd_ca0106_pcm_prepare_capture,
  834. .trigger = snd_ca0106_pcm_trigger_capture,
  835. .pointer = snd_ca0106_pcm_pointer_capture,
  836. };
  837. static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
  838. .open = snd_ca0106_pcm_open_2_capture,
  839. .close = snd_ca0106_pcm_close_capture,
  840. .ioctl = snd_pcm_lib_ioctl,
  841. .hw_params = snd_ca0106_pcm_hw_params_capture,
  842. .hw_free = snd_ca0106_pcm_hw_free_capture,
  843. .prepare = snd_ca0106_pcm_prepare_capture,
  844. .trigger = snd_ca0106_pcm_trigger_capture,
  845. .pointer = snd_ca0106_pcm_pointer_capture,
  846. };
  847. static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
  848. .open = snd_ca0106_pcm_open_3_capture,
  849. .close = snd_ca0106_pcm_close_capture,
  850. .ioctl = snd_pcm_lib_ioctl,
  851. .hw_params = snd_ca0106_pcm_hw_params_capture,
  852. .hw_free = snd_ca0106_pcm_hw_free_capture,
  853. .prepare = snd_ca0106_pcm_prepare_capture,
  854. .trigger = snd_ca0106_pcm_trigger_capture,
  855. .pointer = snd_ca0106_pcm_pointer_capture,
  856. };
  857. static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
  858. .open = snd_ca0106_pcm_open_playback_center_lfe,
  859. .close = snd_ca0106_pcm_close_playback,
  860. .ioctl = snd_pcm_lib_ioctl,
  861. .hw_params = snd_ca0106_pcm_hw_params_playback,
  862. .hw_free = snd_ca0106_pcm_hw_free_playback,
  863. .prepare = snd_ca0106_pcm_prepare_playback,
  864. .trigger = snd_ca0106_pcm_trigger_playback,
  865. .pointer = snd_ca0106_pcm_pointer_playback,
  866. };
  867. static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
  868. .open = snd_ca0106_pcm_open_playback_unknown,
  869. .close = snd_ca0106_pcm_close_playback,
  870. .ioctl = snd_pcm_lib_ioctl,
  871. .hw_params = snd_ca0106_pcm_hw_params_playback,
  872. .hw_free = snd_ca0106_pcm_hw_free_playback,
  873. .prepare = snd_ca0106_pcm_prepare_playback,
  874. .trigger = snd_ca0106_pcm_trigger_playback,
  875. .pointer = snd_ca0106_pcm_pointer_playback,
  876. };
  877. static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
  878. .open = snd_ca0106_pcm_open_playback_rear,
  879. .close = snd_ca0106_pcm_close_playback,
  880. .ioctl = snd_pcm_lib_ioctl,
  881. .hw_params = snd_ca0106_pcm_hw_params_playback,
  882. .hw_free = snd_ca0106_pcm_hw_free_playback,
  883. .prepare = snd_ca0106_pcm_prepare_playback,
  884. .trigger = snd_ca0106_pcm_trigger_playback,
  885. .pointer = snd_ca0106_pcm_pointer_playback,
  886. };
  887. static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
  888. unsigned short reg)
  889. {
  890. struct snd_ca0106 *emu = ac97->private_data;
  891. unsigned long flags;
  892. unsigned short val;
  893. spin_lock_irqsave(&emu->emu_lock, flags);
  894. outb(reg, emu->port + AC97ADDRESS);
  895. val = inw(emu->port + AC97DATA);
  896. spin_unlock_irqrestore(&emu->emu_lock, flags);
  897. return val;
  898. }
  899. static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
  900. unsigned short reg, unsigned short val)
  901. {
  902. struct snd_ca0106 *emu = ac97->private_data;
  903. unsigned long flags;
  904. spin_lock_irqsave(&emu->emu_lock, flags);
  905. outb(reg, emu->port + AC97ADDRESS);
  906. outw(val, emu->port + AC97DATA);
  907. spin_unlock_irqrestore(&emu->emu_lock, flags);
  908. }
  909. static int snd_ca0106_ac97(struct snd_ca0106 *chip)
  910. {
  911. struct snd_ac97_bus *pbus;
  912. struct snd_ac97_template ac97;
  913. int err;
  914. static struct snd_ac97_bus_ops ops = {
  915. .write = snd_ca0106_ac97_write,
  916. .read = snd_ca0106_ac97_read,
  917. };
  918. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  919. return err;
  920. pbus->no_vra = 1; /* we don't need VRA */
  921. memset(&ac97, 0, sizeof(ac97));
  922. ac97.private_data = chip;
  923. ac97.scaps = AC97_SCAP_NO_SPDIF;
  924. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  925. }
  926. static int snd_ca0106_free(struct snd_ca0106 *chip)
  927. {
  928. if (chip->res_port != NULL) { /* avoid access to already used hardware */
  929. // disable interrupts
  930. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  931. outl(0, chip->port + INTE);
  932. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  933. udelay(1000);
  934. // disable audio
  935. //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
  936. outl(0, chip->port + HCFG);
  937. /* FIXME: We need to stop and DMA transfers here.
  938. * But as I am not sure how yet, we cannot from the dma pages.
  939. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  940. */
  941. }
  942. // release the data
  943. #if 1
  944. if (chip->buffer.area)
  945. snd_dma_free_pages(&chip->buffer);
  946. #endif
  947. // release the i/o port
  948. release_and_free_resource(chip->res_port);
  949. // release the irq
  950. if (chip->irq >= 0)
  951. free_irq(chip->irq, (void *)chip);
  952. pci_disable_device(chip->pci);
  953. kfree(chip);
  954. return 0;
  955. }
  956. static int snd_ca0106_dev_free(struct snd_device *device)
  957. {
  958. struct snd_ca0106 *chip = device->device_data;
  959. return snd_ca0106_free(chip);
  960. }
  961. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id)
  962. {
  963. unsigned int status;
  964. struct snd_ca0106 *chip = dev_id;
  965. int i;
  966. int mask;
  967. unsigned int stat76;
  968. struct snd_ca0106_channel *pchannel;
  969. status = inl(chip->port + IPR);
  970. if (! status)
  971. return IRQ_NONE;
  972. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  973. //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
  974. //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  975. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  976. for(i = 0; i < 4; i++) {
  977. pchannel = &(chip->playback_channels[i]);
  978. if (stat76 & mask) {
  979. /* FIXME: Select the correct substream for period elapsed */
  980. if(pchannel->use) {
  981. snd_pcm_period_elapsed(pchannel->epcm->substream);
  982. //printk(KERN_INFO "interrupt [%d] used\n", i);
  983. }
  984. }
  985. //printk(KERN_INFO "channel=%p\n",pchannel);
  986. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  987. mask <<= 1;
  988. }
  989. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  990. for(i = 0; i < 4; i++) {
  991. pchannel = &(chip->capture_channels[i]);
  992. if (stat76 & mask) {
  993. /* FIXME: Select the correct substream for period elapsed */
  994. if(pchannel->use) {
  995. snd_pcm_period_elapsed(pchannel->epcm->substream);
  996. //printk(KERN_INFO "interrupt [%d] used\n", i);
  997. }
  998. }
  999. //printk(KERN_INFO "channel=%p\n",pchannel);
  1000. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1001. mask <<= 1;
  1002. }
  1003. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  1004. if (chip->midi.dev_id &&
  1005. (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
  1006. if (chip->midi.interrupt)
  1007. chip->midi.interrupt(&chip->midi, status);
  1008. else
  1009. chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
  1010. }
  1011. // acknowledge the interrupt if necessary
  1012. outl(status, chip->port+IPR);
  1013. return IRQ_HANDLED;
  1014. }
  1015. static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device, struct snd_pcm **rpcm)
  1016. {
  1017. struct snd_pcm *pcm;
  1018. struct snd_pcm_substream *substream;
  1019. int err;
  1020. if (rpcm)
  1021. *rpcm = NULL;
  1022. if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
  1023. return err;
  1024. pcm->private_data = emu;
  1025. switch (device) {
  1026. case 0:
  1027. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  1028. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  1029. break;
  1030. case 1:
  1031. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  1032. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  1033. break;
  1034. case 2:
  1035. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  1036. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  1037. break;
  1038. case 3:
  1039. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  1040. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  1041. break;
  1042. }
  1043. pcm->info_flags = 0;
  1044. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1045. strcpy(pcm->name, "CA0106");
  1046. emu->pcm = pcm;
  1047. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1048. substream;
  1049. substream = substream->next) {
  1050. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1051. SNDRV_DMA_TYPE_DEV,
  1052. snd_dma_pci_data(emu->pci),
  1053. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  1054. return err;
  1055. }
  1056. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1057. substream;
  1058. substream = substream->next) {
  1059. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1060. SNDRV_DMA_TYPE_DEV,
  1061. snd_dma_pci_data(emu->pci),
  1062. 64*1024, 64*1024)) < 0)
  1063. return err;
  1064. }
  1065. if (rpcm)
  1066. *rpcm = pcm;
  1067. return 0;
  1068. }
  1069. static unsigned int spi_dac_init[] = {
  1070. 0x00ff,
  1071. 0x02ff,
  1072. 0x0400,
  1073. 0x0520,
  1074. 0x0620, /* Set 24 bit. Was 0x0600 */
  1075. 0x08ff,
  1076. 0x0aff,
  1077. 0x0cff,
  1078. 0x0eff,
  1079. 0x10ff,
  1080. 0x1200,
  1081. 0x1400,
  1082. 0x1480,
  1083. 0x1800,
  1084. 0x1aff,
  1085. 0x1cff,
  1086. 0x1e00,
  1087. 0x0530,
  1088. 0x0602,
  1089. 0x0622,
  1090. 0x1400,
  1091. };
  1092. static unsigned int i2c_adc_init[][2] = {
  1093. { 0x17, 0x00 }, /* Reset */
  1094. { 0x07, 0x00 }, /* Timeout */
  1095. { 0x0b, 0x22 }, /* Interface control */
  1096. { 0x0c, 0x22 }, /* Master mode control */
  1097. { 0x0d, 0x08 }, /* Powerdown control */
  1098. { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
  1099. { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
  1100. { 0x10, 0x7b }, /* ALC Control 1 */
  1101. { 0x11, 0x00 }, /* ALC Control 2 */
  1102. { 0x12, 0x32 }, /* ALC Control 3 */
  1103. { 0x13, 0x00 }, /* Noise gate control */
  1104. { 0x14, 0xa6 }, /* Limiter control */
  1105. { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
  1106. };
  1107. static int __devinit snd_ca0106_create(struct snd_card *card,
  1108. struct pci_dev *pci,
  1109. struct snd_ca0106 **rchip)
  1110. {
  1111. struct snd_ca0106 *chip;
  1112. struct snd_ca0106_details *c;
  1113. int err;
  1114. int ch;
  1115. static struct snd_device_ops ops = {
  1116. .dev_free = snd_ca0106_dev_free,
  1117. };
  1118. *rchip = NULL;
  1119. if ((err = pci_enable_device(pci)) < 0)
  1120. return err;
  1121. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  1122. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1123. printk(KERN_ERR "error to set 32bit mask DMA\n");
  1124. pci_disable_device(pci);
  1125. return -ENXIO;
  1126. }
  1127. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1128. if (chip == NULL) {
  1129. pci_disable_device(pci);
  1130. return -ENOMEM;
  1131. }
  1132. chip->card = card;
  1133. chip->pci = pci;
  1134. chip->irq = -1;
  1135. spin_lock_init(&chip->emu_lock);
  1136. chip->port = pci_resource_start(pci, 0);
  1137. if ((chip->res_port = request_region(chip->port, 0x20,
  1138. "snd_ca0106")) == NULL) {
  1139. snd_ca0106_free(chip);
  1140. printk(KERN_ERR "cannot allocate the port\n");
  1141. return -EBUSY;
  1142. }
  1143. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1144. IRQF_DISABLED|IRQF_SHARED, "snd_ca0106",
  1145. (void *)chip)) {
  1146. snd_ca0106_free(chip);
  1147. printk(KERN_ERR "cannot grab irq\n");
  1148. return -EBUSY;
  1149. }
  1150. chip->irq = pci->irq;
  1151. /* This stores the periods table. */
  1152. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
  1153. snd_ca0106_free(chip);
  1154. return -ENOMEM;
  1155. }
  1156. pci_set_master(pci);
  1157. /* read revision & serial */
  1158. pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
  1159. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1160. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1161. #if 1
  1162. printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  1163. chip->revision, chip->serial);
  1164. #endif
  1165. strcpy(card->driver, "CA0106");
  1166. strcpy(card->shortname, "CA0106");
  1167. for (c = ca0106_chip_details; c->serial; c++) {
  1168. if (c->serial == chip->serial)
  1169. break;
  1170. }
  1171. chip->details = c;
  1172. sprintf(card->longname, "%s at 0x%lx irq %i",
  1173. c->name, chip->port, chip->irq);
  1174. outl(0, chip->port + INTE);
  1175. /*
  1176. * Init to 0x02109204 :
  1177. * Clock accuracy = 0 (1000ppm)
  1178. * Sample Rate = 2 (48kHz)
  1179. * Audio Channel = 1 (Left of 2)
  1180. * Source Number = 0 (Unspecified)
  1181. * Generation Status = 1 (Original for Cat Code 12)
  1182. * Cat Code = 12 (Digital Signal Mixer)
  1183. * Mode = 0 (Mode 0)
  1184. * Emphasis = 0 (None)
  1185. * CP = 1 (Copyright unasserted)
  1186. * AN = 0 (Audio data)
  1187. * P = 0 (Consumer)
  1188. */
  1189. snd_ca0106_ptr_write(chip, SPCS0, 0,
  1190. chip->spdif_bits[0] =
  1191. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1192. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1193. SPCS_GENERATIONSTATUS | 0x00001200 |
  1194. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1195. /* Only SPCS1 has been tested */
  1196. snd_ca0106_ptr_write(chip, SPCS1, 0,
  1197. chip->spdif_bits[1] =
  1198. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1199. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1200. SPCS_GENERATIONSTATUS | 0x00001200 |
  1201. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1202. snd_ca0106_ptr_write(chip, SPCS2, 0,
  1203. chip->spdif_bits[2] =
  1204. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1205. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1206. SPCS_GENERATIONSTATUS | 0x00001200 |
  1207. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1208. snd_ca0106_ptr_write(chip, SPCS3, 0,
  1209. chip->spdif_bits[3] =
  1210. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1211. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1212. SPCS_GENERATIONSTATUS | 0x00001200 |
  1213. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1214. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1215. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1216. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1217. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1218. outw(0x8000, chip->port + AC97DATA);
  1219. #if 0
  1220. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1221. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1222. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1223. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1224. #endif
  1225. //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
  1226. /* Analog or Digital output */
  1227. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1228. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
  1229. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1230. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1231. //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
  1232. //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
  1233. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1234. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
  1235. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
  1236. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1237. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1238. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1239. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1240. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1241. for(ch = 0; ch < 4; ch++) {
  1242. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
  1243. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1244. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
  1245. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
  1246. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
  1247. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
  1248. }
  1249. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
  1250. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1251. if (chip->details->gpio_type == 2) { /* The SB0438 use GPIO differently. */
  1252. /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
  1253. outl(0x0, chip->port+GPIO);
  1254. //outl(0x00f0e000, chip->port+GPIO); /* Analog */
  1255. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1256. } else if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
  1257. /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
  1258. outl(0x0, chip->port+GPIO);
  1259. //outl(0x00f0e000, chip->port+GPIO); /* Analog */
  1260. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1261. } else {
  1262. outl(0x0, chip->port+GPIO);
  1263. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1264. //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
  1265. }
  1266. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1267. //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  1268. //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1269. //outl(0x00000009, chip->port+HCFG);
  1270. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
  1271. if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  1272. int size, n;
  1273. size = ARRAY_SIZE(i2c_adc_init);
  1274. //snd_printk("I2C:array size=0x%x\n", size);
  1275. for (n=0; n < size; n++) {
  1276. snd_ca0106_i2c_write(chip, i2c_adc_init[n][0], i2c_adc_init[n][1]);
  1277. }
  1278. for (n=0; n < 4; n++) {
  1279. chip->i2c_capture_volume[n][0]= 0xcf;
  1280. chip->i2c_capture_volume[n][1]= 0xcf;
  1281. }
  1282. chip->i2c_capture_source=2; /* Line in */
  1283. //snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
  1284. }
  1285. if (chip->details->spi_dac == 1) { /* The SB0570 use SPI to control DAC. */
  1286. int size, n;
  1287. size = ARRAY_SIZE(spi_dac_init);
  1288. for (n=0; n < size; n++)
  1289. snd_ca0106_spi_write(chip, spi_dac_init[n]);
  1290. }
  1291. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  1292. chip, &ops)) < 0) {
  1293. snd_ca0106_free(chip);
  1294. return err;
  1295. }
  1296. *rchip = chip;
  1297. return 0;
  1298. }
  1299. static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
  1300. {
  1301. snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
  1302. }
  1303. static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
  1304. {
  1305. snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
  1306. }
  1307. static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
  1308. {
  1309. return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
  1310. midi->port + idx, 0);
  1311. }
  1312. static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
  1313. {
  1314. snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
  1315. }
  1316. static struct snd_card *ca0106_dev_id_card(void *dev_id)
  1317. {
  1318. return ((struct snd_ca0106 *)dev_id)->card;
  1319. }
  1320. static int ca0106_dev_id_port(void *dev_id)
  1321. {
  1322. return ((struct snd_ca0106 *)dev_id)->port;
  1323. }
  1324. static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
  1325. {
  1326. struct snd_ca_midi *midi;
  1327. char *name;
  1328. int err;
  1329. if (channel == CA0106_MIDI_CHAN_B) {
  1330. name = "CA0106 MPU-401 (UART) B";
  1331. midi = &chip->midi2;
  1332. midi->tx_enable = INTE_MIDI_TX_B;
  1333. midi->rx_enable = INTE_MIDI_RX_B;
  1334. midi->ipr_tx = IPR_MIDI_TX_B;
  1335. midi->ipr_rx = IPR_MIDI_RX_B;
  1336. midi->port = MIDI_UART_B_DATA;
  1337. } else {
  1338. name = "CA0106 MPU-401 (UART)";
  1339. midi = &chip->midi;
  1340. midi->tx_enable = INTE_MIDI_TX_A;
  1341. midi->rx_enable = INTE_MIDI_TX_B;
  1342. midi->ipr_tx = IPR_MIDI_TX_A;
  1343. midi->ipr_rx = IPR_MIDI_RX_A;
  1344. midi->port = MIDI_UART_A_DATA;
  1345. }
  1346. midi->reset = CA0106_MPU401_RESET;
  1347. midi->enter_uart = CA0106_MPU401_ENTER_UART;
  1348. midi->ack = CA0106_MPU401_ACK;
  1349. midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
  1350. midi->output_ready = CA0106_MIDI_OUTPUT_READY;
  1351. midi->channel = channel;
  1352. midi->interrupt_enable = ca0106_midi_interrupt_enable;
  1353. midi->interrupt_disable = ca0106_midi_interrupt_disable;
  1354. midi->read = ca0106_midi_read;
  1355. midi->write = ca0106_midi_write;
  1356. midi->get_dev_id_card = ca0106_dev_id_card;
  1357. midi->get_dev_id_port = ca0106_dev_id_port;
  1358. midi->dev_id = chip;
  1359. if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
  1360. return err;
  1361. return 0;
  1362. }
  1363. static int __devinit snd_ca0106_probe(struct pci_dev *pci,
  1364. const struct pci_device_id *pci_id)
  1365. {
  1366. static int dev;
  1367. struct snd_card *card;
  1368. struct snd_ca0106 *chip;
  1369. int err;
  1370. if (dev >= SNDRV_CARDS)
  1371. return -ENODEV;
  1372. if (!enable[dev]) {
  1373. dev++;
  1374. return -ENOENT;
  1375. }
  1376. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1377. if (card == NULL)
  1378. return -ENOMEM;
  1379. if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
  1380. snd_card_free(card);
  1381. return err;
  1382. }
  1383. if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
  1384. snd_card_free(card);
  1385. return err;
  1386. }
  1387. if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
  1388. snd_card_free(card);
  1389. return err;
  1390. }
  1391. if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
  1392. snd_card_free(card);
  1393. return err;
  1394. }
  1395. if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
  1396. snd_card_free(card);
  1397. return err;
  1398. }
  1399. if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
  1400. if ((err = snd_ca0106_ac97(chip)) < 0) {
  1401. snd_card_free(card);
  1402. return err;
  1403. }
  1404. }
  1405. if ((err = snd_ca0106_mixer(chip)) < 0) {
  1406. snd_card_free(card);
  1407. return err;
  1408. }
  1409. snd_printdd("ca0106: probe for MIDI channel A ...");
  1410. if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
  1411. snd_card_free(card);
  1412. snd_printdd(" failed, err=0x%x\n",err);
  1413. return err;
  1414. }
  1415. snd_printdd(" done.\n");
  1416. #ifdef CONFIG_PROC_FS
  1417. snd_ca0106_proc_init(chip);
  1418. #endif
  1419. if ((err = snd_card_register(card)) < 0) {
  1420. snd_card_free(card);
  1421. return err;
  1422. }
  1423. pci_set_drvdata(pci, card);
  1424. dev++;
  1425. return 0;
  1426. }
  1427. static void __devexit snd_ca0106_remove(struct pci_dev *pci)
  1428. {
  1429. snd_card_free(pci_get_drvdata(pci));
  1430. pci_set_drvdata(pci, NULL);
  1431. }
  1432. // PCI IDs
  1433. static struct pci_device_id snd_ca0106_ids[] = {
  1434. { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
  1435. { 0, }
  1436. };
  1437. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1438. // pci_driver definition
  1439. static struct pci_driver driver = {
  1440. .name = "CA0106",
  1441. .id_table = snd_ca0106_ids,
  1442. .probe = snd_ca0106_probe,
  1443. .remove = __devexit_p(snd_ca0106_remove),
  1444. };
  1445. // initialization of the module
  1446. static int __init alsa_card_ca0106_init(void)
  1447. {
  1448. return pci_register_driver(&driver);
  1449. }
  1450. // clean up the module
  1451. static void __exit alsa_card_ca0106_exit(void)
  1452. {
  1453. pci_unregister_driver(&driver);
  1454. }
  1455. module_init(alsa_card_ca0106_init)
  1456. module_exit(alsa_card_ca0106_exit)