ca0106_main.c 50 KB

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