ca0106_main.c 42 KB

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