cmi8330.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 <linux/init.h>
  45. #include <linux/err.h>
  46. #include <linux/isa.h>
  47. #include <linux/slab.h>
  48. #include <linux/pnp.h>
  49. #include <linux/moduleparam.h>
  50. #include <sound/core.h>
  51. #include <sound/wss.h>
  52. #include <sound/opl3.h>
  53. #include <sound/sb.h>
  54. #include <sound/initval.h>
  55. /*
  56. */
  57. /* #define ENABLE_SB_MIXER */
  58. #define PLAYBACK_ON_SB
  59. /*
  60. */
  61. MODULE_AUTHOR("George Talusan <gstalusan@uwaterloo.ca>");
  62. MODULE_DESCRIPTION("C-Media CMI8330");
  63. MODULE_LICENSE("GPL");
  64. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8330,isapnp:{CMI0001,@@@0001,@X@0001}}}");
  65. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  66. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  67. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
  68. #ifdef CONFIG_PNP
  69. static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  70. #endif
  71. static long sbport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  72. static int sbirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  73. static int sbdma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  74. static int sbdma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  75. static long wssport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  76. static int wssirq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  77. static int wssdma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  78. static long fmport[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  79. module_param_array(index, int, NULL, 0444);
  80. MODULE_PARM_DESC(index, "Index value for CMI8330 soundcard.");
  81. module_param_array(id, charp, NULL, 0444);
  82. MODULE_PARM_DESC(id, "ID string for CMI8330 soundcard.");
  83. module_param_array(enable, bool, NULL, 0444);
  84. MODULE_PARM_DESC(enable, "Enable CMI8330 soundcard.");
  85. #ifdef CONFIG_PNP
  86. module_param_array(isapnp, bool, NULL, 0444);
  87. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  88. #endif
  89. module_param_array(sbport, long, NULL, 0444);
  90. MODULE_PARM_DESC(sbport, "Port # for CMI8330 SB driver.");
  91. module_param_array(sbirq, int, NULL, 0444);
  92. MODULE_PARM_DESC(sbirq, "IRQ # for CMI8330 SB driver.");
  93. module_param_array(sbdma8, int, NULL, 0444);
  94. MODULE_PARM_DESC(sbdma8, "DMA8 for CMI8330 SB driver.");
  95. module_param_array(sbdma16, int, NULL, 0444);
  96. MODULE_PARM_DESC(sbdma16, "DMA16 for CMI8330 SB driver.");
  97. module_param_array(wssport, long, NULL, 0444);
  98. MODULE_PARM_DESC(wssport, "Port # for CMI8330 WSS driver.");
  99. module_param_array(wssirq, int, NULL, 0444);
  100. MODULE_PARM_DESC(wssirq, "IRQ # for CMI8330 WSS driver.");
  101. module_param_array(wssdma, int, NULL, 0444);
  102. MODULE_PARM_DESC(wssdma, "DMA for CMI8330 WSS driver.");
  103. module_param_array(fmport, long, NULL, 0444);
  104. MODULE_PARM_DESC(fmport, "FM port # for CMI8330 driver.");
  105. #ifdef CONFIG_PNP
  106. static int isa_registered;
  107. static int pnp_registered;
  108. #endif
  109. #define CMI8330_RMUX3D 16
  110. #define CMI8330_MUTEMUX 17
  111. #define CMI8330_OUTPUTVOL 18
  112. #define CMI8330_MASTVOL 19
  113. #define CMI8330_LINVOL 20
  114. #define CMI8330_CDINVOL 21
  115. #define CMI8330_WAVVOL 22
  116. #define CMI8330_RECMUX 23
  117. #define CMI8330_WAVGAIN 24
  118. #define CMI8330_LINGAIN 25
  119. #define CMI8330_CDINGAIN 26
  120. static unsigned char snd_cmi8330_image[((CMI8330_CDINGAIN)-16) + 1] =
  121. {
  122. 0x40, /* 16 - recording mux (SB-mixer-enabled) */
  123. #ifdef ENABLE_SB_MIXER
  124. 0x40, /* 17 - mute mux (Mode2) */
  125. #else
  126. 0x0, /* 17 - mute mux */
  127. #endif
  128. 0x0, /* 18 - vol */
  129. 0x0, /* 19 - master volume */
  130. 0x0, /* 20 - line-in volume */
  131. 0x0, /* 21 - cd-in volume */
  132. 0x0, /* 22 - wave volume */
  133. 0x0, /* 23 - mute/rec mux */
  134. 0x0, /* 24 - wave rec gain */
  135. 0x0, /* 25 - line-in rec gain */
  136. 0x0 /* 26 - cd-in rec gain */
  137. };
  138. typedef int (*snd_pcm_open_callback_t)(struct snd_pcm_substream *);
  139. struct snd_cmi8330 {
  140. #ifdef CONFIG_PNP
  141. struct pnp_dev *cap;
  142. struct pnp_dev *play;
  143. #endif
  144. struct snd_card *card;
  145. struct snd_wss *wss;
  146. struct snd_sb *sb;
  147. struct snd_pcm *pcm;
  148. struct snd_cmi8330_stream {
  149. struct snd_pcm_ops ops;
  150. snd_pcm_open_callback_t open;
  151. void *private_data; /* sb or wss */
  152. } streams[2];
  153. };
  154. #ifdef CONFIG_PNP
  155. static struct pnp_card_device_id snd_cmi8330_pnpids[] = {
  156. { .id = "CMI0001", .devs = { { "@@@0001" }, { "@X@0001" } } },
  157. { .id = "" }
  158. };
  159. MODULE_DEVICE_TABLE(pnp_card, snd_cmi8330_pnpids);
  160. #endif
  161. static struct snd_kcontrol_new snd_cmi8330_controls[] __devinitdata = {
  162. WSS_DOUBLE("Master Playback Volume", 0,
  163. CMI8330_MASTVOL, CMI8330_MASTVOL, 4, 0, 15, 0),
  164. WSS_SINGLE("Loud Playback Switch", 0,
  165. CMI8330_MUTEMUX, 6, 1, 1),
  166. WSS_DOUBLE("PCM Playback Switch", 0,
  167. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  168. WSS_DOUBLE("PCM Playback Volume", 0,
  169. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  170. WSS_DOUBLE("Line Playback Switch", 0,
  171. CMI8330_MUTEMUX, CMI8330_MUTEMUX, 4, 3, 1, 0),
  172. WSS_DOUBLE("Line Playback Volume", 0,
  173. CMI8330_LINVOL, CMI8330_LINVOL, 4, 0, 15, 0),
  174. WSS_DOUBLE("Line Capture Switch", 0,
  175. CMI8330_RMUX3D, CMI8330_RMUX3D, 2, 1, 1, 0),
  176. WSS_DOUBLE("Line Capture Volume", 0,
  177. CMI8330_LINGAIN, CMI8330_LINGAIN, 4, 0, 15, 0),
  178. WSS_DOUBLE("CD Playback Switch", 0,
  179. CMI8330_MUTEMUX, CMI8330_MUTEMUX, 2, 1, 1, 0),
  180. WSS_DOUBLE("CD Capture Switch", 0,
  181. CMI8330_RMUX3D, CMI8330_RMUX3D, 4, 3, 1, 0),
  182. WSS_DOUBLE("CD Playback Volume", 0,
  183. CMI8330_CDINVOL, CMI8330_CDINVOL, 4, 0, 15, 0),
  184. WSS_DOUBLE("CD Capture Volume", 0,
  185. CMI8330_CDINGAIN, CMI8330_CDINGAIN, 4, 0, 15, 0),
  186. WSS_SINGLE("Mic Playback Switch", 0,
  187. CMI8330_MUTEMUX, 0, 1, 0),
  188. WSS_SINGLE("Mic Playback Volume", 0,
  189. CMI8330_OUTPUTVOL, 0, 7, 0),
  190. WSS_SINGLE("Mic Capture Switch", 0,
  191. CMI8330_RMUX3D, 0, 1, 0),
  192. WSS_SINGLE("Mic Capture Volume", 0,
  193. CMI8330_OUTPUTVOL, 5, 7, 0),
  194. WSS_DOUBLE("Wavetable Playback Switch", 0,
  195. CMI8330_RECMUX, CMI8330_RECMUX, 1, 0, 1, 0),
  196. WSS_DOUBLE("Wavetable Playback Volume", 0,
  197. CMI8330_WAVVOL, CMI8330_WAVVOL, 4, 0, 15, 0),
  198. WSS_DOUBLE("Wavetable Capture Switch", 0,
  199. CMI8330_RECMUX, CMI8330_RECMUX, 5, 4, 1, 0),
  200. WSS_DOUBLE("Wavetable Capture Volume", 0,
  201. CMI8330_WAVGAIN, CMI8330_WAVGAIN, 4, 0, 15, 0),
  202. WSS_SINGLE("3D Control - Switch", 0,
  203. CMI8330_RMUX3D, 5, 1, 1),
  204. WSS_SINGLE("PC Speaker Playback Volume", 0,
  205. CMI8330_OUTPUTVOL, 3, 3, 0),
  206. WSS_DOUBLE("FM Playback Switch", 0,
  207. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  208. WSS_DOUBLE("FM Playback Volume", 0,
  209. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  210. WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", CAPTURE, SWITCH), 0,
  211. CMI8330_RMUX3D, 7, 1, 1),
  212. WSS_SINGLE(SNDRV_CTL_NAME_IEC958("Input ", PLAYBACK, SWITCH), 0,
  213. CMI8330_MUTEMUX, 7, 1, 1),
  214. };
  215. #ifdef ENABLE_SB_MIXER
  216. static struct sbmix_elem cmi8330_sb_mixers[] __devinitdata = {
  217. SB_DOUBLE("SB Master Playback Volume", SB_DSP4_MASTER_DEV, (SB_DSP4_MASTER_DEV + 1), 3, 3, 31),
  218. SB_DOUBLE("Tone Control - Bass", SB_DSP4_BASS_DEV, (SB_DSP4_BASS_DEV + 1), 4, 4, 15),
  219. SB_DOUBLE("Tone Control - Treble", SB_DSP4_TREBLE_DEV, (SB_DSP4_TREBLE_DEV + 1), 4, 4, 15),
  220. SB_DOUBLE("SB PCM Playback Volume", SB_DSP4_PCM_DEV, (SB_DSP4_PCM_DEV + 1), 3, 3, 31),
  221. SB_DOUBLE("SB Synth Playback Volume", SB_DSP4_SYNTH_DEV, (SB_DSP4_SYNTH_DEV + 1), 3, 3, 31),
  222. SB_DOUBLE("SB CD Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 2, 1, 1),
  223. SB_DOUBLE("SB CD Playback Volume", SB_DSP4_CD_DEV, (SB_DSP4_CD_DEV + 1), 3, 3, 31),
  224. SB_DOUBLE("SB Line Playback Switch", SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, 4, 3, 1),
  225. SB_DOUBLE("SB Line Playback Volume", SB_DSP4_LINE_DEV, (SB_DSP4_LINE_DEV + 1), 3, 3, 31),
  226. SB_SINGLE("SB Mic Playback Switch", SB_DSP4_OUTPUT_SW, 0, 1),
  227. SB_SINGLE("SB Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  228. SB_SINGLE("SB PC Speaker Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  229. SB_DOUBLE("SB Capture Volume", SB_DSP4_IGAIN_DEV, (SB_DSP4_IGAIN_DEV + 1), 6, 6, 3),
  230. SB_DOUBLE("SB Playback Volume", SB_DSP4_OGAIN_DEV, (SB_DSP4_OGAIN_DEV + 1), 6, 6, 3),
  231. SB_SINGLE("SB Mic Auto Gain", SB_DSP4_MIC_AGC, 0, 1),
  232. };
  233. static unsigned char cmi8330_sb_init_values[][2] __devinitdata = {
  234. { SB_DSP4_MASTER_DEV + 0, 0 },
  235. { SB_DSP4_MASTER_DEV + 1, 0 },
  236. { SB_DSP4_PCM_DEV + 0, 0 },
  237. { SB_DSP4_PCM_DEV + 1, 0 },
  238. { SB_DSP4_SYNTH_DEV + 0, 0 },
  239. { SB_DSP4_SYNTH_DEV + 1, 0 },
  240. { SB_DSP4_INPUT_LEFT, 0 },
  241. { SB_DSP4_INPUT_RIGHT, 0 },
  242. { SB_DSP4_OUTPUT_SW, 0 },
  243. { SB_DSP4_SPEAKER_DEV, 0 },
  244. };
  245. static int __devinit cmi8330_add_sb_mixers(struct snd_sb *chip)
  246. {
  247. int idx, err;
  248. unsigned long flags;
  249. spin_lock_irqsave(&chip->mixer_lock, flags);
  250. snd_sbmixer_write(chip, 0x00, 0x00); /* mixer reset */
  251. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  252. /* mute and zero volume channels */
  253. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_init_values); idx++) {
  254. spin_lock_irqsave(&chip->mixer_lock, flags);
  255. snd_sbmixer_write(chip, cmi8330_sb_init_values[idx][0],
  256. cmi8330_sb_init_values[idx][1]);
  257. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  258. }
  259. for (idx = 0; idx < ARRAY_SIZE(cmi8330_sb_mixers); idx++) {
  260. if ((err = snd_sbmixer_add_ctl_elem(chip, &cmi8330_sb_mixers[idx])) < 0)
  261. return err;
  262. }
  263. return 0;
  264. }
  265. #endif
  266. static int __devinit snd_cmi8330_mixer(struct snd_card *card, struct snd_cmi8330 *acard)
  267. {
  268. unsigned int idx;
  269. int err;
  270. strcpy(card->mixername, "CMI8330/C3D");
  271. for (idx = 0; idx < ARRAY_SIZE(snd_cmi8330_controls); idx++) {
  272. err = snd_ctl_add(card,
  273. snd_ctl_new1(&snd_cmi8330_controls[idx],
  274. acard->wss));
  275. if (err < 0)
  276. return err;
  277. }
  278. #ifdef ENABLE_SB_MIXER
  279. if ((err = cmi8330_add_sb_mixers(acard->sb)) < 0)
  280. return err;
  281. #endif
  282. return 0;
  283. }
  284. #ifdef CONFIG_PNP
  285. static int __devinit snd_cmi8330_pnp(int dev, struct snd_cmi8330 *acard,
  286. struct pnp_card_link *card,
  287. const struct pnp_card_device_id *id)
  288. {
  289. struct pnp_dev *pdev;
  290. int err;
  291. acard->cap = pnp_request_card_device(card, id->devs[0].id, NULL);
  292. if (acard->cap == NULL)
  293. return -EBUSY;
  294. acard->play = pnp_request_card_device(card, id->devs[1].id, NULL);
  295. if (acard->play == NULL)
  296. return -EBUSY;
  297. pdev = acard->cap;
  298. err = pnp_activate_dev(pdev);
  299. if (err < 0) {
  300. snd_printk(KERN_ERR "CMI8330/C3D (AD1848) PnP configure failure\n");
  301. return -EBUSY;
  302. }
  303. wssport[dev] = pnp_port_start(pdev, 0);
  304. wssdma[dev] = pnp_dma(pdev, 0);
  305. wssirq[dev] = pnp_irq(pdev, 0);
  306. fmport[dev] = pnp_port_start(pdev, 1);
  307. /* allocate SB16 resources */
  308. pdev = acard->play;
  309. err = pnp_activate_dev(pdev);
  310. if (err < 0) {
  311. snd_printk(KERN_ERR "CMI8330/C3D (SB16) PnP configure failure\n");
  312. return -EBUSY;
  313. }
  314. sbport[dev] = pnp_port_start(pdev, 0);
  315. sbdma8[dev] = pnp_dma(pdev, 0);
  316. sbdma16[dev] = pnp_dma(pdev, 1);
  317. sbirq[dev] = pnp_irq(pdev, 0);
  318. return 0;
  319. }
  320. #endif
  321. /*
  322. * PCM interface
  323. *
  324. * since we call the different chip interfaces for playback and capture
  325. * directions, we need a trick.
  326. *
  327. * - copy the ops for each direction into a local record.
  328. * - replace the open callback with the new one, which replaces the
  329. * substream->private_data with the corresponding chip instance
  330. * and calls again the original open callback of the chip.
  331. *
  332. */
  333. #ifdef PLAYBACK_ON_SB
  334. #define CMI_SB_STREAM SNDRV_PCM_STREAM_PLAYBACK
  335. #define CMI_AD_STREAM SNDRV_PCM_STREAM_CAPTURE
  336. #else
  337. #define CMI_SB_STREAM SNDRV_PCM_STREAM_CAPTURE
  338. #define CMI_AD_STREAM SNDRV_PCM_STREAM_PLAYBACK
  339. #endif
  340. static int snd_cmi8330_playback_open(struct snd_pcm_substream *substream)
  341. {
  342. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  343. /* replace the private_data and call the original open callback */
  344. substream->private_data = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].private_data;
  345. return chip->streams[SNDRV_PCM_STREAM_PLAYBACK].open(substream);
  346. }
  347. static int snd_cmi8330_capture_open(struct snd_pcm_substream *substream)
  348. {
  349. struct snd_cmi8330 *chip = snd_pcm_substream_chip(substream);
  350. /* replace the private_data and call the original open callback */
  351. substream->private_data = chip->streams[SNDRV_PCM_STREAM_CAPTURE].private_data;
  352. return chip->streams[SNDRV_PCM_STREAM_CAPTURE].open(substream);
  353. }
  354. static int __devinit snd_cmi8330_pcm(struct snd_card *card, struct snd_cmi8330 *chip)
  355. {
  356. struct snd_pcm *pcm;
  357. const struct snd_pcm_ops *ops;
  358. int err;
  359. static snd_pcm_open_callback_t cmi_open_callbacks[2] = {
  360. snd_cmi8330_playback_open,
  361. snd_cmi8330_capture_open
  362. };
  363. if ((err = snd_pcm_new(card, "CMI8330", 0, 1, 1, &pcm)) < 0)
  364. return err;
  365. strcpy(pcm->name, "CMI8330");
  366. pcm->private_data = chip;
  367. /* SB16 */
  368. ops = snd_sb16dsp_get_pcm_ops(CMI_SB_STREAM);
  369. chip->streams[CMI_SB_STREAM].ops = *ops;
  370. chip->streams[CMI_SB_STREAM].open = ops->open;
  371. chip->streams[CMI_SB_STREAM].ops.open = cmi_open_callbacks[CMI_SB_STREAM];
  372. chip->streams[CMI_SB_STREAM].private_data = chip->sb;
  373. /* AD1848 */
  374. ops = snd_wss_get_pcm_ops(CMI_AD_STREAM);
  375. chip->streams[CMI_AD_STREAM].ops = *ops;
  376. chip->streams[CMI_AD_STREAM].open = ops->open;
  377. chip->streams[CMI_AD_STREAM].ops.open = cmi_open_callbacks[CMI_AD_STREAM];
  378. chip->streams[CMI_AD_STREAM].private_data = chip->wss;
  379. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK].ops);
  380. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &chip->streams[SNDRV_PCM_STREAM_CAPTURE].ops);
  381. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  382. snd_dma_isa_data(),
  383. 64*1024, 128*1024);
  384. chip->pcm = pcm;
  385. return 0;
  386. }
  387. #ifdef CONFIG_PM
  388. static int snd_cmi8330_suspend(struct snd_card *card)
  389. {
  390. struct snd_cmi8330 *acard = card->private_data;
  391. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  392. snd_pcm_suspend_all(acard->pcm);
  393. acard->wss->suspend(acard->wss);
  394. snd_sbmixer_suspend(acard->sb);
  395. return 0;
  396. }
  397. static int snd_cmi8330_resume(struct snd_card *card)
  398. {
  399. struct snd_cmi8330 *acard = card->private_data;
  400. snd_sbdsp_reset(acard->sb);
  401. snd_sbmixer_suspend(acard->sb);
  402. acard->wss->resume(acard->wss);
  403. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  404. return 0;
  405. }
  406. #endif
  407. /*
  408. */
  409. #ifdef CONFIG_PNP
  410. #define is_isapnp_selected(dev) isapnp[dev]
  411. #else
  412. #define is_isapnp_selected(dev) 0
  413. #endif
  414. #define PFX "cmi8330: "
  415. static struct snd_card *snd_cmi8330_card_new(int dev)
  416. {
  417. struct snd_card *card;
  418. struct snd_cmi8330 *acard;
  419. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  420. sizeof(struct snd_cmi8330));
  421. if (card == NULL) {
  422. snd_printk(KERN_ERR PFX "could not get a new card\n");
  423. return NULL;
  424. }
  425. acard = card->private_data;
  426. acard->card = card;
  427. return card;
  428. }
  429. static int __devinit snd_cmi8330_probe(struct snd_card *card, int dev)
  430. {
  431. struct snd_cmi8330 *acard;
  432. int i, err;
  433. struct snd_opl3 *opl3;
  434. acard = card->private_data;
  435. err = snd_wss_create(card, wssport[dev] + 4, -1,
  436. wssirq[dev],
  437. wssdma[dev], -1,
  438. WSS_HW_DETECT, 0, &acard->wss);
  439. if (err < 0) {
  440. snd_printk(KERN_ERR PFX "(AD1848) device busy??\n");
  441. return err;
  442. }
  443. if (acard->wss->hardware != WSS_HW_CMI8330) {
  444. snd_printk(KERN_ERR PFX "(AD1848) not found during probe\n");
  445. return -ENODEV;
  446. }
  447. if ((err = snd_sbdsp_create(card, sbport[dev],
  448. sbirq[dev],
  449. snd_sb16dsp_interrupt,
  450. sbdma8[dev],
  451. sbdma16[dev],
  452. SB_HW_AUTO, &acard->sb)) < 0) {
  453. snd_printk(KERN_ERR PFX "(SB16) device busy??\n");
  454. return err;
  455. }
  456. if (acard->sb->hardware != SB_HW_16) {
  457. snd_printk(KERN_ERR PFX "(SB16) not found during probe\n");
  458. return err;
  459. }
  460. snd_wss_out(acard->wss, CS4231_MISC_INFO, 0x40); /* switch on MODE2 */
  461. for (i = CMI8330_RMUX3D; i <= CMI8330_CDINGAIN; i++)
  462. snd_wss_out(acard->wss, i,
  463. snd_cmi8330_image[i - CMI8330_RMUX3D]);
  464. if ((err = snd_cmi8330_mixer(card, acard)) < 0) {
  465. snd_printk(KERN_ERR PFX "failed to create mixers\n");
  466. return err;
  467. }
  468. if ((err = snd_cmi8330_pcm(card, acard)) < 0) {
  469. snd_printk(KERN_ERR PFX "failed to create pcms\n");
  470. return err;
  471. }
  472. if (fmport[dev] != SNDRV_AUTO_PORT) {
  473. if (snd_opl3_create(card,
  474. fmport[dev], fmport[dev] + 2,
  475. OPL3_HW_AUTO, 0, &opl3) < 0) {
  476. snd_printk(KERN_ERR PFX
  477. "no OPL device at 0x%lx-0x%lx ?\n",
  478. fmport[dev], fmport[dev] + 2);
  479. } else {
  480. err = snd_opl3_timer_new(opl3, 0, 1);
  481. if (err < 0)
  482. return err;
  483. err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  484. if (err < 0)
  485. return err;
  486. }
  487. }
  488. strcpy(card->driver, "CMI8330/C3D");
  489. strcpy(card->shortname, "C-Media CMI8330/C3D");
  490. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  491. card->shortname,
  492. acard->wss->port,
  493. wssirq[dev],
  494. wssdma[dev]);
  495. return snd_card_register(card);
  496. }
  497. static int __devinit snd_cmi8330_isa_match(struct device *pdev,
  498. unsigned int dev)
  499. {
  500. if (!enable[dev] || is_isapnp_selected(dev))
  501. return 0;
  502. if (wssport[dev] == SNDRV_AUTO_PORT) {
  503. snd_printk(KERN_ERR PFX "specify wssport\n");
  504. return 0;
  505. }
  506. if (sbport[dev] == SNDRV_AUTO_PORT) {
  507. snd_printk(KERN_ERR PFX "specify sbport\n");
  508. return 0;
  509. }
  510. return 1;
  511. }
  512. static int __devinit snd_cmi8330_isa_probe(struct device *pdev,
  513. unsigned int dev)
  514. {
  515. struct snd_card *card;
  516. int err;
  517. card = snd_cmi8330_card_new(dev);
  518. if (! card)
  519. return -ENOMEM;
  520. snd_card_set_dev(card, pdev);
  521. if ((err = snd_cmi8330_probe(card, dev)) < 0) {
  522. snd_card_free(card);
  523. return err;
  524. }
  525. dev_set_drvdata(pdev, card);
  526. return 0;
  527. }
  528. static int __devexit snd_cmi8330_isa_remove(struct device *devptr,
  529. unsigned int dev)
  530. {
  531. snd_card_free(dev_get_drvdata(devptr));
  532. dev_set_drvdata(devptr, NULL);
  533. return 0;
  534. }
  535. #ifdef CONFIG_PM
  536. static int snd_cmi8330_isa_suspend(struct device *dev, unsigned int n,
  537. pm_message_t state)
  538. {
  539. return snd_cmi8330_suspend(dev_get_drvdata(dev));
  540. }
  541. static int snd_cmi8330_isa_resume(struct device *dev, unsigned int n)
  542. {
  543. return snd_cmi8330_resume(dev_get_drvdata(dev));
  544. }
  545. #endif
  546. #define DEV_NAME "cmi8330"
  547. static struct isa_driver snd_cmi8330_driver = {
  548. .match = snd_cmi8330_isa_match,
  549. .probe = snd_cmi8330_isa_probe,
  550. .remove = __devexit_p(snd_cmi8330_isa_remove),
  551. #ifdef CONFIG_PM
  552. .suspend = snd_cmi8330_isa_suspend,
  553. .resume = snd_cmi8330_isa_resume,
  554. #endif
  555. .driver = {
  556. .name = DEV_NAME
  557. },
  558. };
  559. #ifdef CONFIG_PNP
  560. static int __devinit snd_cmi8330_pnp_detect(struct pnp_card_link *pcard,
  561. const struct pnp_card_device_id *pid)
  562. {
  563. static int dev;
  564. struct snd_card *card;
  565. int res;
  566. for ( ; dev < SNDRV_CARDS; dev++) {
  567. if (enable[dev] && isapnp[dev])
  568. break;
  569. }
  570. if (dev >= SNDRV_CARDS)
  571. return -ENODEV;
  572. card = snd_cmi8330_card_new(dev);
  573. if (! card)
  574. return -ENOMEM;
  575. if ((res = snd_cmi8330_pnp(dev, card->private_data, pcard, pid)) < 0) {
  576. snd_printk(KERN_ERR PFX "PnP detection failed\n");
  577. snd_card_free(card);
  578. return res;
  579. }
  580. snd_card_set_dev(card, &pcard->card->dev);
  581. if ((res = snd_cmi8330_probe(card, dev)) < 0) {
  582. snd_card_free(card);
  583. return res;
  584. }
  585. pnp_set_card_drvdata(pcard, card);
  586. dev++;
  587. return 0;
  588. }
  589. static void __devexit snd_cmi8330_pnp_remove(struct pnp_card_link * pcard)
  590. {
  591. snd_card_free(pnp_get_card_drvdata(pcard));
  592. pnp_set_card_drvdata(pcard, NULL);
  593. }
  594. #ifdef CONFIG_PM
  595. static int snd_cmi8330_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
  596. {
  597. return snd_cmi8330_suspend(pnp_get_card_drvdata(pcard));
  598. }
  599. static int snd_cmi8330_pnp_resume(struct pnp_card_link *pcard)
  600. {
  601. return snd_cmi8330_resume(pnp_get_card_drvdata(pcard));
  602. }
  603. #endif
  604. static struct pnp_card_driver cmi8330_pnpc_driver = {
  605. .flags = PNP_DRIVER_RES_DISABLE,
  606. .name = "cmi8330",
  607. .id_table = snd_cmi8330_pnpids,
  608. .probe = snd_cmi8330_pnp_detect,
  609. .remove = __devexit_p(snd_cmi8330_pnp_remove),
  610. #ifdef CONFIG_PM
  611. .suspend = snd_cmi8330_pnp_suspend,
  612. .resume = snd_cmi8330_pnp_resume,
  613. #endif
  614. };
  615. #endif /* CONFIG_PNP */
  616. static int __init alsa_card_cmi8330_init(void)
  617. {
  618. int err;
  619. err = isa_register_driver(&snd_cmi8330_driver, SNDRV_CARDS);
  620. #ifdef CONFIG_PNP
  621. if (!err)
  622. isa_registered = 1;
  623. err = pnp_register_card_driver(&cmi8330_pnpc_driver);
  624. if (!err)
  625. pnp_registered = 1;
  626. if (isa_registered)
  627. err = 0;
  628. #endif
  629. return err;
  630. }
  631. static void __exit alsa_card_cmi8330_exit(void)
  632. {
  633. #ifdef CONFIG_PNP
  634. if (pnp_registered)
  635. pnp_unregister_card_driver(&cmi8330_pnpc_driver);
  636. if (isa_registered)
  637. #endif
  638. isa_unregister_driver(&snd_cmi8330_driver);
  639. }
  640. module_init(alsa_card_cmi8330_init)
  641. module_exit(alsa_card_cmi8330_exit)