ca0106_main.c 45 KB

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