ca0106_main.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
  3. * Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
  4. * Version: 0.0.23
  5. *
  6. * FEATURES currently supported:
  7. * Front, Rear and Center/LFE.
  8. * Surround40 and Surround51.
  9. * Capture from MIC an LINE IN input.
  10. * SPDIF digital playback of PCM stereo and AC3/DTS works.
  11. * (One can use a standard mono mini-jack to one RCA plugs cable.
  12. * or one can use a standard stereo mini-jack to two RCA plugs cable.
  13. * Plug one of the RCA plugs into the Coax input of the external decoder/receiver.)
  14. * ( In theory one could output 3 different AC3 streams at once, to 3 different SPDIF outputs. )
  15. * Notes on how to capture sound:
  16. * The AC97 is used in the PLAYBACK direction.
  17. * The output from the AC97 chip, instead of reaching the speakers, is fed into the Philips 1361T ADC.
  18. * So, to record from the MIC, set the MIC Playback volume to max,
  19. * unmute the MIC and turn up the MASTER Playback volume.
  20. * So, to prevent feedback when capturing, minimise the "Capture feedback into Playback" volume.
  21. *
  22. * The only playback controls that currently do anything are: -
  23. * Analog Front
  24. * Analog Rear
  25. * Analog Center/LFE
  26. * SPDIF Front
  27. * SPDIF Rear
  28. * SPDIF Center/LFE
  29. *
  30. * For capture from Mic in or Line in.
  31. * Digital/Analog ( switch must be in Analog mode for CAPTURE. )
  32. *
  33. * CAPTURE feedback into PLAYBACK
  34. *
  35. * Changelog:
  36. * Support interrupts per period.
  37. * Removed noise from Center/LFE channel when in Analog mode.
  38. * Rename and remove mixer controls.
  39. * 0.0.6
  40. * Use separate card based DMA buffer for periods table list.
  41. * 0.0.7
  42. * Change remove and rename ctrls into lists.
  43. * 0.0.8
  44. * Try to fix capture sources.
  45. * 0.0.9
  46. * Fix AC3 output.
  47. * Enable S32_LE format support.
  48. * 0.0.10
  49. * Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
  50. * 0.0.11
  51. * Add Model name recognition.
  52. * 0.0.12
  53. * Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
  54. * Remove redundent "voice" handling.
  55. * 0.0.13
  56. * Single trigger call for multi channels.
  57. * 0.0.14
  58. * Set limits based on what the sound card hardware can do.
  59. * playback periods_min=2, periods_max=8
  60. * capture hw constraints require period_size = n * 64 bytes.
  61. * playback hw constraints require period_size = n * 64 bytes.
  62. * 0.0.15
  63. * Minor updates.
  64. * 0.0.16
  65. * Implement 192000 sample rate.
  66. * 0.0.17
  67. * Add support for SB0410 and SB0413.
  68. * 0.0.18
  69. * Modified Copyright message.
  70. * 0.0.19
  71. * Finally fix support for SB Live 24 bit. SB0410 and SB0413.
  72. * The output codec needs resetting, otherwise all output is muted.
  73. * 0.0.20
  74. * Merge "pci_disable_device(pci);" fixes.
  75. * 0.0.21
  76. * Add 4 capture channels. (SPDIF only comes in on channel 0. )
  77. * Add SPDIF capture using optional digital I/O module for SB Live 24bit. (Analog capture does not yet work.)
  78. * 0.0.22
  79. * Add support for MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97. From kiksen, bug #901
  80. * 0.0.23
  81. * Implement support for Line-in capture on SB Live 24bit.
  82. *
  83. * BUGS:
  84. * Some stability problems when unloading the snd-ca0106 kernel module.
  85. * --
  86. *
  87. * TODO:
  88. * 4 Capture channels, only one implemented so far.
  89. * Other capture rates apart from 48khz not implemented.
  90. * MIDI
  91. * --
  92. * GENERAL INFO:
  93. * Model: SB0310
  94. * P17 Chip: CA0106-DAT
  95. * AC97 Codec: STAC 9721
  96. * ADC: Philips 1361T (Stereo 24bit)
  97. * DAC: WM8746EDS (6-channel, 24bit, 192Khz)
  98. *
  99. * GENERAL INFO:
  100. * Model: SB0410
  101. * P17 Chip: CA0106-DAT
  102. * AC97 Codec: None
  103. * ADC: WM8775EDS (4 Channel)
  104. * DAC: CS4382 (114 dB, 24-Bit, 192 kHz, 8-Channel D/A Converter with DSD Support)
  105. * SPDIF Out control switches between Mic in and SPDIF out.
  106. * No sound out or mic input working yet.
  107. *
  108. * GENERAL INFO:
  109. * Model: SB0413
  110. * P17 Chip: CA0106-DAT
  111. * AC97 Codec: None.
  112. * ADC: Unknown
  113. * DAC: Unknown
  114. * Trying to handle it like the SB0410.
  115. *
  116. * This code was initally based on code from ALSA's emu10k1x.c which is:
  117. * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
  118. *
  119. * This program is free software; you can redistribute it and/or modify
  120. * it under the terms of the GNU General Public License as published by
  121. * the Free Software Foundation; either version 2 of the License, or
  122. * (at your option) any later version.
  123. *
  124. * This program is distributed in the hope that it will be useful,
  125. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  126. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  127. * GNU General Public License for more details.
  128. *
  129. * You should have received a copy of the GNU General Public License
  130. * along with this program; if not, write to the Free Software
  131. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  132. *
  133. */
  134. #include <sound/driver.h>
  135. #include <linux/delay.h>
  136. #include <linux/init.h>
  137. #include <linux/interrupt.h>
  138. #include <linux/pci.h>
  139. #include <linux/slab.h>
  140. #include <linux/moduleparam.h>
  141. #include <linux/dma-mapping.h>
  142. #include <sound/core.h>
  143. #include <sound/initval.h>
  144. #include <sound/pcm.h>
  145. #include <sound/ac97_codec.h>
  146. #include <sound/info.h>
  147. MODULE_AUTHOR("James Courtier-Dutton <James@superbug.demon.co.uk>");
  148. MODULE_DESCRIPTION("CA0106");
  149. MODULE_LICENSE("GPL");
  150. MODULE_SUPPORTED_DEVICE("{{Creative,SB CA0106 chip}}");
  151. // module parameters (see "Module Parameters")
  152. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  153. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  154. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  155. module_param_array(index, int, NULL, 0444);
  156. MODULE_PARM_DESC(index, "Index value for the CA0106 soundcard.");
  157. module_param_array(id, charp, NULL, 0444);
  158. MODULE_PARM_DESC(id, "ID string for the CA0106 soundcard.");
  159. module_param_array(enable, bool, NULL, 0444);
  160. MODULE_PARM_DESC(enable, "Enable the CA0106 soundcard.");
  161. #include "ca0106.h"
  162. static struct snd_ca0106_details ca0106_chip_details[] = {
  163. /* AudigyLS[SB0310] */
  164. { .serial = 0x10021102,
  165. .name = "AudigyLS [SB0310]",
  166. .ac97 = 1 } ,
  167. /* Unknown AudigyLS that also says SB0310 on it */
  168. { .serial = 0x10051102,
  169. .name = "AudigyLS [SB0310b]",
  170. .ac97 = 1 } ,
  171. /* New Sound Blaster Live! 7.1 24bit. This does not have an AC97. 53SB041000001 */
  172. { .serial = 0x10061102,
  173. .name = "Live! 7.1 24bit [SB0410]",
  174. .gpio_type = 1,
  175. .i2c_adc = 1 } ,
  176. /* New Dell Sound Blaster Live! 7.1 24bit. This does not have an AC97. */
  177. { .serial = 0x10071102,
  178. .name = "Live! 7.1 24bit [SB0413]",
  179. .gpio_type = 1,
  180. .i2c_adc = 1 } ,
  181. /* New Audigy SE. Has a different DAC. */
  182. /* SB0570:
  183. * CTRL:CA0106-DAT
  184. * ADC: WM8775EDS
  185. * DAC: WM8768GEDS
  186. */
  187. { .serial = 0x100a1102,
  188. .name = "Audigy SE [SB0570]",
  189. .gpio_type = 1,
  190. .i2c_adc = 1,
  191. .spi_dac = 1 } ,
  192. /* MSI K8N Diamond Motherboard with onboard SB Live 24bit without AC97 */
  193. /* SB0438
  194. * CTRL:CA0106-DAT
  195. * ADC: WM8775SEDS
  196. * DAC: CS4382-KQZ
  197. */
  198. { .serial = 0x10091462,
  199. .name = "MSI K8N Diamond MB [SB0438]",
  200. .gpio_type = 2,
  201. .i2c_adc = 1 } ,
  202. /* Shuttle XPC SD31P which has an onboard Creative Labs
  203. * Sound Blaster Live! 24-bit EAX
  204. * high-definition 7.1 audio processor".
  205. * Added using info from andrewvegan in alsa bug #1298
  206. */
  207. { .serial = 0x30381297,
  208. .name = "Shuttle XPC SD31P [SD31P]",
  209. .gpio_type = 1,
  210. .i2c_adc = 1 } ,
  211. /* Shuttle XPC SD11G5 which has an onboard Creative Labs
  212. * Sound Blaster Live! 24-bit EAX
  213. * high-definition 7.1 audio processor".
  214. * Fixes ALSA bug#1600
  215. */
  216. { .serial = 0x30411297,
  217. .name = "Shuttle XPC SD11G5 [SD11G5]",
  218. .gpio_type = 1,
  219. .i2c_adc = 1 } ,
  220. { .serial = 0,
  221. .name = "AudigyLS [Unknown]" }
  222. };
  223. /* hardware definition */
  224. static struct snd_pcm_hardware snd_ca0106_playback_hw = {
  225. .info = (SNDRV_PCM_INFO_MMAP |
  226. SNDRV_PCM_INFO_INTERLEAVED |
  227. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  228. SNDRV_PCM_INFO_MMAP_VALID),
  229. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  230. .rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
  231. SNDRV_PCM_RATE_192000),
  232. .rate_min = 48000,
  233. .rate_max = 192000,
  234. .channels_min = 2, //1,
  235. .channels_max = 2, //6,
  236. .buffer_bytes_max = ((65536 - 64) * 8),
  237. .period_bytes_min = 64,
  238. .period_bytes_max = (65536 - 64),
  239. .periods_min = 2,
  240. .periods_max = 8,
  241. .fifo_size = 0,
  242. };
  243. static struct snd_pcm_hardware snd_ca0106_capture_hw = {
  244. .info = (SNDRV_PCM_INFO_MMAP |
  245. SNDRV_PCM_INFO_INTERLEAVED |
  246. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  247. SNDRV_PCM_INFO_MMAP_VALID),
  248. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
  249. .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  250. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000),
  251. .rate_min = 44100,
  252. .rate_max = 192000,
  253. .channels_min = 2,
  254. .channels_max = 2,
  255. .buffer_bytes_max = ((65536 - 64) * 8),
  256. .period_bytes_min = 64,
  257. .period_bytes_max = (65536 - 64),
  258. .periods_min = 2,
  259. .periods_max = 2,
  260. .fifo_size = 0,
  261. };
  262. unsigned int snd_ca0106_ptr_read(struct snd_ca0106 * emu,
  263. unsigned int reg,
  264. unsigned int chn)
  265. {
  266. unsigned long flags;
  267. unsigned int regptr, val;
  268. regptr = (reg << 16) | chn;
  269. spin_lock_irqsave(&emu->emu_lock, flags);
  270. outl(regptr, emu->port + PTR);
  271. val = inl(emu->port + DATA);
  272. spin_unlock_irqrestore(&emu->emu_lock, flags);
  273. return val;
  274. }
  275. void snd_ca0106_ptr_write(struct snd_ca0106 *emu,
  276. unsigned int reg,
  277. unsigned int chn,
  278. unsigned int data)
  279. {
  280. unsigned int regptr;
  281. unsigned long flags;
  282. regptr = (reg << 16) | chn;
  283. spin_lock_irqsave(&emu->emu_lock, flags);
  284. outl(regptr, emu->port + PTR);
  285. outl(data, emu->port + DATA);
  286. spin_unlock_irqrestore(&emu->emu_lock, flags);
  287. }
  288. int snd_ca0106_spi_write(struct snd_ca0106 * emu,
  289. unsigned int data)
  290. {
  291. unsigned int reset, set;
  292. unsigned int reg, tmp;
  293. int n, result;
  294. reg = SPI;
  295. if (data > 0xffff) /* Only 16bit values allowed */
  296. return 1;
  297. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  298. reset = (tmp & ~0x3ffff) | 0x20000; /* Set xxx20000 */
  299. set = reset | 0x10000; /* Set xxx1xxxx */
  300. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  301. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* write post */
  302. snd_ca0106_ptr_write(emu, reg, 0, set | data);
  303. result = 1;
  304. /* Wait for status bit to return to 0 */
  305. for (n = 0; n < 100; n++) {
  306. udelay(10);
  307. tmp = snd_ca0106_ptr_read(emu, reg, 0);
  308. if (!(tmp & 0x10000)) {
  309. result = 0;
  310. break;
  311. }
  312. }
  313. if (result) /* Timed out */
  314. return 1;
  315. snd_ca0106_ptr_write(emu, reg, 0, reset | data);
  316. tmp = snd_ca0106_ptr_read(emu, reg, 0); /* Write post */
  317. return 0;
  318. }
  319. /* The ADC does not support i2c read, so only write is implemented */
  320. int snd_ca0106_i2c_write(struct snd_ca0106 *emu,
  321. u32 reg,
  322. u32 value)
  323. {
  324. u32 tmp;
  325. int timeout = 0;
  326. int status;
  327. int retry;
  328. if ((reg > 0x7f) || (value > 0x1ff)) {
  329. snd_printk(KERN_ERR "i2c_write: invalid values.\n");
  330. return -EINVAL;
  331. }
  332. tmp = reg << 25 | value << 16;
  333. // snd_printk("I2C-write:reg=0x%x, value=0x%x\n", reg, value);
  334. /* Not sure what this I2C channel controls. */
  335. /* snd_ca0106_ptr_write(emu, I2C_D0, 0, tmp); */
  336. /* This controls the I2C connected to the WM8775 ADC Codec */
  337. snd_ca0106_ptr_write(emu, I2C_D1, 0, tmp);
  338. for (retry = 0; retry < 10; retry++) {
  339. /* Send the data to i2c */
  340. //tmp = snd_ca0106_ptr_read(emu, I2C_A, 0);
  341. //tmp = tmp & ~(I2C_A_ADC_READ|I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD_MASK);
  342. tmp = 0;
  343. tmp = tmp | (I2C_A_ADC_LAST|I2C_A_ADC_START|I2C_A_ADC_ADD);
  344. snd_ca0106_ptr_write(emu, I2C_A, 0, tmp);
  345. /* Wait till the transaction ends */
  346. while (1) {
  347. status = snd_ca0106_ptr_read(emu, I2C_A, 0);
  348. //snd_printk("I2C:status=0x%x\n", status);
  349. timeout++;
  350. if ((status & I2C_A_ADC_START) == 0)
  351. break;
  352. if (timeout > 1000)
  353. break;
  354. }
  355. //Read back and see if the transaction is successful
  356. if ((status & I2C_A_ADC_ABORT) == 0)
  357. break;
  358. }
  359. if (retry == 10) {
  360. snd_printk(KERN_ERR "Writing to ADC failed!\n");
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static void snd_ca0106_intr_enable(struct snd_ca0106 *emu, unsigned int intrenb)
  366. {
  367. unsigned long flags;
  368. unsigned int enable;
  369. spin_lock_irqsave(&emu->emu_lock, flags);
  370. enable = inl(emu->port + INTE) | intrenb;
  371. outl(enable, emu->port + INTE);
  372. spin_unlock_irqrestore(&emu->emu_lock, flags);
  373. }
  374. static void snd_ca0106_intr_disable(struct snd_ca0106 *emu, unsigned int intrenb)
  375. {
  376. unsigned long flags;
  377. unsigned int enable;
  378. spin_lock_irqsave(&emu->emu_lock, flags);
  379. enable = inl(emu->port + INTE) & ~intrenb;
  380. outl(enable, emu->port + INTE);
  381. spin_unlock_irqrestore(&emu->emu_lock, flags);
  382. }
  383. static void snd_ca0106_pcm_free_substream(struct snd_pcm_runtime *runtime)
  384. {
  385. kfree(runtime->private_data);
  386. }
  387. /* open_playback callback */
  388. static int snd_ca0106_pcm_open_playback_channel(struct snd_pcm_substream *substream,
  389. int channel_id)
  390. {
  391. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  392. struct snd_ca0106_channel *channel = &(chip->playback_channels[channel_id]);
  393. struct snd_ca0106_pcm *epcm;
  394. struct snd_pcm_runtime *runtime = substream->runtime;
  395. int err;
  396. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  397. if (epcm == NULL)
  398. return -ENOMEM;
  399. epcm->emu = chip;
  400. epcm->substream = substream;
  401. epcm->channel_id=channel_id;
  402. runtime->private_data = epcm;
  403. runtime->private_free = snd_ca0106_pcm_free_substream;
  404. runtime->hw = snd_ca0106_playback_hw;
  405. channel->emu = chip;
  406. channel->number = channel_id;
  407. channel->use = 1;
  408. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  409. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  410. channel->epcm = epcm;
  411. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  412. return err;
  413. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  414. return err;
  415. return 0;
  416. }
  417. /* close callback */
  418. static int snd_ca0106_pcm_close_playback(struct snd_pcm_substream *substream)
  419. {
  420. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  421. struct snd_pcm_runtime *runtime = substream->runtime;
  422. struct snd_ca0106_pcm *epcm = runtime->private_data;
  423. chip->playback_channels[epcm->channel_id].use = 0;
  424. /* FIXME: maybe zero others */
  425. return 0;
  426. }
  427. static int snd_ca0106_pcm_open_playback_front(struct snd_pcm_substream *substream)
  428. {
  429. return snd_ca0106_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
  430. }
  431. static int snd_ca0106_pcm_open_playback_center_lfe(struct snd_pcm_substream *substream)
  432. {
  433. return snd_ca0106_pcm_open_playback_channel(substream, PCM_CENTER_LFE_CHANNEL);
  434. }
  435. static int snd_ca0106_pcm_open_playback_unknown(struct snd_pcm_substream *substream)
  436. {
  437. return snd_ca0106_pcm_open_playback_channel(substream, PCM_UNKNOWN_CHANNEL);
  438. }
  439. static int snd_ca0106_pcm_open_playback_rear(struct snd_pcm_substream *substream)
  440. {
  441. return snd_ca0106_pcm_open_playback_channel(substream, PCM_REAR_CHANNEL);
  442. }
  443. /* open_capture callback */
  444. static int snd_ca0106_pcm_open_capture_channel(struct snd_pcm_substream *substream,
  445. int channel_id)
  446. {
  447. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  448. struct snd_ca0106_channel *channel = &(chip->capture_channels[channel_id]);
  449. struct snd_ca0106_pcm *epcm;
  450. struct snd_pcm_runtime *runtime = substream->runtime;
  451. int err;
  452. epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
  453. if (epcm == NULL) {
  454. snd_printk(KERN_ERR "open_capture_channel: failed epcm alloc\n");
  455. return -ENOMEM;
  456. }
  457. epcm->emu = chip;
  458. epcm->substream = substream;
  459. epcm->channel_id=channel_id;
  460. runtime->private_data = epcm;
  461. runtime->private_free = snd_ca0106_pcm_free_substream;
  462. runtime->hw = snd_ca0106_capture_hw;
  463. channel->emu = chip;
  464. channel->number = channel_id;
  465. channel->use = 1;
  466. //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
  467. //channel->interrupt = snd_ca0106_pcm_channel_interrupt;
  468. channel->epcm = epcm;
  469. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  470. return err;
  471. //snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hw_constraints_capture_period_sizes);
  472. if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
  473. return err;
  474. return 0;
  475. }
  476. /* close callback */
  477. static int snd_ca0106_pcm_close_capture(struct snd_pcm_substream *substream)
  478. {
  479. struct snd_ca0106 *chip = snd_pcm_substream_chip(substream);
  480. struct snd_pcm_runtime *runtime = substream->runtime;
  481. struct snd_ca0106_pcm *epcm = runtime->private_data;
  482. chip->capture_channels[epcm->channel_id].use = 0;
  483. /* FIXME: maybe zero others */
  484. return 0;
  485. }
  486. static int snd_ca0106_pcm_open_0_capture(struct snd_pcm_substream *substream)
  487. {
  488. return snd_ca0106_pcm_open_capture_channel(substream, 0);
  489. }
  490. static int snd_ca0106_pcm_open_1_capture(struct snd_pcm_substream *substream)
  491. {
  492. return snd_ca0106_pcm_open_capture_channel(substream, 1);
  493. }
  494. static int snd_ca0106_pcm_open_2_capture(struct snd_pcm_substream *substream)
  495. {
  496. return snd_ca0106_pcm_open_capture_channel(substream, 2);
  497. }
  498. static int snd_ca0106_pcm_open_3_capture(struct snd_pcm_substream *substream)
  499. {
  500. return snd_ca0106_pcm_open_capture_channel(substream, 3);
  501. }
  502. /* hw_params callback */
  503. static int snd_ca0106_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  504. struct snd_pcm_hw_params *hw_params)
  505. {
  506. return snd_pcm_lib_malloc_pages(substream,
  507. params_buffer_bytes(hw_params));
  508. }
  509. /* hw_free callback */
  510. static int snd_ca0106_pcm_hw_free_playback(struct snd_pcm_substream *substream)
  511. {
  512. return snd_pcm_lib_free_pages(substream);
  513. }
  514. /* hw_params callback */
  515. static int snd_ca0106_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  516. struct snd_pcm_hw_params *hw_params)
  517. {
  518. return snd_pcm_lib_malloc_pages(substream,
  519. params_buffer_bytes(hw_params));
  520. }
  521. /* hw_free callback */
  522. static int snd_ca0106_pcm_hw_free_capture(struct snd_pcm_substream *substream)
  523. {
  524. return snd_pcm_lib_free_pages(substream);
  525. }
  526. /* prepare playback callback */
  527. static int snd_ca0106_pcm_prepare_playback(struct snd_pcm_substream *substream)
  528. {
  529. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  530. struct snd_pcm_runtime *runtime = substream->runtime;
  531. struct snd_ca0106_pcm *epcm = runtime->private_data;
  532. int channel = epcm->channel_id;
  533. u32 *table_base = (u32 *)(emu->buffer.area+(8*16*channel));
  534. u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
  535. u32 hcfg_mask = HCFG_PLAYBACK_S32_LE;
  536. u32 hcfg_set = 0x00000000;
  537. u32 hcfg;
  538. u32 reg40_mask = 0x30000 << (channel<<1);
  539. u32 reg40_set = 0;
  540. u32 reg40;
  541. /* FIXME: Depending on mixer selection of SPDIF out or not, select the spdif rate or the DAC rate. */
  542. u32 reg71_mask = 0x03030000 ; /* Global. Set SPDIF rate. We only support 44100 to spdif, not to DAC. */
  543. u32 reg71_set = 0;
  544. u32 reg71;
  545. int i;
  546. //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));
  547. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  548. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  549. /* Rate can be set per channel. */
  550. /* reg40 control host to fifo */
  551. /* reg71 controls DAC rate. */
  552. switch (runtime->rate) {
  553. case 44100:
  554. reg40_set = 0x10000 << (channel<<1);
  555. reg71_set = 0x01010000;
  556. break;
  557. case 48000:
  558. reg40_set = 0;
  559. reg71_set = 0;
  560. break;
  561. case 96000:
  562. reg40_set = 0x20000 << (channel<<1);
  563. reg71_set = 0x02020000;
  564. break;
  565. case 192000:
  566. reg40_set = 0x30000 << (channel<<1);
  567. reg71_set = 0x03030000;
  568. break;
  569. default:
  570. reg40_set = 0;
  571. reg71_set = 0;
  572. break;
  573. }
  574. /* Format is a global setting */
  575. /* FIXME: Only let the first channel accessed set this. */
  576. switch (runtime->format) {
  577. case SNDRV_PCM_FORMAT_S16_LE:
  578. hcfg_set = 0;
  579. break;
  580. case SNDRV_PCM_FORMAT_S32_LE:
  581. hcfg_set = HCFG_PLAYBACK_S32_LE;
  582. break;
  583. default:
  584. hcfg_set = 0;
  585. break;
  586. }
  587. hcfg = inl(emu->port + HCFG) ;
  588. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  589. outl(hcfg, emu->port + HCFG);
  590. reg40 = snd_ca0106_ptr_read(emu, 0x40, 0);
  591. reg40 = (reg40 & ~reg40_mask) | reg40_set;
  592. snd_ca0106_ptr_write(emu, 0x40, 0, reg40);
  593. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  594. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  595. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  596. /* FIXME: Check emu->buffer.size before actually writing to it. */
  597. for(i=0; i < runtime->periods; i++) {
  598. table_base[i*2] = runtime->dma_addr + (i * period_size_bytes);
  599. table_base[i*2+1] = period_size_bytes << 16;
  600. }
  601. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_ADDR, channel, emu->buffer.addr+(8*16*channel));
  602. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
  603. snd_ca0106_ptr_write(emu, PLAYBACK_LIST_PTR, channel, 0);
  604. snd_ca0106_ptr_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
  605. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
  606. /* FIXME test what 0 bytes does. */
  607. snd_ca0106_ptr_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
  608. snd_ca0106_ptr_write(emu, PLAYBACK_POINTER, channel, 0);
  609. snd_ca0106_ptr_write(emu, 0x07, channel, 0x0);
  610. snd_ca0106_ptr_write(emu, 0x08, channel, 0);
  611. snd_ca0106_ptr_write(emu, PLAYBACK_MUTE, 0x0, 0x0); /* Unmute output */
  612. #if 0
  613. snd_ca0106_ptr_write(emu, SPCS0, 0,
  614. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  615. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  616. SPCS_GENERATIONSTATUS | 0x00001200 |
  617. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT );
  618. }
  619. #endif
  620. return 0;
  621. }
  622. /* prepare capture callback */
  623. static int snd_ca0106_pcm_prepare_capture(struct snd_pcm_substream *substream)
  624. {
  625. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  626. struct snd_pcm_runtime *runtime = substream->runtime;
  627. struct snd_ca0106_pcm *epcm = runtime->private_data;
  628. int channel = epcm->channel_id;
  629. u32 hcfg_mask = HCFG_CAPTURE_S32_LE;
  630. u32 hcfg_set = 0x00000000;
  631. u32 hcfg;
  632. u32 over_sampling=0x2;
  633. u32 reg71_mask = 0x0000c000 ; /* Global. Set ADC rate. */
  634. u32 reg71_set = 0;
  635. u32 reg71;
  636. //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));
  637. //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
  638. //snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->buffer.addr, emu->buffer.area, emu->buffer.bytes);
  639. /* reg71 controls ADC rate. */
  640. switch (runtime->rate) {
  641. case 44100:
  642. reg71_set = 0x00004000;
  643. break;
  644. case 48000:
  645. reg71_set = 0;
  646. break;
  647. case 96000:
  648. reg71_set = 0x00008000;
  649. over_sampling=0xa;
  650. break;
  651. case 192000:
  652. reg71_set = 0x0000c000;
  653. over_sampling=0xa;
  654. break;
  655. default:
  656. reg71_set = 0;
  657. break;
  658. }
  659. /* Format is a global setting */
  660. /* FIXME: Only let the first channel accessed set this. */
  661. switch (runtime->format) {
  662. case SNDRV_PCM_FORMAT_S16_LE:
  663. hcfg_set = 0;
  664. break;
  665. case SNDRV_PCM_FORMAT_S32_LE:
  666. hcfg_set = HCFG_CAPTURE_S32_LE;
  667. break;
  668. default:
  669. hcfg_set = 0;
  670. break;
  671. }
  672. hcfg = inl(emu->port + HCFG) ;
  673. hcfg = (hcfg & ~hcfg_mask) | hcfg_set;
  674. outl(hcfg, emu->port + HCFG);
  675. reg71 = snd_ca0106_ptr_read(emu, 0x71, 0);
  676. reg71 = (reg71 & ~reg71_mask) | reg71_set;
  677. snd_ca0106_ptr_write(emu, 0x71, 0, reg71);
  678. if (emu->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  679. snd_ca0106_i2c_write(emu, ADC_MASTER, over_sampling); /* Adjust the over sampler to better suit the capture rate. */
  680. }
  681. //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));
  682. snd_ca0106_ptr_write(emu, 0x13, channel, 0);
  683. snd_ca0106_ptr_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
  684. snd_ca0106_ptr_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
  685. snd_ca0106_ptr_write(emu, CAPTURE_POINTER, channel, 0);
  686. return 0;
  687. }
  688. /* trigger_playback callback */
  689. static int snd_ca0106_pcm_trigger_playback(struct snd_pcm_substream *substream,
  690. int cmd)
  691. {
  692. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  693. struct snd_pcm_runtime *runtime;
  694. struct snd_ca0106_pcm *epcm;
  695. int channel;
  696. int result = 0;
  697. struct list_head *pos;
  698. struct snd_pcm_substream *s;
  699. u32 basic = 0;
  700. u32 extended = 0;
  701. int running=0;
  702. switch (cmd) {
  703. case SNDRV_PCM_TRIGGER_START:
  704. running=1;
  705. break;
  706. case SNDRV_PCM_TRIGGER_STOP:
  707. default:
  708. running=0;
  709. break;
  710. }
  711. snd_pcm_group_for_each(pos, substream) {
  712. s = snd_pcm_group_substream_entry(pos);
  713. runtime = s->runtime;
  714. epcm = runtime->private_data;
  715. channel = epcm->channel_id;
  716. //snd_printk("channel=%d\n",channel);
  717. epcm->running = running;
  718. basic |= (0x1<<channel);
  719. extended |= (0x10<<channel);
  720. snd_pcm_trigger_done(s, substream);
  721. }
  722. //snd_printk("basic=0x%x, extended=0x%x\n",basic, extended);
  723. switch (cmd) {
  724. case SNDRV_PCM_TRIGGER_START:
  725. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (extended));
  726. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(basic));
  727. break;
  728. case SNDRV_PCM_TRIGGER_STOP:
  729. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
  730. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(extended));
  731. break;
  732. default:
  733. result = -EINVAL;
  734. break;
  735. }
  736. return result;
  737. }
  738. /* trigger_capture callback */
  739. static int snd_ca0106_pcm_trigger_capture(struct snd_pcm_substream *substream,
  740. int cmd)
  741. {
  742. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  743. struct snd_pcm_runtime *runtime = substream->runtime;
  744. struct snd_ca0106_pcm *epcm = runtime->private_data;
  745. int channel = epcm->channel_id;
  746. int result = 0;
  747. switch (cmd) {
  748. case SNDRV_PCM_TRIGGER_START:
  749. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
  750. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
  751. epcm->running = 1;
  752. break;
  753. case SNDRV_PCM_TRIGGER_STOP:
  754. snd_ca0106_ptr_write(emu, BASIC_INTERRUPT, 0, snd_ca0106_ptr_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
  755. snd_ca0106_ptr_write(emu, EXTENDED_INT_MASK, 0, snd_ca0106_ptr_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
  756. epcm->running = 0;
  757. break;
  758. default:
  759. result = -EINVAL;
  760. break;
  761. }
  762. return result;
  763. }
  764. /* pointer_playback callback */
  765. static snd_pcm_uframes_t
  766. snd_ca0106_pcm_pointer_playback(struct snd_pcm_substream *substream)
  767. {
  768. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  769. struct snd_pcm_runtime *runtime = substream->runtime;
  770. struct snd_ca0106_pcm *epcm = runtime->private_data;
  771. snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
  772. int channel = epcm->channel_id;
  773. if (!epcm->running)
  774. return 0;
  775. ptr3 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  776. ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  777. ptr4 = snd_ca0106_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
  778. if (ptr3 != ptr4) ptr1 = snd_ca0106_ptr_read(emu, PLAYBACK_POINTER, channel);
  779. ptr2 = bytes_to_frames(runtime, ptr1);
  780. ptr2+= (ptr4 >> 3) * runtime->period_size;
  781. ptr=ptr2;
  782. if (ptr >= runtime->buffer_size)
  783. ptr -= runtime->buffer_size;
  784. //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);
  785. return ptr;
  786. }
  787. /* pointer_capture callback */
  788. static snd_pcm_uframes_t
  789. snd_ca0106_pcm_pointer_capture(struct snd_pcm_substream *substream)
  790. {
  791. struct snd_ca0106 *emu = snd_pcm_substream_chip(substream);
  792. struct snd_pcm_runtime *runtime = substream->runtime;
  793. struct snd_ca0106_pcm *epcm = runtime->private_data;
  794. snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
  795. int channel = channel=epcm->channel_id;
  796. if (!epcm->running)
  797. return 0;
  798. ptr1 = snd_ca0106_ptr_read(emu, CAPTURE_POINTER, channel);
  799. ptr2 = bytes_to_frames(runtime, ptr1);
  800. ptr=ptr2;
  801. if (ptr >= runtime->buffer_size)
  802. ptr -= runtime->buffer_size;
  803. //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);
  804. return ptr;
  805. }
  806. /* operators */
  807. static struct snd_pcm_ops snd_ca0106_playback_front_ops = {
  808. .open = snd_ca0106_pcm_open_playback_front,
  809. .close = snd_ca0106_pcm_close_playback,
  810. .ioctl = snd_pcm_lib_ioctl,
  811. .hw_params = snd_ca0106_pcm_hw_params_playback,
  812. .hw_free = snd_ca0106_pcm_hw_free_playback,
  813. .prepare = snd_ca0106_pcm_prepare_playback,
  814. .trigger = snd_ca0106_pcm_trigger_playback,
  815. .pointer = snd_ca0106_pcm_pointer_playback,
  816. };
  817. static struct snd_pcm_ops snd_ca0106_capture_0_ops = {
  818. .open = snd_ca0106_pcm_open_0_capture,
  819. .close = snd_ca0106_pcm_close_capture,
  820. .ioctl = snd_pcm_lib_ioctl,
  821. .hw_params = snd_ca0106_pcm_hw_params_capture,
  822. .hw_free = snd_ca0106_pcm_hw_free_capture,
  823. .prepare = snd_ca0106_pcm_prepare_capture,
  824. .trigger = snd_ca0106_pcm_trigger_capture,
  825. .pointer = snd_ca0106_pcm_pointer_capture,
  826. };
  827. static struct snd_pcm_ops snd_ca0106_capture_1_ops = {
  828. .open = snd_ca0106_pcm_open_1_capture,
  829. .close = snd_ca0106_pcm_close_capture,
  830. .ioctl = snd_pcm_lib_ioctl,
  831. .hw_params = snd_ca0106_pcm_hw_params_capture,
  832. .hw_free = snd_ca0106_pcm_hw_free_capture,
  833. .prepare = snd_ca0106_pcm_prepare_capture,
  834. .trigger = snd_ca0106_pcm_trigger_capture,
  835. .pointer = snd_ca0106_pcm_pointer_capture,
  836. };
  837. static struct snd_pcm_ops snd_ca0106_capture_2_ops = {
  838. .open = snd_ca0106_pcm_open_2_capture,
  839. .close = snd_ca0106_pcm_close_capture,
  840. .ioctl = snd_pcm_lib_ioctl,
  841. .hw_params = snd_ca0106_pcm_hw_params_capture,
  842. .hw_free = snd_ca0106_pcm_hw_free_capture,
  843. .prepare = snd_ca0106_pcm_prepare_capture,
  844. .trigger = snd_ca0106_pcm_trigger_capture,
  845. .pointer = snd_ca0106_pcm_pointer_capture,
  846. };
  847. static struct snd_pcm_ops snd_ca0106_capture_3_ops = {
  848. .open = snd_ca0106_pcm_open_3_capture,
  849. .close = snd_ca0106_pcm_close_capture,
  850. .ioctl = snd_pcm_lib_ioctl,
  851. .hw_params = snd_ca0106_pcm_hw_params_capture,
  852. .hw_free = snd_ca0106_pcm_hw_free_capture,
  853. .prepare = snd_ca0106_pcm_prepare_capture,
  854. .trigger = snd_ca0106_pcm_trigger_capture,
  855. .pointer = snd_ca0106_pcm_pointer_capture,
  856. };
  857. static struct snd_pcm_ops snd_ca0106_playback_center_lfe_ops = {
  858. .open = snd_ca0106_pcm_open_playback_center_lfe,
  859. .close = snd_ca0106_pcm_close_playback,
  860. .ioctl = snd_pcm_lib_ioctl,
  861. .hw_params = snd_ca0106_pcm_hw_params_playback,
  862. .hw_free = snd_ca0106_pcm_hw_free_playback,
  863. .prepare = snd_ca0106_pcm_prepare_playback,
  864. .trigger = snd_ca0106_pcm_trigger_playback,
  865. .pointer = snd_ca0106_pcm_pointer_playback,
  866. };
  867. static struct snd_pcm_ops snd_ca0106_playback_unknown_ops = {
  868. .open = snd_ca0106_pcm_open_playback_unknown,
  869. .close = snd_ca0106_pcm_close_playback,
  870. .ioctl = snd_pcm_lib_ioctl,
  871. .hw_params = snd_ca0106_pcm_hw_params_playback,
  872. .hw_free = snd_ca0106_pcm_hw_free_playback,
  873. .prepare = snd_ca0106_pcm_prepare_playback,
  874. .trigger = snd_ca0106_pcm_trigger_playback,
  875. .pointer = snd_ca0106_pcm_pointer_playback,
  876. };
  877. static struct snd_pcm_ops snd_ca0106_playback_rear_ops = {
  878. .open = snd_ca0106_pcm_open_playback_rear,
  879. .close = snd_ca0106_pcm_close_playback,
  880. .ioctl = snd_pcm_lib_ioctl,
  881. .hw_params = snd_ca0106_pcm_hw_params_playback,
  882. .hw_free = snd_ca0106_pcm_hw_free_playback,
  883. .prepare = snd_ca0106_pcm_prepare_playback,
  884. .trigger = snd_ca0106_pcm_trigger_playback,
  885. .pointer = snd_ca0106_pcm_pointer_playback,
  886. };
  887. static unsigned short snd_ca0106_ac97_read(struct snd_ac97 *ac97,
  888. unsigned short reg)
  889. {
  890. struct snd_ca0106 *emu = ac97->private_data;
  891. unsigned long flags;
  892. unsigned short val;
  893. spin_lock_irqsave(&emu->emu_lock, flags);
  894. outb(reg, emu->port + AC97ADDRESS);
  895. val = inw(emu->port + AC97DATA);
  896. spin_unlock_irqrestore(&emu->emu_lock, flags);
  897. return val;
  898. }
  899. static void snd_ca0106_ac97_write(struct snd_ac97 *ac97,
  900. unsigned short reg, unsigned short val)
  901. {
  902. struct snd_ca0106 *emu = ac97->private_data;
  903. unsigned long flags;
  904. spin_lock_irqsave(&emu->emu_lock, flags);
  905. outb(reg, emu->port + AC97ADDRESS);
  906. outw(val, emu->port + AC97DATA);
  907. spin_unlock_irqrestore(&emu->emu_lock, flags);
  908. }
  909. static int snd_ca0106_ac97(struct snd_ca0106 *chip)
  910. {
  911. struct snd_ac97_bus *pbus;
  912. struct snd_ac97_template ac97;
  913. int err;
  914. static struct snd_ac97_bus_ops ops = {
  915. .write = snd_ca0106_ac97_write,
  916. .read = snd_ca0106_ac97_read,
  917. };
  918. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
  919. return err;
  920. pbus->no_vra = 1; /* we don't need VRA */
  921. memset(&ac97, 0, sizeof(ac97));
  922. ac97.private_data = chip;
  923. ac97.scaps = AC97_SCAP_NO_SPDIF;
  924. return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  925. }
  926. static int snd_ca0106_free(struct snd_ca0106 *chip)
  927. {
  928. if (chip->res_port != NULL) { /* avoid access to already used hardware */
  929. // disable interrupts
  930. snd_ca0106_ptr_write(chip, BASIC_INTERRUPT, 0, 0);
  931. outl(0, chip->port + INTE);
  932. snd_ca0106_ptr_write(chip, EXTENDED_INT_MASK, 0, 0);
  933. udelay(1000);
  934. // disable audio
  935. //outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
  936. outl(0, chip->port + HCFG);
  937. /* FIXME: We need to stop and DMA transfers here.
  938. * But as I am not sure how yet, we cannot from the dma pages.
  939. * So we can fix: snd-malloc: Memory leak? pages not freed = 8
  940. */
  941. }
  942. // release the data
  943. #if 1
  944. if (chip->buffer.area)
  945. snd_dma_free_pages(&chip->buffer);
  946. #endif
  947. // release the i/o port
  948. release_and_free_resource(chip->res_port);
  949. // release the irq
  950. if (chip->irq >= 0)
  951. free_irq(chip->irq, (void *)chip);
  952. pci_disable_device(chip->pci);
  953. kfree(chip);
  954. return 0;
  955. }
  956. static int snd_ca0106_dev_free(struct snd_device *device)
  957. {
  958. struct snd_ca0106 *chip = device->device_data;
  959. return snd_ca0106_free(chip);
  960. }
  961. static irqreturn_t snd_ca0106_interrupt(int irq, void *dev_id,
  962. struct pt_regs *regs)
  963. {
  964. unsigned int status;
  965. struct snd_ca0106 *chip = dev_id;
  966. int i;
  967. int mask;
  968. unsigned int stat76;
  969. struct snd_ca0106_channel *pchannel;
  970. status = inl(chip->port + IPR);
  971. if (! status)
  972. return IRQ_NONE;
  973. stat76 = snd_ca0106_ptr_read(chip, EXTENDED_INT, 0);
  974. //snd_printk("interrupt status = 0x%08x, stat76=0x%08x\n", status, stat76);
  975. //snd_printk("ptr=0x%08x\n",snd_ca0106_ptr_read(chip, PLAYBACK_POINTER, 0));
  976. mask = 0x11; /* 0x1 for one half, 0x10 for the other half period. */
  977. for(i = 0; i < 4; i++) {
  978. pchannel = &(chip->playback_channels[i]);
  979. if (stat76 & mask) {
  980. /* FIXME: Select the correct substream for period elapsed */
  981. if(pchannel->use) {
  982. snd_pcm_period_elapsed(pchannel->epcm->substream);
  983. //printk(KERN_INFO "interrupt [%d] used\n", i);
  984. }
  985. }
  986. //printk(KERN_INFO "channel=%p\n",pchannel);
  987. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  988. mask <<= 1;
  989. }
  990. mask = 0x110000; /* 0x1 for one half, 0x10 for the other half period. */
  991. for(i = 0; i < 4; i++) {
  992. pchannel = &(chip->capture_channels[i]);
  993. if (stat76 & mask) {
  994. /* FIXME: Select the correct substream for period elapsed */
  995. if(pchannel->use) {
  996. snd_pcm_period_elapsed(pchannel->epcm->substream);
  997. //printk(KERN_INFO "interrupt [%d] used\n", i);
  998. }
  999. }
  1000. //printk(KERN_INFO "channel=%p\n",pchannel);
  1001. //printk(KERN_INFO "interrupt stat76[%d] = %08x, use=%d, channel=%d\n", i, stat76, pchannel->use, pchannel->number);
  1002. mask <<= 1;
  1003. }
  1004. snd_ca0106_ptr_write(chip, EXTENDED_INT, 0, stat76);
  1005. if (chip->midi.dev_id &&
  1006. (status & (chip->midi.ipr_tx|chip->midi.ipr_rx))) {
  1007. if (chip->midi.interrupt)
  1008. chip->midi.interrupt(&chip->midi, status);
  1009. else
  1010. chip->midi.interrupt_disable(&chip->midi, chip->midi.tx_enable | chip->midi.rx_enable);
  1011. }
  1012. // acknowledge the interrupt if necessary
  1013. outl(status, chip->port+IPR);
  1014. return IRQ_HANDLED;
  1015. }
  1016. static int __devinit snd_ca0106_pcm(struct snd_ca0106 *emu, int device, struct snd_pcm **rpcm)
  1017. {
  1018. struct snd_pcm *pcm;
  1019. struct snd_pcm_substream *substream;
  1020. int err;
  1021. if (rpcm)
  1022. *rpcm = NULL;
  1023. if ((err = snd_pcm_new(emu->card, "ca0106", device, 1, 1, &pcm)) < 0)
  1024. return err;
  1025. pcm->private_data = emu;
  1026. switch (device) {
  1027. case 0:
  1028. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_front_ops);
  1029. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_0_ops);
  1030. break;
  1031. case 1:
  1032. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_rear_ops);
  1033. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_1_ops);
  1034. break;
  1035. case 2:
  1036. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_center_lfe_ops);
  1037. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_2_ops);
  1038. break;
  1039. case 3:
  1040. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ca0106_playback_unknown_ops);
  1041. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ca0106_capture_3_ops);
  1042. break;
  1043. }
  1044. pcm->info_flags = 0;
  1045. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1046. strcpy(pcm->name, "CA0106");
  1047. emu->pcm = pcm;
  1048. for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  1049. substream;
  1050. substream = substream->next) {
  1051. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1052. SNDRV_DMA_TYPE_DEV,
  1053. snd_dma_pci_data(emu->pci),
  1054. 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
  1055. return err;
  1056. }
  1057. for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1058. substream;
  1059. substream = substream->next) {
  1060. if ((err = snd_pcm_lib_preallocate_pages(substream,
  1061. SNDRV_DMA_TYPE_DEV,
  1062. snd_dma_pci_data(emu->pci),
  1063. 64*1024, 64*1024)) < 0)
  1064. return err;
  1065. }
  1066. if (rpcm)
  1067. *rpcm = pcm;
  1068. return 0;
  1069. }
  1070. static unsigned int spi_dac_init[] = {
  1071. 0x00ff,
  1072. 0x02ff,
  1073. 0x0400,
  1074. 0x0520,
  1075. 0x0620, /* Set 24 bit. Was 0x0600 */
  1076. 0x08ff,
  1077. 0x0aff,
  1078. 0x0cff,
  1079. 0x0eff,
  1080. 0x10ff,
  1081. 0x1200,
  1082. 0x1400,
  1083. 0x1480,
  1084. 0x1800,
  1085. 0x1aff,
  1086. 0x1cff,
  1087. 0x1e00,
  1088. 0x0530,
  1089. 0x0602,
  1090. 0x0622,
  1091. 0x1400,
  1092. };
  1093. static unsigned int i2c_adc_init[][2] = {
  1094. { 0x17, 0x00 }, /* Reset */
  1095. { 0x07, 0x00 }, /* Timeout */
  1096. { 0x0b, 0x22 }, /* Interface control */
  1097. { 0x0c, 0x22 }, /* Master mode control */
  1098. { 0x0d, 0x08 }, /* Powerdown control */
  1099. { 0x0e, 0xcf }, /* Attenuation Left 0x01 = -103dB, 0xff = 24dB */
  1100. { 0x0f, 0xcf }, /* Attenuation Right 0.5dB steps */
  1101. { 0x10, 0x7b }, /* ALC Control 1 */
  1102. { 0x11, 0x00 }, /* ALC Control 2 */
  1103. { 0x12, 0x32 }, /* ALC Control 3 */
  1104. { 0x13, 0x00 }, /* Noise gate control */
  1105. { 0x14, 0xa6 }, /* Limiter control */
  1106. { 0x15, ADC_MUX_LINEIN }, /* ADC Mixer control */
  1107. };
  1108. static int __devinit snd_ca0106_create(struct snd_card *card,
  1109. struct pci_dev *pci,
  1110. struct snd_ca0106 **rchip)
  1111. {
  1112. struct snd_ca0106 *chip;
  1113. struct snd_ca0106_details *c;
  1114. int err;
  1115. int ch;
  1116. static struct snd_device_ops ops = {
  1117. .dev_free = snd_ca0106_dev_free,
  1118. };
  1119. *rchip = NULL;
  1120. if ((err = pci_enable_device(pci)) < 0)
  1121. return err;
  1122. if (pci_set_dma_mask(pci, DMA_32BIT_MASK) < 0 ||
  1123. pci_set_consistent_dma_mask(pci, DMA_32BIT_MASK) < 0) {
  1124. printk(KERN_ERR "error to set 32bit mask DMA\n");
  1125. pci_disable_device(pci);
  1126. return -ENXIO;
  1127. }
  1128. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1129. if (chip == NULL) {
  1130. pci_disable_device(pci);
  1131. return -ENOMEM;
  1132. }
  1133. chip->card = card;
  1134. chip->pci = pci;
  1135. chip->irq = -1;
  1136. spin_lock_init(&chip->emu_lock);
  1137. chip->port = pci_resource_start(pci, 0);
  1138. if ((chip->res_port = request_region(chip->port, 0x20,
  1139. "snd_ca0106")) == NULL) {
  1140. snd_ca0106_free(chip);
  1141. printk(KERN_ERR "cannot allocate the port\n");
  1142. return -EBUSY;
  1143. }
  1144. if (request_irq(pci->irq, snd_ca0106_interrupt,
  1145. IRQF_DISABLED|IRQF_SHARED, "snd_ca0106",
  1146. (void *)chip)) {
  1147. snd_ca0106_free(chip);
  1148. printk(KERN_ERR "cannot grab irq\n");
  1149. return -EBUSY;
  1150. }
  1151. chip->irq = pci->irq;
  1152. /* This stores the periods table. */
  1153. if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), 1024, &chip->buffer) < 0) {
  1154. snd_ca0106_free(chip);
  1155. return -ENOMEM;
  1156. }
  1157. pci_set_master(pci);
  1158. /* read revision & serial */
  1159. pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
  1160. pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
  1161. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
  1162. #if 1
  1163. printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
  1164. chip->revision, chip->serial);
  1165. #endif
  1166. strcpy(card->driver, "CA0106");
  1167. strcpy(card->shortname, "CA0106");
  1168. for (c = ca0106_chip_details; c->serial; c++) {
  1169. if (c->serial == chip->serial)
  1170. break;
  1171. }
  1172. chip->details = c;
  1173. sprintf(card->longname, "%s at 0x%lx irq %i",
  1174. c->name, chip->port, chip->irq);
  1175. outl(0, chip->port + INTE);
  1176. /*
  1177. * Init to 0x02109204 :
  1178. * Clock accuracy = 0 (1000ppm)
  1179. * Sample Rate = 2 (48kHz)
  1180. * Audio Channel = 1 (Left of 2)
  1181. * Source Number = 0 (Unspecified)
  1182. * Generation Status = 1 (Original for Cat Code 12)
  1183. * Cat Code = 12 (Digital Signal Mixer)
  1184. * Mode = 0 (Mode 0)
  1185. * Emphasis = 0 (None)
  1186. * CP = 1 (Copyright unasserted)
  1187. * AN = 0 (Audio data)
  1188. * P = 0 (Consumer)
  1189. */
  1190. snd_ca0106_ptr_write(chip, SPCS0, 0,
  1191. chip->spdif_bits[0] =
  1192. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1193. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1194. SPCS_GENERATIONSTATUS | 0x00001200 |
  1195. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1196. /* Only SPCS1 has been tested */
  1197. snd_ca0106_ptr_write(chip, SPCS1, 0,
  1198. chip->spdif_bits[1] =
  1199. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1200. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1201. SPCS_GENERATIONSTATUS | 0x00001200 |
  1202. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1203. snd_ca0106_ptr_write(chip, SPCS2, 0,
  1204. chip->spdif_bits[2] =
  1205. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1206. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1207. SPCS_GENERATIONSTATUS | 0x00001200 |
  1208. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1209. snd_ca0106_ptr_write(chip, SPCS3, 0,
  1210. chip->spdif_bits[3] =
  1211. SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
  1212. SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
  1213. SPCS_GENERATIONSTATUS | 0x00001200 |
  1214. 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
  1215. snd_ca0106_ptr_write(chip, PLAYBACK_MUTE, 0, 0x00fc0000);
  1216. snd_ca0106_ptr_write(chip, CAPTURE_MUTE, 0, 0x00fc0000);
  1217. /* Write 0x8000 to AC97_REC_GAIN to mute it. */
  1218. outb(AC97_REC_GAIN, chip->port + AC97ADDRESS);
  1219. outw(0x8000, chip->port + AC97DATA);
  1220. #if 0
  1221. snd_ca0106_ptr_write(chip, SPCS0, 0, 0x2108006);
  1222. snd_ca0106_ptr_write(chip, 0x42, 0, 0x2108006);
  1223. snd_ca0106_ptr_write(chip, 0x43, 0, 0x2108006);
  1224. snd_ca0106_ptr_write(chip, 0x44, 0, 0x2108006);
  1225. #endif
  1226. //snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0xf0f003f); /* OSS drivers set this. */
  1227. /* Analog or Digital output */
  1228. snd_ca0106_ptr_write(chip, SPDIF_SELECT1, 0, 0xf);
  1229. snd_ca0106_ptr_write(chip, SPDIF_SELECT2, 0, 0x000f0000); /* 0x0b000000 for digital, 0x000b0000 for analog, from win2000 drivers. Use 0x000f0000 for surround71 */
  1230. chip->spdif_enable = 0; /* Set digital SPDIF output off */
  1231. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1232. //snd_ca0106_ptr_write(chip, 0x45, 0, 0); /* Analogue out */
  1233. //snd_ca0106_ptr_write(chip, 0x45, 0, 0xf00); /* Digital out */
  1234. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 0, 0x40c81000); /* goes to 0x40c80000 when doing SPDIF IN/OUT */
  1235. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 1, 0xffffffff); /* (Mute) CAPTURE feedback into PLAYBACK volume. Only lower 16 bits matter. */
  1236. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 2, 0x30300000); /* SPDIF IN Volume */
  1237. snd_ca0106_ptr_write(chip, CAPTURE_CONTROL, 3, 0x00700000); /* SPDIF IN Volume, 0x70 = (vol & 0x3f) | 0x40 */
  1238. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING1, 0, 0x32765410);
  1239. snd_ca0106_ptr_write(chip, PLAYBACK_ROUTING2, 0, 0x76767676);
  1240. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING1, 0, 0x32765410);
  1241. snd_ca0106_ptr_write(chip, CAPTURE_ROUTING2, 0, 0x76767676);
  1242. for(ch = 0; ch < 4; ch++) {
  1243. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME1, ch, 0x30303030); /* Only high 16 bits matter */
  1244. snd_ca0106_ptr_write(chip, CAPTURE_VOLUME2, ch, 0x30303030);
  1245. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0x40404040); /* Mute */
  1246. //snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0x40404040); /* Mute */
  1247. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME1, ch, 0xffffffff); /* Mute */
  1248. snd_ca0106_ptr_write(chip, PLAYBACK_VOLUME2, ch, 0xffffffff); /* Mute */
  1249. }
  1250. snd_ca0106_ptr_write(chip, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC, Line in, TAD in, AUX in */
  1251. chip->capture_source = 3; /* Set CAPTURE_SOURCE */
  1252. if (chip->details->gpio_type == 2) { /* The SB0438 use GPIO differently. */
  1253. /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
  1254. outl(0x0, chip->port+GPIO);
  1255. //outl(0x00f0e000, chip->port+GPIO); /* Analog */
  1256. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1257. } else if (chip->details->gpio_type == 1) { /* The SB0410 and SB0413 use GPIO differently. */
  1258. /* FIXME: Still need to find out what the other GPIO bits do. E.g. For digital spdif out. */
  1259. outl(0x0, chip->port+GPIO);
  1260. //outl(0x00f0e000, chip->port+GPIO); /* Analog */
  1261. outl(0x005f5301, chip->port+GPIO); /* Analog */
  1262. } else {
  1263. outl(0x0, chip->port+GPIO);
  1264. outl(0x005f03a3, chip->port+GPIO); /* Analog */
  1265. //outl(0x005f02a2, chip->port+GPIO); /* SPDIF */
  1266. }
  1267. snd_ca0106_intr_enable(chip, 0x105); /* Win2000 uses 0x1e0 */
  1268. //outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
  1269. //outl(0x00001409, chip->port+HCFG); /* 0x1000 causes AC3 to fails. Maybe it effects 24 bit output. */
  1270. //outl(0x00000009, chip->port+HCFG);
  1271. outl(HCFG_AC97 | HCFG_AUDIOENABLE, chip->port+HCFG); /* AC97 2.0, Enable outputs. */
  1272. if (chip->details->i2c_adc == 1) { /* The SB0410 and SB0413 use I2C to control ADC. */
  1273. int size, n;
  1274. size = ARRAY_SIZE(i2c_adc_init);
  1275. //snd_printk("I2C:array size=0x%x\n", size);
  1276. for (n=0; n < size; n++) {
  1277. snd_ca0106_i2c_write(chip, i2c_adc_init[n][0], i2c_adc_init[n][1]);
  1278. }
  1279. for (n=0; n < 4; n++) {
  1280. chip->i2c_capture_volume[n][0]= 0xcf;
  1281. chip->i2c_capture_volume[n][1]= 0xcf;
  1282. }
  1283. chip->i2c_capture_source=2; /* Line in */
  1284. //snd_ca0106_i2c_write(chip, ADC_MUX, ADC_MUX_LINEIN); /* Enable Line-in capture. MIC in currently untested. */
  1285. }
  1286. if (chip->details->spi_dac == 1) { /* The SB0570 use SPI to control DAC. */
  1287. int size, n;
  1288. size = ARRAY_SIZE(spi_dac_init);
  1289. for (n=0; n < size; n++)
  1290. snd_ca0106_spi_write(chip, spi_dac_init[n]);
  1291. }
  1292. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  1293. chip, &ops)) < 0) {
  1294. snd_ca0106_free(chip);
  1295. return err;
  1296. }
  1297. *rchip = chip;
  1298. return 0;
  1299. }
  1300. static void ca0106_midi_interrupt_enable(struct snd_ca_midi *midi, int intr)
  1301. {
  1302. snd_ca0106_intr_enable((struct snd_ca0106 *)(midi->dev_id), intr);
  1303. }
  1304. static void ca0106_midi_interrupt_disable(struct snd_ca_midi *midi, int intr)
  1305. {
  1306. snd_ca0106_intr_disable((struct snd_ca0106 *)(midi->dev_id), intr);
  1307. }
  1308. static unsigned char ca0106_midi_read(struct snd_ca_midi *midi, int idx)
  1309. {
  1310. return (unsigned char)snd_ca0106_ptr_read((struct snd_ca0106 *)(midi->dev_id),
  1311. midi->port + idx, 0);
  1312. }
  1313. static void ca0106_midi_write(struct snd_ca_midi *midi, int data, int idx)
  1314. {
  1315. snd_ca0106_ptr_write((struct snd_ca0106 *)(midi->dev_id), midi->port + idx, 0, data);
  1316. }
  1317. static struct snd_card *ca0106_dev_id_card(void *dev_id)
  1318. {
  1319. return ((struct snd_ca0106 *)dev_id)->card;
  1320. }
  1321. static int ca0106_dev_id_port(void *dev_id)
  1322. {
  1323. return ((struct snd_ca0106 *)dev_id)->port;
  1324. }
  1325. static int __devinit snd_ca0106_midi(struct snd_ca0106 *chip, unsigned int channel)
  1326. {
  1327. struct snd_ca_midi *midi;
  1328. char *name;
  1329. int err;
  1330. if (channel == CA0106_MIDI_CHAN_B) {
  1331. name = "CA0106 MPU-401 (UART) B";
  1332. midi = &chip->midi2;
  1333. midi->tx_enable = INTE_MIDI_TX_B;
  1334. midi->rx_enable = INTE_MIDI_RX_B;
  1335. midi->ipr_tx = IPR_MIDI_TX_B;
  1336. midi->ipr_rx = IPR_MIDI_RX_B;
  1337. midi->port = MIDI_UART_B_DATA;
  1338. } else {
  1339. name = "CA0106 MPU-401 (UART)";
  1340. midi = &chip->midi;
  1341. midi->tx_enable = INTE_MIDI_TX_A;
  1342. midi->rx_enable = INTE_MIDI_TX_B;
  1343. midi->ipr_tx = IPR_MIDI_TX_A;
  1344. midi->ipr_rx = IPR_MIDI_RX_A;
  1345. midi->port = MIDI_UART_A_DATA;
  1346. }
  1347. midi->reset = CA0106_MPU401_RESET;
  1348. midi->enter_uart = CA0106_MPU401_ENTER_UART;
  1349. midi->ack = CA0106_MPU401_ACK;
  1350. midi->input_avail = CA0106_MIDI_INPUT_AVAIL;
  1351. midi->output_ready = CA0106_MIDI_OUTPUT_READY;
  1352. midi->channel = channel;
  1353. midi->interrupt_enable = ca0106_midi_interrupt_enable;
  1354. midi->interrupt_disable = ca0106_midi_interrupt_disable;
  1355. midi->read = ca0106_midi_read;
  1356. midi->write = ca0106_midi_write;
  1357. midi->get_dev_id_card = ca0106_dev_id_card;
  1358. midi->get_dev_id_port = ca0106_dev_id_port;
  1359. midi->dev_id = chip;
  1360. if ((err = ca_midi_init(chip, midi, 0, name)) < 0)
  1361. return err;
  1362. return 0;
  1363. }
  1364. static int __devinit snd_ca0106_probe(struct pci_dev *pci,
  1365. const struct pci_device_id *pci_id)
  1366. {
  1367. static int dev;
  1368. struct snd_card *card;
  1369. struct snd_ca0106 *chip;
  1370. int err;
  1371. if (dev >= SNDRV_CARDS)
  1372. return -ENODEV;
  1373. if (!enable[dev]) {
  1374. dev++;
  1375. return -ENOENT;
  1376. }
  1377. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1378. if (card == NULL)
  1379. return -ENOMEM;
  1380. if ((err = snd_ca0106_create(card, pci, &chip)) < 0) {
  1381. snd_card_free(card);
  1382. return err;
  1383. }
  1384. if ((err = snd_ca0106_pcm(chip, 0, NULL)) < 0) {
  1385. snd_card_free(card);
  1386. return err;
  1387. }
  1388. if ((err = snd_ca0106_pcm(chip, 1, NULL)) < 0) {
  1389. snd_card_free(card);
  1390. return err;
  1391. }
  1392. if ((err = snd_ca0106_pcm(chip, 2, NULL)) < 0) {
  1393. snd_card_free(card);
  1394. return err;
  1395. }
  1396. if ((err = snd_ca0106_pcm(chip, 3, NULL)) < 0) {
  1397. snd_card_free(card);
  1398. return err;
  1399. }
  1400. if (chip->details->ac97 == 1) { /* The SB0410 and SB0413 do not have an AC97 chip. */
  1401. if ((err = snd_ca0106_ac97(chip)) < 0) {
  1402. snd_card_free(card);
  1403. return err;
  1404. }
  1405. }
  1406. if ((err = snd_ca0106_mixer(chip)) < 0) {
  1407. snd_card_free(card);
  1408. return err;
  1409. }
  1410. snd_printdd("ca0106: probe for MIDI channel A ...");
  1411. if ((err = snd_ca0106_midi(chip,CA0106_MIDI_CHAN_A)) < 0) {
  1412. snd_card_free(card);
  1413. snd_printdd(" failed, err=0x%x\n",err);
  1414. return err;
  1415. }
  1416. snd_printdd(" done.\n");
  1417. #ifdef CONFIG_PROC_FS
  1418. snd_ca0106_proc_init(chip);
  1419. #endif
  1420. if ((err = snd_card_register(card)) < 0) {
  1421. snd_card_free(card);
  1422. return err;
  1423. }
  1424. pci_set_drvdata(pci, card);
  1425. dev++;
  1426. return 0;
  1427. }
  1428. static void __devexit snd_ca0106_remove(struct pci_dev *pci)
  1429. {
  1430. snd_card_free(pci_get_drvdata(pci));
  1431. pci_set_drvdata(pci, NULL);
  1432. }
  1433. // PCI IDs
  1434. static struct pci_device_id snd_ca0106_ids[] = {
  1435. { 0x1102, 0x0007, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Audigy LS or Live 24bit */
  1436. { 0, }
  1437. };
  1438. MODULE_DEVICE_TABLE(pci, snd_ca0106_ids);
  1439. // pci_driver definition
  1440. static struct pci_driver driver = {
  1441. .name = "CA0106",
  1442. .id_table = snd_ca0106_ids,
  1443. .probe = snd_ca0106_probe,
  1444. .remove = __devexit_p(snd_ca0106_remove),
  1445. };
  1446. // initialization of the module
  1447. static int __init alsa_card_ca0106_init(void)
  1448. {
  1449. return pci_register_driver(&driver);
  1450. }
  1451. // clean up the module
  1452. static void __exit alsa_card_ca0106_exit(void)
  1453. {
  1454. pci_unregister_driver(&driver);
  1455. }
  1456. module_init(alsa_card_ca0106_init)
  1457. module_exit(alsa_card_ca0106_exit)