ca0106_main.c 43 KB

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