ca0106_main.c 49 KB

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