ca0106_main.c 50 KB

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