cmi8330.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Driver for C-Media's CMI8330 soundcards.
  3. * Copyright (c) by George Talusan <gstalusan@uwaterloo.ca>
  4. * http://www.undergrad.math.uwaterloo.ca/~gstalusa
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * NOTES
  23. *
  24. * The extended registers contain mixer settings which are largely
  25. * untapped for the time being.
  26. *
  27. * MPU401 and SPDIF are not supported yet. I don't have the hardware
  28. * to aid in coding and testing, so I won't bother.
  29. *
  30. * To quickly load the module,
  31. *
  32. * modprobe -a snd-cmi8330 sbport=0x220 sbirq=5 sbdma8=1
  33. * sbdma16=5 wssport=0x530 wssirq=11 wssdma=0
  34. *
  35. * This card has two mixers and two PCM devices. I've cheesed it such
  36. * that recording and playback can be done through the same device.
  37. * The driver "magically" routes the capturing to the AD1848 codec,
  38. * and playback to the SB16 codec. This allows for full-duplex mode
  39. * to some extent.
  40. * The utilities in alsa-utils are aware of both devices, so passing
  41. * the appropriate parameters to amixer and alsactl will give you
  42. * full control over both mixers.
  43. */
  44. #include <sound/driver.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/pnp.h>
  48. #include <linux/moduleparam.h>
  49. #include <sound/core.h>
  50. #include <sound/ad1848.h>
  51. #include <sound/sb.h>
  52. #include <sound/initval.h>
  53. /*
  54. */
  55. /* #define ENABLE_SB_MIXER */
  56. #define PLAYBACK_ON_SB
  57. /*
  58. */
  59. MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
  60. MODULE_DESCRIPTION("C-Media CMI8330");
  61. MODULE_LICENSE("GPL");
  62. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
  63. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  64. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  65. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
  66. #ifdef CONFIG_PNP
  67. static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  68. #endif
  69. static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  70. static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  71. static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  72. static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  73. static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  74. static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  75. static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  76. module_param_array(index, int, NULL, 0444);
  77. MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
  78. module_param_array(id, charp, NULL, 0444);
  79. MODULE_PARM_DESC(id, "ID string for CMI8330 soundcard.");
  80. module_param_array(enable, bool, NULL, 0444);
  81. MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
  82. #ifdef CONFIG_PNP
  83. module_param_array(isapnp, bool, NULL, 0444);
  84. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  85. #endif
  86. module_param_array(sbport, long, NULL, 0444);
  87. MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
  88. module_param_array(sbirq, int, NULL, 0444);
  89. MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
  90. module_param_array(sbdma8, int, NULL, 0444);
  91. MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
  92. module_param_array(sbdma16, int, NULL, 0444);
  93. MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
  94. module_param_array(wssport, long, NULL, 0444);
  95. MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
  96. module_param_array(wssirq, int, NULL, 0444);
  97. MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
  98. module_param_array(wssdma, int, NULL, 0444);
  99. MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
  100. #define CMI8330_RMUX3D 16
  101. #define CMI8330_MUTEMUX 17
  102. #define CMI8330_OUTPUTVOL 18
  103. #define CMI8330_MASTVOL 19
  104. #define CMI8330_LINVOL 20
  105. #define CMI8330_CDINVOL 21
  106. #define CMI8330_WAVVOL 22
  107. #define CMI8330_RECMUX 23
  108. #define CMI8330_WAVGAIN 24
  109. #define CMI8330_LINGAIN 25
  110. #define CMI8330_CDINGAIN 26
  111. static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
  112. {
  113. 0x40, /* 16 - recording mux (SB-mixer-enabled) */
  114. #ifdef ENABLE_SB_MIXER
  115. 0x40, /* 17 - mute mux (Mode2) */
  116. #else
  117. 0x0, /* 17 - mute mux */
  118. #endif
  119. 0x0, /* 18 - vol */
  120. 0x0, /* 19 - master volume */
  121. 0x0, /* 20 - line-in volume */
  122. 0x0, /* 21 - cd-in volume */
  123. 0x0, /* 22 - wave volume */
  124. 0x0, /* 23 - mute/rec mux */
  125. 0x0, /* 24 - wave rec gain */
  126. 0x0, /* 25 - line-in rec gain */
  127. 0x0 /* 26 - cd-in rec gain */
  128. };
  129. typedef int (*snd_pcm_open_callback_t)(snd_pcm_substream_t *);
  130. struct snd_cmi8330 {
  131. #ifdef CONFIG_PNP
  132. struct pnp_dev *cap;
  133. struct pnp_dev *play;
  134. #endif
  135. snd_card_t *card;
  136. ad1848_t *wss;
  137. sb_t *sb;
  138. snd_pcm_t *pcm;
  139. struct snd_cmi8330_stream {
  140. snd_pcm_ops_t ops;
  141. snd_pcm_open_callback_t open;
  142. void *private_data; /* sb or wss */
  143. } streams[2];
  144. };
  145. static snd_card_t *snd_cmi8330_legacy[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  146. #ifdef CONFIG_PNP
  147. static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
  148. { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" } } },
  149. { .id = "" }
  150. };
  151. MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
  152. #endif
  153. static struct ad1848_mix_elem snd_cmi8330_controls[] __initdata = {
  154. AD1848_DOUBLE("Master Playback Volume", 0, CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
  155. AD1848_SINGLE("Loud Playback Switch", 0, CMI8330_MUTEMUX, 6, 1, 1),
  156. AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
  157. AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1),
  158. AD1848_DOUBLE("Line Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
  159. AD1848_DOUBLE("Line Playback Volume", 0, CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
  160. AD1848_DOUBLE("Line Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
  161. AD1848_DOUBLE("Line Capture Volume", 0, CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
  162. AD1848_DOUBLE("CD Playback Switch", 0, CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
  163. AD1848_DOUBLE("CD Capture Switch", 0, CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
  164. AD1848_DOUBLE("CD Playback Volume", 0, CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
  165. AD1848_DOUBLE("CD Capture Volume", 0, CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
  166. AD1848_SINGLE("Mic Playback Switch", 0, CMI8330_MUTEMUX, 0, 1, 0),
  167. AD1848_SINGLE("Mic Playback Volume", 0, CMI8330_OUTPUTVOL, 0, 7, 0),
  168. AD1848_SINGLE("Mic Capture Switch", 0, CMI8330_RMUX3D, 0, 1, 0),
  169. AD1848_SINGLE("Mic Capture Volume", 0, CMI8330_OUTPUTVOL, 5, 7, 0),
  170. AD1848_DOUBLE("Wavetable Playback Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
  171. AD1848_DOUBLE("Wavetable Playback Volume", 0, CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
  172. AD1848_DOUBLE("Wavetable Capture Switch", 0, CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
  173. AD1848_DOUBLE("Wavetable Capture Volume", 0, CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
  174. AD1848_SINGLE("3D Control - Switch", 0, CMI8330_RMUX3D, 5, 1, 1),
  175. AD1848_SINGLE("PC Speaker Playback Volume", 0, CMI8330_OUTPUTVOL, 3, 3, 0),
  176. AD1848_SINGLE("FM Playback Switch", 0, CMI8330_RECMUX, 3, 1, 1),
  177. AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",CAPTURE,SWITCH), 0, CMI8330_RMUX3D, 7, 1, 1),
  178. AD1848_SINGLE(SNDRV_CTL_NAME_IEC958("Input ",PLAYBACK,SWITCH), 0, CMI8330_MUTEMUX, 7, 1, 1),
  179. };
  180. #ifdef ENABLE_SB_MIXER
  181. static struct sbmix_elem cmi8330_sb_mixers[] __initdata = {
  182. SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
  183. SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
  184. SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
  185. SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
  186. SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
  187. SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
  188. SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
  189. SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
  190. SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
  191. SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
  192. SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  193. SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  194. SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
  195. SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
  196. SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
  197. };
  198. static unsigned char cmi8330_sb_init_values[][2] __initdata = {
  199. { SB_DSP4_MASTER_DEV + 0, 0 },
  200. { SB_DSP4_MASTER_DEV + 1, 0 },
  201. { SB_DSP4_PCM_DEV + 0, 0 },
  202. { SB_DSP4_PCM_DEV + 1, 0 },
  203. { SB_DSP4_SYNTH_DEV + 0, 0 },
  204. { SB_DSP4_SYNTH_DEV + 1, 0 },
  205. { SB_DSP4_INPUT_LEFT, 0 },
  206. { SB_DSP4_INPUT_RIGHT, 0 },
  207. { SB_DSP4_OUTPUT_SW, 0 },
  208. { SB_DSP4_SPEAKER_DEV, 0 },
  209. };
  210. static int __devinit cmi8330_add_sb_mixers(sb_t *chip)
  211. {
  212. int idx, err;
  213. unsigned long flags;
  214. spin_lock_irqsave(&chip->mixer_lock, flags);
  215. snd_sbmixer_write(chip, 0x00, 0x00); /* mixer reset */
  216. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  217. /* mute and zero volume channels */
  218. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
  219. spin_lock_irqsave(&chip->mixer_lock, flags);
  220. snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
  221. cmi8330_sb_init_values[idx][1]);
  222. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  223. }
  224. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
  225. if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
  226. return err;
  227. }
  228. return 0;
  229. }
  230. #endif
  231. static int __devinit snd_cmi8330_mixer(snd_card_t *card, struct snd_cmi8330 *acard)
  232. {
  233. unsigned int idx;
  234. int err;
  235. strcpy(card->mixername, "CMI8330/C3D");
  236. for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
  237. if ((err = snd_ad1848_add_ctl_elem(acard->wss, &snd_cmi8330_controls[idx])) < 0)
  238. return err;
  239. }
  240. #ifdef ENABLE_SB_MIXER
  241. if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
  242. return err;
  243. #endif
  244. return 0;
  245. }
  246. #ifdef CONFIG_PNP
  247. static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
  248. struct pnp_card_link *card,
  249. const struct pnp_card_device_id *id)
  250. {
  251. struct pnp_dev *pdev;
  252. struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
  253. int err;
  254. acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
  255. if (acard->cap == NULL) {
  256. kfree(cfg);
  257. return -EBUSY;
  258. }
  259. acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
  260. if (acard->play == NULL) {
  261. kfree(cfg);
  262. return -EBUSY;
  263. }
  264. pdev = acard->cap;
  265. pnp_init_resource_table(cfg);
  266. /* allocate AD1848 resources */
  267. if (wssport[dev] != SNDRV_AUTO_PORT)
  268. pnp_resource_change(&cfg->port_resource[0], wssport[dev], 8);
  269. if (wssdma[dev] != SNDRV_AUTO_DMA)
  270. pnp_resource_change(&cfg->dma_resource[0], wssdma[dev], 1);
  271. if (wssirq[dev] != SNDRV_AUTO_IRQ)
  272. pnp_resource_change(&cfg->irq_resource[0], wssirq[dev], 1);
  273. err = pnp_manual_config_dev(pdev, cfg, 0);
  274. if (err < 0)
  275. snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP manual resources are invalid, using auto config\n");
  276. err = pnp_activate_dev(pdev);
  277. if (err < 0) {
  278. snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP configure failure\n");
  279. kfree(cfg);
  280. return -EBUSY;
  281. }
  282. wssport[dev] = pnp_port_start(pdev, 0);
  283. wssdma[dev] = pnp_dma(pdev, 0);
  284. wssirq[dev] = pnp_irq(pdev, 0);
  285. /* allocate SB16 resources */
  286. pdev = acard->play;
  287. pnp_init_resource_table(cfg);
  288. if (sbport[dev] != SNDRV_AUTO_PORT)
  289. pnp_resource_change(&cfg->port_resource[0], sbport[dev], 16);
  290. if (sbdma8[dev] != SNDRV_AUTO_DMA)
  291. pnp_resource_change(&cfg->dma_resource[0], sbdma8[dev], 1);
  292. if (sbdma16[dev] != SNDRV_AUTO_DMA)
  293. pnp_resource_change(&cfg->dma_resource[1], sbdma16[dev], 1);
  294. if (sbirq[dev] != SNDRV_AUTO_IRQ)
  295. pnp_resource_change(&cfg->irq_resource[0], sbirq[dev], 1);
  296. err = pnp_manual_config_dev(pdev, cfg, 0);
  297. if (err < 0)
  298. snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP manual resources are invalid, using auto config\n");
  299. err = pnp_activate_dev(pdev);
  300. if (err < 0) {
  301. snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
  302. kfree(cfg);
  303. return -EBUSY;
  304. }
  305. sbport[dev] = pnp_port_start(pdev, 0);
  306. sbdma8[dev] = pnp_dma(pdev, 0);
  307. sbdma16[dev] = pnp_dma(pdev, 1);
  308. sbirq[dev] = pnp_irq(pdev, 0);
  309. kfree(cfg);
  310. return 0;
  311. }
  312. #endif
  313. /*
  314. * PCM interface
  315. *
  316. * since we call the different chip interfaces for playback and capture
  317. * directions, we need a trick.
  318. *
  319. * - copy the ops for each direction into a local record.
  320. * - replace the open callback with the new one, which replaces the
  321. * substream->private_data with the corresponding chip instance
  322. * and calls again the original open callback of the chip.
  323. *
  324. */
  325. #ifdef PLAYBACK_ON_SB
  326. #define CMI_SB_STREAM SNDRV_PCM_STREAM_PLAYBACK
  327. #define CMI_AD_STREAM SNDRV_PCM_STREAM_CAPTURE
  328. #else
  329. #define CMI_SB_STREAM SNDRV_PCM_STREAM_CAPTURE
  330. #define CMI_AD_STREAM SNDRV_PCM_STREAM_PLAYBACK
  331. #endif
  332. static int snd_cmi8330_playback_open(snd_pcm_substream_t * substream)
  333. {
  334. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  335. /* replace the private_data and call the original open callback */
  336. substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
  337. return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
  338. }
  339. static int snd_cmi8330_capture_open(snd_pcm_substream_t * substream)
  340. {
  341. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  342. /* replace the private_data and call the original open callback */
  343. substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
  344. return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
  345. }
  346. static void snd_cmi8330_pcm_free(snd_pcm_t *pcm)
  347. {
  348. snd_pcm_lib_preallocate_free_for_all(pcm);
  349. }
  350. static int __devinit snd_cmi8330_pcm(snd_card_t *card, struct snd_cmi8330 *chip)
  351. {
  352. snd_pcm_t *pcm;
  353. const snd_pcm_ops_t *ops;
  354. int err;
  355. static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
  356. snd_cmi8330_playback_open,
  357. snd_cmi8330_capture_open
  358. };
  359. if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
  360. return err;
  361. strcpy(pcm->name, "CMI8330");
  362. pcm->private_data = chip;
  363. pcm->private_free = snd_cmi8330_pcm_free;
  364. /* SB16 */
  365. ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
  366. chip->streams[CMI_SB_STREAM].ops = *ops;
  367. chip->streams[CMI_SB_STREAM].open = ops->open;
  368. chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
  369. chip->streams[CMI_SB_STREAM].private_data = chip->sb;
  370. /* AD1848 */
  371. ops = snd_ad1848_get_pcm_ops(CMI_AD_STREAM);
  372. chip->streams[CMI_AD_STREAM].ops = *ops;
  373. chip->streams[CMI_AD_STREAM].open = ops->open;
  374. chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
  375. chip->streams[CMI_AD_STREAM].private_data = chip->wss;
  376. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
  377. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
  378. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  379. snd_dma_isa_data(),
  380. 64*1024, 128*1024);
  381. chip->pcm = pcm;
  382. return 0;
  383. }
  384. /*
  385. */
  386. static int __devinit snd_cmi8330_probe(int dev,
  387. struct pnp_card_link *pcard,
  388. const struct pnp_card_device_id *pid)
  389. {
  390. snd_card_t *card;
  391. struct snd_cmi8330 *acard;
  392. unsigned long flags;
  393. int i, err;
  394. #ifdef CONFIG_PNP
  395. if (!isapnp[dev]) {
  396. #endif
  397. if (wssport[dev] == SNDRV_AUTO_PORT) {
  398. snd_printk("specify wssport\n");
  399. return -EINVAL;
  400. }
  401. if (sbport[dev] == SNDRV_AUTO_PORT) {
  402. snd_printk("specify sbport\n");
  403. return -EINVAL;
  404. }
  405. #ifdef CONFIG_PNP
  406. }
  407. #endif
  408. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  409. sizeof(struct snd_cmi8330));
  410. if (card == NULL) {
  411. snd_printk("could not get a new card\n");
  412. return -ENOMEM;
  413. }
  414. acard = (struct snd_cmi8330 *)card->private_data;
  415. acard->card = card;
  416. #ifdef CONFIG_PNP
  417. if (isapnp[dev]) {
  418. if ((err = snd_cmi8330_pnp(dev, acard, pcard, pid)) < 0) {
  419. snd_printk("PnP detection failed\n");
  420. snd_card_free(card);
  421. return err;
  422. }
  423. snd_card_set_dev(card, &pcard->card->dev);
  424. }
  425. #endif
  426. if ((err = snd_ad1848_create(card,
  427. wssport[dev] + 4,
  428. wssirq[dev],
  429. wssdma[dev],
  430. AD1848_HW_DETECT,
  431. &acard->wss)) < 0) {
  432. snd_printk("(AD1848) device busy??\n");
  433. snd_card_free(card);
  434. return err;
  435. }
  436. if (acard->wss->hardware != AD1848_HW_CMI8330) {
  437. snd_printk("(AD1848) not found during probe\n");
  438. snd_card_free(card);
  439. return -ENODEV;
  440. }
  441. if ((err = snd_sbdsp_create(card, sbport[dev],
  442. sbirq[dev],
  443. snd_sb16dsp_interrupt,
  444. sbdma8[dev],
  445. sbdma16[dev],
  446. SB_HW_AUTO, &acard->sb)) < 0) {
  447. snd_printk("(SB16) device busy??\n");
  448. snd_card_free(card);
  449. return err;
  450. }
  451. if (acard->sb->hardware != SB_HW_16) {
  452. snd_printk("(SB16) not found during probe\n");
  453. snd_card_free(card);
  454. return -ENODEV;
  455. }
  456. spin_lock_irqsave(&acard->wss->reg_lock, flags);
  457. snd_ad1848_out(acard->wss, AD1848_MISC_INFO, 0x40); /* switch on MODE2 */
  458. for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
  459. snd_ad1848_out(acard->wss, i, snd_cmi8330_image[i - CMI8330_RMUX3D]);
  460. spin_unlock_irqrestore(&acard->wss->reg_lock, flags);
  461. if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
  462. snd_printk("failed to create mixers\n");
  463. snd_card_free(card);
  464. return err;
  465. }
  466. if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
  467. snd_printk("failed to create pcms\n");
  468. snd_card_free(card);
  469. return err;
  470. }
  471. strcpy(card->driver, "CMI8330/C3D");
  472. strcpy(card->shortname, "C-Media CMI8330/C3D");
  473. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  474. card->shortname,
  475. acard->wss->port,
  476. wssirq[dev],
  477. wssdma[dev]);
  478. if ((err = snd_card_register(card)) < 0) {
  479. snd_card_free(card);
  480. return err;
  481. }
  482. if (pcard)
  483. pnp_set_card_drvdata(pcard, card);
  484. else
  485. snd_cmi8330_legacy[dev] = card;
  486. return 0;
  487. }
  488. #ifdef CONFIG_PNP
  489. static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *card,
  490. const struct pnp_card_device_id *id)
  491. {
  492. static int dev;
  493. int res;
  494. for ( ; dev < SNDRV_CARDS; dev++) {
  495. if (!enable[dev] || !isapnp[dev])
  496. continue;
  497. res = snd_cmi8330_probe(dev, card, id);
  498. if (res < 0)
  499. return res;
  500. dev++;
  501. return 0;
  502. }
  503. return -ENODEV;
  504. }
  505. static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
  506. {
  507. snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
  508. snd_card_disconnect(card);
  509. snd_card_free_in_thread(card);
  510. }
  511. static struct pnp_card_driver cmi8330_pnpc_driver = {
  512. .flags = PNP_DRIVER_RES_DISABLE,
  513. .name = "cmi8330",
  514. .id_table = snd_cmi8330_pnpids,
  515. .probe = snd_cmi8330_pnp_detect,
  516. .remove = __devexit_p(snd_cmi8330_pnp_remove),
  517. };
  518. #endif /* CONFIG_PNP */
  519. static int __init alsa_card_cmi8330_init(void)
  520. {
  521. int dev, cards = 0;
  522. for (dev = 0; dev < SNDRV_CARDS; dev++) {
  523. if (!enable[dev])
  524. continue;
  525. #ifdef CONFIG_PNP
  526. if (isapnp[dev])
  527. continue;
  528. #endif
  529. if (snd_cmi8330_probe(dev, NULL, NULL) >= 0)
  530. cards++;
  531. }
  532. #ifdef CONFIG_PNP
  533. cards += pnp_register_card_driver(&cmi8330_pnpc_driver);
  534. #endif
  535. if (!cards) {
  536. #ifdef CONFIG_PNP
  537. pnp_unregister_card_driver(&cmi8330_pnpc_driver);
  538. #endif
  539. #ifdef MODULE
  540. snd_printk(KERN_ERR "CMI8330 not found or device busy\n");
  541. #endif
  542. return -ENODEV;
  543. }
  544. return 0;
  545. }
  546. static void __exit alsa_card_cmi8330_exit(void)
  547. {
  548. int i;
  549. #ifdef CONFIG_PNP
  550. /* PnP cards first */
  551. pnp_unregister_card_driver(&cmi8330_pnpc_driver);
  552. #endif
  553. for (i = 0; i < SNDRV_CARDS; i++)
  554. snd_card_free(snd_cmi8330_legacy[i]);
  555. }
  556. module_init(alsa_card_cmi8330_init)
  557. module_exit(alsa_card_cmi8330_exit)