ca0106_main.c 46 KB

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