ca0106_main.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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 ca0106_details_t 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. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  182. { .serial = 0x10091462,
  183. .name = "MSI K8N Diamond MB [SB0438]",
  184. .gpio_type = 1,
  185. .i2c_adc = 1 } ,
  186. { .serial = 0,
  187. .name = "AudigyLS [Unknown]" }
  188. };
  189. /* hardware definition */
  190. static snd_pcm_hardware_t snd_ca0106_playback_hw = {
  191. .info = (SNDRV_PCM_INFO_MMAP |
  192. SNDRV_PCM_INFO_INTERLEAVED |
  193. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  194. SNDRV_PCM_INFO_MMAP_VALID),
  195. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  196. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
  197. .rate_min = 48000,
  198. .rate_max = 192000,
  199. .channels_min = 2, //1,
  200. .channels_max = 2, //6,
  201. .buffer_bytes_max = ((65536 - 64) * 8),
  202. .period_bytes_min = 64,
  203. .period_bytes_max = (65536 - 64),
  204. .periods_min = 2,
  205. .periods_max = 8,
  206. .fifo_size = 0,
  207. };
  208. static snd_pcm_hardware_t snd_ca0106_capture_hw = {
  209. .info = (SNDRV_PCM_INFO_MMAP |
  210. SNDRV_PCM_INFO_INTERLEAVED |
  211. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  212. SNDRV_PCM_INFO_MMAP_VALID),
  213. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  214. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000,
  215. .rate_min = 44100,
  216. .rate_max = 192000,
  217. .channels_min = 2,
  218. .channels_max = 2,
  219. .buffer_bytes_max = ((65536 - 64) * 8),
  220. .period_bytes_min = 64,
  221. .period_bytes_max = (65536 - 64),
  222. .periods_min = 2,
  223. .periods_max = 2,
  224. .fifo_size = 0,
  225. };
  226. unsigned int snd_ca0106_ptr_read(ca0106_t * emu,
  227. unsigned int reg,
  228. unsigned int chn)
  229. {
  230. unsigned long flags;
  231. unsigned int regptr, val;
  232. regptr = (reg << 16) | chn;
  233. spin_lock_irqsave(&emu->emu_lock, flags);
  234. outl(regptr, emu->port + PTR);
  235. val = inl(emu->port + DATA);
  236. spin_unlock_irqrestore(&emu->emu_lock, flags);
  237. return val;
  238. }
  239. void snd_ca0106_ptr_write(ca0106_t *emu,
  240. unsigned int reg,
  241. unsigned int chn,
  242. unsigned int data)
  243. {
  244. unsigned int regptr;
  245. unsigned long flags;
  246. regptr = (reg << 16) | chn;
  247. spin_lock_irqsave(&emu->emu_lock, flags);
  248. outl(regptr, emu->port + PTR);
  249. outl(data, emu->port + DATA);
  250. spin_unlock_irqrestore(&emu->emu_lock, flags);
  251. }
  252. int snd_ca0106_i2c_write(ca0106_t *emu,
  253. u32 reg,
  254. u32 value)
  255. {
  256. u32 tmp;
  257. int timeout=0;
  258. int status;
  259. int retry;
  260. if ((reg > 0x7f) || (value > 0x1ff))
  261. {
  262. snd_printk("i2c_write: invalid values.\n");
  263. return -EINVAL;
  264. }
  265. tmp = reg << 25 | value << 16;
  266. /* Not sure what this I2C channel controls. */
  267. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  268. /* This controls the I2C connected to the WM8775 ADC Codec */
  269. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  270. for(retry=0;retry<10;retry++)
  271. {
  272. /* Send the data to i2c */
  273. tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  274. tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  275. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  276. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  277. /* Wait till the transaction ends */
  278. while(1)
  279. {
  280. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  281. //snd_printk("I2C:status=0x%x\n", status);
  282. timeout++;
  283. if((status & I2C_A_ADC_START)==0)
  284. break;
  285. if(timeout>1000)
  286. break;
  287. }
  288. //Read back and see if the transaction is successful
  289. if((status & I2C_A_ADC_ABORT)==0)
  290. break;
  291. }
  292. if(retry==10)
  293. {
  294. snd_printk("Writing to ADC failed!\n");
  295. return -EINVAL;
  296. }
  297. return 0;
  298. }
  299. static void snd_ca0106_intr_enable(ca0106_t *emu, unsigned int intrenb)
  300. {
  301. unsigned long flags;
  302. unsigned int enable;
  303. spin_lock_irqsave(&emu->emu_lock, flags);
  304. enable = inl(emu->port + INTE) | intrenb;
  305. outl(enable, emu->port + INTE);
  306. spin_unlock_irqrestore(&emu->emu_lock, flags);
  307. }
  308. static void snd_ca0106_pcm_free_substream(snd_pcm_runtime_t *runtime)
  309. {
  310. kfree(runtime->private_data);
  311. }
  312. /* open_playback callback */
  313. static int snd_ca0106_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
  314. {
  315. ca0106_t *chip = snd_pcm_substream_chip(substream);
  316. ca0106_channel_t *channel = &(chip->playback_channels[channel_id]);
  317. ca0106_pcm_t *epcm;
  318. snd_pcm_runtime_t *runtime = substream->runtime;
  319. int err;
  320. epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
  321. if (epcm == NULL)
  322. return -ENOMEM;
  323. epcm->emu = chip;
  324. epcm->substream = substream;
  325. epcm->channel_id=channel_id;
  326. runtime->private_data = epcm;
  327. runtime->private_free = snd_ca0106_pcm_free_substream;
  328. runtime->hw = snd_ca0106_playback_hw;
  329. channel->emu = chip;
  330. channel->number = channel_id;
  331. channel->use=1;
  332. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  333. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  334. channel->epcm=epcm;
  335. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  336. return err;
  337. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  338. return err;
  339. return 0;
  340. }
  341. /* close callback */
  342. static int snd_ca0106_pcm_close_playback(snd_pcm_substream_t *substream)
  343. {
  344. ca0106_t *chip = snd_pcm_substream_chip(substream);
  345. snd_pcm_runtime_t *runtime = substream->runtime;
  346. ca0106_pcm_t *epcm = runtime->private_data;
  347. chip->playback_channels[epcm->channel_id].use=0;
  348. /* FIXME: maybe zero others */
  349. return 0;
  350. }
  351. static int snd_ca0106_pcm_open_playback_front(snd_pcm_substream_t *substream)
  352. {
  353. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  354. }
  355. static int snd_ca0106_pcm_open_playback_center_lfe(snd_pcm_substream_t *substream)
  356. {
  357. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  358. }
  359. static int snd_ca0106_pcm_open_playback_unknown(snd_pcm_substream_t *substream)
  360. {
  361. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  362. }
  363. static int snd_ca0106_pcm_open_playback_rear(snd_pcm_substream_t *substream)
  364. {
  365. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  366. }
  367. /* open_capture callback */
  368. static int snd_ca0106_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
  369. {
  370. ca0106_t *chip = snd_pcm_substream_chip(substream);
  371. ca0106_channel_t *channel = &(chip->capture_channels[channel_id]);
  372. ca0106_pcm_t *epcm;
  373. snd_pcm_runtime_t *runtime = substream->runtime;
  374. int err;
  375. epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
  376. if (epcm == NULL) {
  377. snd_printk("open_capture_channel: failed epcm alloc\n");
  378. return -ENOMEM;
  379. }
  380. epcm->emu = chip;
  381. epcm->substream = substream;
  382. epcm->channel_id=channel_id;
  383. runtime->private_data = epcm;
  384. runtime->private_free = snd_ca0106_pcm_free_substream;
  385. runtime->hw = snd_ca0106_capture_hw;
  386. channel->emu = chip;
  387. channel->number = channel_id;
  388. channel->use=1;
  389. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  390. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  391. channel->epcm=epcm;
  392. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  393. return err;
  394. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  395. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  396. return err;
  397. return 0;
  398. }
  399. /* close callback */
  400. static int snd_ca0106_pcm_close_capture(snd_pcm_substream_t *substream)
  401. {
  402. ca0106_t *chip = snd_pcm_substream_chip(substream);
  403. snd_pcm_runtime_t *runtime = substream->runtime;
  404. ca0106_pcm_t *epcm = runtime->private_data;
  405. chip->capture_channels[epcm->channel_id].use=0;
  406. /* FIXME: maybe zero others */
  407. return 0;
  408. }
  409. static int snd_ca0106_pcm_open_0_capture(snd_pcm_substream_t *substream)
  410. {
  411. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  412. }
  413. static int snd_ca0106_pcm_open_1_capture(snd_pcm_substream_t *substream)
  414. {
  415. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  416. }
  417. static int snd_ca0106_pcm_open_2_capture(snd_pcm_substream_t *substream)
  418. {
  419. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  420. }
  421. static int snd_ca0106_pcm_open_3_capture(snd_pcm_substream_t *substream)
  422. {
  423. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  424. }
  425. /* hw_params callback */
  426. static int snd_ca0106_pcm_hw_params_playback(snd_pcm_substream_t *substream,
  427. snd_pcm_hw_params_t * hw_params)
  428. {
  429. return snd_pcm_lib_malloc_pages(substream,
  430. params_buffer_bytes(hw_params));
  431. }
  432. /* hw_free callback */
  433. static int snd_ca0106_pcm_hw_free_playback(snd_pcm_substream_t *substream)
  434. {
  435. return snd_pcm_lib_free_pages(substream);
  436. }
  437. /* hw_params callback */
  438. static int snd_ca0106_pcm_hw_params_capture(snd_pcm_substream_t *substream,
  439. snd_pcm_hw_params_t * hw_params)
  440. {
  441. return snd_pcm_lib_malloc_pages(substream,
  442. params_buffer_bytes(hw_params));
  443. }
  444. /* hw_free callback */
  445. static int snd_ca0106_pcm_hw_free_capture(snd_pcm_substream_t *substream)
  446. {
  447. return snd_pcm_lib_free_pages(substream);
  448. }
  449. /* prepare playback callback */
  450. static int snd_ca0106_pcm_prepare_playback(snd_pcm_substream_t *substream)
  451. {
  452. ca0106_t *emu = snd_pcm_substream_chip(substream);
  453. snd_pcm_runtime_t *runtime = substream->runtime;
  454. ca0106_pcm_t *epcm = runtime->private_data;
  455. int channel = epcm->channel_id;
  456. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  457. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  458. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  459. u32 hcfg_set = 0x00000000;
  460. u32 hcfg;
  461. u32 reg40_mask = 0x30000 << (channel<<1);
  462. u32 reg40_set = 0;
  463. u32 reg40;
  464. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  465. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  466. u32 reg71_set = 0;
  467. u32 reg71;
  468. int i;
  469. //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));
  470. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  471. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  472. /* Rate can be set per channel. */
  473. /* reg40 control host to fifo */
  474. /* reg71 controls DAC rate. */
  475. switch (runtime->rate) {
  476. case 44100:
  477. reg40_set = 0x10000 << (channel<<1);
  478. reg71_set = 0x01010000;
  479. break;
  480. case 48000:
  481. reg40_set = 0;
  482. reg71_set = 0;
  483. break;
  484. case 96000:
  485. reg40_set = 0x20000 << (channel<<1);
  486. reg71_set = 0x02020000;
  487. break;
  488. case 192000:
  489. reg40_set = 0x30000 << (channel<<1);
  490. reg71_set = 0x03030000;
  491. break;
  492. default:
  493. reg40_set = 0;
  494. reg71_set = 0;
  495. break;
  496. }
  497. /* Format is a global setting */
  498. /* FIXME: Only let the first channel accessed set this. */
  499. switch (runtime->format) {
  500. case SNDRV_PCM_FORMAT_S16_LE:
  501. hcfg_set = 0;
  502. break;
  503. case SNDRV_PCM_FORMAT_S32_LE:
  504. hcfg_set = HCFG_PLAYBACK_S32_LE;
  505. break;
  506. default:
  507. hcfg_set = 0;
  508. break;
  509. }
  510. hcfg = inl(emu->port + HCFG) ;
  511. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  512. outl(hcfg, emu->port + HCFG);
  513. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  514. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  515. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  516. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  517. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  518. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  519. /* FIXME: Check emu->buffer.size before actually writing to it. */
  520. for(i=0; i < runtime->periods; i++) {
  521. table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
  522. table_base[(i*2)+1]=period_size_bytes<<16;
  523. }
  524. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  525. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  526. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  527. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  528. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  529. /* FIXME test what 0 bytes does. */
  530. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  531. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  532. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  533. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  534. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  535. #if 0
  536. snd_ca0106_ptr_write(emu, SPCS0, 0,
  537. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  538. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  539. SPCS_GENERATIONSTATUS | 0x00001200 |
  540. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  541. }
  542. #endif
  543. return 0;
  544. }
  545. /* prepare capture callback */
  546. static int snd_ca0106_pcm_prepare_capture(snd_pcm_substream_t *substream)
  547. {
  548. ca0106_t *emu = snd_pcm_substream_chip(substream);
  549. snd_pcm_runtime_t *runtime = substream->runtime;
  550. ca0106_pcm_t *epcm = runtime->private_data;
  551. int channel = epcm->channel_id;
  552. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  553. u32 hcfg_set = 0x00000000;
  554. u32 hcfg;
  555. u32 over_sampling=0x2;
  556. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  557. u32 reg71_set = 0;
  558. u32 reg71;
  559. //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));
  560. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  561. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  562. /* reg71 controls ADC rate. */
  563. switch (runtime->rate) {
  564. case 44100:
  565. reg71_set = 0x00004000;
  566. break;
  567. case 48000:
  568. reg71_set = 0;
  569. break;
  570. case 96000:
  571. reg71_set = 0x00008000;
  572. over_sampling=0xa;
  573. break;
  574. case 192000:
  575. reg71_set = 0x0000c000;
  576. over_sampling=0xa;
  577. break;
  578. default:
  579. reg71_set = 0;
  580. break;
  581. }
  582. /* Format is a global setting */
  583. /* FIXME: Only let the first channel accessed set this. */
  584. switch (runtime->format) {
  585. case SNDRV_PCM_FORMAT_S16_LE:
  586. hcfg_set = 0;
  587. break;
  588. case SNDRV_PCM_FORMAT_S32_LE:
  589. hcfg_set = HCFG_CAPTURE_S32_LE;
  590. break;
  591. default:
  592. hcfg_set = 0;
  593. break;
  594. }
  595. hcfg = inl(emu->port + HCFG) ;
  596. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  597. outl(hcfg, emu->port + HCFG);
  598. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  599. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  600. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  601. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  602. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  603. }
  604. //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));
  605. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  606. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  607. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  608. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  609. return 0;
  610. }
  611. /* trigger_playback callback */
  612. static int snd_ca0106_pcm_trigger_playback(snd_pcm_substream_t *substream,
  613. int cmd)
  614. {
  615. ca0106_t *emu = snd_pcm_substream_chip(substream);
  616. snd_pcm_runtime_t *runtime;
  617. ca0106_pcm_t *epcm;
  618. int channel;
  619. int result = 0;
  620. struct list_head *pos;
  621. snd_pcm_substream_t *s;
  622. u32 basic = 0;
  623. u32 extended = 0;
  624. int running=0;
  625. switch (cmd) {
  626. case SNDRV_PCM_TRIGGER_START:
  627. running=1;
  628. break;
  629. case SNDRV_PCM_TRIGGER_STOP:
  630. default:
  631. running=0;
  632. break;
  633. }
  634. snd_pcm_group_for_each(pos, substream) {
  635. s = snd_pcm_group_substream_entry(pos);
  636. runtime = s->runtime;
  637. epcm = runtime->private_data;
  638. channel = epcm->channel_id;
  639. //snd_printk("channel=%d\n",channel);
  640. epcm->running = running;
  641. basic |= (0x1<<channel);
  642. extended |= (0x10<<channel);
  643. snd_pcm_trigger_done(s, substream);
  644. }
  645. //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
  646. switch (cmd) {
  647. case SNDRV_PCM_TRIGGER_START:
  648. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
  649. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
  650. break;
  651. case SNDRV_PCM_TRIGGER_STOP:
  652. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
  653. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
  654. break;
  655. default:
  656. result = -EINVAL;
  657. break;
  658. }
  659. return result;
  660. }
  661. /* trigger_capture callback */
  662. static int snd_ca0106_pcm_trigger_capture(snd_pcm_substream_t *substream,
  663. int cmd)
  664. {
  665. ca0106_t *emu = snd_pcm_substream_chip(substream);
  666. snd_pcm_runtime_t *runtime = substream->runtime;
  667. ca0106_pcm_t *epcm = runtime->private_data;
  668. int channel = epcm->channel_id;
  669. int result = 0;
  670. switch (cmd) {
  671. case SNDRV_PCM_TRIGGER_START:
  672. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  673. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  674. epcm->running = 1;
  675. break;
  676. case SNDRV_PCM_TRIGGER_STOP:
  677. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  678. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  679. epcm->running = 0;
  680. break;
  681. default:
  682. result = -EINVAL;
  683. break;
  684. }
  685. return result;
  686. }
  687. /* pointer_playback callback */
  688. static snd_pcm_uframes_t
  689. snd_ca0106_pcm_pointer_playback(snd_pcm_substream_t *substream)
  690. {
  691. ca0106_t *emu = snd_pcm_substream_chip(substream);
  692. snd_pcm_runtime_t *runtime = substream->runtime;
  693. ca0106_pcm_t *epcm = runtime->private_data;
  694. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  695. int channel = epcm->channel_id;
  696. if (!epcm->running)
  697. return 0;
  698. ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  699. ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  700. ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  701. if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  702. ptr2 = bytes_to_frames(runtime, ptr1);
  703. ptr2+= (ptr4 >> 3) * runtime->period_size;
  704. ptr=ptr2;
  705. if (ptr >= runtime->buffer_size)
  706. ptr -= runtime->buffer_size;
  707. //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);
  708. return ptr;
  709. }
  710. /* pointer_capture callback */
  711. static snd_pcm_uframes_t
  712. snd_ca0106_pcm_pointer_capture(snd_pcm_substream_t *substream)
  713. {
  714. ca0106_t *emu = snd_pcm_substream_chip(substream);
  715. snd_pcm_runtime_t *runtime = substream->runtime;
  716. ca0106_pcm_t *epcm = runtime->private_data;
  717. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  718. int channel = channel=epcm->channel_id;
  719. if (!epcm->running)
  720. return 0;
  721. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  722. ptr2 = bytes_to_frames(runtime, ptr1);
  723. ptr=ptr2;
  724. if (ptr >= runtime->buffer_size)
  725. ptr -= runtime->buffer_size;
  726. //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);
  727. return ptr;
  728. }
  729. /* operators */
  730. static snd_pcm_ops_t snd_ca0106_playback_front_ops = {
  731. .open = snd_ca0106_pcm_open_playback_front,
  732. .close = snd_ca0106_pcm_close_playback,
  733. .ioctl = snd_pcm_lib_ioctl,
  734. .hw_params = snd_ca0106_pcm_hw_params_playback,
  735. .hw_free = snd_ca0106_pcm_hw_free_playback,
  736. .prepare = snd_ca0106_pcm_prepare_playback,
  737. .trigger = snd_ca0106_pcm_trigger_playback,
  738. .pointer = snd_ca0106_pcm_pointer_playback,
  739. };
  740. static snd_pcm_ops_t snd_ca0106_capture_0_ops = {
  741. .open = snd_ca0106_pcm_open_0_capture,
  742. .close = snd_ca0106_pcm_close_capture,
  743. .ioctl = snd_pcm_lib_ioctl,
  744. .hw_params = snd_ca0106_pcm_hw_params_capture,
  745. .hw_free = snd_ca0106_pcm_hw_free_capture,
  746. .prepare = snd_ca0106_pcm_prepare_capture,
  747. .trigger = snd_ca0106_pcm_trigger_capture,
  748. .pointer = snd_ca0106_pcm_pointer_capture,
  749. };
  750. static snd_pcm_ops_t snd_ca0106_capture_1_ops = {
  751. .open = snd_ca0106_pcm_open_1_capture,
  752. .close = snd_ca0106_pcm_close_capture,
  753. .ioctl = snd_pcm_lib_ioctl,
  754. .hw_params = snd_ca0106_pcm_hw_params_capture,
  755. .hw_free = snd_ca0106_pcm_hw_free_capture,
  756. .prepare = snd_ca0106_pcm_prepare_capture,
  757. .trigger = snd_ca0106_pcm_trigger_capture,
  758. .pointer = snd_ca0106_pcm_pointer_capture,
  759. };
  760. static snd_pcm_ops_t snd_ca0106_capture_2_ops = {
  761. .open = snd_ca0106_pcm_open_2_capture,
  762. .close = snd_ca0106_pcm_close_capture,
  763. .ioctl = snd_pcm_lib_ioctl,
  764. .hw_params = snd_ca0106_pcm_hw_params_capture,
  765. .hw_free = snd_ca0106_pcm_hw_free_capture,
  766. .prepare = snd_ca0106_pcm_prepare_capture,
  767. .trigger = snd_ca0106_pcm_trigger_capture,
  768. .pointer = snd_ca0106_pcm_pointer_capture,
  769. };
  770. static snd_pcm_ops_t snd_ca0106_capture_3_ops = {
  771. .open = snd_ca0106_pcm_open_3_capture,
  772. .close = snd_ca0106_pcm_close_capture,
  773. .ioctl = snd_pcm_lib_ioctl,
  774. .hw_params = snd_ca0106_pcm_hw_params_capture,
  775. .hw_free = snd_ca0106_pcm_hw_free_capture,
  776. .prepare = snd_ca0106_pcm_prepare_capture,
  777. .trigger = snd_ca0106_pcm_trigger_capture,
  778. .pointer = snd_ca0106_pcm_pointer_capture,
  779. };
  780. static snd_pcm_ops_t snd_ca0106_playback_center_lfe_ops = {
  781. .open = snd_ca0106_pcm_open_playback_center_lfe,
  782. .close = snd_ca0106_pcm_close_playback,
  783. .ioctl = snd_pcm_lib_ioctl,
  784. .hw_params = snd_ca0106_pcm_hw_params_playback,
  785. .hw_free = snd_ca0106_pcm_hw_free_playback,
  786. .prepare = snd_ca0106_pcm_prepare_playback,
  787. .trigger = snd_ca0106_pcm_trigger_playback,
  788. .pointer = snd_ca0106_pcm_pointer_playback,
  789. };
  790. static snd_pcm_ops_t snd_ca0106_playback_unknown_ops = {
  791. .open = snd_ca0106_pcm_open_playback_unknown,
  792. .close = snd_ca0106_pcm_close_playback,
  793. .ioctl = snd_pcm_lib_ioctl,
  794. .hw_params = snd_ca0106_pcm_hw_params_playback,
  795. .hw_free = snd_ca0106_pcm_hw_free_playback,
  796. .prepare = snd_ca0106_pcm_prepare_playback,
  797. .trigger = snd_ca0106_pcm_trigger_playback,
  798. .pointer = snd_ca0106_pcm_pointer_playback,
  799. };
  800. static snd_pcm_ops_t snd_ca0106_playback_rear_ops = {
  801. .open = snd_ca0106_pcm_open_playback_rear,
  802. .close = snd_ca0106_pcm_close_playback,
  803. .ioctl = snd_pcm_lib_ioctl,
  804. .hw_params = snd_ca0106_pcm_hw_params_playback,
  805. .hw_free = snd_ca0106_pcm_hw_free_playback,
  806. .prepare = snd_ca0106_pcm_prepare_playback,
  807. .trigger = snd_ca0106_pcm_trigger_playback,
  808. .pointer = snd_ca0106_pcm_pointer_playback,
  809. };
  810. static unsigned short snd_ca0106_ac97_read(ac97_t *ac97,
  811. unsigned short reg)
  812. {
  813. ca0106_t *emu = ac97->private_data;
  814. unsigned long flags;
  815. unsigned short val;
  816. spin_lock_irqsave(&emu->emu_lock, flags);
  817. outb(reg, emu->port + AC97ADDRESS);
  818. val = inw(emu->port + AC97DATA);
  819. spin_unlock_irqrestore(&emu->emu_lock, flags);
  820. return val;
  821. }
  822. static void snd_ca0106_ac97_write(ac97_t *ac97,
  823. unsigned short reg, unsigned short val)
  824. {
  825. ca0106_t *emu = ac97->private_data;
  826. unsigned long flags;
  827. spin_lock_irqsave(&emu->emu_lock, flags);
  828. outb(reg, emu->port + AC97ADDRESS);
  829. outw(val, emu->port + AC97DATA);
  830. spin_unlock_irqrestore(&emu->emu_lock, flags);
  831. }
  832. static int snd_ca0106_ac97(ca0106_t *chip)
  833. {
  834. ac97_bus_t *pbus;
  835. ac97_template_t ac97;
  836. int err;
  837. static ac97_bus_ops_t ops = {
  838. .write = snd_ca0106_ac97_write,
  839. .read = snd_ca0106_ac97_read,
  840. };
  841. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  842. return err;
  843. pbus->no_vra = 1; /* we don't need VRA */
  844. memset(&ac97, 0, sizeof(ac97));
  845. ac97.private_data = chip;
  846. ac97.scaps = AC97_SCAP_NO_SPDIF;
  847. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  848. }
  849. static int snd_ca0106_free(ca0106_t *chip)
  850. {
  851. if (chip->res_port != NULL) { /* avoid access to already used hardware */
  852. // disable interrupts
  853. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  854. outl(0, chip->port + INTE);
  855. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  856. udelay(1000);
  857. // disable audio
  858. //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
  859. outl(0, chip->port + HCFG);
  860. /* FIXME: We need to stop and DMA transfers here.
  861. * But as I am not sure how yet, we cannot from the dma pages.
  862. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  863. */
  864. }
  865. // release the data
  866. #if 1
  867. if (chip->buffer.area)
  868. snd_dma_free_pages(&chip->buffer);
  869. #endif
  870. // release the i/o port
  871. if (chip->res_port) {
  872. release_resource(chip->res_port);
  873. kfree_nocheck(chip->res_port);
  874. }
  875. // release the irq
  876. if (chip->irq >= 0)
  877. free_irq(chip->irq, (void *)chip);
  878. pci_disable_device(chip->pci);
  879. kfree(chip);
  880. return 0;
  881. }
  882. static int snd_ca0106_dev_free(snd_device_t *device)
  883. {
  884. ca0106_t *chip = device->device_data;
  885. return snd_ca0106_free(chip);
  886. }
  887. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
  888. struct pt_regs *regs)
  889. {
  890. unsigned int status;
  891. ca0106_t *chip = dev_id;
  892. int i;
  893. int mask;
  894. unsigned int stat76;
  895. ca0106_channel_t *pchannel;
  896. spin_lock(&chip->emu_lock);
  897. status = inl(chip->port + IPR);
  898. // call updater, unlock before it
  899. spin_unlock(&chip->emu_lock);
  900. if (! status)
  901. return IRQ_NONE;
  902. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  903. //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
  904. //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  905. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  906. for(i = 0; i < 4; i++) {
  907. pchannel = &(chip->playback_channels[i]);
  908. if(stat76 & mask) {
  909. /* FIXME: Select the correct substream for period elapsed */
  910. if(pchannel->use) {
  911. snd_pcm_period_elapsed(pchannel->epcm->substream);
  912. //printk(KERN_INFO "interrupt [%d] used\n", i);
  913. }
  914. }
  915. //printk(KERN_INFO "channel=%p\n",pchannel);
  916. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  917. mask <<= 1;
  918. }
  919. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  920. for(i = 0; i < 4; i++) {
  921. pchannel = &(chip->capture_channels[i]);
  922. if(stat76 & mask) {
  923. /* FIXME: Select the correct substream for period elapsed */
  924. if(pchannel->use) {
  925. snd_pcm_period_elapsed(pchannel->epcm->substream);
  926. //printk(KERN_INFO "interrupt [%d] used\n", i);
  927. }
  928. }
  929. //printk(KERN_INFO "channel=%p\n",pchannel);
  930. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  931. mask <<= 1;
  932. }
  933. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  934. spin_lock(&chip->emu_lock);
  935. // acknowledge the interrupt if necessary
  936. outl(status, chip->port+IPR);
  937. spin_unlock(&chip->emu_lock);
  938. return IRQ_HANDLED;
  939. }
  940. static void snd_ca0106_pcm_free(snd_pcm_t *pcm)
  941. {
  942. ca0106_t *emu = pcm->private_data;
  943. emu->pcm = NULL;
  944. snd_pcm_lib_preallocate_free_for_all(pcm);
  945. }
  946. static int __devinit snd_ca0106_pcm(ca0106_t *emu, int device, snd_pcm_t **rpcm)
  947. {
  948. snd_pcm_t *pcm;
  949. snd_pcm_substream_t *substream;
  950. int err;
  951. if (rpcm)
  952. *rpcm = NULL;
  953. if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
  954. return err;
  955. pcm->private_data = emu;
  956. pcm->private_free = snd_ca0106_pcm_free;
  957. switch (device) {
  958. case 0:
  959. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  960. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  961. break;
  962. case 1:
  963. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  964. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  965. break;
  966. case 2:
  967. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  968. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  969. break;
  970. case 3:
  971. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  972. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  973. break;
  974. }
  975. pcm->info_flags = 0;
  976. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  977. strcpy(pcm->name, "CA0106");
  978. emu->pcm = pcm;
  979. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  980. substream;
  981. substream = substream->next) {
  982. if ((err = snd_pcm_lib_preallocate_pages(substream,
  983. SNDRV_DMA_TYPE_DEV,
  984. snd_dma_pci_data(emu->pci),
  985. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  986. return err;
  987. }
  988. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  989. substream;
  990. substream = substream->next) {
  991. if ((err = snd_pcm_lib_preallocate_pages(substream,
  992. SNDRV_DMA_TYPE_DEV,
  993. snd_dma_pci_data(emu->pci),
  994. 64*1024, 64*1024)) < 0)
  995. return err;
  996. }
  997. if (rpcm)
  998. *rpcm = pcm;
  999. return 0;
  1000. }
  1001. static int __devinit snd_ca0106_create(snd_card_t *card,
  1002. struct pci_dev *pci,
  1003. ca0106_t **rchip)
  1004. {
  1005. ca0106_t *chip;
  1006. ca0106_details_t *c;
  1007. int err;
  1008. int ch;
  1009. static snd_device_ops_t ops = {
  1010. .dev_free = snd_ca0106_dev_free,
  1011. };
  1012. *rchip = NULL;
  1013. if ((err = pci_enable_device(pci)) < 0)
  1014. return err;
  1015. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  1016. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1017. printk(KERN_ERR "error to set 32bit mask DMA\n");
  1018. pci_disable_device(pci);
  1019. return -ENXIO;
  1020. }
  1021. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  1022. if (chip == NULL) {
  1023. pci_disable_device(pci);
  1024. return -ENOMEM;
  1025. }
  1026. chip->card = card;
  1027. chip->pci = pci;
  1028. chip->irq = -1;
  1029. spin_lock_init(&chip->emu_lock);
  1030. chip->port = pci_resource_start(pci, 0);
  1031. if ((chip->res_port = request_region(chip->port, 0x20,
  1032. "snd_ca0106")) == NULL) {
  1033. snd_ca0106_free(chip);
  1034. printk(KERN_ERR "cannot allocate the port\n");
  1035. return -EBUSY;
  1036. }
  1037. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1038. SA_INTERRUPT|SA_SHIRQ, "snd_ca0106",
  1039. (void *)chip)) {
  1040. snd_ca0106_free(chip);
  1041. printk(KERN_ERR "cannot grab irq\n");
  1042. return -EBUSY;
  1043. }
  1044. chip->irq = pci->irq;
  1045. /* This stores the periods table. */
  1046. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
  1047. snd_ca0106_free(chip);
  1048. return -ENOMEM;
  1049. }
  1050. pci_set_master(pci);
  1051. /* read revision & serial */
  1052. pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
  1053. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1054. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1055. #if 1
  1056. printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  1057. chip->revision, chip->serial);
  1058. #endif
  1059. strcpy(card->driver, "CA0106");
  1060. strcpy(card->shortname, "CA0106");
  1061. for (c=ca0106_chip_details; c->serial; c++) {
  1062. if (c->serial == chip->serial) break;
  1063. }
  1064. chip->details = c;
  1065. sprintf(card->longname, "%s at 0x%lx irq %i",
  1066. c->name, chip->port, chip->irq);
  1067. outl(0, chip->port + INTE);
  1068. /*
  1069. * Init to 0x02109204 :
  1070. * Clock accuracy = 0 (1000ppm)
  1071. * Sample Rate = 2 (48kHz)
  1072. * Audio Channel = 1 (Left of 2)
  1073. * Source Number = 0 (Unspecified)
  1074. * Generation Status = 1 (Original for Cat Code 12)
  1075. * Cat Code = 12 (Digital Signal Mixer)
  1076. * Mode = 0 (Mode 0)
  1077. * Emphasis = 0 (None)
  1078. * CP = 1 (Copyright unasserted)
  1079. * AN = 0 (Audio data)
  1080. * P = 0 (Consumer)
  1081. */
  1082. snd_ca0106_ptr_write(chip, SPCS0, 0,
  1083. chip->spdif_bits[0] =
  1084. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1085. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1086. SPCS_GENERATIONSTATUS | 0x00001200 |
  1087. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1088. /* Only SPCS1 has been tested */
  1089. snd_ca0106_ptr_write(chip, SPCS1, 0,
  1090. chip->spdif_bits[1] =
  1091. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1092. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1093. SPCS_GENERATIONSTATUS | 0x00001200 |
  1094. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1095. snd_ca0106_ptr_write(chip, SPCS2, 0,
  1096. chip->spdif_bits[2] =
  1097. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1098. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1099. SPCS_GENERATIONSTATUS | 0x00001200 |
  1100. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1101. snd_ca0106_ptr_write(chip, SPCS3, 0,
  1102. chip->spdif_bits[3] =
  1103. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1104. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1105. SPCS_GENERATIONSTATUS | 0x00001200 |
  1106. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1107. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1108. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1109. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1110. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1111. outw(0x8000, chip->port + AC97DATA);
  1112. #if 0
  1113. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1114. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1115. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1116. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1117. #endif
  1118. //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
  1119. /* Analog or Digital output */
  1120. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1121. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
  1122. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1123. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1124. //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
  1125. //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
  1126. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1127. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
  1128. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
  1129. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1130. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1131. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1132. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1133. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1134. for(ch = 0; ch < 4; ch++) {
  1135. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
  1136. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1137. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
  1138. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
  1139. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
  1140. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
  1141. }
  1142. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
  1143. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1144. if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
  1145. /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
  1146. outl(0x0, chip->port+GPIO);
  1147. //outl(0x00f0e000, chip->port+GPIO); /* Analog */
  1148. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1149. } else {
  1150. outl(0x0, chip->port+GPIO);
  1151. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1152. //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
  1153. }
  1154. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1155. //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  1156. //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1157. //outl(0x00000009, chip->port+HCFG);
  1158. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
  1159. if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  1160. snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
  1161. }
  1162. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  1163. chip, &ops)) < 0) {
  1164. snd_ca0106_free(chip);
  1165. return err;
  1166. }
  1167. *rchip = chip;
  1168. return 0;
  1169. }
  1170. static int __devinit snd_ca0106_probe(struct pci_dev *pci,
  1171. const struct pci_device_id *pci_id)
  1172. {
  1173. static int dev;
  1174. snd_card_t *card;
  1175. ca0106_t *chip;
  1176. int err;
  1177. if (dev >= SNDRV_CARDS)
  1178. return -ENODEV;
  1179. if (!enable[dev]) {
  1180. dev++;
  1181. return -ENOENT;
  1182. }
  1183. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1184. if (card == NULL)
  1185. return -ENOMEM;
  1186. if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
  1187. snd_card_free(card);
  1188. return err;
  1189. }
  1190. if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
  1191. snd_card_free(card);
  1192. return err;
  1193. }
  1194. if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
  1195. snd_card_free(card);
  1196. return err;
  1197. }
  1198. if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
  1199. snd_card_free(card);
  1200. return err;
  1201. }
  1202. if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
  1203. snd_card_free(card);
  1204. return err;
  1205. }
  1206. if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
  1207. if ((err = snd_ca0106_ac97(chip)) < 0) {
  1208. snd_card_free(card);
  1209. return err;
  1210. }
  1211. }
  1212. if ((err = snd_ca0106_mixer(chip)) < 0) {
  1213. snd_card_free(card);
  1214. return err;
  1215. }
  1216. snd_ca0106_proc_init(chip);
  1217. if ((err = snd_card_register(card)) < 0) {
  1218. snd_card_free(card);
  1219. return err;
  1220. }
  1221. pci_set_drvdata(pci, card);
  1222. dev++;
  1223. return 0;
  1224. }
  1225. static void __devexit snd_ca0106_remove(struct pci_dev *pci)
  1226. {
  1227. snd_card_free(pci_get_drvdata(pci));
  1228. pci_set_drvdata(pci, NULL);
  1229. }
  1230. // PCI IDs
  1231. static struct pci_device_id snd_ca0106_ids[] = {
  1232. { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
  1233. { 0, }
  1234. };
  1235. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1236. // pci_driver definition
  1237. static struct pci_driver driver = {
  1238. .name = "CA0106",
  1239. .id_table = snd_ca0106_ids,
  1240. .probe = snd_ca0106_probe,
  1241. .remove = __devexit_p(snd_ca0106_remove),
  1242. };
  1243. // initialization of the module
  1244. static int __init alsa_card_ca0106_init(void)
  1245. {
  1246. int err;
  1247. if ((err = pci_register_driver(&driver)) > 0)
  1248. return err;
  1249. return 0;
  1250. }
  1251. // clean up the module
  1252. static void __exit alsa_card_ca0106_exit(void)
  1253. {
  1254. pci_unregister_driver(&driver);
  1255. }
  1256. module_init(alsa_card_ca0106_init)
  1257. module_exit(alsa_card_ca0106_exit)